summaryrefslogtreecommitdiff
path: root/c1/ConnectFour.java
diff options
context:
space:
mode:
authorpants <niklasjo2004@gmail.com>2026-09-21 14:16:34 -0700
committerpants <niklasjo2004@gmail.com>2026-09-21 14:16:34 -0700
commit81ba9eb66f8284d8760012973f84cdbe617ceb94 (patch)
tree466432644e9e3fd66ee183ec53a1f10abb1b7faa /c1/ConnectFour.java
Diffstat (limited to 'c1/ConnectFour.java')
-rw-r--r--c1/ConnectFour.java117
1 files changed, 117 insertions, 0 deletions
diff --git a/c1/ConnectFour.java b/c1/ConnectFour.java
new file mode 100644
index 0000000..ab3e33b
--- /dev/null
+++ b/c1/ConnectFour.java
@@ -0,0 +1,117 @@
+// Nik Johnson
+// 4-15-2024
+// TA: Zachary Bi
+
+import java.util.*;
+
+public class ConnectFour extends AbstractStrategyGame {
+
+ private char[][] board;
+ private int currentPlayer;
+ private String[] players;
+ private List<Character> playerSymbols;
+
+ public ConnectFour(String player1, String player2) {
+ this.board = new char[6][7];
+ this.players = new String[2];
+ this.playerSymbols = new ArrayList<>();
+ this.currentPlayer = 0;
+ players[0] = player1;
+ players[1] = player2;
+ playerSymbols.add('X');
+ playerSymbols.add('O');
+
+ // initialize empty board
+ for (int i = 0; i < board.length; i++) {
+ for (int n = 0; n < board[i].length; n++) {
+ board[i][n] = ' ';
+ }
+ }
+ }
+
+ public String instructions() {
+ String output = "";
+
+ output += "Welcome to Connect Four! This game consists of a 7x6 grid, each column of";
+ output += " which you may choose to 'drop' a disc down by inputting numbers 1-7.\n";
+ output += "Player 1's symbol is 'X', Player 2's is 'O'\n";
+ output += "To win, you must place 4 of your discs in a row, vertically or horizontally.\n";
+ output += "Have fun!";
+
+ return output;
+ }
+
+ // constructs game board in readable format
+ // no parameters
+ // returns formatted representation of game board
+ public String toString() {
+ String output = "";
+ for (char[] row : board) {
+ for (char loc : row) {
+ output += "[" + loc + "]";
+ }
+ output += "\n";
+ }
+ return output;
+ }
+
+
+ public int getWinner() {
+ // rows
+ for (int i = 0; i < board.length; i++) {
+ for (int j = 0; j <= board[i].length - 4; j++) {
+ char symbol = board[i][j];
+ if (symbol != ' ' && board[i][j + 1] == symbol && board[i][j + 2] == symbol && board[i][j + 3] == symbol) {
+ return playerSymbols.indexOf(symbol) + 1;
+ }
+ }
+ }
+
+ // columns
+ for (int j = 0; j < board[0].length; j++) {
+ for (int i = 0; i <= board.length - 4; i++) {
+ char symbol = board[i][j];
+ if (symbol != ' ' && board[i + 1][j] == symbol && board[i + 2][j] == symbol && board[i + 3][j] == symbol) {
+ return playerSymbols.indexOf(symbol) + 1;
+ }
+ }
+ }
+
+ return -1;
+}
+
+
+
+
+ public int getNextPlayer() {
+ return this.currentPlayer + 1;
+ }
+
+ public void makeMove(Scanner input) {
+ System.out.println(players[getNextPlayer() - 1] + ": Choose a column (1-7)");
+ int col = Integer.parseInt(input.next()) - 1;
+
+ if (col < 0 || col > 6) {
+ throw new IllegalArgumentException("selected column out of bounds");
+ }
+
+ boolean notPlaced = true;
+ for (int n = board.length - 1; n >= 0 && notPlaced; n--) {
+ if (board[n][col] == ' ') {
+ if (currentPlayer == 0) {
+ board[n][col] = 'X';
+ } else if (currentPlayer == 1) {
+ board[n][col] = 'O';
+ }
+ notPlaced = false;
+ }
+ }
+
+ if (currentPlayer == 0) {
+ currentPlayer = 1;
+ } else if (currentPlayer == 1) {
+ currentPlayer = 0;
+ }
+ }
+
+}