0

我一直在尝试使用几种静态方法制作这个滴答作响的游戏。我对此仍然相对较新,并且无法完全弄清楚鼠标点击的整个想法。我已经阅读了有关 mouseclick 和 mouseevent 的内容,但它并不完全有意义,并且当我尝试这样做时会遇到很多错误。最初我有部分以自己的方法获取鼠标信息,但后来我不知道如何返回 x 和 y 值。所以我添加了填充下面数组的方法。现在我搞砸了它并设法让它们以自己的方法获得,但仍然在运行程序时遇到问题。(他们不必采用自己的方法,我只是认为它会简化事情)当我运​​行这个程序时,它所做的只是打印无限量的行,说明我点击了哪一行和哪一列,并在第一行和第一列中放置一个 O,无论我是否点击。此外,它似乎也不会在玩家之间切换回合。如果有人可以帮助我,我将不胜感激。谢谢!

import java.util.*;
public class Game {

  public static int x;
  public static int y;
  public static double a;
  public static double b;
  public static int empty = 0;
  public static int Cross = 1;
  public static int Oh = -1;
  public static double[][] board = new double[3][3];
  public static int currentPlayer;
  public static int Point;


  public static void main (String args[]) {



    drawBoard();


    Fill();
  }

    public static void drawBoard(){
    StdDraw.setXscale(0,9);
    StdDraw.setYscale(0,9);
    StdDraw.setPenRadius(.01);
    StdDraw.setPenColor(StdDraw.BLACK);
    StdDraw.line(0,3,9,3);
    StdDraw.line(0,6,9,6);
    StdDraw.line(3,0,3,9);
    StdDraw.line(6,0,6,9);
  } //end draw board




  //get mouse click and turn into array spot
  public static void Mouse(){
    while(true){
      if (StdDraw.mousePressed()){
        a = StdDraw.mouseX();
        b = StdDraw.mouseY();
        System.out.println( a + " " + b);
      }



      //set column
      if ( 0<=a && a< 3){
        x =  0;}
      if ( 3<=a && a<6){
        x = 1;}
      if ( 6<=a && a< 9){
        x = 2;}
      //set row
      if ( 0<=b && b< 3){
        y = 0;}
      if ( 3<=b && b< 6){
        y = (int)1;}
      if ( 6<=b && b< 9){
        y = 2;}
      System.out.println("You clicked in Row" + x + "and column" +y);
    }
  }

      public static void Fill(){
      //fill array
        Mouse();
      boolean validInput = false;
      do{    
      for (int i = 0 ; i <=9 ; i++){
    if (i % 2 == 0){
          currentPlayer = Cross;
        }
        else {
          currentPlayer = Oh;
        }}
          if (0 <= x && x<=2 && 0 <=y && y <= 2 && board[x][y] == 0){

            //fill array spot
        board[x][y] = currentPlayer;
        //check game status and print board
        GameStatus();
        PrintBoard();
        validInput = true; //input is good, exit the loop
        }
        else { 
          System.out.println("This move is not valid. Try again.");
          }
      }while (!validInput);
    }














    public static void PrintBoard(){
  for (int j = 0; j<=2; j++){
    for (int k = 0; k<=2; k++){
      if (board[j][k] == 0){
        //do nothing leave empty
      }
      if (board[j][k] == 1){
        double l = ((j+1) * 3) - 1.5;
        double m = ((k+1) * 3) - 1.5;
        //print x
        StdDraw.text(l,m,"X");}
      if (board[j][k] == -1){
        double l = ((j+1) * 3) - 1.5;
        double m = ((k+1) * 3) - 1.5;
        //print O
        StdDraw.text(l,m,"O");}
    }
  }
    }


  public static void GameStatus(){
        //check for win
        if (// First column
            board[0][0] == currentPlayer
              && board[0][1] == currentPlayer
              && board[0][2] == currentPlayer
              //second column
              || board[1][0] == currentPlayer
              && board[1][1] == currentPlayer
              && board[1][2] == currentPlayer
              //third column
              || board[2][0] == currentPlayer
              && board[2][1] == currentPlayer
              && board[2][2] == currentPlayer
              //first row
              ||board[0][0] == currentPlayer
              && board[1][0] == currentPlayer
              && board[2][0] == currentPlayer
              //second row
              || board[0][1] == currentPlayer
              && board[1][1] == currentPlayer
              && board[2][1] == currentPlayer
              //third row
              || board[0][2] == currentPlayer
              && board[1][2] == currentPlayer
              && board[2][2] == currentPlayer
              //diagonal 1
              || board[0][2] == currentPlayer
              && board[1][1] == currentPlayer
              && board[2][0] == currentPlayer
              // diagonal 2
              || board[2][2] == currentPlayer
              && board[1][1] == currentPlayer
              && board[0][0] == currentPlayer){
          //X win
          while (currentPlayer==1){
            StdDraw.text(0.5, 0.5, "X Won!");}
          //O win
          while (currentPlayer==-1){
            StdDraw.text(0.5, 0.5, "O Won!");}
          return;
        }

        //draw
        if  (board[0][0] != 0 
               && board[0][1] != 0
               && board[0][2] != 0
               && board[1][0] != 0
               && board[1][1] != 0
               && board[1][2] != 0
               && board[2][0] != 0
               && board[2][1] != 0
               && board[2][2] != 0){
          StdDraw.text(0.5, 0.5, "Cat's Game!");
          return;}

        //still playing
        else {
          System.out.println("Keep Playing.");
          //keep playing
        }

  }//Ends playerMove

}// end game
4

2 回答 2

0

首先,您有一个“while(true)”语句。那是一个无限循环,你需要一些东西来告诉它爆发。这就是为什么您会看到垃圾邮件的原因。

其次,您用来获取“mousePressed”事件的 StdDraw 对象在哪里?如果您只是单击,我建议使用“MOUSE_CLICKED”事件,但即使您一直使用 mousePressed,如果未按下鼠标,您也需要确保它没有检测到它。

于 2016-11-02T23:28:28.553 回答
0

它不断打印这些行,因为您在 Mouse() 方法中创建了一个无限循环,并且它每秒检查 if 语句中的条件数百万次。

我建议创建一个单独的方法,例如 gameLoop(),并从 gameLoop() 中调用所有其他方法 [精心构造]。

在 stdlib 包文档中,我们读到:

[键盘和鼠标输入] 您应该在动画循环中使用这些方法,在尝试轮询鼠标的当前状态之前等待一小段时间。

所以在你的游戏中你可以尝试这样的事情:

gameLoop() {
    isPlaying = true;
    drawBoard();
    ...;
    while (isPlaying) {
        isPressed = false;
        checkMousePressed();
        if (isPressed) {
            checkMove();
            ...;
        }  
        Thread.sleep(100); // 100 ms, so you repeat the loop 10 times/sec.
    }
    printWinner();
}

此外,方法名称的一个好习惯是使用动词,而不是名词。因此,您说出方法的作用,换句话说,您说明了它的目的,因此更容易遵循程序的逻辑。如果您将其称为 mouse() ,则不清楚该方法应该做什么。

于 2016-11-02T23:29:11.517 回答