-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConnectFourI.java
More file actions
158 lines (143 loc) · 3.62 KB
/
ConnectFourI.java
File metadata and controls
158 lines (143 loc) · 3.62 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import java.util.Arrays;
import java.util.Scanner;
import java.util.Stack;
public abstract class ConnectFourI {
protected int BOARD_HEIGHT = 6;
protected int BOARD_WIDTH = 7;
protected Player[][] board = new Player[BOARD_HEIGHT][BOARD_WIDTH];
protected Player x; // The player who moves first
protected Player o; // The player who moves second
protected Player toMove; // The player whose turn it is to move
/**
* @return the board width
*/
public int getWidth() {
return BOARD_WIDTH;
}
/**
* @return the board height
*/
public int getHeight() {
return BOARD_HEIGHT;
}
/**
* @return columns representing legal moves.
*/
public abstract int[] getMoves();
/**
* @return whether or not the game is over
*/
public abstract boolean isGameOver();
/**
* @return a winner, if there is one, otherwise null
*/
public abstract Player getWinner();
/**
* Makes a move for the current player to move.
*
* @param col
* @return true if the move was played, false otherwise
*/
public abstract boolean makeMove(int col);
/**
* Undoes the last move
*/
public abstract void undoMove();
/**
* @param row
* @param col
* @return the player who owns the piece at (row, col)
*/
public abstract Player getSquare(int row, int col);
/**
* Sets the state of the board given a string of (e.g. 'x', 'o', and ' ') in
* row-major order. Treats invalid characters as spaces and ignores excess
* characters.
*
* @param state
* @throws IllegalArgumentException
* if (num(x) - num(o)) not in {0, 1}
*/
public void setState(String state) {
int counter = 0;
int size = BOARD_WIDTH * BOARD_HEIGHT;
this.board = new Player[BOARD_HEIGHT][BOARD_WIDTH];
for (int i = 0; i < size; i++) {
char sq = state.charAt(i);
if (x.getName().equals(sq)) {
counter++;
this.board[i / BOARD_WIDTH][i % BOARD_WIDTH] = x;
} else if (o.getName().equals(sq)) {
counter--;
this.board[i / BOARD_WIDTH][i % BOARD_WIDTH] = o;
}
}
if (counter == 1) {
this.toMove = x;
}
if (counter == 0) {
this.toMove = o;
} else
throw new IllegalArgumentException();
}
/*
* Returns the string representation of the board state in row-major order,
* using 'x', 'o', and ' ' characters. (non-Javadoc)
*
* @see java.lang.Object#toString()
*/
public String toString() {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < BOARD_HEIGHT; i++) {
for (int j = 0; j < BOARD_WIDTH; j++) {
Player sq = this.board[i][j];
if (sq != null) {
sb.append(sq.toString());
} else {
sb.append(' ');
}
}
}
return sb.toString();
}
/**
* Prints the board.
*/
public void printBoard() {
for (int i = 0; i < BOARD_HEIGHT; i++) {
System.out.print('|');
for (int j = 0; j < BOARD_WIDTH; j++) {
Player sq = this.board[i][j];
if (sq != null) {
System.out.print(sq.toString() + "|");
} else {
System.out.print(" |");
}
}
System.out.println();
}
}
/**
* @return the player whose turn it is to move
*/
public abstract Player getToMove();
public static void main(String[] args) {
// just to play around with it...
ConnectFourI cf = new ConnectFour();
EvaluationI ev = new Evaluation(cf.x);
AII ai = new AI(ev);
Scanner scan = new Scanner(System.in);
while (!cf.isGameOver()) {
int move = ai.findMove(cf, 5);
cf.makeMove(move);
cf.printBoard();
System.out.println(Arrays.toString(ev.nInARow(cf, 2)));
if (cf.isGameOver())
break;
System.out.println("AI Move: " + move + " Your move? ");
cf.printBoard();
System.out.println(ev.evaluate(cf, 7));
cf.makeMove(scan.nextInt());
}
}
}