java - 用 JAVA 编写井字棋游戏。使用许多 if 语句我如何让游戏读取平局游戏?

标签 java if-statement tic-tac-toe

提示

欢迎来玩猜谁赢的游戏! 请输入您的游戏1板(*退出)> XXOOOOXXOO
您的游戏1如下:
XXO
OOX
XOO
您的游戏 1:平局

请进入您的游戏2板(*表示退出)> XXXOOXXOO
您的游戏2如下:
XXX
OOX
XOO
您的游戏 2:X 在第 1 行赢得了游戏。
等等

<小时/>

我怎样才能让它读取平局游戏?

package PA6;
import java.util.Scanner;

public class PA6 {
private static char [ ] [ ] ttt = new char [4] [4] ;
private static int gameNum = 1;


public static void main(String[] args)
{//MAIN OPEN
System.out.println("Welcome to play the game of guessing who is winning!");
Scanner scan = new Scanner (System.in);
String gameString;
System.out.println("Please enter your game " + gameNum + " board (* to exit) > ");
gameString = scan.nextLine();
int n; 

while (!gameString.equals("*"))
    {//WHILE LOOP OPEN
    System.out.println("Your game " + gameNum + " is as follows: ");
        n = 0;
    ttt = new char [4][4];
    for(int i = 1 ; i < 4; i++)
        {//FOR LOOP OPEN
            for(int j = 1; j < 4; j++)
            {//NESTED FOR LOOP OPEN 
                ttt [i][j]= gameString.charAt(n);
                    n++;
            }//NESTED FOR LOOP CLOSED
        }//FOR LOOP CLOSED
    System.out.println(ttt[1][1] + "" + ttt[1][2] + "" + ttt[1][3]);
    System.out.println(ttt[2][1] + "" + ttt[2][2] + "" + ttt[2][3]);
    System.out.println(ttt[3][1] + "" + ttt[3][2] + "" + ttt[3][3]);

    if(winRow1('O' , ttt))
        System.out.println("Your game " + gameNum + ": O won the game by row 1");
    if(winRow2('O' , ttt))
        System.out.println("Your game " + gameNum + ": O won the game by row 2");
    if(winRow3('O' , ttt))
        System.out.println("Your game " + gameNum + ": O won the game by row 3");
    if(winRow1('X' , ttt))
        System.out.println("Your game " + gameNum + ": X won the game by row 1");
    if(winRow2('X' , ttt))
        System.out.println("Your game " + gameNum + ": X won the game by row 2");
    if(winRow3('X' , ttt))
        System.out.println("Your game " + gameNum + ": X won the game by row 3");
    if(winColumn1('O' , ttt))
        System.out.println("Your game " + gameNum + ": O won the game by Column 1");
    if(winColumn2('O' , ttt))
        System.out.println("Your game " + gameNum + ": O won the game by Column 2");
    if(winColumn3('O' , ttt))
        System.out.println("Your game " + gameNum + ": O won the game by Column 3");
    if(winColumn1('X' , ttt))
        System.out.println("Your game " + gameNum + ": X won the game by Column 1");
    if(winColumn2('X' , ttt))
        System.out.println("Your game " + gameNum + ": X won the game by Column 2");
    if(winColumn3('X' , ttt))
        System.out.println("Your game " + gameNum + ": X won the game by Column 3");
    if(winDiagonal1('O' , ttt))
        System.out.println("Your game " + gameNum + ": O won the game by diagonal 1");
    if(winDiagonal1('X' , ttt))
        System.out.println("Your game " + gameNum + ": X won the game by diagonal 1");
    if(winDiagonal2('O' , ttt))
        System.out.println("Your game " + gameNum + ": O won the game by diagonal 2");
    if(winDiagonal2('X' , ttt))
        System.out.println("Your game " + gameNum + ": X won the game by diagonal 2");

    gameNum++;

    System.out.print("Please enter your game " + gameNum + " board (* to exit) > ");
    gameString = scan.nextLine();




    }//WHILE LOOP CLOSED
}//MAIN CLOSED

public static boolean winDiagonal1( char player, char a [][]) 
   { 
if ( ttt[1][1] == player && ttt[2][2] == player && ttt[3][3] == player )
    return true;   
return false; 
   }

public static boolean winDiagonal2 (char player, char a [][])
   {
    if( ttt[1][3] == player && ttt[2][2] == player && ttt[3][1] == player )
        return true;
    return false;
   }
public static boolean winRow1 (char player, char a [][])
   {
if (ttt[1][1] == player && ttt[1][2] == player && ttt[1][3] == player)
    return true;
return false;
   }

public static boolean winRow2 (char player, char a [][])
   {
if (ttt[2][1] == player && ttt[2][2] == player && ttt[2][3] == player)
    return true;
return false;
   }

public static boolean winRow3 (char player, char a [][])
   {    
if (ttt[3][1] == player && ttt[3][2] == player && ttt[3][3] == player)
    return true;
return false;
   }
public static boolean winColumn1 (char player, char a [][])
   {    
if (ttt[1][1] == player && ttt[2][1] == player && ttt [3][1] == player)
    return true;
return false;
   }

public static boolean winColumn2 (char player, char a [][])
   { 
if (ttt[1][2] == player && ttt[2][2] == player && ttt [3][2] == player)
    return true;
return false;
   }

public static boolean winColumn3 (char player, char a [][])
   {
if (ttt[1][3] == player && ttt[2][3] == player && ttt [3][3] == player)
    return true;
return false;
   }
}

最佳答案

如果您将 if 语句转换为组合的 if else if 语句,那么 else 情况下剩下的内容将是平局。

if(winRow1('O' , ttt))
        System.out.println("Your game " + gameNum + ": O won the game by row 1");
else if(winRow2('O' , ttt))
        System.out.println("Your game " + gameNum + ": O won the game by row 2");
.
.
.
else
        System.out.println("Your game " + gameNum + ": It is a tie");

顺便说一句,这样您就可以避免出现多条消息,例如同时选中一行和一列时;)。

关于java - 用 JAVA 编写井字棋游戏。使用许多 if 语句我如何让游戏读取平局游戏?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27339480/

相关文章:

java - 从对话框 EditText 更改 TextView

java - 特殊形式的字符串输入验证

if-statement - 为什么我的 CoffeeScript if/else 语句不起作用?

c++ - tictactoe 游戏没有运算符 "=="匹配这些操作数

java - ruby 与 Java : Why world is going to end faster with Java?

java - First-Fit Bin Packing 算法跳过数字

javascript - 使用条件语句在 Javascript 中的不同 for 循环之间进行选择

python - 在一个 'if' 语句中评估多个变量?

python - Python 2 中的 TicTacToe 项目 : I am trying to avoid using global variables and return variables instead

c++ - 尝试学习 C++,Tic Tac Toe 程序不起作用