简体   繁体   中英

Java tic tac toe

Hi I am writing a tic tac toe game. I have spelled out in comments in my code what I need. What I am having trouble with right now is making a getMove method. I assume I would need to call the getMove method in my if/else statements after the row and column are pressed?

I am unsure about how to go from getting the row/column numbers and putting them into my board for what the user inputted.

Here is my code:

import java.util.*;

public class TicTac{
   //declare a constant variable
   public static final int SIZE = 3; //size of each row and each column

   public static void main(String[] args) {

      //initialize the board
      char[][] board = new char[3][3];

      //display the board
      displayBoard(board);

      //prompt for the first player
      //determine if X or O is pressed

      System.out.println("Who wants to go first (X or O)? ");
      Scanner xOrO = new Scanner(System.in);
      String entOp = xOrO.nextLine();
      char enterOp = entOp.charAt(0);

      if (enterOp == 'X'){
           System.out.println("Enter a row (0,1,2) for player X: ");
           Scanner enterRow = new Scanner(System.in);
           int fTurn = enterRow.nextInt();

           System.out.println("Enter a column (0,1,2) for player X: ");
           Scanner enterCol = new Scanner(System.in);
           int fCol = enterCol.nextInt();

      }  else if (enterOp == 'O') {
           System.out.println("Enter a row (0,1,2) for player O: ");
           Scanner enterRow = new Scanner(System.in);
           int fTurn = enterRow.nextInt();

           System.out.println("Enter a column (0,1,2) for player X: ");
           Scanner enterCol = new Scanner(System.in);
           int fCol = enterCol.nextInt();

      }  else {
           System.out.println("Must enter either X or O");  
         }

      //and display the board
      //displayBoard(board);

      }

   //initializeBoard method


   //displayBoard method
   public static void drawLine() {
      for (int i = 0; i <= 9 * SIZE; i++) {
         System.out.print("-");
      }
      System.out.println();
   }

   public static void displayBoard(char[][] board) {
      drawLine();
      for (int i = 0; i < SIZE; i++) {
         for (int j = 0; j < SIZE; j++) {
            System.out.print("| " + board[i][j] + " ");
         }
         System.out.println("|");
         drawLine();
      }
      System.out.println();
   }


   //getMove method: to prompt the current player for target position. And place the mark in the position if the position is available.
//    public static void getMove() {
//    
//    
//    
//    
//    
//    }
   //findWinner method: after each move, check the board see if there is a winner



   //hasEmptyCell method: check if there is still empty spot in the board
}

I would try something like this to get the move. Bear in mind it has been a little bit since I have played with java. I'm assuming the 2D char array has null values to begin with. I recommend changing that declaration to and move it into the global maybe:

 char[][] board = {{'', '', ''},{'', '', ''},{'', '', ''}};

Then I would call getMove after the second read is done like this:

int x = Integer.parseInt(firstTurn);
int y = Integer.parseInt(firstCol);
getMove(x, y, 'X');

You would want to catch an exception since you don't know if the user will actually input an integer. A loop of some kind (such as while) works pretty good to do this.

Your getMove function should be something like this:

public static bool getMove(int x, int y, char player) {
    if (board[x][y] == '') {
        board[x][y] = player;
        return true;  //Here you are returning true to show the spot was available.
    }
    return false; //And here you are returning false to show the spot was not available.
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM