-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathApp.java
More file actions
90 lines (79 loc) · 3.48 KB
/
App.java
File metadata and controls
90 lines (79 loc) · 3.48 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
import java.util.Scanner;
public class App {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
byte input;
byte rand;
byte i;
boolean boxAvailable = false;
byte winner = 0;
char box[] = { '1', '2', '3', '4', '5', '6', '7', '8', '9' };
System.out.println("Enter box number to select. Enjoy!\n");
boolean boxEmpty = false;
while (true) {
System.out.println("\n\n " + box[0] + " | " + box[1] + " | " + box[2] + " ");
System.out.println("-----------");
System.out.println(" " + box[3] + " | " + box[4] + " | " + box[5] + " ");
System.out.println("-----------");
System.out.println(" " + box[6] + " | " + box[7] + " | " + box[8] + " \n");
if(!boxEmpty){
for(i = 0; i < 9; i++)
box[i] = ' ';
boxEmpty = true;
}
if(winner == 1){
System.out.println("You won the game!\nCreated by Shreyas Saha. Thanks for playing!");
break;
} else if(winner == 2){
System.out.println("You lost the game!\nCreated by Shreyas Saha. Thanks for playing!");
break;
} else if(winner == 3){
System.out.println("It's a draw!\nCreated by Shreyas Saha. Thanks for playing!");
break;
}
while (true) {
input = scan.nextByte();
if (input > 0 && input < 10) {
if (box[input - 1] == 'X' || box[input - 1] == 'O')
System.out.println("That one is already in use. Enter another.");
else {
box[input - 1] = 'X';
break;
}
}
else
System.out.println("Invalid input. Enter again.");
}
if((box[0]=='X' && box[1]=='X' && box[2]=='X') || (box[3]=='X' && box[4]=='X' && box[5]=='X') || (box[6]=='X' && box[7]=='X' && box[8]=='X') ||
(box[0]=='X' && box[3]=='X' && box[6]=='X') || (box[1]=='X' && box[4]=='X' && box[7]=='X') || (box[2]=='X' && box[5]=='X' && box[8]=='X') ||
(box[0]=='X' && box[4]=='X' && box[8]=='X') || (box[2]=='X' && box[4]=='X' && box[6]=='X')){
winner = 1;
continue;
}
boxAvailable = false;
for(i=0; i<9; i++){
if(box[i] != 'X' && box[i] != 'O'){
boxAvailable = true;
break;
}
}
if(boxAvailable == false){
winner = 3;
continue;
}
while (true) {
rand = (byte) (Math.random() * (9 - 1 + 1) + 1);
if (box[rand - 1] != 'X' && box[rand - 1] != 'O') {
box[rand - 1] = 'O';
break;
}
}
if((box[0]=='O' && box[1]=='O' && box[2]=='O') || (box[3]=='O' && box[4]=='O' && box[5]=='O') || (box[6]=='O' && box[7]=='O' && box[8]=='O') ||
(box[0]=='O' && box[3]=='O' && box[6]=='O') || (box[1]=='O' && box[4]=='O' && box[7]=='O') || (box[2]=='O' && box[5]=='O' && box[8]=='O') ||
(box[0]=='O' && box[4]=='O' && box[8]=='O') || (box[2]=='O' && box[4]=='O' && box[6]=='O')){
winner = 2;
continue;
}
}
}
}