Search code examples
javamethodstic-tac-toe

How I can print the printBoard with modification?


I write a TicTacToe game.

Here I create a 3x3 table with char [][] board, then call method printBoard(). I input for example X_X_O____, and print this table with these characters.

In the changeBoard() method I want to input coordinates of board[][] and if there is char _ this will be replaced with X. I give the coordinates at compile to see that the coordinate is _ but when call method printBoard(), the console prints the same board without any changes. Can you please help me as I don't know what I am doing wrong?

import java.util.Scanner;

public class TicTacToe {
     private char[][] board = new char[3][3];
     private String state;
     private int n;
     private int m;
     private int i;
     private int j;
     Scanner sc = new Scanner(System.in);
     public TicTacToe() {
         System.out.print("Enter cells: ");
         this.state = sc.nextLine();
     }
 
     public void printBoard() {
         int nextChar = 0;
         System.out.println("---------");
         for (i = 0; i < 3; i++) {
             System.out.print("| ");
             for (j = 0; j < 3; j++) {
                 board[i][j] = state.charAt(nextChar++);
                 System.out.print(board[i][j] + " ");
             }
             System.out.println("|");
         }
         System.out.println("---------");
     }
     public void changeBoard() {
         while (true) {
             System.out.print("Enter the coordinates: ");
             n = sc.nextInt();
             m = sc.nextInt();
             if (n < 1 || n > 3 || m < 1 || m > 3) {
                 System.out.println("Coordinates should be from 1 to 3!");
             } else {
                 int x = n - 1;
                 int y = m - 1;
                 this.i = x;
                 this.j = y;
                 if (board[i][j] == '_') {
                     this.board[i][j] = 'X';
                     break;
                 } else {
                     System.out.println("This cell is occupied! Choose another one!");
                 }
 
             }
         }
        // printBoard();
     }
}

Solution

  • Why you use fields for your n, m, i, j? If you remove it, code will be cleaner.

    Also, you change your board in changeBoard, but then in line board[i][j] = state.charAt(nextChar++); you erase your changes. You can move this from printBoard to constructor. I think want to write something like this

    class TicTacToe {
    
        private char[][] board = new char[3][3];
        Scanner sc = new Scanner(System.in);
    
        public TicTacToe() {
            int nextChar = 0;
            System.out.print("Enter cells: ");
            String state = sc.nextLine();
    
            for (int i = 0; i < 3; i++) {
                for (int j = 0; j < 3; j++) {
                    board[i][j] = state.charAt(nextChar++);
                }
            }
    
        }
    
        public void printBoard() {
            System.out.println("---------");
            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 void changeBoard() {
            while (true) {
                System.out.print("Enter the coordinates: ");
                int n = sc.nextInt();
                int m = sc.nextInt();
                if (n < 1 || n > 3 || m < 1 || m > 3) {
                    System.out.println("Coordinates should be from 1 to 3!");
                } else {
                    int x = n - 1;
                    int y = m - 1;
                    if (board[x][y] == '_') {
                        this.board[x][y] = 'X';
                        break;
                    } else {
                        System.out.println("This cell is occupied! Choose another one!");
                    }
    
                }
            }
            printBoard();
        }
    }