gpt4 book ai didi

java - Tic Tac Toe - 如果用户想再次玩,如何使程序重新启动

转载 作者:行者123 更新时间:2023-12-02 02:12:38 24 4
gpt4 key购买 nike

我已经为井字棋游戏编写了这段代码,它工作得很好,除了在代码末尾我问用户一个问题是否想再玩一次。我知道他们说不关闭程序,但如果他们说是,我找不到办法让程序重新启动。谢谢您的帮助!

import hsa.Console;
import java.awt.*;
import java.util.*;

public class tictactoe88
{ static Console c;
public static void main(String[]args)

{ c = new Console();

//Drawing the tic tac toe table
c.setColor(Color.black);
c.drawLine(450,250,450,475);
c.drawLine(550,250,550,475);
c.drawLine(350,325,650,325);
c.drawLine(350,400,650,400);

//Declaring variables
int location;
String name1, name2, endGame;

String[][] names = new String[3][3];

int[][] chart =
{
{1,2,3},
{4,5,6},
{7,8,9},
};

//Showing players what number corresponds to tic tac toe board
c.println("1 2 3");
c.println("4 5 6");
c.println("7 8 9");

//Asking players for their names
c.println("Names");
c.print("Player One : ");
name1 = c.readLine ();
c.print("Player Two : ");
name2 = c.readLine ();

//Starting of main loop
while(true)
{
c.setCursor(7,0);
c.println("Where would you like to place the 'X' " + name1);

//Making sure the number entered is valid
while(true)
{
try
{ String locationStr = c.readLine();
location = Integer.parseInt(locationStr);
break;
}
catch(NumberFormatException e)
{ c.println("Bad number, please try again.");
}
}
//Drawing the 'X' and adjusting the 2d array's correspomding to the number entered
{
if (location == 1)
{
c.drawLine(350,250,450,325);
c.drawLine(450,250,350,325);
chart [0][0] = 20;
}
else if (location == 2)
{
c.drawLine(450,250,550,325);
c.drawLine(550,250,450,325);
chart [0][1] = 20;
}
else if(location == 3)
{
c.drawLine(550,250,650,325);
c.drawLine(650,250,550,325);
chart [0][2] = 20;
}
else if (location == 4)
{
c.drawLine(350,325,450,400);
c.drawLine(350,400,450,325);
chart [1][0] = 20;
}
else if (location == 5)
{
c.drawLine(450,325,550,400);
c.drawLine(450,400,550,325);
chart [1][1] = 20;
}
else if (location == 6)
{
c.drawLine(550,325,650,400);
c.drawLine(550,400,650,325);
chart [1][2] = 20;
}
else if (location == 7)
{
c.drawLine(350,400,450,475);
c.drawLine(350,475,450,400);
chart [2][0] = 20;
}
else if (location == 8)
{
c.drawLine(450,400,550,475);
c.drawLine(450,475,550,400);
chart [2][1] = 20;
}
else if (location == 9)
{
c.drawLine(550,400,650,475);
c.drawLine(550,475,650,400);
chart [2][2] = 20;
}
}
//Checks the winner by all 8 possibilities
if (chart[0][0]+chart[0][1]+chart[0][2] == 60||
chart[1][0]+chart[1][1]+chart[0][2] == 60||
chart[2][0]+chart[2][1]+chart[2][2] == 60||
chart[0][0]+chart[1][1]+chart[2][2] == 60||
chart[2][0]+chart[1][1]+chart[0][2] == 60||
chart[0][0]+chart[1][0]+chart[2][0] == 60||
chart[0][1]+chart[1][1]+chart[2][1] == 60||
chart[0][2]+chart[1][2]+chart[2][2] == 60)
{break;}
c.setCursor(9,0);
c.println("Where would you like to place 'O' " + name2);
//Making sure the number entered is valid
while(true)
{
try
{ String locationStr = c.readLine();
location = Integer.parseInt(locationStr);
break;
}
catch(NumberFormatException e)
{ c.println("Bad number, please try again.");
}
}
{
//Drawing the 'O' and adjusting the 2d array's correspomding to the number entered
if (location == 1)
{
c.drawOval(350,250,100,75);
chart [0][0] = 30;
}
else if (location == 2)
{
c.drawOval(450,250,100,75);
chart [0][1] = 30;
}
else if(location == 3)
{
c.drawOval(550,250,100,75);
chart [0][2] = 30;
}
else if (location == 4)
{
c.drawOval(350,325,100,75);
chart [1][0] = 30;
}
else if (location == 5)
{
c.drawOval(450,325,100,75);
chart [1][1] = 30;
}
else if (location == 6)
{
c.drawOval(550,325,100,75);
chart [1][2] = 30;
}
else if (location == 7)
{
c.drawOval(350,400,100,75);
chart [2][0] = 30;
}
else if (location == 8)
{
c.drawOval(450,400,100,75);
chart [2][1] = 30;
}
else if (location == 9)
{
c.drawOval(550,400,100,75);
chart [2][2] = 30;
}

//Checks the winner by all 8 possibilities
if (chart[0][0]+chart[0][1]+chart[0][2] == 90||
chart[1][0]+chart[1][1]+chart[0][2] == 90||
chart[2][0]+chart[2][1]+chart[2][2] == 90||
chart[0][0]+chart[1][1]+chart[2][2] == 90||
chart[2][0]+chart[1][1]+chart[0][2] == 90||
chart[0][0]+chart[1][0]+chart[2][0] == 90||
chart[0][1]+chart[1][1]+chart[2][1] == 90||
chart[0][2]+chart[1][2]+chart[2][2] == 90)
{break;}
}
}
//Ends the game by saying who teh winner is
if (chart[0][0]+chart[0][1]+chart[0][2] == 60||
chart[1][0]+chart[1][1]+chart[0][2] == 60||
chart[2][0]+chart[2][1]+chart[2][2] == 60||
chart[0][0]+chart[1][1]+chart[2][2] == 60||
chart[2][0]+chart[1][1]+chart[0][2] == 60||
chart[0][0]+chart[1][0]+chart[2][0] == 60||
chart[0][1]+chart[1][1]+chart[2][1] == 60||
chart[0][2]+chart[1][2]+chart[2][2] == 60)
{c.println("Congratulations "+name1+" you are the winner!");}

else if (chart[0][0]+chart[0][1]+chart[0][2] == 90||
chart[1][0]+chart[1][1]+chart[0][2] == 90||
chart[2][0]+chart[2][1]+chart[2][2] == 90||
chart[0][0]+chart[1][1]+chart[2][2] == 90||
chart[2][0]+chart[1][1]+chart[0][2] == 90||
chart[0][0]+chart[1][0]+chart[2][0] == 90||
chart[0][1]+chart[1][1]+chart[2][1] == 90||
chart[0][2]+chart[1][2]+chart[2][2] == 90)
{c.println("Congratulations "+name2+" you are the winner!");}

这就是我目前所拥有的

c.println("Would you like to play again? Enter 'y' if you want to play 
again, Enter 'n' if you do not wish to play again");
endGame = c.readLine ();
if (endGame == "y")
{

}
else if (endGame == "n")
{
System.exit(0);
}
}
}

最佳答案

根据 Vince 的建议,您可以使用循环循环回到游戏开始处。

// Your setup logic

// Start of main loop
boolean stillPlaying = true;
while(stillPlaying)
{
// Your game logic

c.println("Would you like to play again? Enter 'y' if you want to play again, Enter 'n' if you do not wish to play again");
endGame = c.readLine().trim();
if (endGame.equalsIgnoreCase("y") || endGame.equalsIgnoreCase("yes"))
{
// Reset your board
c.clear();
c.setColor(Color.black);
c.drawLine(450,250,450,475);
c.drawLine(550,250,550,475);
c.drawLine(350,325,650,325);
c.drawLine(350,400,650,400);

// Reinitialize chart[][] to 1,2,3,4,5,6,7,8,9 your starting state
resetChart(chart);
}
else if (endGame.equalsIgnoreCase("n") || endGame.equalsIgnoreCase("no"))
{
stillPlaying = false;
}
else
{
// Should probably re prompt for y or n but I'm just gonna exit
stillPlaying = false
}
}

关于java - Tic Tac Toe - 如果用户想再次玩,如何使程序重新启动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49764080/

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