-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayerHuman.java
More file actions
62 lines (44 loc) · 1.6 KB
/
PlayerHuman.java
File metadata and controls
62 lines (44 loc) · 1.6 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
/**
* Edward Skrod, ejs09f
* @version 1.0, February 2014
* @author Edward Skrod ejs09f@my.fsu.edu
*
* PlayerHuman extends abstract class Player
* Key differences:
*
* takeTurn() instantiates a Scanner object and reads
* the Player input from stdin. It performs error checking:
* for when the board position has already been played
* for when the number is not in the range 1-9
*
*/
import java.util.Scanner;
public class PlayerHuman extends Player {
Scanner playerInput = new Scanner(System.in);
public PlayerHuman (int playerNumber) {
// Constructor creates Human Player
super(playerNumber);
}
public void takeTurn(TicTacToeBoard board) {
int boardPosition = 0;
// What does it mean for a player to take a turn?
// It means that a player returns a position on the board.
System.out.printf("%s %d, %s", "Player", this.getPlayerNumber(), "please enter a move (1 - 9): ");
boardPosition = playerInput.nextInt();
System.out.println();
// Error Checking
while ((board.isPositionTaken( boardPosition ) == true) ||
(boardPosition < 1 || boardPosition > 9 )) {
if (boardPosition < 1 || boardPosition > 9 ) {
System.out.printf("%s %d, %s", "Invalid number. Player", this.getPlayerNumber(), "please enter a move (1 - 9): ");
} else {
System.out.printf("%s %d, %s", "Position taken. Player", this.getPlayerNumber(),
"please enter a move (1 - 9): ");
}
boardPosition = playerInput.nextInt();
System.out.println();
}
// Take the turn
board.selectPosition(boardPosition, this.getToken());
} // end takeTurn()
}