-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlayer.java
180 lines (152 loc) · 4.28 KB
/
Player.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
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
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;
public class Player {
private String name;
private char mark;
private Player opponent;
private Board board;
private Socket gameSocket;
private BufferedReader socketIn;
private PrintWriter socketOut;
/**
* Constructor for Player class.
* @param name The Players name
* @param mark The Players mark ('X' or 'O')
*/
public Player(Socket socket, char mark) {
this.gameSocket = socket;
setMark(mark);
try {
socketIn = new BufferedReader(new InputStreamReader(gameSocket.getInputStream()));
socketOut = new PrintWriter(gameSocket.getOutputStream(), true);
} catch (IOException e) {
System.err.println(e.getStackTrace());
}
}
/**
* Allows the players to play the game and checks for win/tie conditions
* @throws IOException
*
*/
public void play() throws IOException {
while (!board.xWins() || !board.oWins() || !board.isFull()) { // checks if Player X or Player O has won the game or if its a tie
this.makeMove(); // calls the makeMove method
socketOut.println(board.display()); // displays the board after the move has been made
opponent.socketOut.println(board.display());
if (board.xWins() == true) {
socketOut.println("GAME OVER! "+ name + " is the winner. ");
opponent.socketOut.println("GAME OVER! "+ name + " is the winner. ");
break;
}
System.out.println("HERE 1");
this.opponent.makeMove(); // Passing the turn to the opponent
System.out.println("HERE 2");
socketOut.println(board.display()); // displays the board after the move has been made
opponent.socketOut.println(board.display());
//board.display();
if (board.oWins() == true) {
socketOut.println("GAME OVER! "+ opponent.getName() + " is the winner.");
opponent.socketOut.println("GAME OVER! "+ opponent.getName() + " is the winner. ");
break;
}
if (board.isFull() == true) {
socketOut.println("It is a tie game! ");
opponent.socketOut.println("It is a tie game! ");
break;
}
}
}
/**
* Allows the user to input the row and column on the board where the Player would like to add the mark
* @throws IOException
*/
public void makeMove() throws IOException {
System.out.println("Inside make move 1");
sendString(name + ", Enter row for your next "+ mark+ " be placed in:\0");
System.out.println("Inside make move 2");
int selectedRow = Integer.parseInt(socketIn.readLine());
sendString(name + ", Enter column should your next "+ mark+ " be placed in:\0 ");
int selectedColumn = Integer.parseInt(socketIn.readLine());
board.addMark(selectedRow, selectedColumn, mark);
}
private void sendString (String toSend) {
//socketOut.println(toSend + "\0");
socketOut.println(toSend);
socketOut.flush();
}
public void getPlayerName() {
try {
sendString("Please enter the name of " + mark + " player:\0");
name = socketIn.readLine();
while (name == null) {
sendString("Please enter a name:\0");
name = socketIn.readLine();
}
}catch (IOException e) {
e.printStackTrace();
}
}
/**
* Getter for name
* @return name
*/
public String getName() {
return name;
}
/**
* Setter for name
* @param name Player name
*/
public void setName(String name) {
this.name = name;
}
/**
* Getter for mark
* @return mark
*/
public char getMark() {
return mark;
}
/**
* Setter for mark
* @param mark Player mark
*/
public void setMark(char mark) {
this.mark = mark;
}
/**
* Getter for opponent
* @return opponent
*/
public Player getOpponent() {
return opponent;
}
/**
* Setter for opponent
* @param opponent opponent Player
*/
public void setOpponent(Player opponent) {
this.opponent = opponent;
}
/**
* Getter for board
* @return board
*/
public Board getBoard() {
return board;
}
public PrintWriter getSocketOut() {
return socketOut;
}
/**
* Setter for board
* @param board board for the game
*/
public void setBoard(Board board) {
this.board = board;
}
}