baekbb
baekbb

Reputation: 17

How to make java TicTacToe table?

How do I create a 3x3 without changing the given method?

I will need to create 4 methods beside the main method, but I am stuck on the display board.

Do I need to create a variable or something that represent each number so that I can use it to update the board each time after the user enter a move?

it's supposed to look like this:

1 2 3

4 5 6

7 8 9

the following is the main method can't be changed.

public class TicTacToe {
    public static void main (String args[]) {  
    char[] board = {'1', '2', '3', '4', '5', '6', '7', '8', '9'};
    char move = 'O';
    displayBoard(board);

    while (!checkWinner(board, move)){              
      move = changeMove(move);
      makeMove(board, move);  
      displayBoard(board);  
}

Upvotes: 0

Views: 741

Answers (3)

Marvin
Marvin

Reputation: 1

import java.util.Scanner;

public class TicTacToe {
public static void main (String args[]) {  
    char[] board = {'1', '2', '3', '4', '5', '6', '7', '8', '9'};
    char move = 'O';
    displayBoard(board);

while (!checkWinner(board, move)){              
        move = changeMove(move);
        makeMove(board, move);  
        displayBoard(board);  
}
}

public static void displayBoard(char[] b){
    int k = 0;
    for(int i = 0; i <3 ; i++){
        for (int j=0; j<3; j++){
            System.out.print(b[k]); 
            k++;
        }
        System.out.println();
}       
}

public static char changeMove(char m){
    if(m == 'O'){
        return 'X'; 
    }
    else return 'O';
}

public static void makeMove(char[] b, char m){
    boolean valid = false; 
    System.out.println("========== " + m + "'s Move: Enter [1-9] to choose square =========");  
    Scanner s= new Scanner(System.in);
    char square = s.next().charAt(0);
    for(int i=0; i<9; i++){
        if (b[i] == square){
            b[i] = m;
            valid = true;
        }
    }    
    if(!valid){
            System.out.println("Invalid move");
        makeMove(b,m);
    }
}

public static boolean checkWinner(char[] b , char m){   

    //Horizontal
    if(b[0] == b[1] && b[1] == b[2] ){
        System.out.println(b[0] +" Won!");
        return true;
    }
    if(b[3] == b[4] && b[4] == b[5]){
        System.out.println(b[3] +" Won!");
        return true;
}
    if(b[6] == b[7] && b[7] == b[8]){
        System.out.println(b[6] +" Won!");
        return true;
    }

    //Vertical
    if(b[0] == b[3] && b[3] == b[6]){
        System.out.println(b[0] +" Won!");
        return true;
    }
    if(b[1] == b[4] && b[4] == b[7]){
        System.out.println(b[1] +" Won!");
        return true;
    }
    if(b[2] == b[5] && b[4] == b[8]){
        System.out.println(b[2] +" Won!");
            return true;
    }

    //Diagonal
    if(b[0] == b[4] && b[4] == b[8]){
        System.out.println(b[0] +" Won!");
        return true;            
    }
    if(b[2] == b[4] && b[4] == b[6]){
        System.out.println(b[2] +" Won!");
        return true;
    }

    return false;    
}

}

Upvotes: 0

deezy
deezy

Reputation: 1480

For displayBoard(), you can use nested for loops. The outer loop iterates through each row, and the inner loop prints the three values that belong to the row.

public void displayBoard(char[] board) {
    for(int i = 1, i < 4, i++) {
        for(int j = 1, j < 4, j++) {
            System.out.print(board[(i*j)-1]);
        }
        System.out.println();
    }
}

Let me know if you need additional help.

Upvotes: 0

SwiftySwift
SwiftySwift

Reputation: 477

From your code provided, it looks like you are already given the board.

It is:

char[] board = {'1', '2', '3', '4', '5', '6', '7', '8', '9'};

What you want to do is turn it in to a 3x3 display.

This could be as simple as:

int k =0;
char[] board = {'1', '2', '3', '4', '5', '6', '7', '8', '9'};
for (int i=1;i<=3;i++) {
    for (int j=1;j<=3;j++) {
        System.out.print(board[k]);
        k++;
    }
System.out.println();
}

I think what is confusing you is thinking that you will have to update the tic-tac-toe logic part of the problem in the display itself.

What you should do, when you make a move, is change whatever needs to be changed in the board array, and then you can redraw the board.

Upvotes: 1

Related Questions