Question

I need help with my programming assignment. The language used should be java and the algorithm...

I need help with my programming assignment. The language used should be java and the algorithm should use search trees so that you play against the computer and he chooses the best move. The tree should have all possibilities on the leaves and you could use recursion to so that it populates itself. The game can be a 3*3 board (no need the make it n*n). Please put comments so that I can understand it. Thanks

The game of ‘Walls’ is comprised of an NxN board of squares (like chess, for example). Each game is played twice, one time with player A starting the game and another with player B making the first move. The game has a start state, play rules that determine legal moves and an end state. Once a game ends, there is a certain accounting that decides the game result.

Your objective, as the programmer is to design, in detail, build and test an artificial agent (AA for short) capable of playing the game, as optimally as possible, against a human player or another AA. To do so you need to build a search tree (often called game tree) that includes every possible move starting from the initial state of the game, and all possible future moves, all the way down to the leaf nodes (dead-ends with no possibility of future moves). The search tree is searched to decide on the best current move, which is the move that is most likely to lead to an eventual win in the game. An example of a search tree and how it is used to decide game is referred to below, also.

Start state. The game starts with a clear board with the two players placed at horizontally symmetric positions, which are at equal distances from and as close as possible to the center of the board (without overlapping). A player has a ‘face’ which indicates the direction of its forward movement. At the start of the game each player is facing outward towards the closest edge to it.

Play rules. The players take turns making moves. A player can: try and move forward (try-forward), turn 90 degrees left (turn-left) or turn 90 degrees right (turn-right). A player can only move into an empty square or into a square with a ‘brick’ bearing its name. A player cannot move into a square that has a ‘brick’ of the opposite type, or off the edge of the board, or into a square currently occupied by the opposite player. Anytime player X moves forward from an unmarked square, that square is filled with a brick marked X (signifying its origin).

End state. The game ends when either every square on the board contains a brick or both players are stalled. A player is stalled, by definition, if it makes 9 consecutive moves without laying a new brick. A stalled player is not allowed to make any more moves.

Game result. Once the end state of the game is reached, the player with the greater number of bricks bearing its name wins 2 points (the other receiving 0). If both players lay the same number of bricks then the result is a draw, with each player receiving 1 point
0 0
Add a comment Improve this question Transcribed image text
Answer #1

// C++ program to find the next optimal move for
// a player
#include<bits/stdc++.h>
using namespace std;

struct Move
{
   int row, col;
};

char player = 'x', opponent = 'o';

// This function returns true if there are moves
// remaining on the board. It returns false if
// there are no moves left to play.
bool isMovesLeft(char board[3][3])
{
   for (int i = 0; i<3; i++)
       for (int j = 0; j<3; j++)
           if (board[i][j]=='_')
               return true;
   return false;
}

// This is the evaluation function as discussed
// in the previous article ( http://goo.gl/sJgv68 )
int evaluate(char b[3][3])
{
   // Checking for Rows for X or O victory.
   for (int row = 0; row<3; row++)
   {
       if (b[row][0]==b[row][1] &&
           b[row][1]==b[row][2])
       {
           if (b[row][0]==player)
               return +10;
           else if (b[row][0]==opponent)
               return -10;
       }
   }

   // Checking for Columns for X or O victory.
   for (int col = 0; col<3; col++)
   {
       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;
       }
   }

   // Checking for Diagonals for X or O victory.
   if (b[0][0]==b[1][1] && b[1][1]==b[2][2])
   {
       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])
   {
       if (b[0][2]==player)
           return +10;
       else if (b[0][2]==opponent)
           return -10;
   }

   // Else if none of them have won then return 0
   return 0;
}

// This is the minimax function. It considers all
// the possible ways the game can go and returns
// the value of the board
int minimax(char board[3][3], int depth, bool isMax)
{
   int score = evaluate(board);

   // If Maximizer has won the game return his/her
   // evaluated score
   if (score == 10)
       return score;

   // If Minimizer has won the game return his/her
   // evaluated score
   if (score == -10)
       return score;

   // If there are no more moves and no winner then
   // it is a tie
   if (isMovesLeft(board)==false)
       return 0;

   // If this maximizer's move
   if (isMax)
   {
       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 (board[i][j]=='_')
               {
                   // Make the move
                   board[i][j] = player;

                   // Call minimax recursively and choose
                   // the maximum value
                   best = max( best,
                       minimax(board, depth+1, !isMax) );

                   // Undo the move
                   board[i][j] = '_';
               }
           }
       }
       return best;
   }

   // If this minimizer's move
   else
   {
       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 (board[i][j]=='_')
               {
                   // Make the move
                   board[i][j] = opponent;

                   // Call minimax recursively and choose
                   // the minimum value
                   best = min(best,
                       minimax(board, depth+1, !isMax));

                   // Undo the move
                   board[i][j] = '_';
               }
           }
       }
       return best;
   }
}

// This will return the best possible move for the player
Move findBestMove(char board[3][3])
{
   int bestVal = -1000;
   Move bestMove;
   bestMove.row = -1;
   bestMove.col = -1;

   // Traverse all cells, evalutae 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++)
       {
           // Check if celll is empty
           if (board[i][j]=='_')
           {
               // Make the move
               board[i][j] = player;

               // compute evaluation function for this
               // move.
               int moveVal = minimax(board, 0, false);

               // Undo the move
               board[i][j] = '_';

               // If the value of the current move is
               // more than the best value, then update
               // best/
               if (moveVal > bestVal)
               {
                   bestMove.row = i;
                   bestMove.col = j;
                   bestVal = moveVal;
               }
           }
       }
   }

   printf("The value of the best Move is : %d\n\n",
           bestVal);

   return bestMove;
}

// Driver code
int main()
{
   char board[3][3] =
   {
       { 'x', 'o', 'x' },
       { 'o', 'o', 'x' },
       { '_', '_', '_' }
   };

   Move bestMove = findBestMove(board);

   printf("The Optimal Move is :\n");
   printf("ROW: %d COL: %d\n\n", bestMove.row,
                               bestMove.col );
   return 0;
}

Add a comment
Know the answer?
Add Answer to:
I need help with my programming assignment. The language used should be java and the algorithm...
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
  • I need to complete the code by implementing the min function and the alpha betta pruning...

    I need to complete the code by implementing the min function and the alpha betta pruning in order to complete the tic tac toe game using pything. code: # -*- coding: utf-8 -*- """ Created on: @author: """ import random from collections import namedtuple GameState = namedtuple('GameState', 'to_move, utility, board, moves') infinity = float('inf') game_result = { 1:"Player 1 Wins", -1:"Player 2 Wins", 0:"It is a Tie" } class Game: """To create a game, subclass this class and implement actions,...

  • This is my code for my game called Reversi, I need to you to make the...

    This is my code for my game called Reversi, I need to you to make the Tester program that will run and complete the game. Below is my code, please add comments and Javadoc. Thank you. public class Cell { // Displays 'B' for the black disk player. public static final char BLACK = 'B'; // Displays 'W' for the white disk player. public static final char WHITE = 'W'; // Displays '*' for the possible moves available. public static...

  • NEED HELP WITH DISCRETE MATH: . Consider the following game. Alice and Bob have a an...

    NEED HELP WITH DISCRETE MATH: . Consider the following game. Alice and Bob have a an infinite quarter chessboard in front of them. The chessboard has a left edge and a bottom edge. There is one checker on some square the chessboard. The player whose turn it is can move the checker down any positive number of squares, or can move the check one column to the left, but anywhere in that column. The game ends when a player cannot...

  • JAVA Only Help on the sections that say Student provide code. The student Provide code has...

    JAVA Only Help on the sections that say Student provide code. The student Provide code has comments that i put to state what i need help with. import java.util.Scanner; public class TicTacToe {     private final int BOARDSIZE = 3; // size of the board     private enum Status { WIN, DRAW, CONTINUE }; // game states     private char[][] board; // board representation     private boolean firstPlayer; // whether it's player 1's move     private boolean gameOver; // whether...

  • Java question for two classes: Given the upper limit n as a parameter, the result of...

    Java question for two classes: Given the upper limit n as a parameter, the result of both methods is a boolean[] array of n elements that reveals the answers to that problem for all natural numbers below n in one swoop. public static boolean[] sumOfTwoDistinctSquares(int n) Determines which natural numbers can be expressed in the form a 2 + b 2 so that a and b are two distinct positive integers. In the boolean array returned as result, the i...

  • Please, I need help with program c++. This is a chutes and ladders program. The code...

    Please, I need help with program c++. This is a chutes and ladders program. The code must be a novel code to the specifications of the problem statement. Thank you very much. Assignment Overview This program will implement a variation of the game “chutes and ladders” or “snakes and ladders:” https://en.wikipedia.org/wiki/Snakes_and_Ladders#Gameplay. Just like in the original game, landing on certain squares will jump the player ahead or behind. In this case, you are trying to reach to bottom of the...

  • I have already finished most of this assignment. I just need some help with the canMove...

    I have already finished most of this assignment. I just need some help with the canMove and main methods; pseudocode is also acceptable. Also, the programming language is java. Crickets and Grasshoppers is a simple two player game played on a strip of n spaces. Each space can be empty or have a piece. The first player plays as the cricket pieces and the other plays as grasshoppers and the turns alternate. We’ll represent crickets as C and grasshoppers as...

  • C programming language Purpose The purpose of this assignment is to help you to practice working...

    C programming language Purpose The purpose of this assignment is to help you to practice working with functions, arrays, and design simple algorithms Learning Outcomes ● Develop skills with multidimensional arrays ● Learn how to traverse multidimensional arrays ● Passing arrays to functions ● Develop algorithm design skills (e.g. recursion) Problem Overview Problem Overview Tic-Tac-Toe (also known as noughts and crosses or Xs and Os) is a paper-and-pencil game for two players, X and O, who take turns marking the...

  • Please develop the following code using C programming and using the specific functions, instructi...

    Please develop the following code using C programming and using the specific functions, instructions and format given below. Again please use the functions given especially. Also don't copy any existing solution please write your own code. This is the first part of a series of two labs (Lab 7 and Lab 8) that will complete an implementation for a board-type game called Reversi (also called Othello). The goal of this lab is to write code that sets up the input...

  • Instructions This assignment has to be completed by each student individually. NO COLLABORATION I...

    I need help creating a class diagram for this please: I am not sure what more you want? it has 2 classes at least Connect4 and Connect4TextConsole. Instructions This assignment has to be completed by each student individually. NO COLLABORATION IS ALLOWED Submit YourASURitelD ProjectDeliverable2.zip compressed folder should contain the following files following This the 1. 2. Connect4.java 〔Game Logic Module) Connect4TextConsole.java (Console-based Ul to test the gamel 3. JavaDoc documentation files index.html and all other supporting files such as.cs5...

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