Java Tic Tac Toe-如果用户想再次玩,如何让程序自动重启
我已经为一个tic-tac-toe游戏编写了这个代码,它工作得非常好,除了在代码末尾我问用户一个问题,如果他们想再次玩。我知道他们说不关闭程序,但如果他们说是,我找不到让程序重新启动的方法。谢谢你的帮助Java Tic Tac Toe-如果用户想再次玩,如何让程序自动重启,java,tic-tac-toe,Java,Tic Tac Toe,我已经为一个tic-tac-toe游戏编写了这个代码,它工作得非常好,除了在代码末尾我问用户一个问题,如果他们想再次玩。我知道他们说不关闭程序,但如果他们说是,我找不到让程序重新启动的方法。谢谢你的帮助 import hsa.Console; import java.awt.*; import java.util.*; public class tictactoe88 { static Console c; public static void main(String[]args) { c
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);
}
}
}
将主函数中的代码放入类似“draw()”的方法中,然后调用它来代替System.exit(0) 将主函数中的代码放入像“draw()”这样的方法中,然后在系统中调用它。退出(0) 根据文斯的建议,你可以使用一个循环回到游戏开始
// 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
}
}
根据文斯的建议,你使用一个循环回到游戏开始
// 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
}
}
可能重复的Hello和欢迎来到StackOverflow,这是一个网站,您可以在其中获得有关代码特定问题的帮助。从问题开始,什么是问题,哪些是问题。你可能也想退房。退房。您可能会发现它对您正在寻找的内容很有帮助。可能重复Hello,欢迎访问StackOverflow,这是一个网站,您可以在其中获得有关代码特定问题的帮助。从问题开始,什么是问题,哪些是问题。你可能也想退房。退房。您可能会发现它对您正在寻找的内容很有帮助。这最终会导致出现
StackOverflowException
。处理这种行为的正确方法是使用循环。有道理,没有注意到这一点(需要大量的tic-tac-toe,但仍然可能发生)。介意补充一下吗?我尽量避免回答那些对网站没有多大价值的问题。这个问题已经被回答了很多次(仅在这个网站上就有、、吨)。相反,在这种情况下,我更愿意帮助那些试图帮助的人。请随意将其包含在您的答案中!尽管您应该将其标记为重复。这最终会导致出现StackOverflowException
。处理这种行为的正确方法是使用循环。有道理,没有注意到这一点(需要大量的tic-tac-toe,但仍然可能发生)。介意补充一下吗?我尽量避免回答那些对网站没有多大价值的问题。这个问题已经被回答了很多次(仅在这个网站上就有、、吨)。相反,在这种情况下,我更愿意帮助那些试图帮助的人。请随意将其包含在您的答案中!虽然你应该把它标记为重复的。很好。我只是在复制粘贴模式从他的原始代码。我把答案修改得有点干净谢谢你的帮助。我尝试使用代码,如果用户输入“y”,它将清除电路板,但不会重置程序。您需要将图表数组重新初始化为1,2,3,4,5,6,7,8,9良好捕捉。我只是在复制粘贴模式从他的原始代码。我把答案修改得有点干净谢谢你的帮助。我尝试使用代码,如果用户输入“y”,它会清除电路板,但不会重置程序。您需要将图表数组重新初始化为1,2,3,4,5,6,7,8,9