-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTicTacToe.java
More file actions
150 lines (148 loc) · 3.76 KB
/
TicTacToe.java
File metadata and controls
150 lines (148 loc) · 3.76 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
import java.util.*;
public class TicTacToe{
static ArrayList<Integer> playerpos;//array having player position
static ArrayList<Integer> cpos;// array having computer position
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
do {
playerpos = new ArrayList<>();
cpos = new ArrayList<>();
playTicTacToe();
System.out.println("Do you wanna play again? [Y/N]");
String playAgain = sc.next();
if(playAgain.equals("N") || playAgain.equals("n") ){
break;
}
}while(true);
}
public static void playTicTacToe(){
//the main game board
char[][] board = {{' ','|',' ','|',' '},
{'-','+','-','+','-'},
{' ','|',' ','|',' '},
{'-','+','-','+','-'},
{' ','|',' ','|',' '},
{'-','+','-','+','-'}};
printboard(board);
while(true) {
//player plays..
Scanner sc = new Scanner(System.in);
System.out.println("enter the position to place x:[1-9]");
int ppos = sc.nextInt();
while(!validpos(ppos)){
System.out.println("position taken or position invalid..try again");
ppos = sc.nextInt();
}
placepiece(board,ppos,"player");
//check
String result = win();
if(result.length()>0){
printboard(board);
System.out.println(result);
break;
}
//cpu plays...
Random r = new Random();//to generate random cpu moves
int comppos = r.nextInt(9)+1;
while(!validpos(comppos)){
comppos = r.nextInt(9)+1;
}
placepiece(board,comppos,"cpu");
printboard(board);
//check
result = win();
if(result.length()>0){
System.out.println(result);
break;
}
}
}
//verify the valid position
public static boolean validpos(int pos){
if(playerpos.contains(pos) || cpos.contains(pos) || (pos<1 && pos>9))
return false;
return true;
}
//printing the board..
public static void printboard(char[][] board){
for (char[] row : board) {
for (char c : row) {
System.out.print(c);
}
System.out.println();
}
}
public static void placepiece(char[][] board,int pos,String user){
char piece = ' ';
if (user.equals("player")){
piece = 'X';
playerpos.add(pos);
}else if(user.equals("cpu")){
piece = '0';
cpos.add(pos);
}
switch(pos){
case 1:
board[0][0] = piece;
break;
case 2:
board[0][2] = piece;
break;
case 3:
board[0][4] = piece;
break;
case 4:
board[2][0] = piece;
break;
case 5:
board[2][2] = piece;
break;
case 6:
board[2][4] = piece;
break;
case 7:
board[4][0] = piece;
break;
case 8:
board[4][2] = piece;
break;
case 9:
board[4][4] = piece;
break;
default:
System.out.print("position invalid...Enter between 1 and 9");
break;
}
}
public static String win(){
//valid conditions for winning
List<Integer> row1 = Arrays.asList(1, 2, 3);
List<Integer> row2 = Arrays.asList(4, 5, 6);
List<Integer> row3 = Arrays.asList(7, 8, 9);
List<Integer> col1 = Arrays.asList(1, 4, 7);
List<Integer> col2 = Arrays.asList(2, 5, 8);
List<Integer> col3 = Arrays.asList(3, 6, 9);
List<Integer> diag1 = Arrays.asList(1, 5, 9);
List<Integer> diag2 = Arrays.asList(7, 5, 3);
List<List> wins = new ArrayList<List>();
wins.add(row1);
wins.add(row2);
wins.add(row3);
wins.add(col1);
wins.add(col2);
wins.add(col3);
wins.add(diag1);
wins.add(diag2);
String res = "";
for (List l : wins) {
if (playerpos.containsAll(l)) {
return "Congratulations, you won!";
} else if (cpos.containsAll(l)) {
return "computer won!!Better luck next time:( ";
} else if (playerpos.size() + cpos.size() == 9) {
res = "It's a tie!!";
}
}
return res;
}
}