-
Notifications
You must be signed in to change notification settings - Fork 0
/
TicTacToe.java
99 lines (70 loc) · 2.23 KB
/
TicTacToe.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/**
* Edward Skrod
* https://github.com/ejs09f/ticTacToeJava
* February 14, 2014
* Tic-Tac-Toe
*
*
* Tic-Tac-Toe game, written in Java, which will be played and
* drawn with text characters on the console.
* See README.md for usage information
*
* @version 1.0, February 2014
* @author Edward Skrod [email protected]
*
*
*/
public class TicTacToe {
public static void main(String[] args) {
boolean commandLineError = false;
// Create a new board
TicTacToeBoard theBoard = new TicTacToeBoard();
// Create an array of two players
Player [] players = new Player [2];
//For Testing
//players[0] = new PlayerHuman(1);
//players[0] = new PlayerComputer(1);
//players[1] = new PlayerComputer(2);
// COMMAND LINE ARGUMENTS
if (args.length == 0) {
players[0] = new PlayerHuman(1);
players[1] = new PlayerHuman(2);
}
else if ((args.length == 1) && (args[0].equals("-c"))) {
players[0] = new PlayerComputer(1);
players[1] = new PlayerComputer(2);
}
else if ((args.length == 2) && (args[0].equals("-c"))) {
if (args[1].equals("1")) {
players[0] = new PlayerComputer(1);
players[1] = new PlayerHuman(2);
}
else if (args[1].equals("2")) {
players[0] = new PlayerHuman(1);
players[1] = new PlayerComputer(2);
}
}
else {
System.err.println("Usage: java TicTacToe [-c [1|2]]");
commandLineError = true;
}
// Run the program if there were no command line errors
if (commandLineError == false) {
int currentPlayer = 1;
// Print the initial board
theBoard.printBoard();
while (theBoard.isGameOver() != true) {
//System.out.println("Move number: " + theBoard.getTurns()); // For Testing
currentPlayer = (currentPlayer + 1) % players.length;
players[currentPlayer].takeTurn(theBoard);
theBoard.printBoard();
}
if (theBoard.isGameWon() == true) {
System.out.printf("%s %d has won!\n\n", "Game Over! Player", (currentPlayer + 1 ));
} else {
System.out.println("The game resulted in a tie!\n\n");
}
} // end if(commandLineError == false)
System.out.println("Exiting TicTacToe.\n");
} // end public static void main(String[] args)
} // end TicTacToe.java