Java tic tac toe


Salut, j'écris un jeu tic tac toe. J'ai précisé dans les commentaires de mon code ce dont j'ai besoin. Ce avec quoi j'ai du mal en ce moment, c'est de créer une méthode getMove. Je suppose que je devrais appeler la méthode getMove dans mes instructions if/else après avoir appuyé sur la ligne et la colonne?

Je ne sais pas comment obtenir les numéros de ligne / colonne et les mettre dans mon tableau pour que l'utilisateur a entré.

Voici mon 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
}
Author: jSeesFor3ver, 2013-10-23

1 answers

Je voudrais essayer quelque chose comme ça pour obtenir le mouvement. Gardez à l'esprit que cela fait un peu depuis que je joue avec java. Je suppose que le tableau de caractères 2D a des valeurs null pour commencer. Je recommande de changer cette déclaration en et de la déplacer dans le global maybe:

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

Alors j'appellerais getMove après que la deuxième lecture soit faite comme ceci:

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

Vous voudriez attraper une exception car vous ne savez pas si l'utilisateur va réellement entrer un entier. Une boucle d'un certain type (par exemple comme tandis que) fonctionne assez bien pour le faire.

Votre fonction getMove devrait être quelque chose comme ceci:

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.
}
 0
Author: Andrew Jackson, 2013-10-23 00:02:47