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 250 && mouseY = 0 && row = 0 && col <h2 class="daan">解决方法</h2><p>以下源代码演示了使用 rect 类数组创建 3x3 网格来跟踪鼠标按下情况。也许类似的技术可以用在您的游戏中。</p><pre class="brush:php;toolbar:false;">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 = r[i].x) && (mousex = r[i].y) && (mousey <p><strong>建议:</strong>
您的网格不需要二维数组;一维数组工作得很好并且不太复杂。我将向网格类添加两个布尔值(leftpressed 和 rightpressed)并删除符号参数。网格类的“draw()”方法可能应该重命名为“display()”之类的方法,因为“draw”是处理中的关键字,可以避免混淆。可以使用下面所示的技术安全地删除方法 displaygrid() 和 isvalidbox()。主要代码更改应该在 mousepressed() 中,因为它无法正常工作。循环遍历网格中的每个框将正确捕获鼠标按钮单击,此时您可以检查单击的是鼠标右键还是左鼠标按钮,并且可以将相应的布尔值设置为“true”。然后,主“draw()”将使用此信息绘制“x”或“o”。我知道这听起来很多,但这些更改是解决您的问题的一种方法。您修改后的源代码如下所示:</p><pre class="brush:php;toolbar:false;">// 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 = g[i].x) && (mouseX = g[i].y) && (mouseY 250 && mouseX 250 && mouseY
Java免费学习笔记:立即使用
解锁 Java 大师之旅:从入门到精通的终极指南










