0

0

java 处理中的 tic tac toe

PHPz

PHPz

发布时间:2024-02-22 12:52:06

|

870人浏览过

|

来源于stackoverflow

转载

php小编草莓为您带来最新的java问答专栏,本期将探讨java中处理tic tac toe(井字棋)游戏的相关问题。无论您是初学者还是有经验的开发者,都可以在这里找到有关java中处理井字棋游戏的实用技巧和解决方案。让我们一起深入了解这个有趣的话题,提升自己在java编程领域的技能!

问题内容

我目前正在处理中开发一个简单的 tic-tac-toe 游戏,但我在两个方面遇到了问题:

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

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

这是我当前的代码:

百度智能云·曦灵
百度智能云·曦灵

百度旗下的AI数字人平台

下载
// 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速学教程(入门到精通)
java速学教程(入门到精通)

java怎么学习?java怎么入门?java在哪学?java怎么学才快?不用担心,这里为大家提供了java速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载

相关标签:

本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

相关专题

更多
java
java

Java是一个通用术语,用于表示Java软件及其组件,包括“Java运行时环境 (JRE)”、“Java虚拟机 (JVM)”以及“插件”。php中文网还为大家带了Java相关下载资源、相关课程以及相关文章等内容,供大家免费下载使用。

832

2023.06.15

java正则表达式语法
java正则表达式语法

java正则表达式语法是一种模式匹配工具,它非常有用,可以在处理文本和字符串时快速地查找、替换、验证和提取特定的模式和数据。本专题提供java正则表达式语法的相关文章、下载和专题,供大家免费下载体验。

737

2023.07.05

java自学难吗
java自学难吗

Java自学并不难。Java语言相对于其他一些编程语言而言,有着较为简洁和易读的语法,本专题为大家提供java自学难吗相关的文章,大家可以免费体验。

734

2023.07.31

java配置jdk环境变量
java配置jdk环境变量

Java是一种广泛使用的高级编程语言,用于开发各种类型的应用程序。为了能够在计算机上正确运行和编译Java代码,需要正确配置Java Development Kit(JDK)环境变量。php中文网给大家带来了相关的教程以及文章,欢迎大家前来阅读学习。

397

2023.08.01

java保留两位小数
java保留两位小数

Java是一种广泛应用于编程领域的高级编程语言。在Java中,保留两位小数是指在进行数值计算或输出时,限制小数部分只有两位有效数字,并将多余的位数进行四舍五入或截取。php中文网给大家带来了相关的教程以及文章,欢迎大家前来阅读学习。

398

2023.08.02

java基本数据类型
java基本数据类型

java基本数据类型有:1、byte;2、short;3、int;4、long;5、float;6、double;7、char;8、boolean。本专题为大家提供java基本数据类型的相关的文章、下载、课程内容,供大家免费下载体验。

446

2023.08.02

java有什么用
java有什么用

java可以开发应用程序、移动应用、Web应用、企业级应用、嵌入式系统等方面。本专题为大家提供java有什么用的相关的文章、下载、课程内容,供大家免费下载体验。

430

2023.08.02

java在线网站
java在线网站

Java在线网站是指提供Java编程学习、实践和交流平台的网络服务。近年来,随着Java语言在软件开发领域的广泛应用,越来越多的人对Java编程感兴趣,并希望能够通过在线网站来学习和提高自己的Java编程技能。php中文网给大家带来了相关的视频、教程以及文章,欢迎大家前来学习阅读和下载。

16925

2023.08.03

Java 桌面应用开发(JavaFX 实战)
Java 桌面应用开发(JavaFX 实战)

本专题系统讲解 Java 在桌面应用开发领域的实战应用,重点围绕 JavaFX 框架,涵盖界面布局、控件使用、事件处理、FXML、样式美化(CSS)、多线程与UI响应优化,以及桌面应用的打包与发布。通过完整示例项目,帮助学习者掌握 使用 Java 构建现代化、跨平台桌面应用程序的核心能力。

36

2026.01.14

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号