Java Tic Tac Toe(有条件)

Java Tic Tac Toe(有条件),java,arrays,tic-tac-toe,Java,Arrays,Tic Tac Toe,出于某种原因,当我编程getWinner()时,它只在2种情况下有效(对于最后一行)。除了对角线和列之外,我还有其他的东西,但是第2行(好吧,第3行,但是数组,所以第2行)基本上只适用于o。只有当o在0,0和1,1或0,2和1,1时,x才会赢。(基本上,仅当o位于上角和中心时 import java.util.Scanner; public class TicTacToeRunner { public static void main(String[] args)

出于某种原因,当我编程getWinner()时,它只在2种情况下有效(对于最后一行)。除了对角线和列之外,我还有其他的东西,但是第2行(好吧,第3行,但是数组,所以第2行)基本上只适用于o。只有当o在0,0和1,1或0,2和1,1时,x才会赢。(基本上,仅当o位于上角和中心时

import java.util.Scanner;  
public class TicTacToeRunner  
{  
    public static void main(String[] args)  
    {  
        Scanner in = new Scanner(System.in);  
        String player = "x";  
        TicTacToe game = new TicTacToe();  
        boolean done = false;  
        while (!done)  
        {  
            System.out.print(game.toString());   
            System.out.print(  
            "Row for " + player + " (-1 to exit): ");  
            int row = in.nextInt();  
            if (row < 0)  
            { 
            done = true;  
            } 
            else if (row > 2) 
            System.out.println("Invalid, please enter a number from 0 to 2"); 
            else
            {  
                System.out.print("Column for " + player + ": ");  
                int temp = in.nextInt(); 
                if (temp>2) 
                System.out.println("Invalid, please enter a number from 0 to 2"); 
                else
                { 
                int column = temp;  
                game.set(row, column, player);  
                if (player.equals("x"))   
                player = "o";   
                else 
                player = "x";   
            } 


            if(game.getWinner().equals("x")) 
            {     
                System.out.println("x is the winner!"); 
                done = true; 
            } 
            if(game.getWinner().equals("o")) 
            {     
                System.out.println("o is the winner!"); 
                done = true; 
            } 
            } 
            } 
    }  
import java.util.Scanner;
公共类TictatcoeRunner
{  
公共静态void main(字符串[]args)
{  
扫描仪输入=新扫描仪(系统输入);
字符串播放器=“x”;
Tictatcoe游戏=新的Tictatcoe();
布尔完成=假;
而(!完成)
{  
System.out.print(game.toString());
系统输出打印(
“+player+”(-1退出):”行;
int row=in.nextInt();
如果(行<0)
{ 
完成=正确;
} 
否则,如果(行>2)
System.out.println(“无效,请输入0到2之间的数字”);
其他的
{  
System.out.print(“用于“+player+”的列:”);
int temp=in.nextInt();
如果(温度>2)
System.out.println(“无效,请输入0到2之间的数字”);
其他的
{ 
int列=温度;
游戏设置(行、列、玩家);
如果(玩家等于(“x”))
player=“o”;
其他的
player=“x”;
} 
if(game.getWinner()等于(“x”))
{     
System.out.println(“x是赢家!”);
完成=正确;
} 
if(game.getWinner()等于(“o”))
{     
System.out.println(“o是赢家!”);
完成=正确;
} 
} 
} 
}  
}

公共类TicTacToe
{  
专用字符串[][]板;
私有静态最终整数行=3;
私有静态final int COLUMNS=3;
公共交通
{  
board=新字符串[行][列];
对于(int i=0;i对于(int i=0;i如果行、列或对角线中没有空格,则它们都将是
。但是,这仍将满足一个条件,例如

board[i][0].equals(board[i][1]) && board[i][1].equals(board[i][2])
然后它将立即返回
,而不检查其他位置。
是赢家!但您也可以使用
表示没有赢家

在检查条件表达式的其余部分之前,向条件中添加一个附加检查,以确保所检查的第一个位置不等于“
”:

!" ".equals(board[i][0]) &&
board[i][0].equals(board[i][1]) &&
board[i][1].equals(board[i][2])
你可以同样地改变其他条件

!" ".equals(board[i][0]) &&
board[i][0].equals(board[i][1]) &&
board[i][1].equals(board[i][2])