-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlayer.java
executable file
·142 lines (126 loc) · 5.42 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
import java.util.Scanner;
import java.util.InputMismatchException;
import java.io.*;
/** A class used for input into the Tic-Tac-Toe game.
* Represents a player in a Tic-Tac-Toe game/
*
* @author Joel Wong, Student ID 30018132
* @version 1.0
* @since January 23rd, 2019
*/
public class Player implements Constants {
/** Store's the player's name, input at the start of the game. */
private String name;
/** Stores the player's board and allows them to play against others*/
private Board board;
/** Store's the Tic-Tac-Toe opponent of this player */
private Player opponent;
/** Stores the player's marking character (X or O) */
private char mark;
/** Used for received player input when determining where to place their next mark */
private Scanner sc;
/** Creates a new player to play TicTacToe with a given name and mark.
*
* @param name The name of the player
* @param mark The mark (X's or O's) of the player
*/
Player(String name, char mark) {
this.name = name;
this.mark = mark;
this.sc = new Scanner(System.in);
}
/** Returns the name of the player.
*
* @return The name of the player.
*/
String getName(){
return name;
}
/** Returns the player's marking character (X or O).
*
* @return The player's marking character (X or O).
*/
char getMark(){
return mark;
}
// No need for setname or setmark, it's not going to change.
/** Sets the player's board, which allows them to play against others
* @param board The board that the players are playing on. This board can only have two players using it at a time. */
void setBoard(Board board){
this.board = board;
}
/** Sets the player's opponent, which allows them to pass the turn after they make a mark
* @param opponent The opponent of this player. This player will pass the turn to the opponent after they have made a move.*/
void setOpponent(Player opponent) {
this.opponent = opponent;
}
/** Provides an interface to receives the desired location of the X or O from the command line,
* then adds that mark to the board. Will repeatedly ask for input if user inputs incorrect format.
*
* @return The row and column of the move as a String, separated by a space.
*/
public String makeMove() {
// row of input
int row;
// column of input
int col;
// Attempt to get user input. If user input is invalid, tell the user and try to get input again.
while (true) {
// Get row number, but ensure it's an integer between 0 and 2
while (true) {
System.out.println("\n" + name + ", what row should your next " + mark + " be placed in? ");
try {
// Input row number
row = sc.nextInt();
// check if it is in the desired range
if (row < 0 || row > 2) {
System.out.println("That is not a valid input, row must be between 0 and 2");
continue;
} else {
break;
}
} catch (InputMismatchException e) {
// Input was not an integer
System.out.println("That is not an integer. Please enter an integer between 0 and 2 (inclusive).");
// move to next open space in system input (similar to clearing the buffer)
sc.next();
// restart loop
continue;
}
}
// Get column number, but ensure it's an integer between 0 and 2
while (true) {
System.out.println("\n" + name + ", what column should your next " + mark + " be placed in? ");
try {
// Input column number
col = sc.nextInt();
// check if it is in the desired range
if (col < 0 || col > 2) {
System.out.println("That is not a valid input, column must be between 0 and 2");
continue;
} else {
break;
}
} catch (InputMismatchException e) {
// Input was not an integer
System.out.println("That is not an integer. Please enter an integer between 0 and 2 (inclusive).");
// move to next open space in system input (similar to clearing the buffer)
sc.next();
// restart loop
continue;
}
}
// Check if row and column is already filled. If so, reset
if (board.getMark(row, col) != SPACE_CHAR) {
System.out.println("That space is already filled. Please enter the row and column again.");
// move to next open space in system input (similar to clearing the buffer)
// restart loop
continue;
}
// Since the input is valid, add the mark to the board.
board.addMark(row, col, mark);
return new String(row + " " + col);
}
}
public void play(){}
}