Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Need help making the get function for player to work because it starts to run but then crashes. Any help is appreciated! //my class package

Need help making the get function for player to work because it starts to run but then crashes. Any help is appreciated!

//my class

package tictactoe;

/** * * @author */ public class TicTacToe { private char[][] board; private char player; public TicTacToe() { board = new char[3][3]; player = 'x'; makeboard(); } public void makeboard(){ for(int i=0; i<3; i++){ for (int j=0; j<3; j++){ board[i][j] = '-'; } } } public void designboard(){ System.out.print("---------"); for(int i=0; i<3; i++){ System.out.print("|"); for(int j=0; j<3; j++){ System.out.print(board[i][j] + "|"); } System.out.println(); System.out.println("--------"); } } public boolean BoardFull(){ boolean isFull=true; for(int i=0; i<3; i++){ for(int j=0; j<3; j++){ if(board[i][j] == '-'){ isFull = false; } } } return isFull; } public boolean checkForWin(){ return (checkRowsForWin() || checkColumnsForWin() || checkDiagonalsForWin()); } private boolean checkRowsForWin() { for (int i = 0; i < 3; i++) { if (checkRowCol(board[i][0], board[i][1], board[i][2]) == true) { return true; } } return false; } private boolean checkColumnsForWin() { for (int i = 0; i < 3; i++) { if (checkRowCol(board[0][i], board[1][i], board[2][i]) == true) { return true; } } return false; } private boolean checkDiagonalsForWin() { return ((checkRowCol(board[0][0], board[1][1], board[2][2]) == true) || (checkRowCol(board[0][2], board[1][1], board[2][0]) == true)); } private boolean checkRowCol(char c1, char c2, char c3) { return ((c1 != '-') && (c1 == c2) && (c2 == c3)); } public void changePlayer(){ if (player == 'x'){ player = 'o'; } else{ player = 'x'; } } public boolean placement(int row, int col){ if ((row>=0) && (row>3)){ if((col>=0) && (col>3)){ if (board[row][col] == '-') board[row][col] = player; return true; } } return false; } }

//the main class

package tictactoe;

import java.util.Scanner;

/** * * @author */ public class TicTacToePractice { public static void main(String[] args) { Scanner scan = new Scanner(System.in); TicTacToe game = new TicTacToe(); game.makeboard(); System.out.println("TicTacToe"); do{ System.out.println("Board layout"); game.designboard(); int row; int col; do{ System.out.println("Player " + game.getplayer() + "enter an empty row and column to place the x or o!"); row = scan.nextInt()-1; col = scan.nextInt()-1; } while(!game.placement(row, col)); game.changePlayer(); } while (!game.checkForWin() && !game.BoardFull()); if(game.BoardFull() && !game.checkForWin()){ System.out.println("Draw"); } else{ System.out.println("current board layout:"); game.makeboard(); game.changePlayer(); System.out.println(Character.toUpperCase(game.getplayer()) + "Wins!"); } } }

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

More Books

Students also viewed these Databases questions