-
Notifications
You must be signed in to change notification settings - Fork 0
/
tictactoe.java
223 lines (201 loc) · 6.03 KB
/
tictactoe.java
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
import java.util.Scanner;
public class tictactoe{
static char [][] board;
public static void main (String [] args){
int dimensions;
board = new char[3][3];
clearScreen();
run();
}
public static boolean insert(int row, int col, char c){
if (row >=1 && row <= board.length){
if (col>=1 && row <=board[0].length){
if (board[row-1][col-1]=='_'){
board[row-1][col-1] = c;
return true;
} else return false;
} else return false;
} else return false;
}
public static boolean delete(int row, int col, char c){
if (row >=1 && row <= board.length){
if (col>=1 && row <=board[0].length){
board[row-1][col-1] = c;
return true;
} else return false;
} else return false;
}
public static char checkWin(){
boolean hasWon = false;
/* horizontal check, future updates will be more dynamically implemented
* for larger board sizes*/
for (int row = 0; row < board.length; row++){
if(board[row][0] == board[row][1] && board[row][1] ==
board[row][2]){
hasWon = true;
return board[row][0];
}
}
/*vertical checks*/
for (int col = 0; col < board.length; col++){
if(board[0][col] == board[1][col] && board[1][col] ==
board[1][col]){
hasWon = true;
return board[0][col];
}
}
/* Top left to bottom right diagonal*/
if (board[0][0] == board[1][1] && board[1][1] == board[2][2]){
hasWon = true;
return board[0][0];
}
/* Top right to bottom left diagonal*/
if (board[1][2] == board[1][1] && board[1][1] == board[2][1]){
hasWon = true;
return board[1][2];
}
return '_';
}
/* some additional functions to spice up the game:
swap two characters (in the event of a full board, players
may swap two characters whether it be their own or opposition*/
public static boolean swap(int[] a, int[] b){
char temp = board[a[0]][a[1]];
board[a[0]][a[1]] = board[b[0]][b[1]];
board[b[0]][b[1]] = temp;
return true;
/*TODO index validation (ensure index is in bounds)*/
}
/*===============================================================
visualisation methods
===============================================================*/
public static void printBoard(){
System.out.print(" ");
for (int col = 0; col < board[0].length; col ++){
System.out.print(col+1+" ");
}
System.out.println();
for (int row = 0; row < board.length; row++){
System.out.print(row+1+" ");
for (int col =0; col < board[0].length; col++){
System.out.print(board[row][col] + "|");
}
System.out.println();
}
}
private static void clearScreen() {
System.out.print("\033[H\033[2J");
System.out.flush();
}
public static void init() {
for (int row = 0; row < board.length; row++){
for (int col =0; col < board[0].length; col++){
board[row][col]='_';
}
}
}
/*==============================================================
main game loop funtion run()
==============================================================*/
public static void run(){
Scanner In = new Scanner(System.in);
boolean running = true;
boolean playerX = true; /* if false then playerO */
int xCredits = 0, oCredits = 0;
char move = '/';
init();
/* Key
/ none
s swap (cost 3 credits)
i insert (0 Credit)
d delete and replace (allows users to remove another players move and
place their own (costs 4 credits)
credits increase by 1 with each turn
*/
while(running) {
System.out.println("================================================");
System.out.println("X Credits: "+xCredits+"|| O Credits: "+oCredits);
System.out.print("Current Player: ");
if (playerX) System.out.println("X");
else System.out.println("O");
System.out.println("================================================\n");
printBoard();
System.out.print("Move (s,i,d,p): ");
move = In.next().charAt(0);
if (move == 'g'){
oCredits += 1000;
continue;
}
if (move == 's') {
int []a = new int[2];
int []b = new int[2];
System.out.print("Select Source Row: ");
a[0] = In.nextInt()-1;
System.out.print("Select Source Col: ");
a[1] = In.nextInt()-1;
System.out.print("Select Destination Row: ");
b[0] = In.nextInt()-1;
System.out.print("Select Destination Col: ");
b[1] = In.nextInt()-1;
if (playerX){
if (xCredits >= 2) {
/* execute the swap */
swap(a,b);
/*clear previous line and queue next prompt (first
* coordinates, clear and queue second prompt (next
* coordinates then finally clear and indicate status*/
}
else {
/*invalid swap forfeit turn. clear previous line and
* show that the move was invalid*/
System.out.println("not enough credits for x to swap");
//playerX = !playerX;
}
} else if (oCredits >= 2) {
/*execute swap*/
swap(a,b);
} else {
/*invalid swap forfeit turn*/
//playerX = !playerX;
System.out.println("not enough credits for o to swap");
}
} else if (move == 'i' || move == 'd') {
/*prompts to get coordinates*/
System.out.print("Select Row: ");
int r = In.nextInt();
System.out.print("Select Col: ");
int c = In.nextInt();
if (move == 'i'){
if (playerX) insert(r,c, 'X');
else insert(r,c,'O');
} else {
if (playerX && xCredits >= 4){
delete(r,c, 'X');
xCredits -= 4;
} else if (playerX) System.out.println("Not enough credits for X");
if (!playerX && oCredits >= 4){
delete(r,c, 'O');
oCredits -= 4;
} else if (!playerX) System.out.println("Not enough credits for O");
/*TODO remember to do credit reduction*/
/*where character is the
character to replace deleted char with*/
//else if (xCredits <3 ) /*not enough credits*/
//else if (!playerX && yCredits>=3) delete(r,c,'O');
//else /*not enough credits*/
}
}
if (playerX) xCredits++;
else oCredits++;
playerX = !playerX;
char winner = checkWin();
if (winner != '_'){
clearScreen();
printBoard();
System.out.println(winner+ " wins the game!");
running = false;
break;
} else clearScreen();
}
}
}