有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

爪哇井字游戏分数

这是我目前为tic-tac-toe编写的代码,它工作得很好,我修正了如何保持分数,但是,我觉得代码不好,不干净。有没有办法让代码更干净

import java.util.Scanner;
import java.io.*;
import java.lang.String;


      public class TicTacToe{
          public static int a;
          public static int b; 
          public static int c;
          public static int d;
          public static int e;
          public static int f;
          public static int g;
          public static int h;
          public static int i;
                    public static boolean Winner(){
                          if(a == 1 && b == 1 && c == 1){
                                     return true;       
                                    } 
                              else if(d == 1 && e == 1 && f == 1){
                                     return true;       
                                    } 
                              else if(g == 1 && h == 1 && i == 1){
                                     return true;      
                                    } 
                              else if(a == 1 && d == 1 && g == 1){
                                     return true;      
                                    } 
                              else if(b == 1 && e == 1 && h == 1){
                                     return true;       
                                    } 
                              else if(c == 1 && f == 1 && i == 1){
                                     return true;      
                                    } 
                              else if(a == 1 && e == 1 && i == 1){
                                     return true;      
                                    } 
                              else if(c == 1 && e == 1 && g == 1){
                                     return true;       
                                    } 
                              else if(a == 2 && b == 2 && c == 2){
                                     return true;       
                                    }
                              else if(d == 2 && e == 2 && f == 2){
                                     return true;      
                                    }
                              else if(g == 2 && h == 2 && i == 2){
                                     return true;
                                    }       
                              else if(a == 2 && d == 2 && g == 2){
                                     return true;   
                                    }    
                              else if(b == 2 && e == 2 && h == 2){
                                     return true; 
                                    }      
                              else if(c == 2 && f == 2 && i == 2){
                                     return true; 
                                    }      
                              else if(a == 2 && e == 2 && i == 2){
                                     return true;    
                                    }  
                              else if(c == 2 && e == 2 && g == 2){
                                     return true;
                                    }       
                                     return false;
                                    }
                public static void main (String args[]){
                        String line1 = "1 | 2 | 3";
                        String line2 = "-------";
                        String line3 = "4 | 5 | 6";
                        String line4 = "-------";
                        String line5 = "7 | 8 | 9";
                        String line6 = "-------";
                               Scanner tic = new Scanner (System.in);
                               Scanner tac =  new Scanner (System.in);

                                    System.out.println("Welcome to a game of Tic Tac Toe");
                                    System.out.println("The game for those who aren't good at anything else.");
                                    System.out.println (line1);
                                    System.out.println (line2);
                                    System.out.println (line3);
                                    System.out.println (line4);
                                    System.out.println (line5);
                                    System.out.println (line6);


                    for (int i=0; i < 7; i++)
                       {

                        System.out.println("First player please choose a number. ");
                        int number = tic.nextInt();

                        if (number == 1){
                        line1 = line1.replace ("1","O");
                        a = 1;
                        }
                        if (number == 2){
                        line1 = line1.replace ("2","O");
                        b = 1;
                        }
                        if (number  == 3){
                        line1 = line1.replace ("3","O");
                        c = 1;
                        }
                        if (number == 4){
                        line3 = line3.replace ("4","O");
                        d = 1;
                        }
                        if (number == 5){
                        line3 = line3.replace ("5","O");
                        e = 1;
                        }
                        if (number == 6){
                        line3 = line3.replace ("6","O");
                        f = 1;
                        }
                        if (number == 7){
                        line5 = line5.replace ("7","O");
                        g = 1;
                        }
                        if (number == 8){
                        line5 = line5.replace ("8","O");
                        h = 1;
                        }
                        if (number == 9){
                        line5 = line5.replace ("9","O");
                        i = 1;
                        }


                            System.out.println(line1);
                            System.out.println(line2);
                            System.out.println(line3);
                            System.out.println(line4);
                            System.out.println(line5);
                                  if(Winner()){
                                  System.out.println("First Player wins.");
                                  System.exit(1);
                                      }
                            if(a != 0 && b != 0 && c != 0 && d != 0 && e != 0 && f != 0 && g != 0 && h != 0 && i != 0){
                                  System.exit(1);
                                      }
                        System.out.println("Second player please choose a number. ");
                        int number2 = tac.nextInt();

                        if (number2 == 1){
                        line1 = line1.replace ("1","X");
                        a = 2;
                         }
                        if (number2 == 2){
                        line1 = line1.replace ("2","X");
                        b = 2;
                         }
                        if (number2  == 3){
                        line1 = line1.replace ("3","X");
                        c = 2;
                         }
                        if (number2 == 4){
                        line3 = line3.replace ("4","X");
                        d = 2;
                         }
                        if (number2 == 5){
                        line3 = line3.replace ("5","X");
                        e = 2;
                         }
                        if (number2 == 6){
                        line3 = line3.replace ("6","X");
                        f = 2;
                         }
                        if (number2 == 7){
                        line5 = line5.replace ("7","X");
                        g = 2;
                         }
                        if (number2 == 8){
                        line5 = line5.replace ("8","X");
                        h = 2;
                         }
                        if (number2 == 9){
                        line5 = line5.replace ("9","X");
                        i = 2;
                         }
                              System.out.println(line1);
                              System.out.println(line2);
                              System.out.println(line3);
                              System.out.println(line4);
                              System.out.println(line5);
                              System.out.println(line6);
                                 if(Winner()){
                                    System.out.println("Second Player wins.");
                                    System.exit(1);
                                       }
                             if(a != 0 && b != 0 && c != 0 && d != 0 && e != 0 && f != 0 && g != 0 && h != 0 && i != 0){
                                    System.exit(1);
      }
     }
    }
   }

这是可行的,但有没有更有效的方法来修复它


共 (3) 个答案

  1. # 1 楼答案

    你的程序似乎无法检测玩家何时获胜。 一旦你实现了这一点,你所要做的就是用整数追踪每个玩家的分数。当你发现任何一个玩家赢了,增加与他的分数相对应的整数。:)

  2. # 2 楼答案

    import javax.swing.*;
    import java.util.*;
    public class TicTacToe
    {
    private String [] line = new String[5];
    private Scanner input;
    private int counter;
    
    public TicTacToe()
    {
        counter = 0;
        input = new Scanner(System.in);
        line[0] = "1 | 2 | 3";
        line[1] = "_______";
        line[2] = "4 | 5 | 6";
        line[3] = "_______";
        line[4] = "7 | 8 | 9";
    }
    
    public void rulesOfTheGame(){
        System.out.println("Tic Tac Toe");
        System.out.println(line);
    }
    
    public void replace(String str)
    {
        for (int j = 1; j < 10; j++)
        {
            System.out.println("Player choose a number.");
            int number; 
            number = 0;
            for (int i = 1; i < 10; i++)
            {
                number = input.nextInt();
                if ((number == i) && ((i == 1) || (i == 2) || (i == 3))){
                    String num = "" + i + "";
                    line[0] = line[0].replace(num,str);
                    counter++;
                }
                if ((number == i) && ((i == 4) || (i == 5) || (i == 6))){
                    String num = "" + i + "";
                    line[2] = line[2].replace(num,str);
                    counter++;
                }
                else
                {
                    String num = "" + i + "";
                    line[4] = line[4].replace(num,str);
                    counter++;
                }
                System.out.println(line);
            }
        }
    }
    
    public void winnerWinnerChickenDinner()
    {
        if ((counter >= 5) && (counter < 9)){
            String isItOver = JOptionPane.showInputDialog("Getting down to the wire here, is the game over yet! CAPITAL Y FOR YES OR CAPITAL N FOR NO", "Game Over");
            if (isItOver.equals("Y"))
            {
                System.out.println("Game Over");
            }
            else
            {
                System.out.print("");
            }
        }
        else if (counter == 9){
            System.out.println("Game Over");
            System.exit(0);
        }
    }
    }
    

    这是最有效的代码。我真的可以创造。我相信有更简单的方法可以做到这一点;然而,我认为这仍然非常简洁,几乎没有重复的代码

  3. # 3 楼答案

    为了获得游戏的赢家和输家,并给出适当的分数,你必须检查游戏板的状态。解析文本有点复杂,需要做很多不必要的工作,所以我建议你用matrix来代表董事会

    示例:

    char[][] gameBoard = new char[3][3];
    

    这样,当您通过扫描器接收到输入时,就像您的代码一样,您将向gameBoard变量输入所需的字符('O'或'X'),如下所示:

    // number is 1-9 according to your code
    int row = number/3; // Gives you 0 or 1 or 2
    int col = number%3; // Gives you 0 or 1 or 2
    gameBoard[row][col] = 'O'; // or 'X'
    

    每轮到一名玩家后,您将调用以下函数(自己完成代码,查找java matrix iteration):

    public boolean hasWinner(int playerNumber)
    {
        // playerNumber will be passed according to which player did the current turn.
        // The function will try to search for the sequence that wins the game.
    
    }