-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTic Tac Toe
202 lines (188 loc) · 5 KB
/
Tic Tac Toe
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
package TicTacToe;
import java.util.Scanner;
//create board for tic tac toe
public class Board {
//player one symbol
//player two symbol
//size
//count
private char board[][];
Scanner s=new Scanner(System.in);
//System.out.println("enter board size");
private int boardsize=3;
private char player1symbol,player2symbol;
//count the number of cell that are filled.
private int count;
private static final char EMPTY=' ';
public static final int PLAYER1WINS=1;
public static final int PLAYER2WINS=2;
public static final int DRAWS=3;
public static final int INCOMPLITE=4;
public static final int INVALIDMOVE=5;
//create constructor to initialize
public Board(char p1symbol,char p2symbol) {
board=new char[boardsize][boardsize];
for(int i=0;i<boardsize;i++) {
for(int j=0;j<boardsize;j++) {
board[i][j]=EMPTY;
}
}
this.player1symbol=player1symbol;
this .player2symbol=player2symbol;
}
public int move(char symbol, int x, int y) {
if(x<0 || x>=boardsize ||y<0||y>=boardsize || board[x][y]!=EMPTY ) {
return INVALIDMOVE;
}
// if(board[x][y]!=EMPTY) {
// return INVALIDMOVE;
// }
board[x][y]=symbol;
count++;
//row
if(board[x][0]==board[x][1] && board[x][0]==board[x][2]) {
return symbol== player1symbol ? PLAYER1WINS : PLAYER2WINS;
}
//column
if(board[0][y]==board[1][y] && board[0][y]==board[2][y]) {
return symbol== player1symbol ? PLAYER1WINS : PLAYER2WINS;
}
// 1st Digonals
if(board[0][0]!=EMPTY && board[0][0]==board[1][1] && board[0][0]==board[2][2]) {
return symbol==player1symbol ? PLAYER1WINS : PLAYER2WINS;
}
//2nd digonal
if(board[0][2]!=EMPTY && board[0][2]==board[1][1] && board[0][2]==board[2][0]) {
return symbol==player1symbol ? PLAYER1WINS : PLAYER2WINS;
}
if(count==boardsize * boardsize) {
return DRAWS;
}
return INCOMPLITE;
}
public void print() {
System.out.println("-----------------");
for(int i=0;i<boardsize;i++) {
for(int j=0;j<boardsize;j++) {
System.out.print("|"+board[i][j]+"|");
}
System.out.println();
}
System.out.println();
System.out.println("----------------");
}
}
//create method for players of tic tac toe
package TicTacToe;
public class Player {
//1 name of player
//symbol of player
//who won the game
private String name;
private char symbol;
//create constructor initialize value
public Player(String name,char symbol) {
setName(name);
setSymbol(symbol);
}
public void setName(String name) {
if(!name.isEmpty()) {
this .name=name;
}
}
public String getName() {
return this.name;
}
public void setSymbol(char symbol) {
if(symbol!='\0') {
this .symbol=symbol;
}
}
public char getSymbol() {
return this.symbol;
}
}
//create method for start game or move players
package TicTacToe;
import java.util.Scanner;
public class TicTacToe {
//player1
//player2
//board
private Player player1,player2;
private Board board;
private int numplayers;
public static void main(String []args) {
TicTacToe t=new TicTacToe();
t.StartGame();
}
public void StartGame() {
Scanner s=new Scanner(System.in);
//take player input
player1=takeplayerInput(++numplayers);
player2=takeplayerInput(++numplayers);
while(player1.getSymbol()==player2.getSymbol()) {
System.out.println("symbol already taken!! choose again symbol");
player2.setSymbol(s.next().charAt(0));
}
//create the board
board=new Board(player1.getSymbol(),player2.getSymbol());
//play the game
boolean player1turn=true;
int status=Board.INCOMPLITE;
while(status==Board.INCOMPLITE || status==Board.INVALIDMOVE) {
if(player1turn) {
System.out.println("player-1- "+player1.getName()+"'s turn");
System.out.println("Enter x: ");
int x=s.nextInt();
System.out.println("Enter y: ");
int y=s.nextInt();
status =board.move(player1.getSymbol(),x,y);
if(status==Board.INVALIDMOVE) {
System.out.println("invalid move !!please try again");
continue;
}
// else {
// player1turn=false;
// board.print();
// }
}
else {
System.out.println("player-2- "+player2.getName()+"'s turn");
System.out.println("Enter x : ");
int x=s.nextInt();
System.out.println("Enter y : ");
int y=s.nextInt();
status =board.move(player2.getSymbol(),x,y);
if(status==Board.INVALIDMOVE) {
System.out.println("invalid move !!please try again");
continue;
}
// else {
// player1turn=true;
// }
}
player1turn=!player1turn;
board.print();
}
if(status==Board.PLAYER1WINS) {
System.out.println("player-1 "+player1.getName()+" wins");
}
else if(status==Board.PLAYER2WINS) {
System.out.println("player-2 "+player2.getName()+" wins");
}
else {
System.out.println("Match Draw");
}
}
//create method for playerinput
private Player takeplayerInput(int num) {
Scanner s=new Scanner(System.in);
System.out.println("Enter player"+num+"name:");
String name=s.nextLine();
System.out.println("Enter player"+num+"symbol:");
char symbol=s.next().charAt(0);
Player p=new Player(name,symbol);
return p;
}
}