Designing the Program Wsp
Here is a flow chart for this program. The flow chart describes the basic events of what happens in this game, and in what order they can happen And here is the source code for our game. Start a new file and type the code in, and then save the file as bagels.py. We will design our game so that it is very easy to change the size of the secret number. It can be 3 digits or 5 digits or 30 digits. We will do this by using a constant variable named NUMDIGITS nstead of hard-coding the integer 3 into...
Brute Force
That's the entire Caesar Cipher. However, while this cipher may fool some people who don't understand cryptography, it won't keep a message secret from someone who knows cryptanalysis. While cryptography is the science of making codes, cryptanalysis is the study of breaking codes. Do you wish to encrypt or decrypt a message encrypt The door key will be hidden under the mat until the fourth of July. Bpm lwwz smg eqtt jm pqllmv cvlmz bpm uib cvbqt bpm nwczbp wn Rctg. The whole point of...
elif Else If Statements
print 'Your cat is not very fuzzy at all.' We've seen code like this before and it's rather simple. If the catName variable is equal to the string ' Fuzzball', then the if statement's condition is True and we tell the user that her cat is fuzzy. If catName is anything else, then we tell the user her cat is not fuzzy. But what if we wanted something else besides fuzzy and not fuzzy We could put another if and else statement inside the first else block like this print 'Your cat is neither fuzzy...
The isupper and islower String Methods
The isupper and islower string methods which are on line 36 and 41 work in a way that is very similar to the isdigit and isalpha methods. isupper will return True if the string it is called on contains at least one uppercase letter and no lowercase letters. islower returns True if the string it is called on contains at least one lowercase letter and no uppercase letters. Otherwise these methods return False. The existence of non-letter characters like numbers and spaces does not affect the...
Capitalizing our Variables
Have you noticed that variable names that are made up of more than one word have the other words capitalized This is to make the variable names easier to read because variable names can't have spaces in hem. thisnameiskindofhardtoread thisNamelsEasierToRead Leave the first word in lowercase, but start the other words in uppercase. We call something in a certain way like this a convention we don't have to do it this way, but doing it this way makes it a little easier. The convention for...
Code Explanation continued Gwh
116. for i in range len secretWord replace blanks with correctly guessed letters 117. if secretWord i in correctLetters 118. blanks blanks i secretWord i blanks i 1 Let's pretend the value of secretWord is ' otter ' and the value in correctLetters is 'tr'. Then len secretWord will return 5. Then range len secretWord becomes range 5 , which in turn returns the list 0, 1, 2, 3, 4 . Because the value of i will take on each value in 0, 1, 2, 3, 4 , then the for loop code is equivalent to this...
How to Play Reversi
Reversi also called Othello is a board game that is played on a grid so we will use a Cartesian coordinate system with XY coordinates, like we did with Sonar. It is a game played with two players. Our version of the game will have a computer AI that is more complicated than the AI we made for Tic Tac Toe. In fact, this AI is so good that it will probably beat you almost every time you play. I know I lose whenever I play against it Reversi has an 8 x 8 board with tiles that are black on one side...
Learning New Things by Running Simulation Experiments
This chapter didn't really cover a game, but it modeled various strategies for Reversi. If we thought that taking side moves in Reversi was a good idea, we would have to spend days, even weeks, carefully playing games of Reversi by hand and writing down the results. But if we know how to program a computer to play Reversi, then we can have the computer play Reversi using these strategies for us. If you think about it, you will realize that the computer is executing millions of lines of our...
Coordinate System of a Computer Monitor
It is common that computer monitors use a coordinate system that has the origin 0, 0 at the top left corner of the screen, which increases going down and to the right. There are no negative coordinates. This is because text is printed starting at the top left, and is printed going to the right and downwards. Most computer graphics use this coordinate system, and we will use it in our games. Also it is common to assume that monitors can display 80 text characters per row and 25 text characters...






