首頁 >Java >java 處理中的 tic tac toe

java 處理中的 tic tac toe

PHPz
PHPz轉載
2024-02-22 12:52:06855瀏覽

php小編草莓為您帶來最新的java問答專欄,本期將探討java中處理tic tac toe(井字棋)遊戲的相關問題。無論您是初學者還是有經驗的開發者,都可以在這裡找到有關java中處理井字棋遊戲的實用技巧和解決方案。讓我們一起深入了解這個有趣的主題,提升自己在java程式領域的技能!

問題內容

我目前正在處理中開發一個簡單的 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);
  }
}

解決方法

以下原始程式碼示範了使用 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 leftpressed = false;
  boolean rightpressed = 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(_wndw, _wndh);
  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].leftpressed == true) {
      text("x", r[i].x + r[i].w/2, r[i].y + r[i].h/2);
    }
    if (r[i].rightpressed == true) {
      text("o", 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);
      if (mousebutton == left) {
        r[i].leftpressed = true;
      }
      if (mousebutton == right) {
        r[i].rightpressed = true;
      }
    }
  }
}

建議: 您的網格不需要二維數組;一維數組工作得很好且不太複雜。我將向網格類別新增兩個布林值(leftpressed 和 rightpressed)並刪除符號參數。網格類別的「draw()」方法可能應該重新命名為「display()」之類的方法,因為「draw」是處理中的關鍵字,可以避免混淆。可以使用下面所示的技術安全地刪除方法 displaygrid() 和 isvalidbox()。主要程式碼變更應該在 mousepressed() 中,因為它無法正常運作。循環遍歷網格中的每個框將正確捕獲滑鼠按鈕單擊,此時您可以檢查單擊的是滑鼠右鍵還是左滑鼠按鈕,並且可以將相應的布林值設為「true」。然後,主“draw()”將使用此資訊繪製“x”或“o”。我知道這聽起來很多,但這些更改是解決您的問題的一種方法。您修改後的原始程式碼如下所示:

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

// 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;

class Grid {
  float x;
  float y;
  float boxSize;
  boolean leftPressed = false;
  boolean rightPressed = false;

  Grid(float x, float y, float boxSize) {
    this.x = x;
    this.y = y;
    this.boxSize = boxSize;
  }

  void display() {
    stroke(0);
    noFill();
    rect(x, y, boxSize, boxSize);
  }
}

void setup() {
  size(600, 600);
  displayHomeScreen();
  // initialize array
  float boxSize = width / 3.0;
  int id = 0;
  for (int k = 0; k < 3; k++) {
    for (int j = 0; j < 3; j++) {
      float x = j*boxSize;
      float y = k*boxSize;
      g[id] = new Grid(x, y, boxSize);
      id++;
    }
  }
}

void draw() {
  // Draw the appropriate screen based on gameStatus
  if (gameStatus == 1) {
    background(255);
    for (int i = 0; i < g.length; i++) {
      g[i].display(); // Display each object
      if (g[i].leftPressed == true) {
        text("X", g[i].x + g[i].boxSize/2, g[i].y + g[i].boxSize/2);
      }
      if (g[i].rightPressed == true) {
        text("O", g[i].x + g[i].boxSize/2, g[i].y + g[i].boxSize/2);
      }
    }
  } else if (gameStatus == 2) {
    background(0);
    displayGameOver();
  }
}

void mousePressed() {
  // Check the gameStatus and respond to mouse clicks accordingly
  if (gameStatus == 1) {
    for (int i = 0; i < g.length; i++) {
      if ((mouseX >= g[i].x) && (mouseX <= g[i].x +g[i].boxSize) && (mouseY >= g[i].y) && (mouseY <= g[i].y + g[i].boxSize)) {
        println("id =", i);
        if (mouseButton == LEFT) {
          g[i].leftPressed = true;
        }
        if (mouseButton == RIGHT) {
          g[i].rightPressed = true;
        }
      }
    }
  } else if (gameStatus == 0 && mouseX > 250 && mouseX < 350 && mouseY > 250 && mouseY < 300) {
    // Transition to the game screen when PLAY is clicked
    gameStatus = 1;
  }
}

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);
}

以上是java 處理中的 tic tac toe的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:stackoverflow.com。如有侵權,請聯絡admin@php.cn刪除