-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConnectFour.java
More file actions
129 lines (120 loc) · 3.71 KB
/
Copy pathConnectFour.java
File metadata and controls
129 lines (120 loc) · 3.71 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
import java.util.*;
public class ConnectFour
{
//I am a fancy new comment for lab11
public static void printBoard(char[][] array)
{
for(int row = 0; row < array.length; row++)
{
System.out.println(String.valueOf(array[row]));
}
}
public static void initializeBoard(char[][] array)
{
for(int row = 0; row < array.length; row++)
{
for(int column = 0; column < array[0].length; column++)
{
array[row][column] = '-';
}
}
}
public static int insertChip(char[][] array, int col, char chipType)
{
int row = array.length - 1;
while(row >= 0)
{
if (array[row][col] == '-')
{
array[row][col] = chipType;
break;
}
row--;
}
return row;
}
public static boolean checkIfWinner(char[][] array, int col, int row, char chipType)
{
int consecutive = 0;
for(int count = 0; count < array[row].length; count++)
{
if(array[row][count] == chipType)
{
consecutive++;
if(consecutive == 4)
{
return true;
}
}
else
{
consecutive = 0;
}
}
for(int count = 0; count < array.length; count++)
{
if(array[count][col] == chipType)
{
consecutive++;
if(consecutive == 4)
{
return true;
}
}
else
{
consecutive = 0;
}
}
return false;
}
public static void main (String [] args)
{
Scanner reader = new Scanner(System.in);
boolean winner = false;
int boardHeight = 0, boardLength = 0;
System.out.print("What would you like the height of the board to be? ");
boardHeight = reader.nextInt();
System.out.print("What would you like the length of the board to be? ");
boardLength = reader.nextInt();
char boardSize[][]= new char[boardHeight][boardLength];
initializeBoard(boardSize);
printBoard(boardSize);
System.out.println("Player 1: x");
System.out.println("Player 2: o");
for(int count = 1; winner == false; count++)
{
if(count % 2 == 1)
{
int columnChoice = 0;
System.out.print("Player 1: Which column would you like to choose? ");
columnChoice = reader.nextInt();
int rowDrop = insertChip(boardSize, columnChoice, 'x');
printBoard(boardSize);
winner = checkIfWinner(boardSize, columnChoice, rowDrop, 'x');
if(winner == true)
{
System.out.println("Player 1 won the game!");
}
}
else if(count % 2 == 0)
{
int columnChoice = 0;
System.out.print("Player 2: Which column would you like to choose? ");
columnChoice = reader.nextInt();
int rowDrop = insertChip(boardSize, columnChoice, 'o');
printBoard(boardSize);
winner = checkIfWinner(boardSize, columnChoice, rowDrop, 'o');
if(winner == true)
{
System.out.println("Player 2 won the game!");
}
}
if(count == boardHeight * boardLength)
{
System.out.println("Draw. Nobody wins.");
break;
}
}
}
}