java 处理中的井字棋

问题描述 投票:0回答:1

我目前正在处理中开发一个简单的 Tic-Tac-Toe 游戏,我在两个方面遇到了麻烦:

放置问题: X 和 O 符号未正确放置在网格上。位置似乎是随机的,我不确定为什么。每当我单击一个框时,它似乎没有放置在那个协调的框中

鼠标点击问题: 我已经设置了左键单击放置 X,右键单击放置 O,但它似乎没有按预期工作。

这是我当前的代码:

// Declare a 3x3 grid of TicTacToeBox objects
TicTacToeBox[][] grid = new TicTacToeBox[3][3];

// gameStatus:
// 0 - Display Home screen
// 1 - Display Tic Tac Toe grid
// 2 - Display Game over screen
int gameStatus = 0;

// Determine which player's turn it is 
int currentPlayer = 1;

void setup() {
  size(600, 600);
  displayHomeScreen();
}

void draw() {
  // Draw the appropriate screen based on gameStatus
  if (gameStatus == 1) {
    background(255);
    displayGrid();
  } else if (gameStatus == 2) {
    background(0);
    displayGameOver();
  }
}

void mousePressed() {
  // Check the gameStatus and respond to mouse clicks accordingly
  if (gameStatus == 1) {
    float boxSize = width / 3.0;
    int col = floor(mouseX / boxSize);
    int row = floor(mouseY / boxSize);

    // Check if the box is valid and empty
    if (isValidBox(row, col) && grid[row][col].symbol == ' ') {
      // Place X or O based on the mouse button and currentPlayer
      if (mouseButton == LEFT && currentPlayer == 1) {
        grid[row][col].symbol = 'X';
        currentPlayer = 2;
      } else if (mouseButton == RIGHT && currentPlayer == 2) {
        grid[row][col].symbol = 'O';
        currentPlayer = 1;
      }
    }
  } else if (gameStatus == 0 && mouseX > 250 && mouseX < 350 && mouseY > 250 && mouseY < 300) {
    // Transition to the game screen when PLAY is clicked
    gameStatus = 1;
  }
}

void displayGrid() {
  float boxSize = width / 3.0;

  // Loop through the grid and draw each TicTacToeBox
  for (int row = 0; row < 3; row++) {
    for (int col = 0; col < 3; col++) {
      if (grid[row][col] == null) {
        grid[row][col] = new TicTacToeBox(row, col, boxSize, ' ');
      }
      grid[row][col].draw();
    }
  }
}

// Check if the row and column are clear to place
boolean isValidBox(int row, int col) {
  return row >= 0 && row < 3 && col >= 0 && col < 3;
}

void displayHomeScreen() {
  // Display the home screen with instructions
  background(255);
  fill(0);
  textAlign(CENTER, TOP);
  textSize(50);
  text("Tic-Tac-Toe", width/2, 100);
  textSize(25);
  fill(0);
  text("Click PLAY to start", width/2, 200);
  noFill();
  rect(250, 250, 100, 50);
  textSize(20);
  fill(0);
  text("PLAY", width/2, 265);
}

void displayGameOver() {
  // Display the game over screen with a prompt to play again
  fill(255, 0, 0);
  textAlign(CENTER, TOP);
  textSize(50);
  text("GAME OVER!", width/2, 100);
  textSize(25);
  fill(0, 0, 255);
  text("CLICK TO PLAY AGAIN", width/2, 200);
}

class TicTacToeBox {
  float x;
  float y;
  float boxSize;
  char symbol = ' ';

  TicTacToeBox(float x, float y, float boxSize, char symbol) {
    this.x = x;
    this.y = y;
    this.boxSize = boxSize;
    this.symbol = symbol;
  }

  void draw() {
    stroke(0);
    noFill();
    rect(x * boxSize, y * boxSize, boxSize, boxSize);
    textAlign(CENTER, CENTER);
    textSize(32);
    fill(0);
    float symbolX = x * boxSize + boxSize/2;
    float symbolY = y * boxSize + boxSize/2;
    text(symbol, symbolX, symbolY);
  }
}

java processing
1个回答
0
投票

以下源代码演示了使用 Rect 类数组创建 3x3 网格来跟踪鼠标按下情况。也许类似的技术可以用在你的游戏中。

Rect[] r;

final int _numCols = 3;
final int _numRows = 3;

final int _wndW = 400;
final int _wndH = 400;

class Rect {
  int x, y, w, h;
  boolean isPressed = false;
  
  // Constructor
  Rect(int xpos, int ypos, int wide, int ht) {
    x = xpos;
    y = ypos;
    w = wide;
    h = ht;
  }

  void display(int id) {
    fill(255); // background color
    rect(x, y, w, h);
    fill(0); // text color
    textSize(18);
    text(str(id), x + w - 18, y + 18);
  }
}

void rectGrid(int left, int top, int w, int h, int vg, int hg) {
  int id = 0;
  // build by row
  for (int k = 0; k < _numRows; k++) {
    for (int j = 0; j < _numCols; j++) {
      int x = left + j*(w+vg);
      int y = top + k*(h+hg);
      r[id] = new Rect(x, y, w, h);
      id++;
    }
  }
}

void setup() {
  size(400, 400);
  background(0, 0, 245);
  r = new Rect[_numRows*_numCols];
  rectGrid(0, 0, _wndW/_numCols, _wndH/_numRows, 0, 0);
}

void draw() {
  for (int i = 0; i < r.length; i++) {
    r[i].display(i); // Display each object
    if (r[i].isPressed == true) {
      text("X", r[i].x + r[i].w/2, r[i].y + r[i].h/2);
    }
  }
}

void mousePressed() {
  for (int i = 0; i < r.length; i++) {
    if ((mouseX >= r[i].x) && (mouseX <= r[i].x +r[i].w) && (mouseY >= r[i].y) && (mouseY <= r[i].y + r[i].h)) {
      println("id =", i);
      r[i].isPressed = true;
    }
  }
}

© www.soinside.com 2019 - 2024. All rights reserved.