Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- f (gamecompleted[n - 1] == 0)
- {
- int winLost = 0, playerMove, cpuMove, checkWin, movesLeft = 0, winner = -1;
- int[] fieldMain = {-1,-1,-1,-1,-1,-1,-1,-1,-1};
- Randomgames m = new Randomgames(fieldMain);
- String[] fieldPosition = {" 0 "," 1 "," 2 "," 3 "," 4 "," 5 "," 6 "," 7 "," 8 "};
- System.out.println("This game will be tic tac toe!");
- Thread.sleep(1000);
- System.out.println("You will play against the computer.");
- Thread.sleep(1000);
- System.out.println("When prompted enter the number of the box you would like to go, as shown below.");
- System.out.println("-------------");
- System.out.println("| 0 | 1 | 2 |");
- System.out.println("|-----------|");
- System.out.println("| 3 | 4 | 5 |");
- System.out.println("|-----------|");
- System.out.println("| 6 | 7 | 8 |");
- System.out.println("-------------");
- do
- {
- System.out.println("Please make a move");
- playerMove = scan.nextInt();
- do
- {
- if (playerMove > -1)
- if(playerMove < 9)
- if (fieldMain[playerMove] != -1)
- {
- System.out.println("Invalid Move! Try again.");
- playerMove = scan.nextInt();
- }
- else
- {
- fieldPosition[playerMove] = " X ";
- fieldMain[playerMove] = 1;
- m.writeField(fieldMain);
- m.printField(fieldPosition);
- break;
- }
- else
- {
- System.out.println("Invalid Move! Try again.");
- playerMove = scan.nextInt();
- }
- else
- {
- System.out.println("Invalid Move! Try again.");
- playerMove = scan.nextInt();
- }
- }while(winLost == 0);
- checkWin = m.winOrLose(1);
- if (checkWin == 1)
- { winLost = 1;
- break;
- }
- else
- {
- for (int i = 0; i < 9; i++)
- if (fieldMain[i] == -1)
- movesLeft++;
- if (movesLeft == 0)
- if (checkWin == 1)
- { winLost = 1;
- break;
- }
- else
- {
- System.out.println("You tied!");
- Thread.sleep(1000);
- System.out.println("Resetting Game!");
- Thread.sleep(3000);
- fieldMain = new int[]{-1,-1,-1,-1,-1,-1,-1,-1,-1};
- fieldPosition = new String[]{" 0 "," 1 "," 2 "," 3 "," 4 "," 5 "," 6 "," 7 "," 8 "};
- }
- }
- System.out.println("Generating CPU move...");
- Thread.sleep(1000);
- cpuMove = (int)(Math.random() * 8 + 0);
- do{
- if(fieldMain[cpuMove] != -1)
- { System.out.println("CPU Made an Invalid move, retrying...");
- cpuMove = (int)(Math.random() * 8 + 0);
- }
- else
- {
- fieldPosition[cpuMove] = " O ";
- fieldMain[cpuMove] = 0;
- m.writeField(fieldMain);
- m.printField(fieldPosition);
- break;
- }
- }while(winLost == 0);
- checkWin = m.winOrLose(0);
- if (checkWin == 1)
- {
- System.out.println("You lost!");
- Thread.sleep(1000);
- System.out.println("Resetting Game!");
- Thread.sleep(3000);
- fieldMain = new int[]{-1,-1,-1,-1,-1,-1,-1,-1,-1};
- fieldPosition = new String[]{" 0 "," 1 "," 2 "," 3 "," 4 "," 5 "," 6 "," 7 "," 8 "};
- }
- for (int i = 0; i < 9; i++)
- if (fieldMain[i] == -1)
- movesLeft++;
- if (movesLeft == 0)
- if (checkWin == 1)
- {
- System.out.println("You lost!");
- Thread.sleep(1000);
- System.out.println("Resetting Game!");
- Thread.sleep(3000);
- fieldMain = new int[]{-1,-1,-1,-1,-1,-1,-1,-1,-1};
- fieldPosition = new String[]{" 0 "," 1 "," 2 "," 3 "," 4 "," 5 "," 6 "," 7 "," 8 "};
- }
- else
- {
- System.out.println("You tied!");
- Thread.sleep(1000);
- System.out.println("Resetting Game!");
- Thread.sleep(3000);
- fieldMain = new int[]{-1,-1,-1,-1,-1,-1,-1,-1,-1};
- fieldPosition = new String[]{" 0 "," 1 "," 2 "," 3 "," 4 "," 5 "," 6 "," 7 "," 8 "};
- }
- }while(winLost == 0);
- if (winLost == 1)
- {
- System.out.println("You win!");
- Thread.sleep(500);
- System.out.println("Setting game to complete and generating new number!");
- game++;
- gamecompleted[n - 1] = 1;
- n = (int)(Math.random() * 10 + 1);
- Thread.sleep(1000);
- System.out.println("Your number is: " + n);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment