-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameManager.java
More file actions
87 lines (71 loc) · 2.83 KB
/
GameManager.java
File metadata and controls
87 lines (71 loc) · 2.83 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
import java.util.Scanner;
public class GameManager {
public void startGame(Scanner scanner, int[][] solutionBoard, int[][] puzzleBoard, Player player) {
int[][] userBoard = BoardUtils.deepCopy(puzzleBoard);
boolean[][] lockedWrongCells = new boolean[9][9];
Difficulty level = player.getDifficulty();
boolean showCorrectness = (level == Difficulty.EASY);
boolean lockWrong = (level == Difficulty.EXTREME);
int lives = 0;
switch (level) {
case HARD:
lives = 5;
break;
case EXTREME:
lives = 3;
break;
default:
// EASY and MEDIUM do not use lives
break;
}
int wrongAttempts = 0;
while (!BoardUtils.isBoardFull(userBoard)) {
BoardUtils.printBoard(userBoard);
System.out.println("Lives: " + (lives > 0 ? lives : "∞") + " | Wrong Attempts: " + wrongAttempts);
System.out.print("Enter row (0-8): ");
int row = scanner.nextInt();
System.out.print("Enter col (0-8): ");
int col = scanner.nextInt();
if (puzzleBoard[row][col] != 0) {
System.out.println(" You cannot change this cell.");
continue;
}
if (lockedWrongCells[row][col]) {
System.out.println(" This cell is locked due to a wrong input earlier.");
continue;
}
System.out.print("Enter number (1-9): ");
int num = scanner.nextInt();
if (num < 1 || num > 9) {
System.out.println(" Invalid number. Try again.");
continue;
}
if (solutionBoard[row][col] == num) {
userBoard[row][col] = num;
if (showCorrectness) System.out.println("✅ Correct!");
} else {
wrongAttempts++;
if (level == Difficulty.HARD || level == Difficulty.EXTREME) {
lives--;
System.out.println(" Wrong! You lost a life.");
if (lives == 0) {
System.out.println(" Game Over! You've run out of lives.");
return;
}
} else {
System.out.println(showCorrectness ? " Wrong input." : "");
}
if (lockWrong) {
lockedWrongCells[row][col] = true;
}
// Don't place incorrect number
continue;
}
}
// Game completed
BoardUtils.printBoard(userBoard);
System.out.println("🎉 You've completed the puzzle!");
System.out.println("Wrong moves made: " + wrongAttempts);
LeaderboardManager.updateLeaderboard(player);
}
}