Changing an array input to a display a letter such as “X” or “O”

I’m in the middle of my program for tic tac toe, and we’re using 2d arrays and methods. I’ve been struggling to figure out how to change the stored null value of 0 to spaces so that it’d be

[] [] []  instead of [0] [0] [0]
[] [] []             [0] [0] [0]
[] [] []             [0] [0] [0]

and on changing the given user position from 1 to the display letter x and -1 to the letter O when printing the board. How do i do this?

This is my code:

import java.util.Scanner;


public class Board{
 int[][] board = new int[4][4];
 boolean victory = false;//boolean victory to be set to 0 (False) until made otherwise.
 public void play() {
  Scanner scanner = new Scanner(System.in);

int playeronerow=0, playeronecol=0,playertworow=0,playertwocol=0; //input variables

int win=-1; //win variable for conditions to be met

printBoard();//displaying game board

   while(!victory){ //continues the game until victory is true(game winning conditions met)
    while(true) { //continues the valid/invalid loop while the game is still on.

       System.out.println("Player one: Choose your row(1-3)");
       playeronerow=scanner.nextInt();//player one input for row
       System.out.println("Player one: choose your column(1-3)");
       playeronecol=scanner.nextInt();//player one input for col
       if (board[playeronerow-1][playeronecol-1]==0) { //checks if spot is not taken       
       board[playeronerow-1][playeronecol-1]=1;//subtracts 1 from the input because 0 isnt optionable.

       printBoard();//displaying game board

       break;//ends the loop
       }
       else {
        System.out.println("Invalid");
       }
    }    

       win=CheckWin();//checks all winning conditions after input
       if(win==1){//sets win to 1 if conditions met so that victory becomes true therefore ending game.
           break;
       }

       while (true) { //repitition of above but for player 2.

       System.out.println("Player two: Choose your row(1-3)");
       playertworow=scanner.nextInt();
       System.out.println("Player two: choose your column(1-3)");
       playertwocol=scanner.nextInt();
      if ( board[playertworow-1][playertwocol-1]== 0)
      {
       board[playertworow-1][playertwocol-1]=-1;
       printBoard();
       break;
      }
      else {
       System.out.println("Invalid");
      }
       }
       win=CheckWin();
       if(win==1){
           break;
       }

  }
 }
   public int CheckWin(){//method for checking winning conditions
       if (sumRow(0) == 3){ //if the first row is equivilent to 3 , x wins
   System.out.println("X won the game");
   victory=true; //returns the value as 1 so that the game can end
   return 1;
  }
  else if (sumRow(0) == -3){
   System.out.println("O won the game"); //if the first row is equivilent to 3 , x wins
   victory=true;//returns the value as 1 so that the game can end
   return 1;
  }



  if (sumRow(1) == 3){
   System.out.println("X won the game"); //if the second row is equivilent to 3 , x wins
   victory=true;//returns the value as 1 so that the game can end
   return 1;
  }
  else if (sumRow(1) == -3){
   System.out.println("O won the game"); //if the second row is equivilent to -3 , o wins
   victory=true;//returns the value as 1 so that the game can end
   return 1;
  }


  if (sumRow(2) == 3){
   System.out.println("X won the game"); //if the third row is equivilent to 3 , x wins
   victory=true;//returns the value as 1 so that the game can end
   return 1;
  }
  else if (sumRow(2) == -3){
   System.out.println("O won the game"); //if the third row is equivilent to -3 , o wins
   victory=true;//returns the value as 1 so that the game can end
   return 1;
  }
   if (sumCol(0) == 3){
   System.out.println("X won the game"); //if the first col is equivilent to 3 , x wins
   victory=true;//returns the value as 1 so that the game can end
   return 1;
  }
  else if (sumCol(0) == -3){
   System.out.println("O won the game"); //if the first col is equivilent to -3 , o wins
   victory=true;//returns the value as 1 so that the game can end
   return 1;
  }

  if (sumCol(1) == 3){
   System.out.println("X won the game"); //if the 2nd col is equivilent to 3 , x wins
   victory=true;//returns the value as 1 so that the game can end
   return 1;
  }
  else if (sumCol(1) == -3){
   System.out.println("O won the game"); //if the 2nd col is equivilent to -3 , o wins
   victory=true;//returns the value as 1 so that the game can end
   return 1;

  }



int diagonal= board[0][0] + board[1][1] + board[2][2]; //coded positions for first diagonal

if(diagonal==3){
    System.out.println("X won the game"); //if the first diagonal is equivilent to 3 , x wins
    victory=true;//returns the value as 1 so that the game can end
    return 1;
}
else if (diagonal==-3){
    System.out.println("O won the game"); //if the first diagonal is equivilent to -3 , o wins
    victory=true;//returns the value as 1 so that the game can end
    return 1;
}
int diagonal2= board[0][2] + board[1][1] + board[2][0]; //coded positions for second diagonal
if (diagonal2==3){
    System.out.println("X won the game"); //if the second diagonal is equivilent to 3 , x wins
    victory=true;//returns the value as 1 so that the game can end
    return 1;
}
else if (diagonal2==-3){
    System.out.println("O won the game"); //if the second diagonal is equivilent to -3 , o wins
    victory=true;//returns the value as 1 so that the game can end
    return 1;
}

  if (sumCol(2) == 3){
   System.out.println("X won the game"); //if the third col is equivilent to 3 , x wins
   victory=true;//returns the value as 1 so that the game can end
   return 1;
  }
  else if (sumCol(2) == -3){
   System.out.println("O won the game"); //if the third col is equivilent to 3 , x wins
   victory=true;//returns the value as 1 so that the game can end
   return 1;
  }
  return 0; //returns a 0 value to redo the loop after each player input if none of the above conditions are met
   }



 public void printBoard(){ //method for displaying the board

  for (int row = 0; row < 3; row++) { 
   for (int col = 0; col < 3; col++) {  
    System.out.print("["+board[row][col]+"]" + " "); //printing the board

   }
   System.out.println("");//spaces between each row and col
  }
 }

 /*public int calculateSum(){ //calculating sum method
  int sum = 0;
  for (int row = 0; row < board.length; row++) {
   for (int col = 0; col < board.length; col++) {
    //sum += board[row][col];
    sum = board[row][col] + sum;

   }
  }
  return sum;
 }*/

 public int sumRow(int row){
  int sum = 0;
  //sum = board[row][0] + board[row][1] + board[row][2];
  for (int i = 0; i < board.length; i++) {
   sum = sum + board[row][i];
  }
  return sum;
 }


 public int sumCol(int col){
  int sum = 0;
  for (int i = 0; i < board.length; i++) {
   sum = sum + board[i][col];
  }
  return sum;
 }
}

Answer

The problem is that you are outputting the integer value (-1, 0 or 1) directly, without first converting it to X, O or a space. One possible solution would be to replace the line

System.out.print("["+board[row][col]+"]" + " "); //printing the board

with

System.out.print("[" + boardValueToString(board[row][col]) + "]" + " ");

and add the method boardValueToString:

public void printBoard() { // method for displaying the board
    for (int row = 0; row < 3; row++) {
        for (int col = 0; col < 3; col++) {
            System.out.print("[" + boardValueToString(board[row][col]) + "]" + " ");
        }
        System.out.println("");// spaces between each row and col
    }
}

public String boardValueToString(int value) {
        if (-1 == value) {
            return "O";
        }
        if (1 == value) {
            return "X";
        }
        return " ";
        // alt solution: return new String[]{"O", " ", "X"}[value + 1];
    }

Other comments: Your code is in dire need of formatting; CTRL+SHIFT+F will take care of that for you in eclipse.

Your CheckWin() function is far too long, mainly because the “X/O has won” output is repeated many times. There are only two possibilities, X has won or O has won, so there should be only two “if”s. Combine the conditions for a win using the || (or) operator.

Leave a Reply

Your email address will not be published. Required fields are marked *