-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathConnect4Text.java
More file actions
133 lines (109 loc) · 2.97 KB
/
Connect4Text.java
File metadata and controls
133 lines (109 loc) · 2.97 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
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
import java.util.InputMismatchException;
import java.util.Scanner;
/**
* Textual view of the Connect 4 board with interaction from keyboard
* @author Delos Chang
*
*/
public class Connect4Text implements Connect4View{
private Scanner input;
public Connect4Text(){
input = new Scanner(System.in);
}
/**
* Displays the current board
* @param state current state of the game
*/
public void display(Connect4State state){
char [][] board = state.getBoard();
for (int row = state.ROWS - 1; row > -1; row--){
System.out.print(row + " |");
for (int column = 0; column < state.COLS; column++){
System.out.print(board[row][column]);
System.out.print(" ");
}
System.out.println("\n");
}
System.out.print(" ");
for (int column = 0; column < state.COLS; column++){
System.out.print(" "+column);
}
System.out.println("\n");
}
/**
* Asks the user for a move
* The move will be in the range 0 to Connect4State.COLS-1.
* @param state current state of the game
* @return the number of the move that player chose
*/
public int getUserMove(Connect4State state){
Scanner column;
int choose;
System.out.println();
System.out.println("Please pick a column");
column = new Scanner(System.in);
// validity checks
for(;;) {
if(!column.hasNextInt() ) {
System.out.println("Integers from 0 to 6 allowed.");
System.out.println("Please pick a column");
column.next(); // discard
continue;
}
choose = column.nextInt();
if( (choose < 0) || (choose > state.COLS - 1) ) {
System.out.println("Illegal column. Please try again");
System.out.println("Please pick a column");
continue;
}
break;
}
return choose;
}
/**
* Reports the move that a player has made.
* The move should be in the range 0 to Connect4State.COLS-1.
* @param chosenMove the move to be reported
* @param name the player's name
*/
public void reportMove (int chosenMove, String name){
System.out.println("\n" + name + " chooses the column " + chosenMove);
}
/**
* Ask the user the question and return the answer as an int
* @param question the question to ask
* @return The depth the player chose
*/
public int getIntAnswer (String question){
int answer = 0;
boolean valid = false;
// Ask question
System.out.println(question + " ");
while (!valid){
try {
answer = input.nextInt();
valid = true;
} catch (NumberFormatException ex) {
reportToUser("Error: "+ ex + " Please enter an integer");
valid = false;
}
}
return answer;
}
/**
* Convey a message to user
* @param message the message to be reported
*/
public void reportToUser(String message){
System.out.println(message);
}
/**
* Ask the question and return the answer
* @param question the question to ask
* @return the answer to the question
*/
public String getAnswer(String question){
System.out.println(question + " ");
return input.nextLine();
}
}