![]() We keep prompting the user to do this until she enters valid coordinates. If the player’s initial move is not valid, we will prompt the user to enter a new row and column. To check if a move if valid, let’s define a helper function that takes the coordinates that the user entered and checks if that space on the board is empty: Row = int(input("P2 enter a row (1-3): ")) - 1Ĭol = int(input("P2 enter a column (1-3): ")) - 1Ĭheckpoint: Your program should endlessly alternate between the two players and ask each to enter a row and column. To do this, we will define a while loop which will alternate the players’ turns (and pieces), and prompt each player to go on their turn. Now let’s alternate between the two players and ask each to make a move. Row = int(input("Player enter a row (1-3): ")) - 1Ĭol = int(input("Player enter a column (1-3): ")) - 1 We subtract 1 from the input because indexing starts at 0 in Python. We will collect the user’s responses using the input() function. We will prompt the user to enter the row and column where they want to place their piece. Print(board + '|' + board + '|' + board)įinally, let’s call these methods to visualize the board.Ĭheckpoint: Your program should print a blank tic-tac-toe board. Next, we will draw the tic-tac-toe board. The code is as follows:īoard = np.empty(shape=(3,3), dtype='object') We will then loop through each row and column and replace the default None value with an empty space (which we can later fill in with an X or O). We will define our board as a 3 x 3 numpy array. Let’s start implementing these features one by one. ![]() If all spots have been filled without a 3 in a row, the game will end in a tie.After each move is made, our program will check for 3’s in a row.If the user enters a valid move, the board will update and print. ![]()
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |