Need help making the get function for player to work because it starts to run bu
ID: 3872416 • Letter: N
Question
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!");
}
}
}
Explanation / Answer
Below is your working program: -
TicTacToePractice.java
//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();
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!");
}
}
}
TicTacToe.java
//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 char getPlayer() {
return player;
}
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;
}
}
Sample Output
TicTacToe
Board layout
---------
|-|-|-|
--------
|-|-|-|
--------
|-|-|-|
--------
Player x enter an empty row and column to place the x or o!
1
1
Board layout
---------
|x|-|-|
--------
|-|-|-|
--------
|-|-|-|
--------
Player o enter an empty row and column to place the x or o!
2
2
Board layout
---------
|x|-|-|
--------
|-|o|-|
--------
|-|-|-|
--------
Player x enter an empty row and column to place the x or o!
3
1
Board layout
---------
|x|-|-|
--------
|-|o|-|
--------
|x|-|-|
--------
Player o enter an empty row and column to place the x or o!
2
1
Board layout
---------
|x|-|-|
--------
|o|o|-|
--------
|x|-|-|
--------
Player x enter an empty row and column to place the x or o!
2
3
Board layout
---------
|x|-|-|
--------
|o|o|x|
--------
|x|-|-|
--------
Player o enter an empty row and column to place the x or o!
1
3
Board layout
---------
|x|-|o|
--------
|o|o|x|
--------
|x|-|-|
--------
Player x enter an empty row and column to place the x or o!
1
2
Board layout
---------
|x|x|o|
--------
|o|o|x|
--------
|x|-|-|
--------
Player o enter an empty row and column to place the x or o!
3
2
Board layout
---------
|x|x|o|
--------
|o|o|x|
--------
|x|o|-|
--------
Player x enter an empty row and column to place the x or o!
3
3
Draw
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.