Jump to content

I have to make dunamic TicTacToe/five in a row for java introduction class last work.

 

I have problem where my gameBoard(X * X array) one side is shorter and thus i can't place everywhere. Not sure where the problem comes from. Array size is one integer so it should be a square...If someone could help that would be amazing!!

import java.util.*;
import java.util.Scanner;
import java.util.InputMismatchException;

class TicTacToe {
    
    private static boolean first = false;
    private static int aiScore = 0;
    private static int playerScore = 0;
    public static boolean error = false;
    public static int size = 0;
    public static char circle = 'O';
    public static char cross = 'X';
    
    public static void compTurn(Scanner console, char[][] board, char circle){

        System.out.println("Now it's my turn");
        System.out.println("");
        
        int size = board.length;
        
        int aiMoveVertical = (int) Math.random() * 1;
        
        while(aiMoveVertical > size) {
                 
            aiMoveVertical = (int) Math.random() * 1;
        }

        int aiMoveHorizontal = (int) Math.random() * 1;

        while(aiMoveHorizontal > size) {

            aiMoveHorizontal = (int) Math.random() * 1;
        }

        if(board[aiMoveVertical][aiMoveHorizontal] == ' ')  {

            board[aiMoveVertical][aiMoveHorizontal] = circle;
            createBoard(board);
            } else {

            }
    }
    
    public static void userTurn(Scanner console, char[][] board) {
        
    
        System.out.println("Select row");

        int row = console.nextInt() - 1;
        
        System.out.println("Select column");
        
        int col = console.nextInt();
        
        if(board[row][col] == ' '){
            board[row][col] = 'X';
            createBoard(board);
        }
        
        if(board[row][col] == 'X'){
            
          //  createBoard();
            
        } else if(board[row][col] == 'X'){
                
           // createBoard();

        }
    }
   
    public static int askSize() {
        
        try {
                
            Scanner input = new Scanner(System.in);

            System.out.println("Enter the size of the board.");

            size = input.nextInt();
                
            error = false;
            
                while(size < 3) {
                    
                            
                        System.out.println("You silly man or woman..Tic Tac Toe" + 
                                           " can't be played with board under 3x3." +
                                           " the game will be played on 3x3");
                        System.out.println();
                size++;

                }
                             
            } catch(InputMismatchException  e) {
                
            }
        return size;
    }
    
    public static char[][] initialitzeBoard() {
        
        char[][] board = new char;
        
        for(int row = 0; row < size; row++){
            for(int col = 0; col < size; col++){
                board[row][col] = ' ';
            }
        }
        return board;
    }
    
    public static void createBoard(char[][] board) {
        
    
        int numRow = board.length ;
        int numCol = board[0].length;
        
            System.out.print("     ");
    
        for (int i = 0; i < numCol; i++){
        
    System.out.print(i + "   ");
        }
        System.out.println(); // blank line after the header
        System.out.println("");
        
        for (int i = 0; i < numRow; i++) { // Prints the game board
        
            System.out.print(i + "  ");
        
            for (int j = 0; j < numCol; j++) {
                    
                if (j != 0) {
            
                    System.out.print("|");
                    System.out.print(" " + board[j] + " ");
                }
            }
            System.out.print("|");
            System.out.println();

            if (i != (numRow - 1)) {
            
                System.out.print("   ");        // separator line
                
                for (int j = 0; j < numCol + 1; j++) {
                    
                    if (j != 0){
                    
                    System.out.print("-");
                    System.out.print("---");
                    }
                    
                }
                System.out.println();
            }
            
        }
  System.out.println();
    }
   
    public static void startGame(Scanner console,char[][] board){

        //clears the board to start the game


        if(first == true){
            userTurn(console, board);
        }else{
            compTurn(console, board, circle);
        }
    }
    
    public static void startMenu(){

        Scanner console = new Scanner(System.in);

        System.out.println();
        System.out.println("Welcome to Tic-Tac-Toe " + "\n" + 
                           "To play this game," +
                           "\n" +
                           "You will choose where to" +
                           " place you mark using coordinates" + 
                           "\n" + " 1-10, " +
                           "you are the X");
        System.out.println();
        System.out.println("Would you like to go first? Yes or No.");
        
        String goFirst = console.nextLine();

        if(goFirst.substring(0,1).equalsIgnoreCase("y")){
            first = true;
        }
        
        askSize();
        char[][] board = initialitzeBoard();
        createBoard(board);
        startGame(console, board);
    }
    
    public static void main(String[] args) {
        
        startMenu();

    }
}

//end of file


 

 

Link to comment
https://linustechtips.com/topic/1006709-java-work-exam/
Share on other sites

Link to post
Share on other sites

Please use the code tags (it's the <> button) so this code is easier to read.

 

Until then:

Spoiler

 import java.util.*;
import java.util.Scanner;
import java.util.InputMismatchException;

class TicTacToe {
    
    private static boolean first = false;
    private static int aiScore = 0;
    private static int playerScore = 0;
    public static boolean error = false;
    public static int size = 0;
    public static char circle = 'O';
    public static char cross = 'X';
    
    public static void compTurn(Scanner console, char[][] board, char circle){

        System.out.println("Now it's my turn");
        System.out.println("");
        
        int size = board.length;
        
        int aiMoveVertical = (int) Math.random() * 1;
        
        while(aiMoveVertical > size) {
                 
            aiMoveVertical = (int) Math.random() * 1;
        }

        int aiMoveHorizontal = (int) Math.random() * 1;

        while(aiMoveHorizontal > size) {

            aiMoveHorizontal = (int) Math.random() * 1;
        }

        if(board[aiMoveVertical][aiMoveHorizontal] == ' ')  {

            board[aiMoveVertical][aiMoveHorizontal] = circle;
            createBoard(board);
            } else {

            }
    }
    
    public static void userTurn(Scanner console, char[][] board) {
        
    
        System.out.println("Select row");

        int row = console.nextInt() - 1;
        
        System.out.println("Select column");
        
        int col = console.nextInt();
        
        if(board[row][col] == ' '){
            board[row][col] = 'X';
            createBoard(board);
        }
        
        if(board[row][col] == 'X'){
            
          //  createBoard();
            
        } else if(board[row][col] == 'X'){
                
           // createBoard();

        }
    }
   
    public static int askSize() {
        
        try {
                
            Scanner input = new Scanner(System.in);

            System.out.println("Enter the size of the board.");

            size = input.nextInt();
                
            error = false;
            
                while(size < 3) {
                    
                            
                        System.out.println("You silly man or woman..Tic Tac Toe" + 
                                           " can't be played with board under 3x3." +
                                           " the game will be played on 3x3");
                        System.out.println();
                size++;

                }
                             
            } catch(InputMismatchException  e) {
                
            }
        return size;
    }
    
    public static char[][] initialitzeBoard() {
        
        char[][] board = new char;
        
        for(int row = 0; row < size; row++){
            for(int col = 0; col < size; col++){
                board[row][col] = ' ';
            }
        }
        return board;
    }
    
    public static void createBoard(char[][] board) {
        
    
        int numRow = board.length ;
        int numCol = board[0].length;
        
            System.out.print("     ");
    
        for (int i = 0; i < numCol; i++){
        
    System.out.print(i + "   ");
        }
        System.out.println(); // blank line after the header
        System.out.println("");
        
        for (int i = 0; i < numRow; i++) { // Prints the game board
        
            System.out.print(i + "  ");
        
            for (int j = 0; j < numCol; j++) {
                    
                if (j != 0) {
            
                    System.out.print("|");
                    System.out.print(" " + board[j] + " ");
                }
            }
            System.out.print("|");
            System.out.println();

            if (i != (numRow - 1)) {
            
                System.out.print("   ");        // separator line
                
                for (int j = 0; j < numCol + 1; j++) {
                    
                    if (j != 0){
                    
                    System.out.print("-");
                    System.out.print("---");
                    }
                    
                }
                System.out.println();
            }
            
        }
  System.out.println();
    }
   
    public static void startGame(Scanner console,char[][] board){

        //clears the board to start the game


        if(first == true){
            userTurn(console, board);
        }else{
            compTurn(console, board, circle);
        }
    }
    
    public static void startMenu(){

        Scanner console = new Scanner(System.in);

        System.out.println();
        System.out.println("Welcome to Tic-Tac-Toe " + "\n" + 
                           "To play this game," +
                           "\n" +
                           "You will choose where to" +
                           " place you mark using coordinates" + 
                           "\n" + " 1-10, " +
                           "you are the X");
        System.out.println();
        System.out.println("Would you like to go first? Yes or No.");
        
        String goFirst = console.nextLine();

        if(goFirst.substring(0,1).equalsIgnoreCase("y")){
            first = true;
        }
        
        askSize();
        char[][] board = initialitzeBoard();
        createBoard(board);
        startGame(console, board);
    }
    
    public static void main(String[] args) {
        
        startMenu();

    }
}

//end of file 

 

 

Link to comment
https://linustechtips.com/topic/1006709-java-work-exam/#findComment-12065720
Share on other sites

Link to post
Share on other sites

3 minutes ago, M.Yurizaki said:

Please use the code tags (it's the <> button) so this code is easier to read.

 

Until then:

  Reveal hidden contents


 import java.util.*;
import java.util.Scanner;
import java.util.InputMismatchException;

class TicTacToe {
    
    private static boolean first = false;
    private static int aiScore = 0;
    private static int playerScore = 0;
    public static boolean error = false;
    public static int size = 0;
    public static char circle = 'O';
    public static char cross = 'X';
    
    public static void compTurn(Scanner console, char[][] board, char circle){

        System.out.println("Now it's my turn");
        System.out.println("");
        
        int size = board.length;
        
        int aiMoveVertical = (int) Math.random() * 1;
        
        while(aiMoveVertical > size) {
                 
            aiMoveVertical = (int) Math.random() * 1;
        }

        int aiMoveHorizontal = (int) Math.random() * 1;

        while(aiMoveHorizontal > size) {

            aiMoveHorizontal = (int) Math.random() * 1;
        }

        if(board[aiMoveVertical][aiMoveHorizontal] == ' ')  {

            board[aiMoveVertical][aiMoveHorizontal] = circle;
            createBoard(board);
            } else {

            }
    }
    
    public static void userTurn(Scanner console, char[][] board) {
        
    
        System.out.println("Select row");

        int row = console.nextInt() - 1;
        
        System.out.println("Select column");
        
        int col = console.nextInt();
        
        if(board[row][col] == ' '){
            board[row][col] = 'X';
            createBoard(board);
        }
        
        if(board[row][col] == 'X'){
            
          //  createBoard();
            
        } else if(board[row][col] == 'X'){
                
           // createBoard();

        }
    }
   
    public static int askSize() {
        
        try {
                
            Scanner input = new Scanner(System.in);

            System.out.println("Enter the size of the board.");

            size = input.nextInt();
                
            error = false;
            
                while(size < 3) {
                    
                            
                        System.out.println("You silly man or woman..Tic Tac Toe" + 
                                           " can't be played with board under 3x3." +
                                           " the game will be played on 3x3");
                        System.out.println();
                size++;

                }
                             
            } catch(InputMismatchException  e) {
                
            }
        return size;
    }
    
    public static char[][] initialitzeBoard() {
        
        char[][] board = new char;
        
        for(int row = 0; row < size; row++){
            for(int col = 0; col < size; col++){
                board[row][col] = ' ';
            }
        }
        return board;
    }
    
    public static void createBoard(char[][] board) {
        
    
        int numRow = board.length ;
        int numCol = board[0].length;
        
            System.out.print("     ");
    
        for (int i = 0; i < numCol; i++){
        
    System.out.print(i + "   ");
        }
        System.out.println(); // blank line after the header
        System.out.println("");
        
        for (int i = 0; i < numRow; i++) { // Prints the game board
        
            System.out.print(i + "  ");
        
            for (int j = 0; j < numCol; j++) {
                    
                if (j != 0) {
            
                    System.out.print("|");
                    System.out.print(" " + board[j] + " ");
                }
            }
            System.out.print("|");
            System.out.println();

            if (i != (numRow - 1)) {
            
                System.out.print("   ");        // separator line
                
                for (int j = 0; j < numCol + 1; j++) {
                    
                    if (j != 0){
                    
                    System.out.print("-");
                    System.out.print("---");
                    }
                    
                }
                System.out.println();
            }
            
        }
  System.out.println();
    }
   
    public static void startGame(Scanner console,char[][] board){

        //clears the board to start the game


        if(first == true){
            userTurn(console, board);
        }else{
            compTurn(console, board, circle);
        }
    }
    
    public static void startMenu(){

        Scanner console = new Scanner(System.in);

        System.out.println();
        System.out.println("Welcome to Tic-Tac-Toe " + "\n" + 
                           "To play this game," +
                           "\n" +
                           "You will choose where to" +
                           " place you mark using coordinates" + 
                           "\n" + " 1-10, " +
                           "you are the X");
        System.out.println();
        System.out.println("Would you like to go first? Yes or No.");
        
        String goFirst = console.nextLine();

        if(goFirst.substring(0,1).equalsIgnoreCase("y")){
            first = true;
        }
        
        askSize();
        char[][] board = initialitzeBoard();
        createBoard(board);
        startGame(console, board);
    }
    
    public static void main(String[] args) {
        
        startMenu();

    }
}

//end of file 

 

 

Uhm..could you elaborate?

Link to comment
https://linustechtips.com/topic/1006709-java-work-exam/#findComment-12065735
Share on other sites

Link to post
Share on other sites

Just now, Teemu249 said:

Uhm..could you elaborate?

https://linustechtips.com/main/announcement/12-please-use-code-tags/

 

When you create a post, on the top of the editing area there's a bunch of buttons. Click on the "<>" button and it'll pop-up a dialog where you can post your code. Also on the bottom right there's a drop down menu, select "C Languages" to get it to properly highlight the code.

Link to comment
https://linustechtips.com/topic/1006709-java-work-exam/#findComment-12065748
Share on other sites

Link to post
Share on other sites

Just now, M.Yurizaki said:

https://linustechtips.com/main/announcement/12-please-use-code-tags/

 

When you create a post, on the top of the editing area there's a bunch of buttons. Click on the "<>" button and it'll pop-up a dialog where you can post your code. Also on the bottom right there's a drop down menu, select "C Languages" to get it to properly highlight the code.

Yeah figured that out right after asking..

Link to comment
https://linustechtips.com/topic/1006709-java-work-exam/#findComment-12065751
Share on other sites

Link to post
Share on other sites

So anyway, when you say you can't place a piece everywhere, I'm going to assume this means a column or a row isn't showing on the printout despite the array being a "square". If that's the case, that points to the printout being a problem.

Link to comment
https://linustechtips.com/topic/1006709-java-work-exam/#findComment-12065766
Share on other sites

Link to post
Share on other sites

Just now, M.Yurizaki said:

So anyway, when you say you can't place a piece everywhere, I'm going to assume this means a column or a row isn't showing on the printout despite the array being a "square". If that's the case, that points to the printout being a problem.

When I tried to place to the "missing" column it states that it is out of bounds

Enter the size of the board.
4
     0   1   2   3   

0  |    |    |     |
   ----------------
1  |    |    |     |
   ----------------
2  |    |    |     |
   ----------------
3  |    |    |     |

Select row
4
Select column
4
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4

Link to comment
https://linustechtips.com/topic/1006709-java-work-exam/#findComment-12065775
Share on other sites

Link to post
Share on other sites

4 minutes ago, Teemu249 said:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4

Arrays in Java are zero-indexed. The zeroth index is the first row/column.

 

Though I think this stems from your userTurn function, and where you're declaring col.

Link to comment
https://linustechtips.com/topic/1006709-java-work-exam/#findComment-12065793
Share on other sites

Link to post
Share on other sites

4 minutes ago, Teemu249 said:

When I tried to place to the "missing" column it states that it is out of bounds

Enter the size of the board.
4
     0   1   2   3   

0  |    |    |     |
   ----------------
1  |    |    |     |
   ----------------
2  |    |    |     |
   ----------------
3  |    |    |     |

Select row
4
Select column
4
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4

The code is working as intended. The print out for the numbers are off by one.

 

EDIT: Remember that arrays are 0-based. A size of 4 means the maximum index value is 3.

Link to comment
https://linustechtips.com/topic/1006709-java-work-exam/#findComment-12065797
Share on other sites

Link to post
Share on other sites

1 minute ago, M.Yurizaki said:

The code is working as intended. The print out for the numbers are off by one.

But anything else goes right where it is supposed to but the right most column..

 

Enter the size of the board.
4
     0   1   2   3   

0  |   |   |   |
   ----------------
1  |   |   |   |
   ----------------
2  |   |   |   |
   ----------------
3  |   |   |   |

Select row
1
Select column
1
     0   1   2   3   

0  | X |   |   |
   ----------------
1  |   |   |   |
   ----------------
2  |   |   |   |
   ----------------
3  |   |   |   |

Link to comment
https://linustechtips.com/topic/1006709-java-work-exam/#findComment-12065802
Share on other sites

Link to post
Share on other sites

3 minutes ago, mshaugh said:

Arrays in Java are zero-indexed. The zeroth index is the first row/column.

I know but I get error only from right most column. If I place it to 1 and 1 (so 0 and 0 on the board) it works

Enter the size of the board.
4
     0   1   2   3   

0  |   |   |   |
   ----------------
1  |   |   |   |
   ----------------
2  |   |   |   |
   ----------------
3  |   |   |   |

Select row
1
Select column
1
     0   1   2   3   

0  | X |   |   |
   ----------------
1  |   |   |   |
   ----------------
2  |   |   |   |
   ----------------
3  |   |   |   |

Link to comment
https://linustechtips.com/topic/1006709-java-work-exam/#findComment-12065807
Share on other sites

Link to post
Share on other sites

1 minute ago, Teemu249 said:

But anything else goes right where it is supposed to but the right most column..

 

Enter the size of the board.
4
     0   1   2   3   

0  |   |   |   |
   ----------------
1  |   |   |   |
   ----------------
2  |   |   |   |
   ----------------
3  |   |   |   |

Select row
1
Select column
1
     0   1   2   3   

0  | X |   |   |
   ----------------
1  |   |   |   |
   ----------------
2  |   |   |   |
   ----------------
3  |   |   |   |

Ah, well then here's your problem now you've told me that:

int col = console.nextInt();

Something's missing here.

Link to comment
https://linustechtips.com/topic/1006709-java-work-exam/#findComment-12065811
Share on other sites

Link to post
Share on other sites

1 minute ago, mshaugh said:

Review the first 4 lines of userTurn().

4 and 4 placed it to 4 and 3

 

Select row
4
Select column
4
     0   1   2   3   

0  |   |   |   |
   ----------------
1  |   |   |   |
   ----------------
2  |   |   |   |
   ----------------
3  |   |   | X |

public static void userTurn(Scanner console, char[][] board){
        
    
        System.out.println("Select row");

        int row = console.nextInt() - 1;
        
        System.out.println("Select column");
        
        int col = console.nextInt() - 1;(tried with and without -1)

........

Link to comment
https://linustechtips.com/topic/1006709-java-work-exam/#findComment-12065821
Share on other sites

Link to post
Share on other sites

4 minutes ago, M.Yurizaki said:

Ah, well then here's your problem now you've told me that:


int col = console.nextInt();

Something's missing here.

4 and 4 placed it to 4 and 3

 

Select row
4
Select column
4
     0   1   2   3   

0  |   |   |   |
   ----------------
1  |   |   |   |
   ----------------
2  |   |   |   |
   ----------------
3  |   |   | X |

public static void userTurn(Scanner console, char[][] board){
        
    
        System.out.println("Select row");

        int row = console.nextInt() - 1;
        
        System.out.println("Select column");
        
        int col = console.nextInt() - 1;(tried with and without -1)

........

Link to comment
https://linustechtips.com/topic/1006709-java-work-exam/#findComment-12065827
Share on other sites

Link to post
Share on other sites

5 minutes ago, Teemu249 said:

4 and 4 placed it to 4 and 3

 

Select row
4
Select column
4
     0   1   2   3   

0  |   |   |   |
   ----------------
1  |   |   |   |
   ----------------
2  |   |   |   |
   ----------------
3  |   |   | X |

public static void userTurn(Scanner console, char[][] board){
        
    
        System.out.println("Select row");

        int row = console.nextInt() - 1;
        
        System.out.println("Select column");
        
        int col = console.nextInt() - 1;(tried with and without -1)

........

There's still something wrong with the print routine. Specifically:

 

for (int j = 0; j < numCol; j++) {

  if (j != 0) {

    System.out.print("|");
    System.out.print(" " + board[j] + " ");
  }
}
Link to comment
https://linustechtips.com/topic/1006709-java-work-exam/#findComment-12065846
Share on other sites

Link to post
Share on other sites

1 minute ago, M.Yurizaki said:

There's still something wrong with the print routine. Specifically:

 


for (int j = 0; j < numCol; j++) {

  if (j != 0) {

    System.out.print("|");
    System.out.print(" " + board[j] + " ");
  }
}

it is like this for me

 

for (int j = 0; j < numCol; j++) {
                    
                if (j != 0) {
            
                    System.out.print("|");
                    System.out.print(" " + board[i][j] + " ");

 

Link to comment
https://linustechtips.com/topic/1006709-java-work-exam/#findComment-12065850
Share on other sites

Link to post
Share on other sites

1 minute ago, Teemu249 said:

it is like this for me

 


for (int j = 0; j < numCol; j++) {
                    
                if (j != 0) {
            
                    System.out.print("|");
                    System.out.print(" " + board[i][j] + " ");

 

Well ask yourself this: why is this skipping index 0?

Link to comment
https://linustechtips.com/topic/1006709-java-work-exam/#findComment-12065857
Share on other sites

Link to post
Share on other sites

5 minutes ago, Teemu249 said:

Can you see easily why my ai only places to 1 and 1(or 0 and 0 on the gameboard), it causes acrash after one round.

Math.random() produces a floating point number between 0 inclusive and 1 exclusive. The way you have it set up is it'll never be anything other than 0.

 

Not sure why it crashes after the first round though. Or rather it's not obvious to me.

Link to comment
https://linustechtips.com/topic/1006709-java-work-exam/#findComment-12065914
Share on other sites

Link to post
Share on other sites

3 minutes ago, M.Yurizaki said:

Math.random() produces a floating point number between 0 inclusive and 1 exclusive. The way you have it set up is it'll never be anything other than 0.

 

Not sure why it crashes after the first round though. Or rather it's not obvious to me.

I figured the random thing and now I just get random crashes that probably are related to not checking inputs and so on. Here is my updated code if you want to look at the changes:

 

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package TicTacToe;

import java.util.*;
import java.util.Scanner;
import java.util.InputMismatchException;

class TicTacToe {
    
    private static boolean first = false;
    private static int aiScore = 0;
    private static int playerScore = 0;
    public static boolean error = false;
    public static int size = 0;
    public static char circle = 'O';
    public static char cross = 'X';
    
    public static void compTurnErr(Scanner console, char[][] board) {
        
        compTurn(console, board, circle, cross);
    }
            
    public static void compTurn(Scanner console, char[][] board, char circle,
                                char cross){

        System.out.println("Now it's my turn");
        System.out.println("");
        
        int size = board.length;
        
        int aiMoveVertical = (int) (Math.random() * 100);
        
        while(aiMoveVertical > size) {
                 
            aiMoveVertical = (int) (Math.random() * 100);
        }

        int aiMoveHorizontal = (int) (Math.random() * 100);

        while(aiMoveHorizontal > size) {

            aiMoveHorizontal = (int) (Math.random() * 100);
        }

        if(board[aiMoveVertical][aiMoveHorizontal] == ' ') {

            board[aiMoveVertical][aiMoveHorizontal] = circle;
            createBoard(board);
            } else {
                compTurnErr(console, board);
            }
        userTurn(console, board);
    }
    
    public static void userTurnErr(Scanner console, char[][] board) {
        
        userTurn(console, board);
    }
    
    public static void userTurn(Scanner console, char[][] board){
        
    
        System.out.println("Select row");

        int row = console.nextInt() - 1;
        
        System.out.println("Select column");
        
        int col = console.nextInt() - 1;
        
        if(board[row][col] == ' '){
            board[row][col] = 'X';
            createBoard(board);
        } else {
            System.out.println("That space is already occupied");
          userTurnErr(console, board);
            
        }
        compTurn(console, board, circle, cross);
    }
   
    public static int askSize() {
        
        try {
                
            Scanner input = new Scanner(System.in);

            System.out.println("Enter the size of the board.");

            size = input.nextInt();
                
            error = false;
            
                while(size < 3) {
                    
                            
                        System.out.println("You silly man or woman..Tic Tac Toe" + 
                                           " can't be played with board under 3x3." +
                                           " the game will be played on 3x3");
                        System.out.println();
                size++;

                }
                             
            } catch(InputMismatchException  e) {
                
            }
        return size;
    }
    
    public static char[][] initialitzeBoard() {
        
        char[][] board = new char[size][size];
        
        for(int row = 0; row < size; row++){
            for(int col = 0; col < size; col++){
                board[row][col] = ' ';
            }
        }
        return board;
    }
    
    public static void createBoard(char[][] board) {
        
    
        int numRow = board.length ;
        int numCol = board[0].length;
        
            System.out.print("     ");
    
        for (int i = 0; i < numCol; i++){
    	
	System.out.print(i + "   ");
        }
        System.out.println(); // blank line after the header
        System.out.println("");
        
        for (int i = 0; i < numRow; i++) { // Prints the game board
        
            System.out.print(i + "  ");
        
            for (int j = 0; j < numCol; j++) {
                    

            
                    System.out.print("|");
                    System.out.print(" " + board[i][j] + " ");
                
            }
            System.out.print("|");
            System.out.println();

            if (i != (numRow - 1)) {
            
                System.out.print("   ");        // separator line
                
                for (int j = 0; j < numCol + 1; j++) {
                    
                    if (j != 0){
                    
                    System.out.print("-");
                    System.out.print("---");
                    }
                    
                }
                System.out.println();
            }
            
        }
  System.out.println();
    }
   
    public static void startGame(Scanner console,char[][] board){

        //clears the board to start the game


        if(first == true){
            userTurn(console, board);
        }else{
            compTurn(console, board, circle, cross);
        }
    }
    
    public static void startMenu(){

        Scanner console = new Scanner(System.in);

        System.out.println();
        System.out.println("Welcome to Tic-Tac-Toe " + "\n" + 
                           "To play this game," +
                           "\n" +
                           "You will choose where to" +
                           " place you mark using coordinates" + 
                           "\n" + " 1-10, " +
                           "you are the X");
        System.out.println();
        System.out.println("Would you like to go first? Yes or No.");
        
        String goFirst = console.nextLine();

        if(goFirst.substring(0,1).equalsIgnoreCase("y")){
            first = true;
        }
        
        askSize();
        char[][] board = initialitzeBoard();
        createBoard(board);
        startGame(console, board);
    }
    
    public static void main(String[] args) {
        
        startMenu();

    }
}

//end of file

 

Link to comment
https://linustechtips.com/topic/1006709-java-work-exam/#findComment-12065929
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×