-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
188 lines (159 loc) · 6.5 KB
/
Main.java
File metadata and controls
188 lines (159 loc) · 6.5 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
import br.com.doug.model.Board;
import br.com.doug.model.Space;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.stream.Stream;
import static br.com.doug.util.BoardTemplate.BOARD_TEMPLATE;
import static java.util.Objects.isNull;
import static java.util.Objects.nonNull;
import static java.util.stream.Collectors.toMap;
public class Main {
private final static Scanner scanner = new Scanner(System.in);
private static Board board;
private final static int BOARD_LIMIT = 9;
public static void main(String[] args) {
final var positions = Stream.of(args)
.collect(toMap(
k -> k.split(";")[0],
v -> v.split(";")[1]
));
var option = -1;
while (true){
System.out.println("Selecione uma das opções a seguir");
System.out.println("1 - Iniciar um novo Jogo");
System.out.println("2 - Colocar um novo número");
System.out.println("3 - Remover um número");
System.out.println("4 - Visualizar jogo atual");
System.out.println("5 - Verificar status do jogo");
System.out.println("6 - limpar jogo");
System.out.println("7 - Finalizar jogo");
System.out.println("8 - Sair");
option = scanner.nextInt();
switch (option){
case 1 -> startGame(positions);
case 2 -> inputNumber();
case 3 -> removeNumber();
case 4 -> showCurrentGame();
case 5 -> showGameStatus();
case 6 -> clearGame();
case 7 -> finishGame();
case 8 -> System.exit(0);
default -> System.out.println("Opção inválida, selecione uma das opções do menu");
}
}
}
private static void startGame(final Map<String, String> positions) {
if (nonNull(board)){
System.out.println("O jogo já foi iniciado");
return;
}
List<List<Space>> spaces = new ArrayList<>();
for (int i = 0; i < BOARD_LIMIT; i++) {
spaces.add(new ArrayList<>());
for (int j = 0; j < BOARD_LIMIT; j++) {
var positionConfig = positions.get("%s,%s".formatted(i, j));
var expected = Integer.parseInt(positionConfig.split(",")[0]);
var fixed = Boolean.parseBoolean(positionConfig.split(",")[1]);
var currentSpace = new Space(expected, fixed);
spaces.get(i).add(currentSpace);
}
}
board = new Board(spaces);
System.out.println("O jogo está pronto para começar");
}
private static void inputNumber() {
if (isNull(board)){
System.out.println("O jogo ainda não foi iniciado iniciado");
return;
}
System.out.println("Informe a coluna que em que o número será inserido");
var col = runUntilGetValidNumber(0, 8);
System.out.println("Informe a linha que em que o número será inserido");
var row = runUntilGetValidNumber(0, 8);
System.out.printf("Informe o número que vai entrar na posição [%s,%s]\n", col, row);
var value = runUntilGetValidNumber(1, 9);
if (!board.changeValue(col, row, value)){
System.out.printf("A posição [%s,%s] tem um valor fixo\n", col, row);
}
}
private static void removeNumber() {
if (isNull(board)){
System.out.println("O jogo ainda não foi iniciado iniciado");
return;
}
System.out.println("Informe a coluna que em que o número será inserido");
var col = runUntilGetValidNumber(0, 8);
System.out.println("Informe a linha que em que o número será inserido");
var row = runUntilGetValidNumber(0, 8);
if (!board.clearValue(col, row)){
System.out.printf("A posição [%s,%s] tem um valor fixo\n", col, row);
}
}
private static void showCurrentGame() {
if (isNull(board)){
System.out.println("O jogo ainda não foi iniciado iniciado");
return;
}
var args = new Object[81];
var argPos = 0;
for (int i = 0; i < BOARD_LIMIT; i++) {
for (var col: board.getSpaces()){
args[argPos ++] = " " + ((isNull(col.get(i).getActual())) ? " " : col.get(i).getActual());
}
}
System.out.println("Seu jogo se encontra da seguinte forma");
System.out.printf((BOARD_TEMPLATE) + "\n", args);
}
private static void showGameStatus() {
if (isNull(board)){
System.out.println("O jogo ainda não foi iniciado iniciado");
return;
}
System.out.printf("O jogo atualmente se encontra no status %s\n", board.getStatus().getLabel());
if(board.hasErrors()){
System.out.println("O jogo contém erros");
} else {
System.out.println("O jogo não contém erros");
}
}
private static void clearGame() {
if (isNull(board)){
System.out.println("O jogo ainda não foi iniciado iniciado");
return;
}
System.out.println("Tem certeza que deseja limpar seu jogo e perder todo seu progresso?");
var confirm = scanner.next();
while (!confirm.equalsIgnoreCase("sim") && !confirm.equalsIgnoreCase("não")){
System.out.println("Informe 'sim' ou 'não'");
confirm = scanner.next();
}
if(confirm.equalsIgnoreCase("sim")){
board.reset();
}
}
private static void finishGame() {
if (isNull(board)){
System.out.println("O jogo ainda não foi iniciado iniciado");
return;
}
if (board.gameIsFinished()){
System.out.println("Parabéns você concluiu o jogo");
showCurrentGame();
board = null;
} else if (board.hasErrors()) {
System.out.println("Seu jogo conté, erros, verifique seu board e ajuste-o");
} else {
System.out.println("Você ainda precisa preenhcer algum espaço");
}
}
private static int runUntilGetValidNumber(final int min, final int max){
var current = scanner.nextInt();
while (current < min || current > max){
System.out.printf("Informe um número entre %s e %s\n", min, max);
current = scanner.nextInt();
}
return current;
}
}