我该如何阻止x和o重写彼此呢?

o4hqfura  于 2021-07-08  发布在  Java
关注(0)|答案(0)|浏览(193)

到目前为止,我添加了一个if语句来计算tictactoe gameboard中的某个特定位置是否已经有一个角色,这个方法基本上是有效的,但是它仍然存在一些问题,因为它只能工作一次。这意味着,如果我输入了行和列,并且在特定的行和列中已经有一个字符,它将阻止我放置它,这是我的意图,尽管如果我错误地放置了两次,它将不再工作,并允许我覆盖该特定位置的字符。
我已经尝试过使用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;
  }

}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题