我有一个井字游戏,用户可以在轮到自己时单击自己选择的按钮,然后轮到计算机时,计算机将使用button.fire()
方法单击该按钮。但是,该代码不会等待用户的输入/单击,并且计算机需要进行多次回合,因此游戏始终以计算机获胜告终。
有没有办法等到用户轮到他们单击按钮时?
我已经尝试了多种方法,并花了很多时间来遵循这个概念并使代码运行。我可以使用计时器,但这仍然允许计算机进行多次移动,而无需用户输入。
while (boardInGame.isNotFull() && !boardInGame.isGameOver()) //continue the game is not over
{
if (turn == 'x') /*human player is always set to 'x'*/ {
switch (computerMove){
case 0: break;
case 1: button1.fire();break;
case 2: button2.fire();break;
case 3: button3.fire();break;
case 4: button4.fire();break;
case 5: button5.fire();break;
case 6: button6.fire();break;
case 7: button7.fire();break;
case 8: button8.fire();break;
case 9: button9.fire();break;
default:break;
}
if (boardInGame.isValidMove("a1")) {
button1.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent actionEvent) {
button1.setStyle("--fx-font: 48 Arial");
button1.setText("X");
boardInGame.addMove('x', "a1");
save[myInt] = "a1";
myInt++;
}
}
)
;
turn = 'o';
}
if (boardInGame.isValidMove("b1")) {
button2.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent actionEvent) {
button2.setStyle("--fx-font: 48 Arial");
{
button2.setText("X");
boardInGame.addMove('x', "b1");
save[myInt] = "b1";
myInt++;
}
}
});
}
if (boardInGame.isValidMove("c1")) {
button3.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent actionEvent) {
button3.setStyle("--fx-font: 48 Arial");
{
button3.setText("X");
boardInGame.addMove('x', "c1");
save[myInt] = "c1";
myInt++;
}
}
});
}
if (boardInGame.isValidMove("a2")) {
button4.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent actionEvent) {
button4.setStyle("--fx-font: 48 Arial");
{
button4.setText("X");
boardInGame.addMove('x', "a2");
save[myInt] = "a2";
myInt++;
}
}
});
}
if (boardInGame.isValidMove("b2")) {
button5.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent actionEvent) {
button5.setStyle("--fx-font: 48 Arial");
{
button5.setText("X");
boardInGame.addMove('x', "b2");
save[myInt] = "b2";
myInt++;
}
}
});
}
if (boardInGame.isValidMove("c2")) {
button6.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent actionEvent) {
button6.setStyle("--fx-font: 48 Arial");
{
button6.setText("X");
boardInGame.addMove('x', "c2");
save[myInt] = "c2";
myInt++;
}
}
});
}
if (boardInGame.isValidMove("a3")) {
button7.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent actionEvent) {
button7.setStyle("--fx-font: 48 Arial");
{
button7.setText("X");
boardInGame.addMove('x', "a3");
save[myInt] = "a3";
myInt++;
}
}
});
}
if (boardInGame.isValidMove("b3")) {
button8.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent actionEvent) {
button8.setStyle("--fx-font: 48 Arial");
{
button8.setText("X");
boardInGame.addMove('x', "b3");
save[myInt] = "b3";
myInt++;
}
}
}
);
}
if (boardInGame.isValidMove("c3")) {
button9.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent actionEvent) {
button9.setStyle("--fx-font: 48 Arial");
{
button9.setText("X");
boardInGame.addMove('x', "c3");
save[myInt] = "c3";
myInt++;
}
}
});
}
turn = 'o';
}
if (turn == 'o' && !boardInGame.isGameOver()) {
boolean added = false;
while (!added) {
move = player2.getComputerMove();
if (boardInGame.isValidMove(move)) {
switch (move) {
case ("a1"): {
button1.setStyle("--fx-font: 48 Arial");
button1.setText("O");
added = boardInGame.addMove('O', "a1");
save[myInt] = "a1";
myInt++;
computerMove=1;
break;
}
case ("b1"): {
button2.setText("O");
added = boardInGame.addMove('O', "b1");
save[myInt] = "b1";
myInt++;
computerMove=2;
break;
}
case ("c1"): {
button3.setText("O");
added = boardInGame.addMove('O', "c1");
save[myInt] = "c1";
myInt++;
computerMove=3;
break;
}
case "a2": {
button4.setText("O");
added = boardInGame.addMove('O', "a2");
save[myInt] = "a2";
myInt++;
computerMove=4;
break;
}
case ("b2"): {
button5.setStyle("--fx-font: 48 Arial");
button5.setText("O");
added = boardInGame.addMove('O', "b2");
save[myInt] = "b2";
myInt++;computerMove=5;
break;
}
case ("c2"): {
button6.setStyle("--fx-font: 48 Arial");
button6.setText("O");
added = boardInGame.addMove('O', "c2");
save[myInt] = "c2";
myInt++;
computerMove=6;break;
}
case ("a3"): {
button7.setStyle("--fx-font: 48 Arial");
button7.setText("O");
added = boardInGame.addMove('O', "a3");
save[myInt] = "a3";
myInt++;
computerMove=7;
break;
}
case ("b3"): {
button8.setStyle("--fx-font: 48 Arial");
button8.setText("O");
added = boardInGame.addMove('O', "b3");
save[myInt] = "b3";
myInt++;
computerMove=8;
break;
}
case ("c3"): {
button9.setStyle("--fx-font: 48 Arial");
button9.setText("O");
added = boardInGame.addMove('O', "c3");
save[myInt] = "c3";
myInt++;
computerMove=9;
break;
}
default:
added = false;
}
}
}
}
}
/*print the result once the game is over.*/
if (boardInGame.isTie()) {
System.out.println("It's a Tie!");
}
if (boardInGame.isWinner('x')) {
System.out.println("Player #1: 'x' wins!");
}
if (boardInGame.isWinner('o')) {
System.out.println("Player #2: 'o' wins!");
}
}
答案 0 :(得分:0)
也许你可以移动
switch (computerMove){
case 0: break;
case 1: button1.fire();break;
case 2: button2.fire();break;
case 3: button3.fire();break;
case 4: button4.fire();break;
case 5: button5.fire();break;
case 6: button6.fire();break;
case 7: button7.fire();break;
case 8: button8.fire();break;
case 9: button9.fire();break;
default:break;
}
设置为单独的方法,并使其在玩家每次移动时都运行该方法。