-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTicTacToe2.java
More file actions
121 lines (89 loc) · 2.87 KB
/
TicTacToe2.java
File metadata and controls
121 lines (89 loc) · 2.87 KB
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import java.util.*;
class Player {
//'x' or 'o'
char name;
public int[] getInput() {
//1. Handle getting input and turning input into an array
//2. (do later) Handle invalid input
}
}
class Board {
private int[][] representation = new int[3][3];
public boolean isSpaceFree(int[] space) {
//(do later) Check if space is taken in representation
}
public void takeSpace(int[] space, Player player) {
//Mark space in representation as taken for player
}
public String toString() {
//Returns the String that is printed when someone System.out.println(board)
}
}
class TicTacToeGame {
Player player1;
Player player2;
Board board;
TicTacToeGame(Player player1, Player player2) {
//Set up a new game
this.player1 = player1;
this.player2 = player2;
this.board = new Board();
}
public Player getNextPlayer(int turn) {
//If turn is even player1 else player 2
}
public boolean isOver() {
//(do later) Check for win
}
public String getWinner() {
//(do later)Get the winner, duh
}
}
public class TicTacToe {
public static void main(String[] args) {
//Test your method implementations here
//Example test
Player p1 = new Player();
Player p2 = new Player();
Board board = new Board();
TicTacToeGame game = new TicTacToeGame(p1, p2, board);
for (int i = 0; i < 10; i++) {
System.out.println(game.getNextPlayer(i));
}
//End of testing section
//Remove System.exit() to allow the actual game to attempt to run.
System.exit();
//Make players
//Make players
Player player1 = new Player();
Player player2 = new Player();
//Make a board
Board board = new Board();
//Make a game with 2 players on a board
TicTacToeGame game = new TicTacToeGame(player1, player2, board);
//Can play up to 10 turns
for (int turn = 0; turn < 10; turn++) {
//Show the board
System.out.println(game.board);
if (game.isOver()) {
System.out.println("Game over!");
System.out.println("Winner is:" + game.getWinner());
//Exits the for loop prematurely
break;
}
//Returns the player whose turn it is
Player currentPlayer = game.getNextPlayer(turn);
int[] input = currentPlayer.getInput();
if (!game.board.isSpaceFree(input)) {
System.out.println("Space is not free!");
//Redo the turn
turn--;
continue;
} else {
game.board.takeSpace(input, currentPlayer);
}
//End of turn
}
// Game over!
}
}