This repository was archived by the owner on Mar 2, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUser.java
More file actions
192 lines (177 loc) · 4.17 KB
/
User.java
File metadata and controls
192 lines (177 loc) · 4.17 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
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
181
182
183
184
185
186
187
188
189
190
191
192
import gameCore.API;
import gameCore.Board;
import gameCore.Cell;
import gameCore.NoSuchCellException;
import java.util.InputMismatchException;
import java.util.Scanner;
public class User implements API {
private static final Scanner INPUT = new Scanner(System.in);
private Board board;
User(Board board){
this.board = board;
}
@Override
public void showBoard() {
int size = this.board.getSize();
System.out.println();
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
System.out.print("--");
}
System.out.print("-");
System.out.println();
for (int j = 0; j < size; j++) {
Cell cell = board.getCell(i, j);
int[] display = cell.display();
if (display[0] == 0) {
System.out.print("| ");
}
else if (display[0] == 1) {
System.out.print("|!");
}
else if (display[0] == 2){
System.out.print("|"+ display[1]);
}
else if (display[0] == 3){
System.out.print("|X");
}
}
System.out.print("|");
System.out.println();
}
for (int j = 0; j < size; j++) {
System.out.print("--");
}
System.out.print("-");
System.out.println();
}
@Override
public void showTrueBoard() {
int size = this.board.getSize();
System.out.println();
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
System.out.print("--");
}
System.out.print("-");
System.out.println();
for (int j = 0; j < size; j++) {
Cell cell = board.getCells()[i][j];
int[] display = cell.trueDisplay();
if (display[0] == 4) {
System.out.print("|x");
}
else {
System.out.print("|" + display[1]);
}
}
System.out.print("|");
System.out.println();
}
for (int j = 0; j < size; j++) {
System.out.print("--");
}
System.out.print("-");
System.out.println();
}
@Override
public void receiveRound(int round){
// do nothing
}
/**
* Show a menu with several options, the user must choose one. If the user fails to choose properly (not an integer, or too small or too big an integer), the menu reappears.
* @param s is an array of type String, s[0] contains the general information and the other elements are the options that can be chosen from.
* @return an integer, showing with option was chosen. The first one correspond with 1, and so on.
*/
private static int menu(String[] s) {
boolean correct = false;
int answer = 0;
while (!correct) {
System.out.println("--------");
System.out.println(s[0]);
for (int i = 1; i < s.length ; i++) {
System.out.println(i +". "+ s[i]);
}
System.out.println();
try {
answer = INPUT.nextInt();
if ((answer > 0) && (answer < s.length)) {
correct = true;
}
else {
System.out.println("Invalid Selection.");
}
}
catch (InputMismatchException ex) {
INPUT.nextLine();
System.out.println("Invalid Selection.");
}
}
return answer;
}
@Override
public Cell selectCell() {
int x = this.getCoordinate("x");
int y = this.getCoordinate("y");
return this.board.getCell(x, y);
}
@Override
public Cell firstCell() {
return this.selectCell();
}
@Override
public int action() {
String[] menu = new String[4];
menu[0] = "Select an action:";
menu[1] = "Reveal a cell.";
menu[2] = "Flag a cell.";
menu[3] = "Unflag a cell";
int answer = -1;
switch(User.menu(menu)) {
case 1:
{
answer = 2;
break;
}
case 2:
{
answer = 1;
break;
}
case 3:
{
answer = 0;
break;
}
}
return answer;
}
/**
* Gets a coordinate of a cell, selected by the user to perform an action on it.
* @param s String, the name of the coordinate.
* @return An integer.
*/
private int getCoordinate(String s) {
boolean correct = false;
int answer = -1;
while(!correct) {
System.out.println("Input "+ s +"-coordinate.");
try {
int coordinate = INPUT.nextInt();
try {
Board.checkCell(coordinate, 0, this.board.getSize());
correct = true;
answer = coordinate;
}
catch (NoSuchCellException ex){
System.out.println("Invalid Selection.");
}
}
catch (InputMismatchException ex) {
INPUT.nextLine();
System.out.println("Invalid Selection.");
}
}
return answer;
}
}