summaryrefslogtreecommitdiff
path: root/c1/ConnectFour.java
blob: ab3e33bd76d27e18f395a35aa07a339c1f7a658d (plain)
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
// 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;
		}
	}

}