-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTicTacToe.java
More file actions
191 lines (172 loc) · 5.84 KB
/
TicTacToe.java
File metadata and controls
191 lines (172 loc) · 5.84 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
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
/**
* Created by Sutriptim Nath on 12th; August; 2020
*/
import java.util.Scanner;
public class TicTacToe {
private char board[][];
private char playerMark;
private String playerName;
private boolean markerIsPlaced;
Scanner sc = new Scanner(System.in);
public TicTacToe() {
board = new char[3][3];
}
public void newGame(){
int c = '1';
for(int row = 0; row <= 2; row++) {
for(int col = 0; col <= 2; col++) {
board[row][col] = (char)c;
c++;
}
}
}
public void display() {
System.out.println("-------------------");
for(int row = 0; row <= 2; row++) {
System.out.print("| ");
for(int col = 0; col <= 2; col++) {
System.out.print(Character.toUpperCase(board[row][col]) + " | ");
}
System.out.println();
}
System.out.println("-------------------");
}
private boolean isFull() {
int c ='1';
for(int row = 0; row <= 2; row++) {
for(int col = 0; col <= 2; col++) {
if(board[row][col] == ((char)c))
return false;
c++;
}
}
return true;
}
private boolean wins() {
if(checkColWins() || checkRowWins() || checkDiagWins()) {
return true;
}
else return false;
}
private boolean checkColWins() {
for(int col = 0; col < 3; col++) {
if (checkRowColDiag(board[0][col], board[1][col], board[2][col]) == true) {
return true;
}
}
return false;
}
private boolean drawMatch() {
return (isFull() && !wins());
}
private boolean checkRowWins() {
for(int row = 0; row < 3; row++) {
if (checkRowColDiag(board[row][0], board[row][1], board[row][2]) == true)
return true;
}
return false;
}
private boolean checkDiagWins() {
if(checkRowColDiag(board[0][0], board[1][1],board[2][2]) == true)
return true;
else if(checkRowColDiag(board[0][2], board[1][1],board[2][0]) == true)
return true;
else return false;
}
private boolean checkRowColDiag(char mark1, char mark2, char mark3) {
return ((mark1 == mark2) && (mark2 == mark3) && (mark1 == mark3));
}
private void setMark(char search) {
markerIsPlaced = false;
for(int i = 0; i <= 2; i++) {
for(int j = 0; j <= 2; j++) {
if((char)board[i][j] == search) {
board[i][j] = playerMark;
markerIsPlaced = true;
return;
}
}
}
System.out.println();
System.out.println("Marker is already used here");
System.out.println("Choose an Empty Space");
display();
}
private void changeMark() {
if(playerMark == 'x') {
playerMark = 'o';
}
else
playerMark = 'x';
}
private void changeName(String player1, String player2) {
if(playerName == player1)
playerName = player2;
else
playerName = player1;
}
public void playGame() {
newGame();
System.out.print("Enter Name (Player 1) -> ");
String player1 = sc.nextLine();
System.out.print("Enter Name (Player 2) -> ");
String player2 = sc.nextLine();
System.out.println(player1 + " Choose your marker ' x ' or ' o ' ");
playerMark = sc.next().charAt(0);
playerName = player1;
char c;
while(!wins() && !isFull()) {
display();
do {
System.out.println("\"" +Character.toUpperCase(playerMark) + "\" " + playerName + "'s Turn ");
System.out.print("Place your mark (1-9) -> " );
c = sc.next().charAt(0);
setMark(c);
} while(!markerIsPlaced);
changeMark();
changeName(player1,player2);
}
if(drawMatch()) {
display();
sc.nextLine();
System.out.println("Draw Match");
}
else {
display();
changeMark();
changeName(player1,player2);
sc.nextLine();
System.out.println("\"" + Character.toUpperCase(playerMark) + "\" " + playerName + " Wins the Game ");
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
TicTacToe ttt = new TicTacToe();
System.out.println();
System.out.println();
System.out.println("----------------- TicTacToe -----------------");
System.out.println("Main Menu : - ");
System.out.println();
System.out.println("1 . Play Game");
System.out.println("2 . Exit");
System.out.print("-> ");
byte choice1 = sc.nextByte();
switch(choice1) {
case 1 :
ttt.playGame();
System.out.println("-------------- Game Over --------------");
System.out.println("Main Menu : - ");
System.out.println("1 . Restart Game");
System.out.println("2 . Exit");
System.out.print("-> ");
byte choice2 = sc.nextByte();
switch(choice2) {
case 1 : ttt.playGame();
case 2 : System.exit(0);
default : System.out.println("Invalid Input");
}
case 2 : System.exit(0);
default : System.out.println("Invalid Input");
}
}
}