Tic Tac Toe:所有州都以最佳动作返回

时间:2016-02-10 22:03:53

标签: java algorithm recursion depth-first-search minimax

我正在开发Tic Tac Toe游戏。请在下面找到所用类的说明。

  • 游戏::主要课程。该课程将启动多人游戏模式。

  • 主席:此课程设置主板配置。

  • 计算机:此类包含minimax算法。

  • BoardState :此类包含Board对象,以及用于生成板的最后x和y坐标
    配置。

  • 状态:此类包含分数以及将在游戏中获得该分数的x和y坐标。

上下文

  1. 在此处使用DFS。

  2. 探索整棵树的工作正常。

  3. 以这样的方式设置游戏,提示用户先播放。
  4. 用户将在游戏中放置x和y坐标。坐标在0到2之间。播放器的播放标记为1
  5. 计算机的响应为2,算法决定。 Th算法将确定要播放的最佳坐标,然后在该位置上播放2。
  6. Player的获胜配置之一是:

    玩家通过对角线获胜     121     112     221

  7. AI的获胜配置之一是:

    222 121 211 AI赢得横向

  8. 问题

    1. 所有州都给出了最高分。
    2. 基本上,当您在系统中启动游戏时,您会发现由于所有州都获得了最高分,因此计算机可以按顺序播放。
    3. 如果你放在(00),电脑在(01)播放,如果你在(02)播放,电脑将在(10)播放
    4. 我一直在尝试调试它很长一段时间。我相信,我可能会用评分功能弄乱一些东西。或者基本递归步骤中可能存在错误。此外,我不认为类中的不变性可能会导致问题,因为我一直在重新创建/创建新对象。

      我明白,这可能不是我能给出的最好的背景,但是如果你们能帮助我弄清楚它究竟出错的地方以及我该怎么做才能解决它,那将是一个很大的帮助。通过使用的代码/逻辑的答案将非常感激。谢谢!

      游戏类

      import java.io.Console;
      import java.util.ArrayList;
      import java.util.HashSet;
      import java.util.Scanner;
      import java.util.Set;
      
      
      public class Game {
      
      
          static int player1 = 1;
          static int player2 = 2;
      
          public static void main(String[] args) {
              // TODO Auto-generated method stub
      
              //
      
              //PLays Game
              playGame();
      
      
      
              /* Test Use Case
              ArrayList<Board> ar = new ArrayList<Board>();
              Board b = new Board();
              b.setVal(0,0,1);
              b.setVal(0,1,1);
              b.setVal(0,2,2);
              b.setVal(1,0,2);
              b.setVal(1,1,2);
              b.setVal(2,0,1);
              b.setVal(2,1,1);
      
              b.display();
              Computer comp = new Computer();
              State x = comp.getMoves(b, 2);
                     */
      
          }
      
      
      
          private static void playGame() {
              // TODO Auto-generated method stub
      
      
              Board b = new Board();
              Computer comp = new Computer();
      
              while(true)
              {
                  if(b.isBoardFull())
                  {
                      System.out.println("Game Drawn");
                      break;
                  }
      
                  b.display();
                  int[] pmove = getPlayerMove(b);
                  b.setVal(pmove[0],pmove[1], player1);
      
      
                  if(b.isVictoriousConfig(player1))
                  {
                      System.out.println("Player 1 Wins");
                      break;
                  }
      
                  if(b.isBoardFull())
                  {
                      System.out.println("Game Drawn");
                      break;
                  }
      
      
                  b.display();
      
                  System.out.println("Computer Moves");
      
      
                  /*For Random Play
                   * int[] cmove = comp.computeMove(b);
                   */
      
      
                   Computer compu = new Computer();
                       State s = compu.getMoves(b, 2);
      
                   int[] cmove = new int[2];
      
                   cmove[0] = s.x;
                   cmove[1] = s.y;
                   b.setVal(cmove[0],cmove[1], player2);
      
                  if(b.isVictoriousConfig(player2))
                  {
                      System.out.println("Computer Wins");
                      break;
                  }
      
              }
      
              System.out.println("Game Over");
      
      
          }
      
      
      
      
      
          //Gets Player Move. Basic Checks on whether the move is a valud move or not.
          private static int[] getPlayerMove(Board b) {
              // TODO Auto-generated method stub
      
      
              int[] playerMove = new int[2];
              System.out.println("You Play");
              while(true)
              {
                  @SuppressWarnings("resource")
                  Scanner reader = new Scanner(System.in);  // Reading from System.in
                  System.out.println("Enter X Position: ");
                  while(true)
                  {
                      playerMove[0] = reader.nextInt(); // Scans the next token of the input as an int.
                      if(playerMove[0] >2)
                      {
                          System.out.println("Incorrect Position");
                      }
                      else 
                      {
                          break;
                      }
                  }
                  System.out.println("Enter Y Position: ");
                  while(true)
                  {
                      playerMove[1] = reader.nextInt(); // Scans the next token of the input as an int.
                      if(playerMove[1] >2)
                      {
                          System.out.println("Incorrect Position");
                      }
                      else 
                      {
                          break;
                      }
                  }
                  System.out.println("You entered positions: X is " + playerMove[0] + " and Y is " + playerMove[1] );
                  if(!b.isPosOccupied(playerMove[0], playerMove[1]))
                  {
                      break;
                  }
                  System.out.println("Incorrect Move. PLease try again");
      
              }
      
              return playerMove;
      
          }
      
      
      }
      

      董事会成员

      import java.util.Arrays;
      
      
      public class Board {
      
          // Defines Board Configuration
          private final int[][] data ;
      
      
          public Board()
          {
              data = new int[3][3];
              for(int i=0;i<data.length;i++ )
              {
                  for(int j=0;j<data.length;j++ )
                  {
                      data[i][j] = 0;
                  }
              }
      
          }
      
      
          //Displays Current State of the board
      
          public void display()
          {
              System.out.println("Board");
      
              for(int i = 0; i< data.length;i++)
              {
                  for(int j = 0; j< data.length;j++)
                  {
                          System.out.print(data[i][j] + " ");
                  }
                  System.out.println();
              }
          }
      
      
          // Gets the Value on a specific board configuration
          public int getVal(int i, int j)
          {
              return data[i][j];
          }
      
          //Sets the value to a particular board location
          public void setVal(int i, int j,int val)
          {
              data[i][j] = val;
      
          }
      
      
      
      
          public boolean isBoardFull()
          {
              for(int i=0;i< data.length ; i++)
              {
                  for(int j=0;j< data.length ;j++)
                  {
                      if(data[i][j] == 0)
                          return false;
                  }
              }       
              return true;
          }
      
          public boolean isVictoriousConfig(int player)
          {
      
              //Noting down victory rules
              //Horizontal Victory
              if   ( (data[0][0] != 0) && ((data[0][0] == data [0][1]) && (data[0][1] == data [0][2]) && (data[0][2] == player)))
                  return true;
      
      
              if   ((data[1][0] != 0) && ((data[1][0] == data [1][1]) && (data[1][1] == data [1][2]) && (data[1][2] == player)))
                  return true;
      
      
              if   ((data[2][0] != 0) && ((data[2][0] == data [2][1]) && (data[2][1] == data [2][2]) && (data[2][2] == player)))
                  return true;
      
      
              //Vertical Victory
      
              if   ( (data[0][0] != 0) && ((data[0][0] == data [1][0]) && (data[1][0] == data [2][0]) && (data[2][0] == player)))
                  return true;
      
      
              if   ((data[0][1] != 0) && ((data[0][1] == data [1][1]) && (data[1][1] == data [2][1]) && (data[2][1] == player)))
                  return true;
      
      
              if   ((data[0][2] != 0) && ((data[0][2] == data [1][2]) && (data[1][2] == data [2][2]) && (data[2][2] == player)))
                  return true;
      
      
      
              //Diagonal Victory
      
              if   ( (data[0][0] != 0) && ((data[0][0] == data [1][1]) && (data[1][1] == data [2][2]) && (data[2][2] == player)))
                  return true;
      
      
              if   ( (data[0][2] != 0) && ((data[0][2] == data [1][1]) && (data[1][1] == data [2][0]) && (data[2][0] == player)))
                  return true;
      
              //If none of the victory rules are met. No one has won just yet ;)
      
              return false;
      
          }
      
          public boolean isPosOccupied(int i, int j)
          {
              if(data[i][j] != 0)
              {
                  return true;
              }
              return false;
      
          }
      
          public Board(int[][] x)
          {
              this.data = Arrays.copyOf(x, x.length);
          }
      
      
      }
      

      董事会成员

      public class BoardState {
      
      
          final Board br;
          final int x ;
          final int y;
          public BoardState(Board b, int posX, int posY)
          {
              br = b;
              x = posX;
              y = posY;
          }
      
      }
      

      国家

      public class State {
      
          final int s;
          final int x;
          final int y;
          public State(int score, int posX, int posY)
          {
              s = score;
              x = posX;
              y = posY;
          }
      
      }
      

      计算机

      import java.util.ArrayList;
      import java.util.Arrays;
      import java.util.HashMap;
      import java.util.HashSet;
      import java.util.Map;
      import java.util.Random;
      import java.util.Set;
      
      
      public class Computer {
      
          int[] pos = new int[2];
          static int player2 = 2;
          static int player1 = 1;
      
      
          ArrayList<Board> win =new ArrayList<Board>();
          ArrayList<Board> loose =new ArrayList<Board>();
          ArrayList<Board> draw =new ArrayList<Board>();
      
          public Computer()
          {
      
          }
      
          public int[] computeMove(Board b)
          {
      
              while(true)
              {
                  Random randX = new Random();
                  Random randY = new Random();
                  pos[0] = randX.nextInt(3);
                  pos[1] = randY.nextInt(3);
      
                  if(!b.isPosOccupied(pos[0], pos[1]))
                  {
                      return pos;
                  }
      
              }
          }
      
          public State getMoves(Board b,int p)
          {
              //System.out.println("test2");
              int x = 0;
              int y = 0;
              BoardState bs = new BoardState(b,0,0);
              //System.out.println("test");
              State s = computeMoveAI(bs,p);
              //System.out.println("Sore : X : Y" + s.s + s.x + s.y);
              return s;
      
          }
      
      
      
      
          private State computeMoveAI(BoardState b, int p) {
              // TODO Auto-generated method stub
              //System.out.println("Hello");
              int[][]sArray = new int[3][3];
              for(int i1 =0;i1<3;i1++)
              {
                  for(int j1=0;j1<3;j1++)
                  {
                      sArray[i1][j1] = b.br.getVal(i1, j1);
                  }
              }
              Board d = new Board(sArray);
              //System.out.println("d is ");
              //d.display();
              //System.out.println("p is "+ p);
              int xvalue= b.x;
              int yvalue = b.y;
              BoardState bs = new BoardState(d,xvalue,yvalue);
      
              if(getConfigScore(d,p) == 1)
              {
                      //System.out.println("Winning Config");
                      //System.out.println("X is " + b.x + " Y is " + b.y);
                  //  b.br.display();
                      State s = new State(-1,bs.x,bs.y);
                      return s;
              }
              else if (getConfigScore(d,p) == -1)
              {
                         //System.out.println("LooseConfig");
                      State s = new State(1,bs.x,bs.y);
                      //System.out.println("Coordinates are "+bs.x + bs.y+ " for " + p);
      
                      return s;
              }
              else if(bs.br.isBoardFull())
              {
                  //System.out.println("Board is full");
                  State s = new State(0,bs.x,bs.y);
                  //System.out.println("score " + s.s + "x "+ s.x + "y "+ s.y);
                  return s;
              }
              else
              {
                  //Get Turn
                  //System.out.println("In else condiyion");
                  int turn;
                  if(p == 2)
                  {
                      turn = 1;
                  }else
                  {
                      turn = 2;
                  }
      
                  ArrayList<BoardState> brr = new ArrayList<BoardState>();
                  ArrayList<State> st = new ArrayList<State>();
      
                  brr = getAllStates(d,p);
      
      
      
                  for(int k=0;k<brr.size();k++)
                  {   
                      //System.out.println("Goes in " + "turn " + turn);
                      //brr.get(k).br.display();
                      int xxxxx = computeMoveAI(brr.get(k),turn).s;
      
                      State temp = new State(xxxxx,brr.get(k).x,brr.get(k).y);
                      st.add(temp);
      
                  }
                  //Find all Nodes.
                  //Go to First Node and proceed with recursion.  
                  //System.out.println("Displaying boards");
                  for(int i=0;i<brr.size();i++)
                  {
                      //brr.get(i).br.display();
                      //System.out.println(brr.get(i).x + " " + brr.get(i).y);
                  }
                  //System.out.println("Board configs are");
                  for(int i=0;i<st.size();i++)
                  {
                      //System.out.println(st.get(i).x + " " + st.get(i).y + " and score " + st.get(i).s);
                  }
                  //System.out.println("xvalue" + xvalue);
                  //System.out.println("yvalue" + yvalue);
                  //System.out.println("Board was");
                  //d.display();
                  //System.out.println("Coming to scores");
                  //System.out.println(" p is "+ p);
                  if(p == 2)
                  {
                      //System.out.println("Size of first response" + st.size());
                      //System.out.println("Returns Max");
                      return max(st);
      
                  }
                  else
                  {
                      //System.out.println("The last");
                      return min(st);
                  }
      
              }
      
      
          }
      
          private State min(ArrayList<State> st) {
              // TODO Auto-generated method stub
              ArrayList<State> st1= new ArrayList<State>();
              st1= st;
              int min = st.get(0).s;
              //System.out.println("Min score is " + min);
      
              for(int i=1;i<st1.size();i++)
              {
                  if(min > st1.get(i).s)
                  {
                      min = st1.get(i).s;
                      //System.out.println("Min is");
                      //System.out.println(min);
                      //System.out.println("Min" + min);
                      State s = new State(min,st1.get(i).x,st1.get(i).y);
                      return s;
      
                  }
              }
              State s = new State(st1.get(0).s,st1.get(0).x,st1.get(0).y);
              //System.out.println("Max" + st1.get(0).s);
              //System.out.println("Exits Min");
              //System.out.println("Min Score" + st1.get(0).s + " x" + st1.get(0).x + "y " + st1.get(0).y);
              return s;
          }
      
          private State max(ArrayList<State> st) {
              // TODO Auto-generated method stub
              //System.out.println("Size of first response in funciton is " + st.size());
              ArrayList<State> st1= new ArrayList<State>();
              st1 = st;
              int max = st1.get(0).s;
              for(int i=0;i<st1.size();i++)
              {
              //  System.out.println(i+1 + " config is: " + "X:" + st1.get(i).x + "Y:" + st1.get(i).y + " with score " + st1.get(i).s);
      
              }
              for(int i=1;i<st1.size();i++)
              {
                  //.out.println("Next Item " + i + st.get(i).s + "Coordinates X"+  st.get(i).x +"Coordinates Y"+  st.get(i).y );
      
                  if(max < st1.get(i).s)
                  {
                      max = st1.get(i).s;
                      //System.out.println("Max" + max);
                      //System.out.println("Max is");
                      //System.out.println(max);
                      State s = new State(max,st1.get(i).x,st1.get(i).y);
                      //System.out.println("Max Score returned" + s.s);
                      //System.out.println("Returned");
                      return s;
      
                  }
              }
              State s = new State(st1.get(0).s,st1.get(0).x,st1.get(0).y);
              //System.out.println("Max" + st1.get(0).s);
              //System.out.println("Max is outer");
              //System.out.println(st.get(0).s);
              return s;
          }
      
          //Basic brain Behind Min Max algorithm
          public int getConfigScore(Board b,int p)
          {
              int score;
              int turn ;
              int opponent;
      
              if(p == player1)
              {
                  turn = p;
                  opponent = player2;
              }
              else
              {
                  turn = player2;
                  opponent = player1;
              }
      
      
              int[][]sArray = new int[3][3];
              for(int i1 =0;i1<3;i1++)
              {
                  for(int j1=0;j1<3;j1++)
                  {
                      sArray[i1][j1] = b.getVal(i1, j1);
                  }
              }
              Board d = new Board(sArray);
              //System.out.println("s arrasy is ");
              //d.display();
              //System.out.println("turn is " + turn);
              if(d.isVictoriousConfig(turn))
              {
                  score = 1;
              }
              else if(d.isVictoriousConfig(opponent))
              {
                  score = -1;
              }
              else
              {
                  score = 0;
              }
      
              return score;
      
          }
      
      
          public static ArrayList<BoardState> getAllStates(Board b, int player)
          {
      
      
              ArrayList<BoardState> arr = new ArrayList<BoardState>();
      
              int[][]s1 = new int[3][3];
              for(int i1 =0;i1<3;i1++)
              {
                  for(int j1=0;j1<3;j1++)
                  {
                      s1[i1][j1] = b.getVal(i1, j1);
                  }
              }
              Board d = new Board(s1);
      
      
              for(int i = 0;i <3; i ++)
              {
                  for (int j=0;j<3;j++)
                  {
                      if(!d.isPosOccupied(i, j))
                      {
      
                          int previousState = d.getVal(i, j);
                          int[][]s = new int[3][3];
                          for(int i1 =0;i1<3;i1++)
                          {
                              for(int j1=0;j1<3;j1++)
                              {
                                  s[i1][j1] = d.getVal(i1, j1);
                              }
                          }
                          s[i][j] = player;
                          Board d1 = new Board(s);
                          BoardState bs = new BoardState(d1,i,j);
                          arr.add(bs);
      
      
                      }
      
                  }
      
              }
      
              return arr;
      
          }
      
      
      
      
      
      }
      

      这是Game类中测试用例的输出:

      Board
      1 1 2 
      2 2 0 
      1 1 0 
      Score : X : Y -> 1: 1: 2
      

      这里的解决方案是得分:1表示x:1,y:2。看起来很正确。 但是,你可以说,因为它是顺序移动的,并且由于位置(1,2)将在(2,2)之前到达,所以它得到了正确的坐标。

      据我所知,这可能是解决问题的大量代码。但 computemoveAI()getConfigScore()可能是需要注意的功能。另外,我不想把你的想法偏向于我认为问题可能在哪里,有时我们会看到某个地方,但问题出在其他地方。!!

0 个答案:

没有答案