Sep-30-2020, 08:35 PM
Hello to everyone here. I have been learning python for a few weeks now with the book 'Learn Python 3 the Hard Way' and Jose Portilla's Udemy Complete Python Bootcamp course. The first milestone project for the Bootcamp course is the creation of a Tic Tac Toe game. These are the requirements:
I attempted this challenge without looking at the suggestions in the lecture or the solutions, simply trying to figure out how it could be done on my own, then trying to make it better. The code works, but it is very different from the code solution given in the course (which can be found here). Here is my code below. I would be very grateful if someone were willing to make comments on how it could be improved, whether there are any glaring problems or anything of that sort.
- 2 players should be able to play the game (both sitting at the same computer).
- The board should be printed out every time a player makes a move.
- You should be able to accept input of the player position and then place a symbol on the board
I attempted this challenge without looking at the suggestions in the lecture or the solutions, simply trying to figure out how it could be done on my own, then trying to make it better. The code works, but it is very different from the code solution given in the course (which can be found here). Here is my code below. I would be very grateful if someone were willing to make comments on how it could be improved, whether there are any glaring problems or anything of that sort.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
player1 = 'X' player2 = 'O' #The grid variable defines the spaces that will be printed to make up #the board. grid = [[ '\t ' , '1' , ' ' , ' ' , ' ' , '2' , ' ' , ' ' , ' ' , '3' , ' ' , ' ' ], [ '\t1 ' , '_' , '_' , '_' , '|' , '_' , '_' , '_' , '|' , '_' , '_' , '_' ], [ '\t2 ' , '_' , '_' , '_' , '|' , '_' , '_' , '_' , '|' , '_' , '_' , '_' ], [ '\t3 ' , ' ' , ' ' , ' ' , '|' , ' ' , ' ' , ' ' , '|' , ' ' , ' ' , ' ' ]] def print_grid(): '''takes the grid variable and prints it as a tic tac toe board.''' print (' '.join(grid[0]), ' \n ', ' '.join(grid[1]), ' \n ', ' '.join(grid[2]), ' \n ', ' '.join(grid[ 3 ])) def play_tic(): '''This function runs the game play.''' player_turn(player1, 'Player one' ) player_turn(player2, 'Player two' ) play_tic() def player_turn(player, player_name): #Allows player to choose a position, places marker in position, checks for winner. print ( f "\t{player_name}, it's your turn." ) choice(player) print_grid() check_winner(player) def choice(player): '''Takes the value returned by the input_request function and places an 'X' or an 'O' in the space chosen by the player.''' x = input_request( 'horizontal row' ) y = input_request( 'vertical column' ) if y = = 2 : y = 6 elif y = = 3 : y = 10 if grid[x][y] = = 'X' or grid[x][y] = = 'O' : print ( "\tThat space is taken. Please choose another." ) choice(player) else : grid[x][y] = player def input_request(axis): '''Asks player where they would like to position marker, and checks to make sure that the input corresponds to position.''' user_input = input ( f "\tWhich {axis} do you choose - 1, 2, or 3?" ) while user_input not in [ '1' , '2' , '3' ]: print ( "\tSorry, but that answer is unacceptable." ) user_input = input ( "\tPlease choose a digit - 1, 2, or 3." ) return int (user_input) def check_winner(player): '''Checks to see if the last move resulted in a winning configuration. ###I feel that there must be a way to make this shorter###''' draw_list = [grid[ 1 ][ 1 ], grid[ 1 ][ 6 ], grid[ 1 ][ 10 ], grid[ 2 ][ 1 ], grid[ 2 ][ 6 ], grid[ 2 ][ 10 ], grid[ 3 ][ 1 ], grid[ 3 ][ 6 ], grid[ 3 ][ 10 ]] if ((grid[ 1 ][ 1 ] = = player and grid[ 1 ][ 6 ] = = player and grid[ 1 ][ 10 ] = = player) or (grid[ 2 ][ 1 ] = = player and grid[ 2 ][ 6 ] = = player and grid[ 2 ][ 10 ] = = player) or (grid[ 3 ][ 1 ] = = player and grid[ 3 ][ 6 ] = = player and grid[ 3 ][ 10 ] = = player) or (grid[ 1 ][ 1 ] = = player and grid[ 2 ][ 1 ] = = player and grid[ 3 ][ 1 ] = = player) or (grid[ 1 ][ 6 ] = = player and grid[ 2 ][ 6 ] = = player and grid[ 3 ][ 6 ] = = player) or (grid[ 1 ][ 10 ] = = player and grid[ 2 ][ 10 ] = = player and grid[ 3 ][ 10 ] = = player) or (grid[ 1 ][ 1 ] = = player and grid[ 2 ][ 6 ] = = player and grid[ 3 ][ 10 ] = = player) or (grid[ 1 ][ 10 ] = = player and grid[ 2 ][ 6 ] = = player and grid[ 3 ][ 1 ] = = player) ): print ( f "Congratulations, {player}, you are the winner!!!" ) play_again() elif '_' and ' ' not in draw_list: print ( "The game is a draw." ) play_again() else : pass def play_again(): answer = input ( "Do you want to play again? Please answer Y or N." ) #I not clear on what global does here. I'm not sure why I thought this would help my code, but it does. # If I remove it, then the grid variable #does not reset to the value shown below, but instead remains the same as at the end of the last game play# #this works, but it seems unsatisfactory. It seems there must be a better way. global grid if answer.upper() = = 'N' : print ( "Thank you for playing. Have a nice day!" ) exit() elif answer.upper() = = 'Y' : #I don't like repeating the grid variable assignment here, but I couldn't think of another way #to reset the board to begin a second game. Well, I tried, but couldn't think of anything that worked. grid = [[ '\t ' , '1' , ' ' , ' ' , ' ' , '2' , ' ' , ' ' , ' ' , '3' , ' ' , ' ' ], [ '\t1 ' , '_' , '_' , '_' , '|' , '_' , '_' , '_' , '|' , '_' , '_' , '_' ], [ '\t2 ' , '_' , '_' , '_' , '|' , '_' , '_' , '_' , '|' , '_' , '_' , '_' ], [ '\t3 ' , ' ' , ' ' , ' ' , '|' , ' ' , ' ' , ' ' , '|' , ' ' , ' ' , ' ' ]] print_grid() play_tic() else : print ( "That is not an acceptable answer." ) play_again() print ( "\n\tLet's play tic tac toe." ) print ( "\tPlayer one will be 'X'." ) print_grid() play_tic() |