-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNumberPuzzleG.java
118 lines (91 loc) · 3.63 KB
/
NumberPuzzleG.java
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
import java.util.Scanner;
public class NumberPuzzleG {
private NumberPuzzleBoard b;
private Scanner scanner;
private Level[] levels;
private int curLevIndex;
private InputR inputR;
private PuzzleTimer timer;
private int moves;
public NumberPuzzleG(Level[] levels) {
this.levels = levels;
this.curLevIndex = 0;
this.b = null;
this.scanner = new Scanner(System.in);
this.inputR = new InputR();
this.timer = new PuzzleTimer();
this.moves = 0;
}
public void showMenu() {
System.out.println("Welcome to Number Puzzle Game!");
System.out.println("Choose a level:");
for (int i = 0; i < levels.length; i++) {
System.out.println((i + 1) + ". Level " + (i + 1) + " (" + levels[i].getGridSize() + "x" + levels[i].getGridSize() + ")");
}
System.out.print("Enter the level number: ");
int choice = scanner.nextInt();
if (choice >= 1 && choice <= levels.length) {
b = new NumberPuzzleBoard(levels[choice - 1].getGridSize());
} else {
System.out.println("Invalid choice. Please select a valid level.");
showMenu();
}
}
public void displayMoves() {
System.out.println("Number of Moves: " + moves);
}
public int[] start() {
int[] scores = new int[levels.length];
while (curLevIndex < levels.length) {
Level currentLevel = levels[b.getBoard().length - 2];
int gridSize = currentLevel.getGridSize();
b = new NumberPuzzleBoard(gridSize);
b.initialize(currentLevel.getInitialGrid());
b.shuffle();
timer.startTimer();
moves = 0;
while (!b.isSolved()) {
int[][] crntBoard = b.getBoard();
b.displayLevel(crntBoard);
String mv = inputR.getValidMove();
b.move(mv);
moves++;
}
timer.stopTimer();
int eSecond = timer.getsecondE();
System.out.println("Level " + (curLevIndex + 1) + " completed in " + eSecond + " seconds.");
System.out.println("Number of Moves: " + moves);
int score = calScore(moves);
scores[curLevIndex] = score;
System.out.println("Score for Level " + (curLevIndex + 1) + ": " + score);
if (curLevIndex < levels.length - 1) {
System.out.println("Next Level...");
}
curLevIndex++;
}
System.out.println("Congratulations! You completed all levels.");
return scores;
}
private int calScore(int moves) {
return 100 - moves;
}
public static void main(String[] args) {
Level[] levels = {
new Level(2,
new int[][] {{1, 2}, {3, 0}},
new int[][] {{1, 2}, {3, 0}}
),
new Level(3,
new int[][] {{1, 2, 3}, {4, 5, 6}, {7, 8, 0}},
new int[][] {{1, 2, 3}, {4, 5, 6}, {7, 8, 0}}
),
new Level(4,
new int[][] {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}, {13, 14, 15, 0}},
new int[][] {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}, {13, 14, 15, 0}}
),
};
NumberPuzzleG game = new NumberPuzzleG(levels);
game.showMenu();
game.start();
}
}