java - 如何返回游戏?完成游戏后

标签 java

我正在做一个 TicTacToe 程序,唯一缺少的部分是让用户选择是否退出游戏或重玩。我找不到“返回”游戏的方法。

import java.io.*;

public class Expierment
{
        static char c1 [] = new char[10];
        static char c2 [] = new char[10];
        static char c3 [] = new char[10];
        static char p1;
        static char p2;
        static boolean gameOver = false;
        public static void main(String args[])
        {
            int counter = 0;
            int p1Wins = 0;
            int p2Wins = 0;
            int r1 = 0;
            int r2 = 0;
            int r3 = 0;
            int r4 = 0;
            int r5 = 0;
            int r6 = 0;
            int r7 = 0;
            int r8 = 0;
            int r9 = 0;
            int pick1 = 0;
            int pick2 = 0;
            int pick3 = 0;
            int pick4 = 0;
            int pick5 = 0;
            int pick6 = 0;
            int pick7 = 0;
            int pick8 = 0;
            int pick9 = 0;
            char turn = 'X';
            int choice = menu();
            switch(choice)
            {
                case 1:
                    System.out.println("The game is called 'Tic-Tac-Toe', you should have known it. If you don't, search it.")  ;
                case 2:
                    gameOver = false;
                    break;
                case 3:
                    System.out.println("\nSee you next time !!");
                    return;
                default:
                    System.out.println("\nYou hit the wrong key......\n");
                    return;
            }//end of switch
            System.out.println("\nPlayer 1 initials ?");
            String n1 = GCS();
            p1 = n1.charAt(0);
            System.out.println("\nPlayer 2 initials ?");
            String n2 = GCS();
            p2 = n2.charAt(0);
            c1[2]='1';
            c2[2]='2';
            c3[2]='3';
            c1[1]='4';
            c2[1]='5';
            c3[1]='6';
            c1[0]='7';
            c2[0]='8';
            c3[0]='9';
        printBoard ();
        while(gameOver!=true)
        {
            System.out.println("Which spot ?");
            int pick = Integer. parseInt(GCS());
                switch (pick)
            {
                case 1:
                    if (r1<1)
                {
                    c1[2] = turn;
                    r1++;
                }
                else
                {
                    System.out.println("That column is full, pick another.\n");
                    continue;
                }
                    break;                  
                case 2:
                    if (r2<1)
                {
                    c2[2] = turn;
                    r2++;
                }
                else
                {
                    System.out.println("That column is full, pick another.\n");
                    continue;
                }
                    break;                  
                case 3:
                    if (r3<1)
                {
                    c3[2] = turn;
                    r3++;
                }
                else
                {
                    System.out.println("That column is full, pick another.\n");
                    continue;
                }
                    break;
                case 4:
                    if (r4<1)
                {
                    c1[1] = turn;
                    r4++;
                }
                else
                {
                    System.out.println("That column is full, pick another.\n");
                    continue;
                }
                    break;
                case 5:
                    if (r5<1)
                {
                    c2[1] = turn;
                    r5++;
                }
                else
                {
                    System.out.println("That column is full, pick another.\n");
                    continue;
                }
                    break;
                case 6:
                    if (r6<1)
                {
                    c3[1] = turn;
                    r6++;
                }
                else
                {
                    System.out.println("That column is full, pick another.\n");
                    continue;
                }
                    break;
                case 7:
                    if (r7<1)
                {
                    c1[0] = turn;
                    r7++;
                }
                else
                {
                    System.out.println("That column is full, pick another.\n");
                    continue;
                }
                    break;
                case 8:
                    if (r8<1)
                {
                    c2[0] = turn;
                    r8++;
                }
                else
                {
                    System.out.println("That column is full, pick another.\n");
                    continue;
                }
                    break;
                case 9:
                    if (r9<1)
                {
                    c3[0] = turn;
                    r9++;
                }
                else
                {
                    System.out.println("That column is full, pick another.\n");
                    continue;
                }
                    break;
                default:
                    System.out.println("Seriously?! Pick a possible spot.\n");
                    continue;
                }//end of switch
            if (turn=='X') turn = 'O';
            else turn = 'X';
            printBoard();
            if (checkWinner())
            {
                while(gameOver==true)
                {
                    int Echoice = EGM();
                    switch(Echoice)
                    {
                        case 1:
                            System.out.println("The game is called 'Tic-Tac-Toe', you should have known it. If you don't, search it.")  ;
                        case 2:
                            gameOver = false;
                            menu();

                            break;
                        case 3:
                            System.out.println("\nSee you next time !!");
                            return;
                        default:
                            System.out.println("\nYou hit the wrong key......\n");
                            return;
                    }//end of switch
                }//end of while true
                return;
            }
            counter ++;
            if (counter==9)
            {
                System.out.println("\n\nYou tied.\n");
                return;
            }
        }//end of while not true
    }//end of main

    public static boolean checkWinner()
    {
            for (int k=0; k<2; k++)
        {
            if ((c1[k]!=' ')&&(c1[k]==c2[k])&&(c1[k]==c3[k]))
            {
                System.out.println("\nYo " + c1[k] + " is the winner!\n");
                gameOver=true;
                return true;
            }//checks column 1-3 horizontally
        }//end of horizontal check

        for (int m=0; m<2; m++)
        {
            if((c1[m]!=' ')&&(c1[m]==c1[m+1])&&(c1[m+1]==c1[m+2])&&(c1[m]==c1[m+2]))
            {
                System.out.println("\nYo " + c1[m] + " is the winner!\n");
                gameOver=true;
                return true;
            }//checks column 1 vertically
            if((c2[m]!=' ')&&(c2[m]==c2[m+1])&&(c2[m+1]==c2[m+2])&&(c2[m]==c2[m+2]))
            {
                System.out.println("\nYo " + c2[m] + " is the winner!\n");
                gameOver=true;
                return true;
            }//checks column 2 vertically
            if((c3[m]!=' ')&&(c3[m]==c3[m+1])&&(c3[m+1]==c3[m+2])&&(c3[m]==c1[m+2]))
            {
                System.out.println("\nYo " + c3[m] + " is the winner!\n");
                gameOver=true;
                return true;
            }//checks column 3 vertically
                if ((c1[m]!=' ')&&(c1[m]==c2[m+1])&&(c1[m]==c3[m+2]))
            {
                System.out.println("\nYo " + c1[m] + " is the winner!\n");
                gameOver=true;
                return true;
            }//checks upward diagonal
                if ((c3[m]!=' ')&&(c3[m]==c2[m+1])&&(c3[m]==c1[m+2]))
            {
                System.out.println("\nYo " + c1[m] + " is the winner!\n");
                gameOver=true;
                return true;
            }
        }//end of vertical check
                return false;
    }//end of checkWinner

    public static void printBoard()
    {
        System.out.println("_______");
        for (int j = 2; j > -1; j--)
        {
            System.out.println("|" + c1[j] + "|" + c2[j] + "|" + c3[j] + "|");
            System.out.println("-------");
        }
    }//end of printBoard

    public static int menu()
    {
        System.out.println("Tic-Tac-Toe ~ Main Menu\n\n1. Instructions\n2. Play a 1 player game"+"\n3. Exit\n");
        int selection = Integer.parseInt(GCS());
        return selection;
    }//end of menu

    public static int EGM()
    {
        System.out.println("Tic-Tac-Toe ~ End of Game Menu\n\n1. Instructions\n2. Play again"+"\n3. Exit\n");
        int Eselection = Integer.parseInt(GCS());
        return Eselection;
    }

    public static String GCS()
    {
        int noMoreInput=-1;
        char enterKeyHit='\n';
        int InputChar;
        StringBuffer InputBuffer = new StringBuffer(100);

        try
        {
            InputChar=System.in.read();
            while(InputChar != noMoreInput)
            {
                if((char)InputChar !=enterKeyHit)
                {
                    InputBuffer.append((char)InputChar);
                }
                else
                {
                    InputBuffer.setLength(InputBuffer.length()-1);
                    break;
                }
                InputChar=System.in.read();
            }
        }
        catch (IOException IOX)
        {
            System.err.println(IOX);
        }
        return InputBuffer.toString();
        }//end of GCS
}//end of public class

最佳答案

您确实应该从 main 函数中获取一些代码。

具体来说,我会将整个游戏循环放在一个单独的函数中,可能称为 playGame(),其中包含玩游戏、检查获胜者等的逻辑,并且返回获胜者或仅打印获胜者并返回 void。

然后主函数可以在循环中调用 playGame(),并在循环结束时询问用户是否想再次玩。

一般来说,您希望每个函数执行一个逻辑任务。您已经很好地移出了 checkWinner,现在对其他一些代码执行相同的操作。

如果您需要有关“询问用户是否再次玩游戏”的帮助,请发表评论,我将进行编辑以解决该问题。

关于java - 如何返回游戏?完成游戏后,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4787892/

相关文章:

java - 生成跨多个表唯一的主键

java - 如何以线程安全的方式将递增的用户 ID 分配给新用户?

java - Java中用私钥加密解密

java - 序列化-为什么readObject不能读取整个文件?

java - 如何在 Java 中确定请求的优先级和限制请求?

java - 为什么复合ID类必须实现Serializable?

Java:在arraylist中存储扩展对象并调用parent toString方法

java - 使用 "NoSuchElementException"定位元素时获取 "@AndroidFindBy()"

java - getPathParameters() 和 getQueryParameters() 之间的区别

java - 从父目录调用 java 类