Answered step by step
Verified Expert Solution
Link Copied!

Question

00
1 Approved Answer

**Need help debugging this java code. my output is somehow not matching. public class TicTacToe { private int turn; private char[][] board; public TicTacToe() {

**Need help debugging this java code. my output is somehow not matching.

public class TicTacToe { private int turn; private char[][] board;

public TicTacToe() { turn = 1; board = new char[3][3]; }

public int getTurn() { return turn; }

public void printBoard() { System.out.println(" 0 1 2"); for (int i = 0; i

public boolean pickLocation(int row, int col) { if ((row >= 0 && row = 0 && col

public void takeTurn(int row, int col) { if (pickLocation(row, col)) { if (getTurn() % 2 == 0) { board[row][col] = 'O'; } else { //System.out.println(" "); board[row][col] = 'X'; } turn++; } }

public boolean checkRow() { for (int i = 0; i

public boolean checkCol() { for (int j = 0; j

public boolean checkDiag() { if (board[0][0] == 'X' || board[0][0] == 'O') { if ((board[0][0] == board[1][1]) && (board[1][1] == board[2][2])) { return true; } } if (board[0][2] == 'X' || board[0][2] == 'O') { if ((board[0][2] == board[1][1]) && (board[1][1] == board[2][0])) { return true; } } return false; }

public boolean checkWin() { if (checkRow() || checkCol() || checkDiag()) { return true; } return false; } }

image text in transcribedimage text in transcribedimage text in transcribed

public class TicTacToeTester public static void main(String[] args) //This is to help you test your methods. Feel free to add code at the end to check //to see if your checkwin method works! TicTacToe game = new TicTacToe(); System.out.println("Initial Game Board: "); game.printBoard; //Prints the first row of turns taken for(int row = 0; row

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered 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