gpt4 book ai didi

java - 如何为我的井字游戏添加计分器

转载 作者:行者123 更新时间:2023-12-02 03:26:21 25 4
gpt4 key购买 nike

我有一个用 java swing 创建的 tic tac toe 游戏,我想添加一个计数器来记录任一玩家赢得的游戏次数。我只需要一个计算分数的计数器,但我不知道如何添加它

这是我的代码:

package Game;

import java.util.Scanner;

public class Main {

public static boolean playerTurn = true;
public static boolean playerWon = false;
public static boolean computerWon = false;
public static boolean playing = true;
public static Scanner scan = new Scanner(System.in);
public static boolean playAgain = false;


public static tictactoe board = new tictactoe();

public static void main(String[] args){
if(board.isVisible() == false){
board.setVisible(true);

if(playerWon == true || computerWon == true){

System.out.println("Would you like to play again!? true or false");
playAgain = scan.nextBoolean();
if(playAgain == true ){
board.setVisible(false);
System.out.println("Player1 Won: " + playerWon);
System.out.println("Player2 Won: " + computerWon);
board.button1.setText("");
board.button2.setText("");
board.button3.setText("");
board.button4.setText("");
board.button5.setText("");
board.button6.setText("");
board.button7.setText("");
board.button8.setText("");
board.button9.setText("");
playerTurn = true;
playerWon = false;
computerWon = false;
board.setVisible(true);
else

System.out.println("Thanks for playing...! ");}

最佳答案

尝试一下并告诉我它是否有效。我添加了一条评论,其中我更改了代码。

public static  boolean playerTurn = true;
public static boolean playerWon = false;
public static boolean computerWon = false;
public static boolean playing = true;
public static Scanner scan = new Scanner(System.in);
public static boolean playAgain = false;
public static int player1WinCount = 0; //At game start nobody has won
public static int player2WinCount = 0;


public static tictactoe board = new tictactoe();

public static void main(String[] args){
if(board.isVisible() == false){
board.setVisible(true);

// Here I added the information about the counter

if (playerWon == true) {
player1WinCount++;
System.out.println("The player has won " + player1WinCount + " time(s)");
}

if (computerWon == true) {
player2WinCount++;
System.out.println("The computer has won " + player2WinCount + " time(s)");
}

if(playerWon == true || computerWon == true){

System.out.println("Would you like to play again!? true or false");
playAgain = scan.nextBoolean();
if(playAgain == true ){
board.setVisible(false);
System.out.println("Player1 Won: " + playerWon);
System.out.println("Player2 Won: " + computerWon);
board.button1.setText("");
board.button2.setText("");
board.button3.setText("");
board.button4.setText("");
board.button5.setText("");
board.button6.setText("");
board.button7.setText("");
board.button8.setText("");
board.button9.setText("");
playerTurn = true;
playerWon = false;
computerWon = false;
board.setVisible(true);
else

System.out.println("Thanks for playing...! ");}

关于java - 如何为我的井字游戏添加计分器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38829978/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com