1 / 11

Conditionals Lab

Conditionals Lab. Dan Maselko. 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.

uma
Download Presentation

Conditionals Lab

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Conditionals Lab Dan Maselko

  2. 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

  3. 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/labsmkdir lab4cd lab4cp /afs/umbc.edu/users/s/l/slupoli/pub/lab4.py .emacs lab4.py &

  4. 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: <link to video>

  5. 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

  6. 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."

  7. 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.

  8. 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[3]) items.insert(3, "grapes") print items ["apples", "cherries", "bananas", "grapes", "lemons"]

  9. 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

  10. 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.

  11. 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.

More Related