首页 > 解决方案 > 如何阻止 X 和 O 相互覆盖 TicTacToe java?

问题描述

到目前为止,我添加了一个 if 语句来计算在井字棋游戏板的特定位置是否已经存在一个角色,这主要是有效的,但它仍然存在一些问题,因为它只有效一次。这意味着如果我输入行和列并且在该特定行和列中已经有一个字符,它将阻止我放置它,这是我想要的,尽管如果我两次错误地放置它,它就不再起作用并且让我覆盖那个特定位置的字符。

我已经尝试使用 while 循环执行此操作,但它没有成功,因为它一直说我输入的每个输入都是无效的。

我要防止的是覆盖游戏板中的字符,例如,如果第 0 行第 1 列已经有一个 X,如果我输入第 0 行和第 1 列,程序应该阻止我将 X 放在那里无限数次。

主要的

import java.util.Random;
import java.util.Scanner;
import java.util.InputMismatchException;

class Main {
  public static void main(String[] args) {
    Board b = new Board();
    Scanner sc = new Scanner(System.in);
    int count = 0;
    char p = 'X';
    int r = -1;
    int c = -1;

    //introduction to game
    System.out.println("-----------------------------------------------------------------");
    System.out.println("                              Welcome      ");
    System.out.println("-----------------------------------------------------------------");
    System.out.println("                                to         ");
    System.out.println("-----------------------------------------------------------------");
    System.out.println("                                my         ");
    System.out.println("-----------------------------------------------------------------");
    System.out.println("                             TicTacToe     ");
    System.out.println("-----------------------------------------------------------------");
    System.out.println("                             program!!!    ");
    System.out.println("-----------------------------------------------------------------");
    
    boolean error = false;

    boolean playing = true;
    while(playing){
      b.print();
      if(count % 2 == 0)
      {
        do {
          error = false;
          try {
            System.out.println("Your turn:");
            p = 'X';
            System.out.print("Enter row:");
            r = sc.nextInt();
            while(r < 0 || r > 2) 
            {
              System.out.println("Sorry, please enter a number that is ONLY in-between 0 and 2.");//sc.nextLine();
              System.out.print("Enter row:");
              r = sc.nextInt();
              if(r >= 0 && r <= 2)
              {
                break;
              }
              }
          } catch(InputMismatchException e) {
            error = true;
            System.out.println("Sorry, please enter a number in-between 0 and 2.");
            sc.nextLine();
          }
        }while(error);

        do{
          error = false;
          try {
            System.out.print("Enter column:");
            c = sc.nextInt();
            while(c < 0 || c > 2) 
            {
              System.out.println("Sorry, please enter a number that is ONLY in-between 0 and 2.");//sc.nextLine();
              System.out.print("Enter column:");
              c = sc.nextInt();
              if(c >= 0 && c <= 2)
              {
                break;
              }
              }
          } catch(InputMismatchException e) {
            error = true;
            System.out.println("Sorry, please enter a number in-between 0 and 2.");
            sc.nextLine();
          }
        }while(error);
      
      if(b.fullSpace(p)) {
        System.out.println("Invalid input, try again.");
          System.out.print("Enter row:");
          r = sc.nextInt();
          System.out.print("Enter column:");
          c = sc.nextInt();
          }else if(b.emptySpace()) {
            System.out.println("You've made it here!");
            //break;
            }
      
      } else if(count % 2 == 1) {
        System.out.println("Computer's turn:");
        p = 'O';
        Random rand = new Random();
        r = rand.nextInt(2 - 0 + 1) + 0;
        c = rand.nextInt(2 - 0 + 1) + 0;
        //int computer = rand.nextInt();
      }

      /*if(getWinner()) {
        System.out.println("You won!");
        playing = false;
      }
      */


      

      //if statement for only X's turn

      //ai that picks a random number from 0 to 2
      
      b.move(p,r,c);
      if(b.isWinner('X') || b.isWinner('O') || b.isTied())
        playing = false;
      count++;
    }
    b.print();

if(b.isWinner('X')) {
  System.out.println("You win! Congratulations!");
}

if(b.isWinner('O')) {
  System.out.println("Computer wins! Boohoo!");
}

if(b.isTied()) {
  System.out.println("Nobody wins! It's a tie!");
}
    
    
  }
}

木板

import java.util.Random;


public class Board {
  private char[][] board = new char[3][3];

  public Board(){
    for(int r = 0; r < 3; r++){
      for(int c = 0; c < 3; c++){
        board[r][c] = ' ';
      }
    }
  }

  public void move(char p, int r, int c){
    board[r][c] = p;
    while(board[r][c] == 'X' || board[r][c] == 'O')
    {
      Random randTwo = new Random();
      r = randTwo.nextInt(2 - 0 + 1) + 0;
      c = randTwo.nextInt(2 - 0 + 1) + 0;
      if(board[r][c] != 'X' || board[r][c] != 'O') {
        break;
      }
    }
  }

  public char get(int r, int c){
    return board[r][c];
  }

  public void print(){
    for(int r = 0; r < 3;r++){
      for(int c = 0; c < 3;c++){
        System.out.print("[" + board[r][c] + "]");
      }
      System.out.println();
    }
  }

  public boolean isWinner(char p){
    for(int r = 0; r < 3; r++) {
      if(board[r][0] == p && board[r][1] == p && board[r][2] == p) {
        return true;
        }
      }
      for(int c = 0; c < 3; c++) {
        if(board[0][c] == p && board[1][c] == p && board[2][c] == p) {
        return true;
        }
      }
      if(board[0][0] == p && board[1][1] == p && board[2][2] == p) {
        return true;
      } else if(board[0][2] == p && board[1][1] == p && board[2][0] == p) {
        return true;
        }
    return false;
    }


  public boolean isTied(){
    for(int r = 0; r < 3; r++) {
      for(int c = 0; c < 3; c++) {
        if(board[r][c] == ' ') {
          return false;
          }
        }  
      }
      return true;
  }

  public boolean fullSpace(char p) {
    for(int r = 0; r < 3; r++) {
      for(int c = 0; c < 3; c++) {
        if(board[r][c] == p) {
          return true;
        }
      }
    }
    return false;
  }

  public boolean emptySpace() {
    for(int r = 0; r < 3; r++) {
      for(int c = 0; c < 3; c++) {
        if(board[r][c] == ' ') {
          return true;
        }
      }
    }
    return false;
  }

}        

标签: javaooptic-tac-toe

解决方案


推荐阅读