Presentation on theme: "Conditionals Lab Dan Maselko. Overview Gain familiarity with working with conditionals Become familiar with using the modulus operator Understand."— Presentation transcript:
Overview Gain familiarity with working with conditionals Become familiar with using the modulus operator Understand how to use conditionals to manage errors and special cases Begin to understand incremental programming
Step 0: Setup Make a lab4 directory in your 201/labs directory and copy the lab4.py file from Mr. Lupoli’s directory. From your home directory: cd 201/labs mkdir lab4 cd lab4 cp /afs/umbc.edu/users/s/l/slupoli/pub/lab4.py. emacs lab4.py &
Step 1: Background The task for this lab will be to implement some of the basic functioning of a tic-tac-toe game A working example of what the final product for this lab should look like is here:
Step 2: Alternating players The first thing we’ll need do is make the players alternate between turns. We should make the value of player be “X” whenever the turn variable is even, and “O” whenever it is odd. To do this, you will need to make use of the % operator. Once you get this done, print out the value of player during each turn to make sure they are alternating properly. >>> 5 % 1 0 >>> 5 % 2 1 >>> 6 % 2 0 >>> 5 % 3 2
Step 3: Get row and column The player needs to know where to move, so you’ll need to ask the user for the row and column of where to move to in the board. Only allow the player to enter values between 0 and 2. If they enter a value less than 0 or greater than 2, have the program exit. The program can be exited using the sys.exit() function. x = raw_input("Enter a number other than zero: ") if x == 0: print "You entered zero." sys.exit() print "You entered a number that isn’t zero."
Step 4: Convert row and column to an index We have a row and column for a 3 x 3 Tic Tac Toe board, but the list is only in one dimension. The list’s indexes would look something like this in a Tic Tac Toe board: Row 0 0 1 2 Row 1 3 4 5 Row 2 6 7 8 Columns: 0 1 2 The value of the index is related to the row and column values. Make use of the fact that there are 3 elements in each row.
Step 5: Putting the player into the list at the index Now that we have our index, we can put our player into the list at that index. First, get rid of the thing at that index by using the del() function. Then put the player at that index by using the insert() function. items = ["apples", "cherries", "bananas", "oranges", "lemons"] del(items) items.insert(3, "grapes") print items ["apples", "cherries", "bananas", "grapes", "lemons"]
Step 6: Printing the Tic-Tac-Toe board It’s hard to play one dimensional tic-tac-toe. Something like: doesn’t make a lot of sense, so it would be better if it were printed out on 3 lines: Copy this code into the for loop we’ve been working inside of, and change the comment to real code: ['_', '_', 'O', 'X', '_', 'O', '_', 'X', '_'] _ _ O X _ O _ X _ for i in range(SIZE_BOARD): print board[i], # if three spaces have been printed print
Bonus Step: Moving in an occupied location Right now, there is nothing stopping any of the players from overwriting the location that another player has already taken. What we really want is only places in the list with “_”s in them to be overwritten. After the index is calculated, implement code that will make the program exit if the element at board[index] is not an underscore.
Challenge Step: Handling Wins Now, our tic-tac-toe game will always take 9 turns, which means it will keep going if one of the players wins. Add some code that will check if a player has won during each turn, and exit the program if that is the case.