我一直在尝试使用几种静态方法制作这种打勾脚趾游戏。我仍然相对较新,并不能完全弄清楚鼠标点击的整个想法。我已经阅读过关于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
答案 0 :(得分:0)
首先,你有一个“while(true)”语句。这是一个无限循环,你需要一些东西告诉它爆发。这就是你看到垃圾文本的原因。
其次,您用于获取“mousePressed”事件的StdDraw对象在哪里?如果您只是点击一下,我建议使用“MOUSE_CLICKED”事件,但即使您使用mousePressed,也需要确保在没有按下鼠标时它没有检测到它。
答案 1 :(得分:0)
它继续打印行,因为你在Mouse()方法中创建了一个无限循环,并且每秒数百万次检查if语句中的条件。
我建议创建一个单独的方法,例如gameLoop(),并在gameLoop()中调用所有其他方法[精心构造]。
在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(),那么该方法应该做什么就不清楚了。