我一直在尝试使用几种静态方法制作这款tick tack toe游戏。我相对来说还是个新手,不能完全理解鼠标点击的概念。我读过关于鼠标点击和鼠标事件,但它不完全有意义,我得到很多错误,当我尝试。最初我有一部分,它在自己的方法中获取鼠标信息,但后来我不知道如何同时返回x和y值。我在下面添加了填充数组的方法。现在我把它弄乱了,并设法让它们在自己的方法中,但仍然有问题运行程序。(它们不必在它们自己的方法中,我只是认为它会简化事情)当我运行这个程序时,它所做的就是打印无限多的行,告诉我点击了哪一行和哪一列,并在第一行和第一列上放一个0,不管我是否点击。此外,它似乎也不会在玩家之间切换回合。如果有人能帮助我,我将不胜感激。谢谢!
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
首先,有一个"while(true)"语句。这是一个无限循环,你需要一些东西来告诉它跳出来。所以你才会看到垃圾短信。
其次,在哪里StdDraw对象你是用来获得你的"mousePressed"事件?我推荐"MOUSE_CLICKED"事件,如果你只是得到一个点击,但即使你停留在mousePressed,你需要确保它不会检测到它,如果鼠标没有被按下。
它继续打印行,因为您在Mouse()方法中创建了一个无限循环,并且它每秒检查if语句中的条件数百万次。
我建议创建一个单独的方法,例如gamelloop(),并从gamelloop()中调用所有其他方法。
在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(),则不清楚该方法应该做什么。