Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

This code is in JAVA its a tic-tac-toe board and my problem is even when a player wins (the two players are AI and their

This code is in JAVA its a tic-tac-toe board and my problem is even when a player wins (the two players are AI and their moves are automated) the isWinner function is not outputting the correct winner. here is the Game class

public class TTTGame {

private ComputerPlayer[] players = new ComputerPlayer[2];

private Board board;

private String[] marks = {\"X\",\"O\"};

private String name = \"TicTacToe\";

private int gameRowSize = 3;

private int gameColSize = 3;

private int gameScoreToWin = 3;

private int currentPlayerIndex = -1;

public TTTGame(){

setPlayers();

setBoard();

}

private void setPlayers() {

for(int i = 0; i

ComputerPlayer p = new ComputerPlayer(\"player\" + i+1 , marks[i]);

players[i] = p;

}

}

private void setBoard() {

this.board = new Board(gameRowSize, gameColSize, \"TTTGame\");

}

public void start(){

System.out.println(\"game started.......\");

do{

switchPlayer();

while(!board.makeMove(players[this.currentPlayerIndex].getMark(),

players[this.currentPlayerIndex].randomNumber(gameRowSize),

players[this.currentPlayerIndex].randomNumber(gameColSize)));

board.print();

}while(!gameOver());

System.out.print(isWinner());

}

private String isWinner() {

String winner = checkRow(0);

if (winner==checkRow(1))

if (winner==checkRow(2))

return winner;

winner = checkCol(0);

if (winner==checkCol(1))

if (winner==checkCol(2))

return winner;

return winner;

}

private String checkRow(int row){

boolean rowIsSame = true;

String lastChar = board.getMark(row,1);

for(int col = 0; col

if (board.getMark(row,col) != lastChar)

rowIsSame = false;

}

if(rowIsSame)

return lastChar;

else

return \"no one\";

}

private String checkCol(int col){

boolean colIsSame = true;

String lastChar = board.getMark(1,col);

for(int row = 0; row

if (board.getMark(row,col) != lastChar)

colIsSame = false;

}

if(colIsSame)

return lastChar;

else

return \"no one\";

}

private void switchPlayer() {

if(currentPlayerIndex == -1)

currentPlayerIndex = 0;

else if(currentPlayerIndex == 0)

currentPlayerIndex = 1;

else if(currentPlayerIndex == 1)

currentPlayerIndex = 0;

}

private boolean gameOver() {

if(board.isFull())

return true;

else

return false;

}

}

there is a board class but I don't believe the problem lies there.when I run my code and someone wins, the output is still no one. why?

board class

public class Board {

private Box[] boxes;

private String name;

private int boardRowSize;

private int boardColSize;

Board() {

this(3, 3, \"3*3 board\");

}

Board(int rowSize, int colSize, String name) {

this.setName(name);

this.setSize(rowSize, colSize);

}

private void setSize(int row, int col) {

if (row

System.out.println(\" minimum board size is 3 \");

} else {

this.boardColSize = col;

this.boardRowSize = row;

init();

}

}

private void init(){

boxes = new Box[boardColSize * boardRowSize];

for(int i = 0; i

Box b = new Box(i/boardColSize, i%boardColSize);

boxes[i] = b;

}

print();

}

void print(){

System.out.println(\"printing the \" + this.name + \" - \" + this.boardRowSize

+ \"*\" + this.boardColSize + \" board info.......\");

for(int i = 0; i

if(i!=0 && i%boardColSize == 0 )System.out.println();

boxes[i].print();

}

System.out.println(\"\");

}

private void setName (String name){

this.name = name;

}

boolean makeMove(String mark, int row, int col) {

int index = (3 * row) + col;

if(boxes[index].isAvailable()){

boxes[index].setPlaceHolder(mark);

return true;

}

return false;

}

public boolean isFull(){

for(Box b : boxes)

if(b.isAvailable()) return false;

return true;

}

public String getMark(int row, int col){

return boxes[row * this.boardRowSize + col].getPlaceHolder();

}

}

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

Entrepreneurship

Authors: Andrew Zacharakis, William D Bygrave

5th Edition

1119563097, 9781119563099

More Books

Students also viewed these Programming questions

Question

What do you think your problem does to you?

Answered: 1 week ago