-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGame.java
More file actions
89 lines (79 loc) · 2.75 KB
/
Copy pathGame.java
File metadata and controls
89 lines (79 loc) · 2.75 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
import java.util.Scanner;
import java.util.HashSet;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Arrays;
public class Game implements Cloneable {
public ArrayList<Integer> playerX = new ArrayList<>();
public ArrayList<Integer> playerO = new ArrayList<>();
private boolean isOn;
private String currentPlayer;
private Board board;
public String getCurrentPlayer() {
return currentPlayer;
}
public boolean getIsOn(){
return isOn;
}
public Board getBoard() {
return board;
}
public Game(String p) {
board = new Board();
currentPlayer = p.toUpperCase();
isOn = true;
}
//cloning
@Override
protected Object clone() throws CloneNotSupportedException {
Game copy = (Game)super.clone();
copy.board = (Board)copy.board.clone();
ArrayList<Integer> xCopy = new ArrayList<>(playerX.size());
ArrayList<Integer> oCopy = new ArrayList<>(playerO.size());
for (Integer i : playerX) {
xCopy.add(Integer.valueOf(i));
}
for (Integer i : playerO) {
oCopy.add(Integer.valueOf(i));
}
copy.playerX = xCopy;
copy.playerO = oCopy;
//copy.currentPlayer = currentPlayer;
return copy;
}
public void makeMove(int pos) {
//might throw CellAlreadyTakenException
board.getCell(pos).setPlayer(currentPlayer);
if (currentPlayer.equals("X")) {
playerX.add(pos);
} else {
playerO.add(pos);
}
board.takeCell();
}
public void checkForWinner() {
if (board.isFull()) {
isOn = false;
TicTacToe.showText("Draw");
}
HashSet<ArrayList<Integer>> winCombs = new HashSet<>(Arrays
.asList(new ArrayList<Integer>(Arrays.asList(0, 1, 2)),
new ArrayList<Integer>(Arrays.asList(3, 4, 5)),
new ArrayList<Integer>(Arrays.asList(6, 7, 8)),
new ArrayList<Integer>(Arrays.asList(0, 3, 6)),
new ArrayList<Integer>(Arrays.asList(1, 4, 7)),
new ArrayList<Integer>(Arrays.asList(2, 5, 8)),
new ArrayList<Integer>(Arrays.asList(0, 4, 8)),
new ArrayList<Integer>(Arrays.asList(2, 4, 6))));
for (ArrayList<Integer> comb : winCombs) {
if (playerO.containsAll(comb) || playerX.containsAll(comb)) {
isOn = false;
TicTacToe.showText("Player " + currentPlayer + " won");
}
}
}
public void switchPlayer() {
currentPlayer = currentPlayer.equals("X") ? "O" : "X";
TicTacToe.showText("Player " + currentPlayer + "'s turn");
}
}