Question

tic-tac-toe game. Add functionality to the program so when the button is clicked for the AI...

tic-tac-toe game. Add functionality to the program so when the button is clicked for the AI to take a turn, a heuristic is applied for each of the possible moves, a possible move is selected and the game state and GUI are properly updated. Once complete, the program should be able to play a single game of tic-tac-toe with the user.

C#

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Assuming that the data structure used is 3X3 matrix.

The game's logic can be broken into different functions for better understanding:

  1. A Boolen function 'moves_left' to check whether any more possible moves can be made.Returns True is moves are available, else returns False.
  2. A function named 'victory' to check for any possible winning at that particular instance after both the user's and the system's turn.
  3. A function named 'my_min_max' to make the next move that can take place and return the value to the game board.
  4. A fucntion named 'find_next_best_move' that selects the best alternative among the possivle moves.

In Detail:

The find_next_best_move function will return the co-ordinates for the next move.

To find the next best move for the AI my_mini_max algorithm can be used. The my_mini_max algorithm is an algorithm which uses backtracking mechanism used in decision making problems. TicTacToe is one such application where the my_mini_max algorithm can be made use of. Here, the idea is to use the same algo to find out the best move for AI.

If interested you may go through the mini_max algorithm to get a bigger picture of what my_mini_max algo does

The function replicates the my_mini_max algorithm to simulate the possible optimal sets of future moves of both human player and AI. The function returns a score which can be used to evaluate if the selected move is the best move.

For more clarity,

Suppose the board is in a position like this:

[ 'x', 'o', '_' ]

[ 'o', 'o', '_' ]

[ 'x', 'x', '_' ]

The find_next_best_move will iterate over the board and if the position is empty, it marks that position and calls the my_mini_max function. myMiniMax function will then optimally simulate the next moves by both user and checks if the current move played by the player returns the max score. The find_next_best_move function will check for the best scores returned for each empty spaces and best position is returned.

This means that the next position will be (2, 2) in the above case.

[ 'x', 'o', '_' ]

[ 'o', 'o', '_' ]

[ 'x', 'x', 'x' ]

The above is just an example to illustrate on single case of selecting the next best move.

Note: During the game, the player and the system earn points based on their move. It is based on this value that the AI is performed to choose the best move that can earn the system a better chance to win.

The functions and the detailed working of the game is explained with comments in the code below.

  // Function 'moves_left'. static Boolean moves_left(char [,]b) { for(int i=0;i<3;i++) for(int j=0;j<3;j++) if(b[i,j] =='_') // if the space is emptyreturn true; return false; } //Function 'victory'. static int victory(char [,]b) { for(int r=0;r<3;r++) // Checking for row victory (Xs or Os) { if(b[r,0] ==b[r,1] && b[r,1] == b[r,2]) { if(b[r,0] == player) return +10; else if(b[r,0] == opponent) return -10; } } for(int col=0;col<3;col++) // Checking for column victory (Xs or Os) { if(b[0,col] ==b[1,col] && b[1,col] == b[2,col]) { if(b[0,col] == player) return +10; else if(b[0,col] == opponent) return -10; } } if(b[0,0] ==b[1,1] && b[1,1] ==b[2,2]) // checking for diagonal victory (Xs or Os) { if(b[0,0] == player return +10; else if(b[0,0] == opponent) return -10; } if(b[0,2] ==b[1,1] && b[1,1] ==b[2,0]) // checking for diagonal victory (Xs or Os) { if(b[0,2] == player return +10; else if(b[0,2] == opponenet) return -10; } return 0; } //Function 'my_min_max'. static int my_min_max(char [,]b,Boolean isuser) { int point = victory(b); if (point == 10) // If user has won the game return point; if (point == -10) // If system has won the game return point; if (moves_left(b) == false) // If there are no more moves and no winner then it is a tie return 0; if (isuser) // If user's move { int best = -1000; // Traverse all cells for (int i=0;i<3;i++) { for (int j=0;j<3;j++) { // Check if cell is empty if (b[i,j] == '_') { // Make the move b[i,j] = player; best = Math.Max(best, my_min_max(b,!isuser)); // Call minimax recursively and choose the maximum value b[i,j] = '_'; // Undo the move } } } return best; } else // If system's move { int best = 1000; // Traverse all cells for (int i=0;i<3;i++) { for (int j=0;j<3;j++) { if (b[i,j] == '_') // Check if cell is empty { b[i,j] = opponent; // Make the move best = Math.Min(best,my_min_max(b,!isuser)); // Call minimax recursively and choose the minimum value // Undo the move b[i,j] = '_'; } } } return best; } } //Function 'find_next_best_move'. static Move find_next_best_move(char [,]b) { int bestvalue = -1000; Move bestMove = new Move(); bestMove.row = -1; bestMove.col = -1; // Traverse all cells, evaluate minimax function // for all empty cells. And return the cell with optimal value. for (int i=0;i<3;i++) { for (int j=0;j<3;j++) { if (b[i,j] == '_') // Check if cell is empty { b[i,j] = player; // Make the move int movevalue = my_mini_max(b,false); // compute evaluation function for this move. b[i,j] = '_'; // Undo the move // update the bestvalue by comparing the movevalue and the existing bestvalue. if (movevalue > bestvalue) { bestMove.row = i; bestMove.col = j; bestvalue = movevalue; } } } } return bestMove; } 

  

Add a comment
Know the answer?
Add Answer to:
tic-tac-toe game. Add functionality to the program so when the button is clicked for the AI...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • JAVAFX PROGRAM Write a program that will allow two users to play a game of tic-tac-toe....

    JAVAFX PROGRAM Write a program that will allow two users to play a game of tic-tac-toe. The game area should consist of nine buttons for the play area, a reset button, and a label that will display the current player's turn. When the program starts, the game area buttons should have no values in them (i.e. they should be blank) When player one clicks on a game area button, the button should get the value X. When player two clicks...

  • (Tic-Tac-Toe) Create a class Tic-Tac-Toe that will enable you to write a program to play Tic-Tac-Toe....

    (Tic-Tac-Toe) Create a class Tic-Tac-Toe that will enable you to write a program to play Tic-Tac-Toe. The class contains a private 3-by-3 two-dimensional array. Use an enumeration to represent the value in each cell of the array. The enumeration’s constants should be named X, O and EMPTY (for a position that does not contain an X or an O). The constructor should initialize the board elements to EMPTY. Allow two human players. Wherever the first player moves, place an X...

  • Please make this into one class. JAVA Please: Tic Tac Toe class - Write a fifth...

    Please make this into one class. JAVA Please: Tic Tac Toe class - Write a fifth game program that can play Tic Tac Toe. This game displays the lines of the Tic Tac Toe game and prompts the player to choose a move. The move is recorded on the screen and the computer picks his move from those that are left. The player then picks his next move. The program should allow only legal moves. The program should indicate when...

  • Write a c program that will allow two users to play a tic-tac-toe game. You should...

    Write a c program that will allow two users to play a tic-tac-toe game. You should write the program such that two people can play the game without any special instructions. (assue they know how to play the game). Prompt first player(X) to enter their first move. .Then draw the gameboard showing the move. .Prompt the second player(O) to enter their first move. . Then draw the gameboard showing both moves. And so on...through 9 moves. You will need to:...

  • 18. Tic-Tac-Toe Game rite a program that allows two players to play a game of tic-tac-toe. Use di...

    18. Tic-Tac-Toe Game rite a program that allows two players to play a game of tic-tac-toe. Use dimensional char array with three rows and three columns as the game board. Each element of the array should be initialized with an asterisk (*). The program should run a loop that does the following: Write . Displays the contents of the board array. . Allows player 1 to select a location on the board for an X. The program should ask the...

  • 1. Use Turtle Graphics to create a tic tac toe game in Python. Write a Python program that allows for one player vs computer to play tic tac toe game, without using turtle.turtle

    1. Use Turtle Graphics to create a tic tac toe game in Python. Write a Python program that allows for one player vs computer to play tic tac toe game, without using turtle.turtle

  • In a game of Tic Tac Toe, two players take turns making an available cell in...

    In a game of Tic Tac Toe, two players take turns making an available cell in a 3 x 3 grid with their respective tokens (either X or O). When one player has placed three tokens in a horizontal, vertical, or diagonal row on the grid, the game is over and that player has won. A stalemate occurs when all the cells on the grid have been filled with tokens and neither player has achieved a win. Write a program...

  • You will create a PYTHON program for Tic-Tac-Toe game. Tic-Tac-Toe is normally played with two people....

    You will create a PYTHON program for Tic-Tac-Toe game. Tic-Tac-Toe is normally played with two people. One player is X and the other player is O. Players take turns placing their X or O. If a player gets three of his/her marks on the board in a row, column, or diagonal, he/she wins. When the board fills up with neither player winning, the game ends in a draw 1) Player 1 VS Player 2: Two players are playing the game...

  • I need help developing this app using java in android studio Assignment: Tic-Tac-Toe app User interface...

    I need help developing this app using java in android studio Assignment: Tic-Tac-Toe app User interface Operation The app allows the user to play a game of Tic-Tac-Toe. The user can click the New Game button at any time to start a new game. The app displays other messages to the user as the game progresses such as (1) whose turn it is, (2) if a player wins, and (3) if the game ends in a tie. Specifications The app...

  • Tic Tac Toe Game: Help, please. Design and implement a console based Tic Tac Toe game....

    Tic Tac Toe Game: Help, please. Design and implement a console based Tic Tac Toe game. The objective of this project is to demonstrate your understanding of various programming concepts including Object Oriented Programming (OOP) and design. Tic Tac Toe is a two player game. In your implementation one opponent will be a human player and the other a computer player. ? The game is played on a 3 x 3 game board. ? The first player is known as...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT