-
Notifications
You must be signed in to change notification settings - Fork 0
/
TicTacToe.java
134 lines (132 loc) · 5.15 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
// a multiplayer program to play tictactoe with 2 people
import java.util.Scanner;
public class TicTacToe {
public static void main(String[] args) {
char [][]map = {{'-', '-', '-'}, {'-', '-', '-'}, {'-', '-', '-'}};
Scanner sc = new Scanner(System.in);
int row1 = 2, col1 = 2, row2 = -1, col2 = -1, turns = 0;
boolean player1HasWon = false, player2HasWon = false;
String player1Name, player2Name;
System.out.println("Enter player 1's name:");
player1Name = sc.nextLine();
System.out.println("Enter player 2's name:");
player2Name = sc.nextLine();
while(!player1HasWon && !player2HasWon) {
System.out.println("Enter the position of player 1's 'X':");
String player1 = sc.next();
switch(player1.charAt(0)) {
case 'a':
row1 = 0;
break;
case 'b':
row1 = 1;
break;
case 'c':
row1 = 2;
break;
default:
System.out.println("Invalid input!");
System.exit(0);
} switch(player1.charAt(1)) {
case '1':
col1 = 0;
break;
case '2':
col1 = 1;
break;
case '3':
col1 = 2;
break;
default:
System.out.println("Invalid input!");
System.exit(0);
}
if (map[row1][col1] != '-') {
System.out.println("Value is already taken.");
System.exit(0);
} else {
map[row1][col1] = 'X';
turns++;
}
// printing the current tictactoe map
for(int i = 0; i < 3; i++) {
for(int j = 0; j < 3; j++) {
System.out.print(map[i][j] + " ");
}
System.out.println();
}
// checking if player1 or player2 has won
if((map[0][0] == map[1][0] && map[1][0] == map[2][0] && map[2][0] != '-')
|| (map[0][0] == map[0][1] && map[0][1] == map[0][2] && map[0][2] != '-')
|| (map[0][0] == map[1][1] && map[1][1] == map[2][2] && map[2][2] != '-')) {
player1HasWon = map[0][0] == 'X' ? true : false;
player2HasWon = player1HasWon ? false : true;
break;
} else if((map[0][1] == map[1][1] && map[1][1] == map[2][1] && map[2][1] != '-')
|| (map[1][0] == map[1][1] && map[1][1] == map[1][2] && map[1][2] != '-')
|| (map[2][0] == map[1][1] && map[1][1] == map[0][2] && map[0][2] != '-')) {
player1HasWon = map[1][1] == 'X' ? true : false;
player2HasWon = player1HasWon ? false : true;
break;
} else if((map[0][2] == map[1][2] && map[1][2] == map[2][2] && map[2][2] != '-')
|| (map[2][0] == map[2][1] && map[2][1] == map[2][2] && map[2][2] != '-')) {
player1HasWon = map[2][2] == 'X' ? true : false;
player2HasWon = player1HasWon ? false : true;
break;
}
// if turns = 9, or if it's a draw, then get out of the loop
if (turns == 9)
break;
System.out.println("Enter the position of player 2's 'O'");
String player2 = sc.next();
switch(player2.charAt(0)) {
case 'a':
row2 = 0;
break;
case 'b':
row2 = 1;
break;
case 'c':
row2 = 2;
break;
default:
System.out.println("Invalid input!");
System.exit(0);
} switch(player2.charAt(1)) {
case '1':
col2 = 0;
break;
case '2':
col2 = 1;
break;
case '3':
col2 = 2;
break;
default:
System.out.println("Invalid input!");
System.exit(0);
}
if (map[row2][col2] != '-') {
System.out.println("Value is already taken.");
System.exit(0);
} else {
map[row2][col2] = 'O';
turns++;
}
// printing the current tictactoe map
for(int i = 0; i < 3; i++) {
for(int j = 0; j < 3; j++) {
System.out.print(map[i][j] + " ");
}
System.out.println();
}
}
if (player1HasWon)
System.out.println("" + player1Name.toUpperCase() + " won the game! Well played!");
else if (player2HasWon)
System.out.println("" + player2Name.toUpperCase() + " won the game! Well played!");
else
System.out.println("It's a draw! Well played!");
sc.close();
}
}