2015-01-08 35 views
-1

我已经完成了90%的Tic TAC脚趾游戏,并且拥有一种电脑AI形式,但是效果并不好。我正在寻找一种新的更容易的电脑来移动游戏板的地方。Tic tac脚趾电脑选择现货?

我需要这台电脑移动选择来选择一个点,但也要确保点是免费提供的,并且在该点没有'X'。关于如何让电脑选择一个点的任何想法?

/* I have 3 other methods drawing the board, and determining a tie, a loss, and a win not shown here for simplicity*/ 

public static void main(String[] args) { 
      //Variable declaration 
      Scanner kbReader = new Scanner(System.in); 
      char[][] Board = new char[3][3]; 
      String MenuInput; 
      int BoardOutput; 
      int UserSpotChoice; 
      int ComputerSpotChoice = 0; 
      int UserTurn = 0; 
      int Winner = 0; 
      Board[0][0] = '-'; 
      Board[0][1] = '-'; 
      Board[0][2] = '-'; 
      Board[1][0] = '-'; 
      Board[1][1] = '-'; 
      Board[1][2] = '-'; 
      Board[2][0] = '-'; 
      Board[2][1] = '-'; 
      Board[2][2] = '-'; 

      //Welcome 
      System.out.println("Welcome to Alex Montague's Tic Tac Toe game!"); 
      System.out.println(""); 
      System.out.println("If you wish to play, type 'Play'"); 
      System.out.println("If you wish to read the instructions, type 'Instructions'"); 
      System.out.println("If you wish to exit, type 'Exit'"); 
      MenuInput = kbReader.next(); 

      do { 
       if (MenuInput.equals("Play") || MenuInput.equals("play")) { 
        while (!GameOver) { 
         System.out.println("\f"); 
         System.out.println(" Tic Tac Toe"); 
         BoardOutput = DrawBoard(Board); 
         System.out.println(" 1 2 3"); 
         System.out.println(" 4 5 6"); 
         System.out.println(" 7 8 9"); 
         System.out.println("Please enter the number you would like to move your spot to"); 
         UserSpotChoice = kbReader.nextInt(); 

         if (UserSpotChoice == 1) Board[0][0] = 'X'; 
         if (UserSpotChoice == 2) Board[0][1] = 'X'; 
         if (UserSpotChoice == 3) Board[0][2] = 'X'; 
         if (UserSpotChoice == 4) Board[1][0] = 'X'; 
         if (UserSpotChoice == 5) Board[1][1] = 'X'; 
         if (UserSpotChoice == 6) Board[1][2] = 'X'; 
         if (UserSpotChoice == 7) Board[2][0] = 'X'; 
         if (UserSpotChoice == 8) Board[2][1] = 'X'; 
         if (UserSpotChoice == 9) Board[2][2] = 'X'; 

         do { 
          ComputerSpotChoice = (int)(Math.random() * 9) + 1; 
         } 
         while (Board[(ComputerSpotChoice - 1)/3][(ComputerSpotChoice - 1) % 3] != '-'); //HERE IS THE OLD COMPUTER SPOT GENERATION THAT DID NOT WORK 

         if (ComputerSpotChoice == 1) Board[0][0] = 'O'; 
         if (ComputerSpotChoice == 2) Board[0][1] = 'O'; 
         if (ComputerSpotChoice == 3) Board[0][2] = 'O'; 
         if (ComputerSpotChoice == 4) Board[1][0] = 'O'; 
         if (ComputerSpotChoice == 5) Board[1][1] = 'O'; 
         if (ComputerSpotChoice == 6) Board[1][2] = 'O'; 
         if (ComputerSpotChoice == 7) Board[2][0] = 'O'; 
         if (ComputerSpotChoice == 8) Board[2][1] = 'O'; 
         if (ComputerSpotChoice == 9) Board[2][2] = 'O'; 

         Winner(Board); 
         Loser(Board); 
         Tie(Board); 

        } //While loop 
        //if (GameOver) System.exit (0) ; 
       } //If play 
       else if (MenuInput.equals("Instructions") || MenuInput.equals("instructions")) { 
        System.out.println("\f"); 
        System.out.println("You will be playing the game of Tic Tac Toe against the computer."); 
        System.out.println("The object of this game is to get three of your own x's or o's in a line."); 
        System.out.println("You take turns placing the x's and o's and whoever gets three in a row first wins."); 
        System.out.println("Good Luck!"); 
        System.out.println(""); 
        System.out.println("If you wish to play, type 'Play'"); 
        System.out.println("If you wish to exit, type 'Exit'"); 
        MenuInput = kbReader.next(); 
       } else if (MenuInput.equals("Exit") || MenuInput.equals("exit")) { 
        System.out.println("Thank you for using Alex Montague's Tic Tac Toe game!"); 
        System.exit(0); 
       } else { 
        System.out.println("Sorry, that is not a valid choice."); 
        System.out.println("If you wish to play, type 'Play'"); 
        System.out.println("If you wish to read the instructions, type 'Instructions'"); 
        System.out.println("If you wish to exit, type 'Exit'"); 
        MenuInput = kbReader.next(); 
       } 

      } //do while 
      while (!MenuInput.equals("Instructions") || !MenuInput.equals("instructions") || !MenuInput.equals("Play") || !MenuInput.equals("play") || !MenuInput.equals("Exit") || !MenuInput.equals("exit")); 
+0

为双方球员的最佳策略可以在[百科]中找到(http://en.wikipedia.org/wiki/Tic-tac-toe)。 – Stefan

+0

这个问题似乎无关紧要,因为它涉及游戏开发,因此更适合http://gamedev.stackexchange.com/。 – TLama

+0

这与您[最近发布的其他问题](http://stackoverflow.com/q/27539015/3097506)有何不同? –

回答

0

创建方法public static boolean isSpotAvailable(int x, int y)。如果没有,则随机找到一个PC的位置后检查,如果没有,则使用它,然后选择一个新位置。

0

一个简单的方法可能是,对于每个正方形,您检查可以从该行创建3个项目的行数。因此,例如,中间的方形将产生4(因为您可以创建从中间通过的水平行,从中间和两个对角线通过的垂直行)。另一方面,边缘的正方形会产生2的值(因为您可以有1个垂直和1个水平)。

如果你想有一个更强大的方法,那么我认为推荐的方法是使用MiniMax Tree。这将允许您构建决策树,以便您的算法决定将放置在下一块的位置。这种针对tic tac脚趾的方法的例子可以参见here

0

您可以使用HashSet对象跟踪索引。只要您可以成功地将1到9之间的位置添加到设置对象中,该位置就可用。另一种方法是使用ArrayDequeue对象。每次使用某个地点时,相关的地点索引将被“删除”,如果它已经出来,计算机将无法使用它。

/* For HashSet<Integer> object - Integer is an immutable wrapper class for int */ 

spotObj.add(spotLoc); // Returns falls if the spot already exists i.e. used 

/* For ArrayDequeue<Integer> object */ - Implementation of Queue interface */ 

spotObj.remove(spotLoc); returns false if it is already removed i.e. used 

希望这有助于你解决问题

0

有是很多需要提高你的AI一个简单的方法。

第一个:检查板的当前状态,并找到AI的所有可能动作(意思是:找到所有空的字段)。

二:找个机会赢下这场比赛,这意味着你应该从我们的第一步模拟所有这些可能的行动,看看有没有人可以让你赢得比赛。

- >如果是这样:做那个动作!

- >如果不是:做一个随机移动。

通过这样做你的AI已经得到改善。但是,通过使用简单的数据库,您可以更进一步。跟踪你的AI和敌人所做的所有动作。 每当你发现自己从现在开始,你会做一个随机移动的情况下,检查你的数据库,如果你曾经在这种情况之前,以及它如何为你。根据数据库中前一个游戏的结果,再次执行相同的操作或尝试不同的操作(通过随机移动)可能是一个好主意。 只需将每个游戏及其结果附加到此数据库中,您就会看到您的AI在游戏中将如何变得更好。

如果您需要任何代码示例只是让我知道。

问候添