-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTicTacToe
More file actions
97 lines (89 loc) · 3.08 KB
/
TicTacToe
File metadata and controls
97 lines (89 loc) · 3.08 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
// Create a program to replicate a tic tac toe game
// Created: Apr 20 2020
// For example:
/* 1 2 3
4 5 6
7 8 9
Input: 5 (move for player 1)
Output:
1 2 3
4 x 6
7 8 9
*/
import java.util.Scanner;
public class TicTacToe {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// create all variables used
int num = 1;
int move = 0;
boolean win = false;
int moveNum = 1;
int i1 = 0;
int i2 = 0;
boolean number = false;
// use two dimensional array for the tic tac toe board
String[][] arr = new String[3][3];
// currentPlayer is 2 because it will switch to 1 at the beginning of the loop
int currentPlayer = 2;
// Array create
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
arr[i][j] = Integer.toString(num);
num++;
}
}
for (int j = 0; j < 9; j++) {
currentPlayer = 3 - currentPlayer;
//print the array
for (int k = 0; k < 3; k++) {
for (int l = 0; l < 3; l++) {
System.out.print(arr[k][l] + " ");
}
System.out.println();
}
System.out.println("Player " + currentPlayer + " turn");
// error system: make sure what they put is a number from 1 to 9
while (true) {
if (sc.hasNextInt() == false) {
sc.next();
System.out.println("Type a whole number from 1 to 9");
continue;
}
move = sc.nextInt();
System.out.println("Move: "+ move);
if (move < 1 || move > 9) {
System.out.println("ERROR: Your move must be between 1 and 9");
continue;
}
// calculate the move to the corresponding array
i1 = Math.round((move-1)/3);
i2 = (move - 1) % 3;
if (arr[i1][i2] == "x" || arr[i1][i2] == "o") {
System.out.println("ERROR: There is already a letter in that box");
continue;
} else {
break;
}
}
// code is to print x or o into the corresponding box
if (currentPlayer == 1) {
arr[i1][i2] = "x";
} else if (currentPlayer == 2) {
arr[i1][i2] = "o";
}
//All code is to check for winner
if ((arr[0][0] == arr[1][1] && arr[1][1] == arr[2][2]) || (arr[0][2] == arr[1][1] && arr[1][1] == arr[2][0]) ||
(arr[0][i2] == arr[1][i2] && arr[1][i2] == arr[2][i2]) || (arr[i1][0] == arr[i1][1] && arr[i1][1] == arr[i1][2])) {
win = true;
break;
}
}
// print winner or tie
if (win) {
System.out.println("Player " + currentPlayer + " wins");
} else {
System.out.println("Tie");
}
}
}