我一直在尝试使用几种静态方法制作这个滴答作响的游戏。我对此仍然相对较新,并且无法完全弄清楚鼠标点击的整个想法。我已经阅读了有关 mouseclick 和 mouseevent 的内容,但它并不完全有意义,并且当我尝试这样做时会遇到很多错误。最初我有部分以自己的方法获取鼠标信息,但后来我不知道如何返回 x 和 y 值。所以我添加了填充下面数组的方法。现在我搞砸了它并设法让它们以自己的方法获得,但仍然在运行程序时遇到问题。(他们不必采用自己的方法,我只是认为它会简化事情)当我运行这个程序时,它所做的只是打印无限量的行,说明我点击了哪一行和哪一列,并在第一行和第一列中放置一个 O,无论我是否点击。此外,它似乎也不会在玩家之间切换回合。如果有人可以帮助我,我将不胜感激。谢谢!
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