Skip to content

Commit 867ef47

Browse files
committed
Uploaded connect4 source
1 parent c3b27d4 commit 867ef47

File tree

11 files changed

+902
-0
lines changed

11 files changed

+902
-0
lines changed

src/connect4/connectFour/Board.java

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
package connect4.connectFour;
2+
3+
import java.util.ArrayList;
4+
5+
public class Board implements Grid
6+
{
7+
private int rows;
8+
private int cols;
9+
private int nextPlayer;
10+
private int[][] playerAtPosition;
11+
private ArrayList<Integer> undoList;
12+
13+
public Board(int rowsP, int colsP)
14+
{
15+
undoList = new ArrayList<Integer>();
16+
rows = rowsP;
17+
cols = colsP;
18+
nextPlayer = PLAYER1;
19+
playerAtPosition = new int[getRows()][getCols()];
20+
}
21+
22+
public int getRows()
23+
{
24+
return rows;
25+
}
26+
27+
public int getCols()
28+
{
29+
return cols;
30+
}
31+
32+
33+
@Override
34+
public int getPlayerAt(int row, int col)
35+
{
36+
return playerAtPosition[row][col];
37+
}
38+
39+
@Override
40+
public boolean isColumnFull(int col)
41+
{
42+
return (playerAtPosition[playerAtPosition.length - 1][col] != PLAYEREMPTY);
43+
}
44+
45+
public boolean makeMove(int col)
46+
{
47+
if (col < 0 || col >= getCols() || isColumnFull(col))
48+
{
49+
// Illegal column!
50+
return false;
51+
}
52+
53+
for (int iRow = 0; iRow < playerAtPosition.length; iRow++)
54+
{
55+
if (playerAtPosition[iRow][col] == 0)
56+
{
57+
undoList.add(col);
58+
playerAtPosition[iRow][col] = nextPlayer;
59+
nextPlayer = 3 - nextPlayer;
60+
return true;
61+
}
62+
}
63+
64+
// Column is full
65+
return false;
66+
}
67+
68+
public void undo()
69+
{
70+
if (undoList.size() == 0)
71+
return;
72+
73+
// Get most recently-played column
74+
int col = undoList.remove(undoList.size() - 1);
75+
76+
// Get highest row played in that column, so we know which checker to remove
77+
for (int row = getRows() - 1; row >= 0; row--)
78+
{
79+
if (playerAtPosition[row][col] != PLAYEREMPTY)
80+
{
81+
playerAtPosition[row][col] = PLAYEREMPTY;
82+
nextPlayer = 3 - nextPlayer;
83+
return;
84+
}
85+
}
86+
}
87+
88+
public int getNextPlayer()
89+
{
90+
return nextPlayer;
91+
}
92+
93+
public int getWinningPlayer()
94+
{
95+
GridUtilities utilities = new GridUtilities(this);
96+
97+
for (int iRow = 0; iRow < getRows(); iRow++)
98+
{
99+
for (int iCol = 0; iCol < getCols(); iCol++)
100+
{
101+
int player = playerAtPosition[iRow][iCol];
102+
if (player != 0)
103+
{
104+
if (utilities.doesHorizontal4StartHere(iRow, iCol) ||
105+
utilities.doesVertical4StartHere(iRow, iCol) ||
106+
utilities.doesDiagonalLeft4StartHere(iRow, iCol) ||
107+
utilities.doesDiagonalRight4StartHere(iRow, iCol))
108+
{
109+
return player;
110+
}
111+
}
112+
}
113+
}
114+
115+
boolean allColumnsFull = true;
116+
for (int iCol = 0; iCol < getCols(); iCol++)
117+
{
118+
if (!isColumnFull(iCol))
119+
{
120+
allColumnsFull = false;
121+
}
122+
}
123+
if (allColumnsFull)
124+
{
125+
return Grid.DRAW;
126+
}
127+
128+
return Grid.PLAYEREMPTY;
129+
}
130+
}

src/connect4/connectFour/Grader.java

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
package connect4.connectFour;
2+
3+
import connect4.contestants.HumanPlayer;
4+
import connect4.contestants.Player;
5+
import connect4.graphics.GamePanel;
6+
import connect4.graphics.HomePanel;
7+
8+
import java.io.PrintStream;
9+
10+
public class Grader {
11+
private static final int ROWS = 6;
12+
private static final int COLS = 7;
13+
14+
private static Board board;
15+
private static GamePanel gamePanel = new GamePanel(ROWS, COLS);
16+
private static HomePanel homePanel = new HomePanel(gamePanel, false);
17+
private static PrintStream debugger = System.out;
18+
19+
static final String NAME1 = "Template";
20+
static final String NAME2 = "Template";
21+
22+
public static void main(String[] args) throws Exception
23+
{
24+
Class<?> bot1 = Class.forName("connect4.contestants." + NAME1);
25+
Class<?> bot2 = Class.forName("connect4.contestants." + NAME2);
26+
27+
Object a = bot1.getConstructor().newInstance();
28+
Object b = bot2.getConstructor().newInstance();
29+
30+
board = new Board(ROWS, COLS);
31+
gamePanel.drawGrid(board);
32+
33+
Thread.sleep(1000);
34+
35+
playGame((Player) a, (Player) b);
36+
}
37+
38+
public static void playGame(Player player1, Player player2) throws InterruptedException
39+
{
40+
String name1 = player1.getPlayerName();
41+
String name2 = player2.getPlayerName();
42+
43+
if(name1.equals(name2)) name2 += "2";
44+
45+
int winningPlayer = 0;
46+
while (winningPlayer == 0)
47+
{
48+
Thread.sleep(500);
49+
int move = getMove(player1);
50+
debugger.println(name1 + " dropped in " + move);
51+
52+
if(!board.makeMove(move)){
53+
System.err.println(name1 + " made an illegal move");
54+
return;
55+
}
56+
gamePanel.drawGrid(board);
57+
winningPlayer = board.getWinningPlayer();
58+
if (winningPlayer == 0) {
59+
Thread.sleep(500);
60+
move = getMove(player2);
61+
if(!board.makeMove(move)){
62+
System.err.println(name2 + " made an illegal move");
63+
return;
64+
}
65+
debugger.println(name2 + " dropped in " + move);
66+
gamePanel.drawGrid(board);
67+
winningPlayer = board.getWinningPlayer();
68+
}
69+
}
70+
71+
if (winningPlayer == -1)
72+
{
73+
System.out.println("Game over. It was a draw.");
74+
debugger.println("Game over. It was a draw.");
75+
return;
76+
}
77+
String winningPlayerName =
78+
(winningPlayer == 1) ? name1: name2;
79+
80+
debugger.println("Game over. Player " + winningPlayer + ": " + winningPlayerName + " won.");
81+
debugger.close();
82+
}
83+
84+
public static int getMove(Player player) throws InterruptedException {
85+
int move;
86+
if(player instanceof HumanPlayer) {
87+
move = player.getMoveColumn(board);
88+
while(move == -1) {
89+
move = player.getMoveColumn(board);
90+
Thread.sleep(20);
91+
}
92+
((HumanPlayer) player).setCol(-1);
93+
} else {
94+
move = player.getMoveColumn(board);
95+
}
96+
return move;
97+
}
98+
99+
public static GamePanel getGamePanel()
100+
{
101+
return gamePanel;
102+
}
103+
}

src/connect4/connectFour/Grid.java

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package connect4.connectFour;
2+
3+
/**
4+
* Represents the grid of a Connect Four game.
5+
* Note: The first position in a column has row index 0.
6+
* For example, consider a game that starts with these four moves:
7+
* Player 1 moves in column 2, which drops to row 0
8+
* Player 2 moves in column 3, which drops to row 0
9+
* Player 1 moves in column 3, which drops to row 1
10+
* Player 2 moves in column 3, which drops to row 2
11+
* The grid would look like this:
12+
* | | | | | | | |
13+
* | | | | | | | |
14+
* | | | | | | | |
15+
* | | | | 2 | | | |
16+
* | | | | 1 | | | |
17+
* | | | 1 | 2 | | | |
18+
* --- --- --- --- --- --- ---
19+
* 0 1 2 3 4 5 6
20+
*
21+
*/
22+
public interface Grid
23+
{
24+
public static final int PLAYEREMPTY = 0; // player number for empty cell
25+
public static final int PLAYER1 = 1; // player number for first player
26+
public static final int PLAYER2 = 2; // player number for second player
27+
public static final int DRAW = -1; // returned by getWinningPlayer() if the
28+
// game ended in a draw
29+
30+
// Directions, used in getLengthAndSpaces
31+
public static final int RIGHT = 0;
32+
public static final int UP = 1;
33+
public static final int UPLEFT = 2;
34+
public static final int UPRIGHT = 3;
35+
36+
/**
37+
* Gets the player number (1 or 2) at the given row and column, or 0 if that
38+
* cell is empty.
39+
*
40+
* @param row
41+
* the row index (0 is bottom)
42+
* @param col
43+
* the column index (0 is left)
44+
* @return the player number (1 or 2) at the given row and column, or 0 if
45+
* empty
46+
*/
47+
public int getPlayerAt(int row, int col);
48+
49+
/**
50+
* Checks whether the given column is full.
51+
*
52+
* @param col
53+
* the column index
54+
* @return true if the column is full, otherwise false
55+
*/
56+
public boolean isColumnFull(int col);
57+
58+
/**
59+
* These return the number of rows & columns on the board
60+
*/
61+
public int getRows();
62+
public int getCols();
63+
64+
/**
65+
* Returns the player number whose move is next
66+
*/
67+
public int getNextPlayer();
68+
69+
/**
70+
* If this Grid is a finished game, this returns the winner (PLAYER1,
71+
* PLAYER2, or DRAW)
72+
*
73+
* If this Grid is not yet a finished game, this returns PLAYEREMPTY
74+
*/
75+
public int getWinningPlayer();
76+
77+
public boolean makeMove(int col);
78+
79+
public void undo();
80+
}

0 commit comments

Comments
 (0)