Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

i need help making the code for the AI player of the black pieces in a checkers game to beat the other AI playing the

i need help making the code for the AI player of the black pieces in a checkers game to beat the other AI playing the red pieces. right now my AI barley ever ties the game. i need to beat red almost every time. please help!!!

-written in Java

This is the only code im allowed to change:

public class AIblackMove {

//This is the current state of the game CheckersGame currentGame; //This array contains the legal moves at this point in the game for black. CheckersMove legalMoves[];

// The constructor. public AIblackMove(CheckersGame game, CheckersMove moves[]) { currentGame = game; legalMoves = moves; }

// This is where your logic goes to make a move. public CheckersMove nextMove() { // Here are some simple ideas: // 1. Always pick the first move //FUNCTION_FIND_ALL_LEGAL_MOVES( currentGame ); //FUNCTION_FIND_BEST_MOVE( curentGame, legalMoves ); //FUNCTION_DO_MOVE( currentGame, legalMoves ); //return legalMoves[0]; // 2. Pick a random move return legalMoves[currentGame.generator.nextInt(legalMoves.length)];

//Or you can create a copy of the current board like this: //CheckersData new_board = new CheckersData(currentGame.boardData); //You can then simulate a move on this new board like this: //currentGame.simulateMove(new_board, legalMoves[0],CheckersData.BLACK); } //After you simulate the move you can evaluate the state of the board //after the move and see how it looks. You can evaluate all the //currently legal moves using a loop and select the best one. }

// One thing you will probably want to do is evaluate the current // goodness of the board. This is a toy example, and probably isn't // very good, but you can tweak it in any way you want. Not only is // number of pieces important, but board position could also be important. // Also, are kings more valuable than regular pieces? How much? int evaluate(CheckersData board) { return board.numBlack()+ board.numBlackKing() - board.numRed() - board.numRedKing(); }

This is the other code I was given, but cannot change:

import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.util.ArrayList; import java.util.Random;

/** * An object of this class holds data about a game of checkers. * It knows what kind of piece is on each square of the checkerboard. * Note that RED moves "up" the board (i.e. row number decreases) * while BLACK moves "down" the board (i.e. row number increases). * Methods are provided to return lists of available legal moves. */ public class CheckersData { /* The following constants represent the possible contents of a square on the board. The constants RED and BLACK also represent players in the game. */

static final int EMPTY = 0, RED = 1, RED_KING = 2, BLACK = 3, BLACK_KING = 4; }

/** * Return an array containing all the legal CheckersMoves * for the specified player on the current board. If the player * has no legal moves, null is returned. The value of player * should be one of the constants RED or BLACK; if not, null * is returned. If the returned value is non-null, it consists * entirely of jump moves or entirely of regular moves, since * if the player can jump, only jumps are legal moves. */ CheckersMove[] getLegalMoves(int player) { if (player != RED && player != BLACK) return null; int playerKing; // The constant representing a King belonging to player. if (player == RED) playerKing = RED_KING; else playerKing = BLACK_KING; ArrayList moves = new ArrayList(); // Moves will be stored in this list. /** * Return a list of the legal jumps that the specified player can * make starting from the specified row and column. If no such * jumps are possible, null is returned. The logic is similar * to the logic of the getLegalMoves() method. */ CheckersMove[] getLegalJumpsFrom(int player, int row, int col) { if (player != RED && player != BLACK) return null; int playerKing; // The constant representing a King belonging to player. if (player == RED) playerKing = RED_KING; else playerKing = BLACK_KING; ArrayList moves = new ArrayList(); // The legal jumps will be stored in this list. if (board[row][col] == player || board[row][col] == playerKing) { if (canJump(player, row, col, row+1, col+1, row+2, col+2)) moves.add(new CheckersMove(row, col, row+2, col+2)); if (canJump(player, row, col, row-1, col+1, row-2, col+2)) moves.add(new CheckersMove(row, col, row-2, col+2)); if (canJump(player, row, col, row+1, col-1, row+2, col-2)) moves.add(new CheckersMove(row, col, row+2, col-2)); if (canJump(player, row, col, row-1, col-1, row-2, col-2)) moves.add(new CheckersMove(row, col, row-2, col-2)); } if (moves.size() == 0) return null; else { CheckersMove[] moveArray = new CheckersMove[moves.size()]; for (int i = 0; i < moves.size(); i++) moveArray[i] = moves.get(i); return moveArray; } } // end getLegalMovesFrom()

// -------------------- Nested Classes ------------------------------- /** * A CheckersMove object represents a move in the game of Checkers. * It holds the row and column of the piece that is to be moved * and the row and column of the square to which it is to be moved. * (This class makes no guarantee that the move is legal.) */ public class CheckersMove { public int fromRow, fromCol; // Position of piece to be moved. public int toRow, toCol; // Square it is to move to. public CheckersMove(int r1, int c1, int r2, int c2) { // Constructor. Just set the values of the instance variables. fromRow = r1; fromCol = c1; toRow = r2; toCol = c2; } public boolean isJump() { // Test whether this move is a jump. It is assumed that // the move is legal. In a jump, the piece moves two // rows. (In a regular move, it only moves one row.) return (fromRow - toRow == 2 || fromRow - toRow == -2); } } // end class CheckersMove.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

Compute the derivative of f(x)cos(-4/5x)

Answered: 1 week ago

Question

Discuss the process involved in selection.

Answered: 1 week ago

Question

Differentiate tan(7x+9x-2.5)

Answered: 1 week ago

Question

Explain the sources of recruitment.

Answered: 1 week ago

Question

Differentiate sin(5x+2)

Answered: 1 week ago