/
TicTacToe.java
153 lines (116 loc) · 3.41 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
143
144
145
146
147
148
149
150
151
152
153
import java.io.*;
import java.util.*;
public class TicTacToe {
private char[][] board;
private int whoseTurn;
private String[] players;
int movesMade;
final static private char[] pieces = {'X','O'};
public TicTacToe(String player1, String player2) {
board = new char[3][3];
for (int i=0;i < 3; i++)
for (int j=0; j < 3; j++)
board[i][j] = '_';
whoseTurn = 0;
movesMade = 0;
players = new String[2];
players[0] = player1;
players[1] = player2;
}
public boolean Move(int row, int column) {
if ( (board[row][column] == '_') && inbounds(row,column) ) {
board[row][column] = pieces[whoseTurn];
movesMade++;
return true;
}
else
return false;
}
public boolean inbounds(int row, int column) {
if ((row < 0) || (column < 0))
return false;
if ((row > 2) || (column > 2))
return false;
return true;
}
public void changeturn() {
whoseTurn = (whoseTurn + 1)%2;
}
public String getCurrentPlayer() {
return players[whoseTurn];
}
public void printboard() {
System.out.println("\t0 1 2");
for (int i=0; i<3; i++) {
System.out.print(i+"\t");
for (int j=0; j<3; j++)
System.out.print(board[i][j]+" ");
System.out.println();
}
}
public char winner() {
for (int i=0; i<3; i++)
if (SameArray(board[i]) && board[i][0] != '_')
return board[i][0];
for (int i=0; i<3; i++)
if ((board[0][i] == board[1][i]) && (board[1][i] == board[2][i]) && board[0][i] != '_')
return board[0][i];
if ((board[0][0] == board[1][1]) && (board[1][1] == board[2][2]))
return board[0][0];
if ((board[2][0] == board[1][1]) && (board[1][1] == board[0][2]))
return board[2][0];
if (movesMade == 9)
return 'T';
return '_';
}
private static boolean SameArray(char[] word) {
char check = word[0];
for (int i=1; i<word.length; i++)
if (check != word[i])
return false;
return true;
}
public String whosePiece(char x) {
for (int i=0; i<2; i++)
if (x == pieces[i])
return players[i];
return "Dummy";
}
public static void main(String[] args) throws IOException {
Scanner stdin = new Scanner(System.in);
System.out.println("Player #1, enter your name.");
String name1 = stdin.next();
System.out.println("Player #2, enter your name.");
String name2 = stdin.next();
TicTacToe mygame = new TicTacToe(name1, name2);
while (mygame.winner() == '_') {
int r,c;
boolean done = false;
do {
mygame.printboard();
System.out.print(mygame.getCurrentPlayer());
System.out.print(", Enter the row(0-2) and column(0-2) ");
System.out.println("of your move.");
r = stdin.nextInt();
c = stdin.nextInt();
if (!mygame.inbounds(r,c))
System.out.println("Sorry, those are invalid entries.");
else {
if (!mygame.Move(r,c))
System.out.println("Sorry, that square is taken.");
else
done = true;
}
} while (!done);
mygame.changeturn();
}
mygame.printboard();
char win = mygame.winner();
if (win == 'T')
System.out.println("Both of you played to a tie.");
else {
System.out.print("Congratulations, " + mygame.whosePiece(win));
System.out.println(", you have won the game.");
}
}
}