Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

ffMy problem with this code is that I turnresult type does not work as I think it does not exists, and I need to write

ffMy problem with this code is that I turnresult type does not work as I think it does not exists, and I need to write either a new method or something for a player or computer to play. Can someone help me fix this code please?: import cs251.lab2.*;
public class Gomoku implements GomokuInterface {
private Square[][] board;
private Square[][] turn;
private Square currentTurn;
private static final int NUM_OF_COLUMNS =15;
private static final int NUM_OF_ROWS =15;
private static final int POINTS_TO_WIN =5;
public static void main(String[] args){
Gomoku game = new Gomoku();
if (args.length >0){
game.initComputerPlayer(args[0]);
}
GomokuGUI.showGUI(game);
}
@Override
public int getNumCols(){
return NUM_OF_COLUMNS;
}
@Override
public int getNumRows(){
return NUM_OF_ROWS;
}
@Override
public int getNumInLineForWin(){
return POINTS_TO_WIN;
}
@Override
public TurnResult handleClickAt(int row, int col){//this is what changes the board
if (row <0|| row >= NUM_OF_ROWS || col <0|| col >= NUM_OF_COLUMNS){
throw new MyCustomException("Illegal move!");
}
if (board[row][col]!= Square.EMPTY){
return new TurnResult(TurnResultType.INVALID_MOVE, getCurrentBoardAsString());
}
board[row][col]= currentTurn;
String currentBoard = getCurrentBoardAsString();
System.out.println("This is the current board: "+ currentBoard );
if( hasWinningMove( row, col, currentTurn )){
return new TurnResult( TurnResultType.WIN, currentBoard);
}
if( boardFull()){
return new TurnResult( TurnResultType.DRAW, currentBoard );
}
switchTurn();
return new TurnResult(TurnResultType.VALID_MOVE, currentBoard);
}
@Override
public void initializeGame(){
this.board = new Square[NUM_OF_ROWS][NUM_OF_COLUMNS];
this.currentTurn = Square.EMPTY;
}
@Override
public String getCurrentBoardAsString(){
StringBuilder squareThing = new StringBuilder();
for (int r =0; r < NUM_OF_COLUMNS; r++){
for (int c =0; c < NUM_OF_ROWS; c++){
squareThing.append(board[r][c]);
}
}
return squareThing.toString();
}
@Override
public Square getCurrentTurn(){
return currentTurn;
}
@Override
public void initComputerPlayer(String s){
if (s.equalsIgnoreCase("human")){
System.out.println("Human player selected.");
}
else if (s.equalsIgnoreCase("AI")){
System.out.println("AI player selected.");
}
else {
System.out.println("Invalid player type. Please choose 'human' or 'AI'.");
}
}
public class MyCustomException extends RuntimeException {
public MyCustomException(String message){
super(message);
}
}
public boolean hasWinningMove(Square[][] board, int row, int col, Square playerMarker){
// Check horizontally
int count =0;
for (int c = Math.max(0, col -4); c <= Math.min(NUM_OF_COLUMNS -1, col +4); c++){
if (board[row][c]== playerMarker){
count++;
if (count == POINTS_TO_WIN){
return true;
}
} else {
count =0;
}
}
// Check vertically
count =0;
for (int r = Math.max(0, row -4); r <= Math.min(NUM_OF_ROWS -1, row +4); r++){
if (board[r][col]== playerMarker){
count++;
if (count == POINTS_TO_WIN){
return true;
}
} else {
count =0;
}
}
// Check diagonals (both directions)
// Left-to-right diagonal
count =0;
for (int i =-4; i <=4; i++){
int r = row + i;
int c = col + i;
if (r >=0 && r < NUM_OF_ROWS && c >=0 && c < NUM_OF_COLUMNS){
if (board[r][c]== playerMarker){
count++;
if (count == POINTS_TO_WIN){
return true;
}h
} else {
count =0;
}
}
}
// Right-to-left diagonal
count =0;
for (int i =-4; i <=4; i++){
int r = row + i;
int c = col - i;
if (r >=0 && r < NUM_OF_ROWS && c >=0 && c < NUM_OF_COLUMNS){
if (board[r][c]== playerMarker){
count++;
if (count == POINTS_TO_WIN){
return true;
}
} else {
count =0;

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

Recommended Textbook for

Modern Database Management

Authors: Jeffrey A. Hoffer Fred R. McFadden

9th Edition

B01JXPZ7AK, 9780805360479

More Books

Students also viewed these Databases questions

Question

=+ e. What happens to Oceanias real exchange rate?

Answered: 1 week ago