Answered step by step
Verified Expert Solution
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 cslab;
public class Gomoku implements GomokuInterface
private Square board;
private Square turn;
private Square currentTurn;
private static final int NUMOFCOLUMNS ;
private static final int NUMOFROWS ;
private static final int POINTSTOWIN ;
public static void mainString args
Gomoku game new Gomoku;
if argslength
game.initComputerPlayerargs;
GomokuGUI.showGUIgame;
@Override
public int getNumCols
return NUMOFCOLUMNS;
@Override
public int getNumRows
return NUMOFROWS;
@Override
public int getNumInLineForWin
return POINTSTOWIN;
@Override
public TurnResult handleClickAtint row, int colthis is what changes the board
if row row NUMOFROWS col col NUMOFCOLUMNS
throw new MyCustomExceptionIllegal move!";
if boardrowcol Square.EMPTY
return new TurnResultTurnResultTypeINVALIDMOVE, getCurrentBoardAsString;
boardrowcol currentTurn;
String currentBoard getCurrentBoardAsString;
System.out.printlnThis 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 TurnResultTurnResultTypeVALIDMOVE, currentBoard;
@Override
public void initializeGame
this.board new SquareNUMOFROWSNUMOFCOLUMNS;
this.currentTurn Square.EMPTY;
@Override
public String getCurrentBoardAsString
StringBuilder squareThing new StringBuilder;
for int r ; r NUMOFCOLUMNS; r
for int c ; c NUMOFROWS; c
squareThing.appendboardrc;
return squareThing.toString;
@Override
public Square getCurrentTurn
return currentTurn;
@Override
public void initComputerPlayerString s
if sequalsIgnoreCasehuman
System.out.printlnHuman player selected.";
else if sequalsIgnoreCaseAI
System.out.printlnAI player selected.";
else
System.out.printlnInvalid player type. Please choose 'human' or AI;
public class MyCustomException extends RuntimeException
public MyCustomExceptionString message
supermessage;
public boolean hasWinningMoveSquare board, int row, int col, Square playerMarker
Check horizontally
int count ;
for int c Math.max col ; c Math.minNUMOFCOLUMNS col ; c
if boardrowc playerMarker
count;
if count POINTSTOWIN
return true;
else
count ;
Check vertically
count ;
for int r Math.max row ; r Math.minNUMOFROWS row ; r
if boardrcol playerMarker
count;
if count POINTSTOWIN
return true;
else
count ;
Check diagonals both directions
Lefttoright diagonal
count ;
for int i ; i ; i
int r row i;
int c col i;
if r && r NUMOFROWS && c && c NUMOFCOLUMNS
if boardrc playerMarker
count;
if count POINTSTOWIN
return true;
h
else
count ;
Righttoleft diagonal
count ;
for int i ; i ; i
int r row i;
int c col i;
if r && r NUMOFROWS && c && c NUMOFCOLUMNS
if boardrc playerMarker
count;
if count POINTSTOWIN
return true;
else
count ;
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started