-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConnectFourRefresher.java
More file actions
154 lines (135 loc) · 4.83 KB
/
ConnectFourRefresher.java
File metadata and controls
154 lines (135 loc) · 4.83 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
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Main.java to edit this template
*/
package connectfourrefresher;
import java.util.Scanner;
/**
*
* @author Owner
*/
public class ConnectFourRefresher {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
String[][] board = new String[8][8]; //board[x][y]
String currentPlayer = "O";
Scanner scanner = new Scanner(System.in);
System.out.println("\nCurrent board:");
printBoard(board);
System.out.println();
while (true) {
if (currentPlayer.equals("O")) {
while (true) {
System.out.println("O to play. Choose a column 0-7");
int column = scanner.nextInt();
if (column < 7 && column >= 0) {
if (board[column][0] == null) {
board = addO(board, column);
currentPlayer = "X";
break;
}
}
else
{
System.out.println("Please try again");
}
}
}
else if (currentPlayer.equals("X")) {
while (true) {
System.out.println("X to play. Choose a column 0-7");
int column = scanner.nextInt();
if (column < 7 && column >= 0) {
if (board[column][0] == null) {
board = addX(board, column);
currentPlayer = "O";
break;
}
}
else
{
System.out.println("Please try again");
}
}
}
System.out.println("\nCurrent board:");
printBoard(board);
System.out.println();
if (checkWin(board, "O")){
System.out.println("O wins!");
break;
}
if (checkWin(board, "X")){
System.out.println("X wins!");
break;
}
}
}
public static boolean checkWin(String[][] myBoard, String player) {
// check for horizontal wins
for (int i = 0; i < myBoard.length; i++) {
for (int j = 0; j < myBoard[0].length - 3; j++) {
if (myBoard[i][j] == player && myBoard[i][j+1] == player
&& myBoard[i][j+2] == player && myBoard[i][j+3] == player) {
return true;
}
}
}
// check for vertical wins
for (int i = 0; i < myBoard.length - 3; i++) {
for (int j = 0; j < myBoard[0].length; j++) {
if (myBoard[i][j] == player && myBoard[i+1][j] == player
&& myBoard[i+2][j] == player && myBoard[i+3][j] == player) {
return true;
}
}
}
// check for diagonal wins (top-left to bottom-right)
for (int i = 0; i < myBoard.length - 3; i++) {
for (int j = 0; j < myBoard[0].length - 3; j++) {
if (myBoard[i][j] == player && myBoard[i+1][j+1] == player
&& myBoard[i+2][j+2] == player && myBoard[i+3][j+3] == player) {
return true;
}
}
}
// check for diagonal wins (top-right to bottom-left)
for (int i = 0; i < myBoard.length - 3; i++) {
for (int j = 3; j < myBoard[0].length; j++) {
if (myBoard[i][j] == player && myBoard[i+1][j-1] == player
&& myBoard[i+2][j-2] == player && myBoard[i+3][j-3] == player) {
return true;
}
}
}
return false;
}
public static void printBoard(String[][] myBoard) {
for (int i=0; i<myBoard.length; i++) {
for (int j=0; j<myBoard[i].length; j++){
System.out.print(myBoard[j][i]+" ");
}
System.out.println();
}
}
public static String[][] addO(String[][] myBoard, int column) {
for (int i=myBoard.length-1; i>0; i--) {
if (myBoard[column][i] == null) {
myBoard[column][i] = "O";
break;
}
}
return myBoard;
}
public static String[][] addX(String[][] myBoard, int column) {
for (int i=myBoard.length-1; i>0; i--) {
if (myBoard[column][i] == null) {
myBoard[column][i] = "X";
break;
}
}
return myBoard;
}
}