Guest User

Untitled

a guest
Sep 13th, 2016
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.01 KB | None | 0 0
  1. f (gamecompleted[n - 1] == 0)
  2.                 {
  3.                     int winLost = 0, playerMove, cpuMove, checkWin, movesLeft = 0, winner = -1;
  4.                     int[] fieldMain = {-1,-1,-1,-1,-1,-1,-1,-1,-1};
  5.                     Randomgames m = new Randomgames(fieldMain);
  6.                     String[] fieldPosition = {" 0 "," 1 "," 2 "," 3 "," 4 "," 5 "," 6 "," 7 "," 8 "};
  7.                     System.out.println("This game will be tic tac toe!");
  8.                     Thread.sleep(1000);
  9.                     System.out.println("You will play against the computer.");
  10.                     Thread.sleep(1000);
  11.                     System.out.println("When prompted enter the number of the box you would like to go, as shown below.");
  12.                     System.out.println("-------------");
  13.                     System.out.println("| 0 | 1 | 2 |");
  14.                     System.out.println("|-----------|");
  15.                     System.out.println("| 3 | 4 | 5 |");
  16.                     System.out.println("|-----------|");
  17.                     System.out.println("| 6 | 7 | 8 |");
  18.                     System.out.println("-------------");
  19.                     do
  20.                     {
  21.                         System.out.println("Please make a move");
  22.                         playerMove = scan.nextInt();
  23.                         do
  24.                         {
  25.                         if (playerMove > -1)
  26.                             if(playerMove < 9)
  27.                                  if (fieldMain[playerMove] != -1)
  28.                                  {
  29.                                      System.out.println("Invalid Move! Try again.");
  30.                                      playerMove = scan.nextInt();
  31.                                  }
  32.                                  else
  33.                                  {    
  34.                                  fieldPosition[playerMove] = " X ";
  35.                                  fieldMain[playerMove] = 1;
  36.                                  m.writeField(fieldMain);
  37.                                  m.printField(fieldPosition);
  38.                                  break;
  39.                                  }
  40.                             else
  41.                                 {
  42.                                   System.out.println("Invalid Move! Try again.");
  43.                                   playerMove = scan.nextInt();
  44.                                  }
  45.                         else
  46.                             {
  47.                               System.out.println("Invalid Move! Try again.");
  48.                                playerMove = scan.nextInt();
  49.                             }
  50.                         }while(winLost == 0);          
  51.                         checkWin = m.winOrLose(1);
  52.                         if (checkWin == 1)
  53.                         {   winLost = 1;
  54.                             break;
  55.                         }
  56.                         else
  57.                         {
  58.                          for (int i = 0; i < 9; i++)
  59.                              if (fieldMain[i] == -1)
  60.                                  movesLeft++;  
  61.                          if (movesLeft == 0)
  62.                             if (checkWin == 1)
  63.                             {  winLost = 1;
  64.                               break;
  65.                              }
  66.                              else
  67.                             {
  68.                              System.out.println("You tied!");
  69.                              Thread.sleep(1000);
  70.                              System.out.println("Resetting Game!");
  71.                              Thread.sleep(3000);
  72.                              fieldMain = new int[]{-1,-1,-1,-1,-1,-1,-1,-1,-1};
  73.                              fieldPosition = new String[]{" 0 "," 1 "," 2 "," 3 "," 4 "," 5 "," 6 "," 7 "," 8 "};
  74.                             }
  75.                         }
  76.  
  77.                         System.out.println("Generating CPU move...");
  78.                         Thread.sleep(1000);
  79.                         cpuMove = (int)(Math.random() * 8 + 0);
  80.                         do{
  81.                            if(fieldMain[cpuMove] != -1)
  82.                            { System.out.println("CPU Made an Invalid move, retrying...");
  83.                              cpuMove = (int)(Math.random() * 8 + 0);
  84.                            }
  85.                            else
  86.                            {
  87.                              fieldPosition[cpuMove] = " O ";
  88.                              fieldMain[cpuMove] = 0;
  89.                              m.writeField(fieldMain);
  90.                              m.printField(fieldPosition);
  91.                              break;
  92.                            }
  93.                         }while(winLost == 0);
  94.                         checkWin = m.winOrLose(0);
  95.                         if (checkWin == 1)
  96.                         {
  97.                             System.out.println("You lost!");
  98.                             Thread.sleep(1000);
  99.                             System.out.println("Resetting Game!");
  100.                             Thread.sleep(3000);
  101.                             fieldMain = new int[]{-1,-1,-1,-1,-1,-1,-1,-1,-1};
  102.                             fieldPosition = new String[]{" 0 "," 1 "," 2 "," 3 "," 4 "," 5 "," 6 "," 7 "," 8 "};
  103.                         }
  104.                         for (int i = 0; i < 9; i++)
  105.                              if (fieldMain[i] == -1)
  106.                                  movesLeft++;  
  107.                          if (movesLeft == 0)
  108.                             if (checkWin == 1)
  109.                             {  
  110.                             System.out.println("You lost!");
  111.                             Thread.sleep(1000);
  112.                             System.out.println("Resetting Game!");
  113.                             Thread.sleep(3000);
  114.                             fieldMain = new int[]{-1,-1,-1,-1,-1,-1,-1,-1,-1};
  115.                             fieldPosition = new String[]{" 0 "," 1 "," 2 "," 3 "," 4 "," 5 "," 6 "," 7 "," 8 "};
  116.                             }
  117.                             else
  118.                             {
  119.                              System.out.println("You tied!");
  120.                              Thread.sleep(1000);
  121.                              System.out.println("Resetting Game!");
  122.                              Thread.sleep(3000);
  123.                              fieldMain = new int[]{-1,-1,-1,-1,-1,-1,-1,-1,-1};
  124.                              fieldPosition = new String[]{" 0 "," 1 "," 2 "," 3 "," 4 "," 5 "," 6 "," 7 "," 8 "};
  125.                            }
  126.                     }while(winLost == 0);
  127.                     if (winLost == 1)
  128.                     {
  129.                        System.out.println("You win!");
  130.                        Thread.sleep(500);
  131.                        System.out.println("Setting game to complete and generating new number!");
  132.                        game++;
  133.                        gamecompleted[n - 1] = 1;
  134.                        n = (int)(Math.random() * 10 + 1);
  135.                        Thread.sleep(1000);
  136.                        System.out.println("Your number is: " + n);
  137.                     }
  138.                 }
Advertisement
Add Comment
Please, Sign In to add comment