Starting today I am going to be creating small programs in python implementing simple games or solutions to simple problems. I am open to suggestions on what I should make. I might jump into more difficult problems later on, but for now:
A simple text-based Rock-Paper-Scissors game!!!
Note: You will have to download the source code from HERE to be able to follow up with what I am writing from this point onwards.
=======================================================================
Line 1: we import the build-in "random" module of python 2.7 which will allow the computer to make a "random" decision.
Line 2: "end" is the variable that keeps the state of the game, it is False if the player wants to keep playing and it becomes True when the player selects to quit.
Line 3: this line makes sure that the game will keep running until end becomes True
Line 4: "computer" is a random number between 0-2, the computer's choice
Line 5:"choices" is just a way to translate "computer" into words. Thus, choices[0] = 'Rock', etc..
Line 7: makes sure that the program will keep asking the user for a choice until he gives us a valid one.
Line 10-13: if the player selected to quit, change end to True (so the game will stop) and "continue" (so the program won't try to print the outcome of the battle - remember our player chose to quit, not to play, so there will be no battle.)
Line 15: No matter what the player and the computer choose (Rock,Paper or Scissors), if they it's the same option then it is a draw.
The rest is just checking and printing the outcome.
=======================================================================-----------------------------------------------------------------------------------------------------------------------
Note: I am going to be using Python 2.7.3. The newest version right now is Python 3.2.3 and trust me, those two have A LOT of differences. That's why my programs might not work on newer versions. To check your Python's version just open a Python Shell. A line like this should be printed at the top.
"Python 2.7.3 (default, Apr 10 2012, 23:24:47) [MSC v.1500 64 bit (AMD64)] on win32"
-----------------------------------------------------------------------------------------------------------------------
A simple text-based Rock-Paper-Scissors game!!!
Note: You will have to download the source code from HERE to be able to follow up with what I am writing from this point onwards.
=======================================================================
Line 1: we import the build-in "random" module of python 2.7 which will allow the computer to make a "random" decision.
Line 2: "end" is the variable that keeps the state of the game, it is False if the player wants to keep playing and it becomes True when the player selects to quit.
Line 3: this line makes sure that the game will keep running until end becomes True
Line 4: "computer" is a random number between 0-2, the computer's choice
Line 5:"choices" is just a way to translate "computer" into words. Thus, choices[0] = 'Rock', etc..
Line 7: makes sure that the program will keep asking the user for a choice until he gives us a valid one.
Line 10-13: if the player selected to quit, change end to True (so the game will stop) and "continue" (so the program won't try to print the outcome of the battle - remember our player chose to quit, not to play, so there will be no battle.)
Line 15: No matter what the player and the computer choose (Rock,Paper or Scissors), if they it's the same option then it is a draw.
The rest is just checking and printing the outcome.
=======================================================================-----------------------------------------------------------------------------------------------------------------------
Note: I am going to be using Python 2.7.3. The newest version right now is Python 3.2.3 and trust me, those two have A LOT of differences. That's why my programs might not work on newer versions. To check your Python's version just open a Python Shell. A line like this should be printed at the top.
"Python 2.7.3 (default, Apr 10 2012, 23:24:47) [MSC v.1500 64 bit (AMD64)] on win32"
-----------------------------------------------------------------------------------------------------------------------
Δεν υπάρχουν σχόλια:
Δημοσίευση σχολίου