move = getComputerMove(theBoard, computerLetter)ġ59. print('Hooray! You have won the game!')ġ58. makeMove(theBoard, playerLetter, move)ġ44. print('The ' + turn + ' will go first.')ġ42. playerLetter, computerLetter = inputPlayerLetter()ġ34. # Return True if every space on the board has been taken. return chooseRandomMoveFromList(board, )ġ20. # Try to take the center, if it is free.ġ17. move = chooseRandomMoveFromList(board, )ġ12. # Try to take one of the corners, if they are free.ġ08. if isWinner(boardCopy, playerLetter):ġ07. # Check if the player could win on their next move and block them.ġ03. if isWinner(boardCopy, computerLetter):ĩ9. makeMove(boardCopy, computerLetter, i)ĩ6. # First, check if we can win in the next move.ĩ5. # Here is the algorithm for our Tic-Tac-Toe AI:ĩ1. # Given a board and the computer's letter, determine where to moveĩ0. def getComputerMove(board, computerLetter):Ĩ4. # Returns None if there is no valid move.Ĩ3. # Returns a valid move from the passed list on the passed board.ħ2. def chooseRandomMoveFromList(board, movesList):ħ1. print('What is your next move? (1-9)')ħ0. while move not in '1 2 3 4 5 6 7 8 9'.split() or notĦ6. # Return True if the passed move is free on the passed board.Ħ5. # Make a copy of the board list and return it.ĥ9. (bo = le and bo = le and bo = le)) # Diagonalĥ2. (bo = le and bo = le and bo = le) or # DiagonalĤ9. (bo = le and bo = le and bo = le) or # Down the rightĤ8. (bo = le and bo = le and bo = le) or # Down the middleĤ7. (bo = le and bo = le and bo = le) or # Down the left sideĤ6. (bo = le and bo = le and bo = le) or # Across the bottomĤ5. (bo = le and bo = le and bo = le) or # Across the middleĤ4. return ((bo = le and bo = le and bo = le) or # Across theĤ3. # We use "bo" instead of "board" and "le" instead of "letter" so weĤ2. # Given a board and a player's letter, this function returns True ifĤ1. # Randomly choose which player goes first.Ĥ0. # The first element in the list is the player's letter the second isģ0. while not (letter = 'X' or letter = 'O'):Ģ3. # Returns a list with the player's letter as the first item and theġ9. # Lets the player type which letter they want to be.ġ7. # "board" is a list of 10 strings representing the board (ignoreĩ. # This function prints out the board that it was passed.Ĩ. To help us remember which index in the list is for which space, we’ll number the board like a keyboard’s number pad, as shown in Figure 10-1.Ħ. The player makes their move by entering the number of the space they want to take. Let’s get started by looking at a sample run of the program. The AI that plays Tic-Tac-Toe isn’t complicated it’s really just a few lines of code. An artificial intelligence (AI) is a computer program that can intelligently respond to the player’s moves. The user will play against a simple artificial intelligence, which we will write using our existing programming knowledge. This chapter doesn’t introduce many new programming concepts. When the board fills up with neither player winning, the game ends in a draw. If a player gets three of their marks on the board in a row, column, or diagonal, they win. One player is X and the other player is O. Tic-Tac-Toe is normally played with two people. This chapter features a Tic-Tac-Toe game.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |