java tic tac toe用户选择板的大小和一行中要赢多少(无GUI)
我正在做一个tic tac toe游戏,用户可以选择棋盘的大小以及需要赢的棋盘数量。例如,用户可以将棋盘设为8*8,并说您需要连续6次才能获胜。一切都很好,只是我似乎无法让胜利的状态对角运行。第一部分是本文的主要研究方法。有人能给我一些建议或想法吗java tic tac toe用户选择板的大小和一行中要赢多少(无GUI),java,multidimensional-array,tic-tac-toe,Java,Multidimensional Array,Tic Tac Toe,我正在做一个tic tac toe游戏,用户可以选择棋盘的大小以及需要赢的棋盘数量。例如,用户可以将棋盘设为8*8,并说您需要连续6次才能获胜。一切都很好,只是我似乎无法让胜利的状态对角运行。第一部分是本文的主要研究方法。有人能给我一些建议或想法吗 import java.util.Scanner; public class TicTacToeApp { public static void main(String[] args) { TicTacToeGame game; int size,
import java.util.Scanner;
public class TicTacToeApp
{
public static void main(String[] args)
{
TicTacToeGame game;
int size, need, player = 1;
String [] names = new String[2];
Scanner kbd = new Scanner(System.in);
System.out.print("Enter Player 1's name: ");
names[0] = kbd.nextLine();
System.out.print("Enter Player 2's name: ");
names[1] = kbd.nextLine();
System.out.print("Enter the TIC-TAC-TOE grid size: ");
size = kbd.nextInt();
System.out.print("Enter how many in a row you need to win: ");
need = kbd.nextInt();
System.out.println();
game = new TicTacToeGame(size, need, names);
while (!game.haveWinner() && !game.isFull())
{
player = (player + 1) % 2;
game.playOneTurn(player);
}
if (game.isFull())
System.out.println("It's a TIE!");
else
System.out.println(names[player] + " is the winner!");
System.out.println("\nBye!");
}
}
上面是主要的方法,下面是获胜的条件和所有其他使它工作的代码
import java.util.Scanner;
public class TicTacToeGame
{
Scanner kbd=new Scanner(System.in);
protected static char X='x';
protected static char O='o';
int size;
int need;
String[] names;
char[][] game;
int x, y;
public TicTacToeGame(int size, int need, String [] names)
{
this.size=size;
this.need=need;
this.names=names;
game=new char[size][size];
for(int i=0;i<size;i++)
{
for (int j = 0; j < size; j++)
game[i][j] = '-';
}
System.out.print(this);
}
public String toString()
{
String theGame="\n";
for(int i=0;i<size;i++){
for(int j=0;j<size;j++){
if(game[i][j]=='-')
theGame=theGame+'-';
else if(game[i][j]=='o')
theGame=theGame+'o';
else if(game[i][j]=='x')
theGame=theGame+'x';
}
theGame=theGame+"\n";
}
return theGame;
}
public boolean haveWinner()
{
// what i tried doing was to comment out the horizontal and vertical winning conditions and just tried the diagonal
int count=0;
for(int i=0;i<size;i++)//horizontal
for(int j=0;j<size;j++)
{
if(game[i][j]==X)
count++;
if(game[i][j]!=X)
count=0;
if (count==need)
return true;
}
if(count!=need)//vertical
count=0;
for(int i=0;i<size;i++)
for(int j=0;j<size;j++)
{
if(game[j][i]==X)
count++;
if(game[j][i]!=X)
count=0;
if (count==need)
return true;
}
if (count!=need)
count = 0;
for(int i = 0; i<size; i++)
for(int j =0; j < size; j++)
{
if(game[i][j] == X)
count++;
if(game[i][j] != X)
count=0;
if(game[i+1][j+1] == X)
count++;
if(game[i+1][j+1] != X)
count=0;
if(count ==need)
return true;
}
return false;
}
public boolean isFull()
{
for(int i=0;i<size;i++){
for(int j=0;j<size;j++)
if(game[i][j]=='-')
return false;
}
return true;
}
public void playOneTurn(int player){
System.out.print("play "+names[player]+":");
x=kbd.nextInt();
y=kbd.nextInt();
while(x >=size || y >=size){
System.out.print("exception enter another move "+names[player]);
x=kbd.nextInt();
y=kbd.nextInt();
}
if(player==0){
if(game[x][y]=='-')
game[x][y]='x';
System.out.print(this);
}
else if(player==1){
if(game[x][y]=='-')
game[x][y]='o';
System.out.print(this);
}
}
import java.util.Scanner;
公共类游戏
{
扫描仪kbd=新扫描仪(System.in);
受保护的静态字符X='X';
受保护的静态字符O='O';
整数大小;
智力需要;
字符串[]名称;
char[][]游戏;
int x,y;
公共TictoEgame(整数大小、整数需要、字符串[]名称)
{
这个。大小=大小;
这个。需要=需要;
这个名称=名称;
游戏=新字符[大小][大小];
对于(inti=0;i我认为让haveWinner执行一些递归检查更有意义
private boolean horizontal(int x,int y,int count) {
bool result;
if(count == need) {
return true;
} else {
if(game[y][x] == X) {
result = horizontal(x++,y,count++)
} else {
result = false;
}
}
return result;
}
vertical() ...
diagonal() ...
public boolean haveWinner(int x, int y, int count) {
bool result;
if(horizontal(x,y,0) || vertical(x,y,0) || diagonal(x,y,0)) {
result = true;
} else {
result = false;
}
}
您可以根据我的horizontal
实现来设计vertical()
和diagonal()
使用此系统时,您需要考虑到它将从左到右进行检查这一事实,因此您需要重新设计此系统的部分,以确保您不会检查将生成ArrayOutOfBoundsException的元素
您还需要记住,diagonal()
将需要up和down方法
现在只检查X,O不能赢
这可能比要求的更复杂。如果是这样,我愿意接受建议。忘了提到我只是在检查X……我没有为O做检查。一旦我完全弄清楚X,我会这样做。我明白你的意思,但我就是不知道如何对角做。如果我没有弄错,有4种可能的方法如果(游戏[i+1][j+1]==X |游戏[i-1][j-1]==X |游戏[i+1][j-1]==X |游戏[i-1][j-1]==X'我只是不知道如何在不越界的情况下进行检查。实际上,只有两种方法可以检查对角线:向右和向上,向右和向下。如果你仔细想想,你列出的另外两种方法(向左和向上,向左和向下)实际上只是前两个的翻译,起点相反(向左和向上==向右和向下,向左和向下==向右和向上)。至于异常,您可以尝试检查代码,并捕获阵列索引边界外异常,但这是一个非常粗糙的解决方案。最好的解决方案是根据需要
场地和比赛场地的宽度和高度计算哪些元素可以安全检查。