-
Notifications
You must be signed in to change notification settings - Fork 0
/
TicTacToe.java
142 lines (129 loc) · 4.74 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
135
136
137
138
139
140
141
142
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;
import java.util.Scanner;
public class TicTacToe {
static ArrayList <Integer> playerPositions = new ArrayList<Integer>();
static ArrayList <Integer> computerPositions = new ArrayList<Integer>();
public static void main(String[] args){
char[][] gameBoard = {
{' ', '|', ' ', '|', ' '},
{'-', '+', '-', '+', '-'},
{' ', '|', ' ', '|', ' '},
{'-', '+', '-', '+', '-'},
{' ', '|', ' ', '|', ' '}
};
gameBoardPrint(gameBoard);
while(true){
Scanner gameInput = new Scanner(System.in);
System.out.print("\nPlease enter the number of the area. (1-9): ");
int gameInputPosition = gameInput.nextInt();
while(playerPositions.contains(gameInputPosition) || computerPositions.contains(gameInputPosition)){
System.out.print("Position taken! Please enter a blank position: ");
gameInputPosition = gameInput.nextInt();
}
gamePlaceInput(gameBoard, gameInputPosition, "Player");
String result = winState();
if(result.length() > 0){
System.out.println(result);
gameBoardPrint(gameBoard);
break;
}
Random computerRandom = new Random();
int computerRandomPosition = computerRandom.nextInt(9) + 1;
while(playerPositions.contains(computerRandomPosition) || computerPositions.contains(computerRandomPosition)){
computerRandomPosition = computerRandom.nextInt(9) + 1;
}
gamePlaceInput(gameBoard, computerRandomPosition, "Computer");
result = winState();
if(result.length() > 0){
System.out.println(result);
gameBoardPrint(gameBoard);
break;
}
gameBoardPrint(gameBoard);
}
}
public static void gameBoardPrint(char[][] gameBoard){
for(char[] row : gameBoard){
for(char column : row){
System.out.print(column);
}
System.out.println();
}
}
public static void gamePlaceInput(char[][] gameBoard, int gameInputPosition, String user){
char symbol = ' ';
if(user.equals("Player")){
symbol = 'X';
playerPositions.add(gameInputPosition);
}
else if(user.equals("Computer")){
symbol = 'O';
computerPositions.add(gameInputPosition);
}
switch(gameInputPosition){
case 1:
gameBoard[0][0] = symbol;
break;
case 2:
gameBoard[0][2] = symbol;
break;
case 3:
gameBoard[0][4] = symbol;
break;
case 4:
gameBoard[2][0] = symbol;
break;
case 5:
gameBoard[2][2] = symbol;
break;
case 6:
gameBoard[2][4] = symbol;
break;
case 7:
gameBoard[4][0] = symbol;
break;
case 8:
gameBoard[4][2] = symbol;
break;
case 9:
gameBoard[4][4] = symbol;
break;
default:
break;
}
}
public static String winState(){
List topRow = Arrays.asList(1, 2, 3);
List midRow = Arrays.asList(4, 5, 6);
List botRow = Arrays.asList(7, 8, 9);
List lftCol = Arrays.asList(1, 4, 7);
List midCol = Arrays.asList(2, 5, 8);
List rgtCol = Arrays.asList(3, 6, 9);
List ltDiag = Arrays.asList(1, 5, 9);
List rtDiag = Arrays.asList(3, 5, 7);
List<List> winningStates = new ArrayList<List>();
winningStates.add(topRow);
winningStates.add(midRow);
winningStates.add(botRow);
winningStates.add(lftCol);
winningStates.add(midCol);
winningStates.add(rgtCol);
winningStates.add(ltDiag);
winningStates.add(rtDiag);
for(List winner : winningStates){
if(playerPositions.containsAll(winner)){
return "Congratulations! You won!";
}
else if (computerPositions.containsAll(winner)){
return "Computer wins! Sorry... :(";
}
else if(playerPositions.size() + computerPositions.size() == 9){
return "It's a tie!";
}
}
return "";
}
}