-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathRunGame.java
84 lines (80 loc) · 2.54 KB
/
RunGame.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/**
* This class prepares and executes the game itself.
*
* @author Ping-Chun Chung and Otakar Andrysek
* @version v1.0.1
*/
public class RunGame
{
// Instance variables
Game game;
GUI gui;
Ai ai;
boolean gameOn = true;
public RunGame()
{
game = new Game();
gui = new GUI(game);
ai = new Ai();
playGame();
}
public void playGame()
{
while(gameOn)
{
// Print the board
if (gui.playGame){
if (game.turn == 0)
{
// Player "X" will go
gui.setGameBackground("X");
}
else if (game.turn == 1)
{
// Player "O" will go
gui.setGameBackground("O");
if (gui.vsCom)
{
int[] aiMove = ai.getMove(game.board,game.whoPlayFirst);
game.playerPlay(aiMove[0],aiMove[1],"O");
game.turn--;
}
}
else if (game.turn == 2)
{
// Player "X" wins
gui.xScore++;
gui.xScoreLabel.setText(Integer.toString(gui.xScore));
gui.winningLine(game.getWinningLine());
try{Thread.sleep(1200);}catch(InterruptedException e){System.out.println(e);}
gui.displayWinScreen("X");
gui.playGame = false;
}
else if (game.turn == 3)
{
// Player "O" wins
gui.oScore++;
gui.oScoreLabel.setText(Integer.toString(gui.oScore));
gui.winningLine(game.getWinningLine());
try{Thread.sleep(1200);}catch(InterruptedException e){System.out.println(e);}
gui.displayWinScreen("O");
gui.playGame = false;
}
else if (game.turn == 4)
{
// There is a tie
try{Thread.sleep(1200);}catch(InterruptedException e){System.out.println(e);}
gui.displayWinScreen("tie");
gui.playGame = false;
}
game.turn = game.checkBoard(game.board, game.turn);
gui.updataButtonIma();
}
}
}
// Main function
public static void main(String[] args)
{
new RunGame();
}
}