-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTicTac.java
164 lines (134 loc) · 4.32 KB
/
TicTac.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
import java.util.*;
import java.util.Scanner;
class TicTac{
/*Steps:
*/
//global variables
boolean filled = false;
int turn = 1;
int[][] board = new int[3][3];
public void setup(){
int won = checkWinner(board);
if(won == 1 && turn == 1){
System.out.println("Winner: Player Number 1!");
}
if(won == 1 && turn == -1){
System.out.println("Winner: Player Number 2!");
}
else if(won == 2){
System.out.println("Tie! Good game to both players!");
}
}
int checkWinner(int[][] board){
boolean full = fullBoard(board);
boolean threes = threeRow(board);
if(threes == true){
createLastBoard(board);
if((turn*-1) == 1) System.out.println("Winner: Player Number 1!"); //find out who the last player was
else System.out.println("Winner: Player Number 2!");
return 1; //three in a row w/o running out of moves
}
else if(full == true){ //no more moves can be made, but no one won
System.out.println("Tie! Good game to both players!");
return 2;
}
createBoard(board); //continue the game
return 0;
}
boolean fullBoard(int[][] board){
for(int[] b : board){
for(int a : b){
if(a == 0){
return false;
}
}
}
return true;
}
boolean threeRow(int[][] board){
//verticals:
//vertical in the 0th col:
if(board[0][0] == board[1][0] && board[1][0] == board[2][0] && board[0][0] != 0) return true;
//vertical in the 1st col:
if(board[0][1] == board[1][1] && board[1][1] == board[2][1] && board[0][1] != 0) return true;
//vertical in the 2nd col:
if(board[0][2] == board[1][2] && board[1][2] == board[2][2] && board[0][2] != 0) return true;
//horizontals:
//horizontal in the 0th row:
if(board[0][0] == board[0][1] && board[0][1] == board[0][2] && board[0][0] != 0) return true;
//horizontal in the 1st row:
if(board[1][0] == board[1][1] && board[1][1] == board[1][2] && board[1][0] != 0) return true;
//horizontal in the 2nd row:
if(board[2][0] == board[2][1] && board[2][1] == board[2][2] && board[2][0] != 0) return true;
//diagonals:
//diagonal sloping down from left to right:
if(board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[0][0] != 0) return true;
//diagonal sloping up from left to right:
if(board[2][0] == board[1][1] && board[1][1] == board[0][2] && board[2][0] != 0) return true;
return false;
}
void createBoard(int[][] board){
System.out.println("--------");
for(int i = 0; i < board.length; i++){
System.out.print("|");
for(int b = 0; b < board.length; b++){
System.out.print(board[i][b]);
System.out.print("|");
}
System.out.println("");
System.out.println("--------");
}
if(turn == 1) System.out.println("Player 1's turn");
else System.out.println("Player 2's turn");
Scanner reader = new Scanner(System.in);
String moveNum = reader.nextLine();
move(moveNum, board);
}
void createLastBoard(int[][] board){
System.out.println("--------");
for(int i = 0; i < board.length; i++){
System.out.print("|");
for(int b = 0; b < board.length; b++){
System.out.print(board[i][b]);
System.out.print("|");
}
System.out.println("");
System.out.println("--------");
}
}
void move(String moveNum, int[][] board){
int space = 0;
try{
space = Integer.parseInt(moveNum);
}
catch(Exception nfe){
System.out.println("");
System.out.println("Oops! That is an invalid input! Please go again and type a number 1-9.");
checkWinner(board);
}
if(space < 1 || space > 9){
System.out.println("");
System.out.println("Oops! That is an invalid input! Please go again and type a number 1-9.");
checkWinner(board);
}
System.out.println("making move..." + space);
int moveS = space-1; //moveS will convert space to be 0-8 so that board indexes are easier to calculate
int r = moveS/3;
int c = moveS%3;
if(board[r][c] == 0){
if(turn == 1) board[r][c] = 1;
else board[r][c] = 2;
turn *= -1;
checkWinner(board);
}
else{
System.out.println("");
System.out.println("Oops! That spot is already taken! Please go again.");
checkWinner(board);
}
}
public static void main(String[] args){
TicTac toe = new TicTac();
toe.setup();
}
}