forked from ZhidongLiu/cs11a03-Fantastic4
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Try.java
122 lines (114 loc) · 3.27 KB
/
Try.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
119
120
121
122
import java.awt.*;
import javax.swing.*;
import java.util.Scanner;
import mosaic.Mosaic;
import java.util.Arrays;
public class Try{
private static class Point{
int a;
int b;
public Point(int a, int b){
this.a = a;
this.b = b;
}
public void setPoint(int x, int y){
this.a = x;
this.b = y;
}
}
public static int[][]route;
public static Scanner scan = new Scanner(System.in);
public static String input;
public static String[] inputStr = new String[50];
public static int checkI;
public static int x=24,y=24;
public static int index;
public static void main(String[] args){
Mosaic.open(49, 49, 10, 10);
System.out.println("You can try how to play the game.");
System.out.println("You have three chances in this 5-level game.");
System.out.println("You can use 'w', 'a', 's', 'd' key to control the up, left, down and right.");
System.out.println("Which level you want to try (from 1 to 5)?");
int level =scan.nextInt();
init(level);
System.out.println("Level "+level);
//Draw the map(route)
Mosaic.setColor(24,24,Color.YELLOW);
for(int i=1; i<route.length; i++){
Mosaic.setColor(route[i][0],route[i][1],Color.WHITE);
}
index = 0;
Point p = new Point(24, 24);
while(index < route.length - 1){
if (!checkSingleInput()){
System.out.println("Wrong Input.");
System.out.println("Please use 'u', 's', 'a','d', 'U', 'S', 'A', 'D' to control.");
}else{
if (testPlayRoute2(index, p)){
lightPath(p);
Mosaic.delay(100);
index++;
}else{
System.out.println("The route is invalid.");
}
}
}
showSolution();
System.out.println("Congratulations! You can try to play the real game.");
Mosaic.delay(500);
Mosaic.close();
}
public static boolean testPlayRoute2(int index, Point p){
char currentChar = inputStr[index].charAt(0);
int x = p.a;
int y = p.b;
switch(currentChar){
case 'W':
x--;break;
case 'S':
x++;break;
case 'A':
y--;break;
case 'D':
y++;break;
}
if (x == route[index + 1][0] && y == route[index + 1][1]){
p.setPoint(x, y);
return true;
}
return false;
}
public static void flashEffect(int x, int y,Color c){
Mosaic.setColor(x,y,c);
Mosaic.delay(80);
Mosaic.setColor(x,y,Color.BLACK);
Mosaic.delay(80);
}
public static void init(int lv){
Maze maze = new Maze(4+2*lv); //min_length = 10, min_level = 1;
maze.route();
route = new int[maze.output[0].length][2];
for(int i=0;i<route.length;i++){
route[i][0] = maze.output[0][i];
route[i][1] = maze.output[1][i];
}
}
public static boolean checkSingleInput(){
String inputTmp = scan.next();
inputTmp = inputTmp.toUpperCase();
if (inputTmp.equals("A") || inputTmp.equals("W") || inputTmp.equals("S")||inputTmp.equals("D")){
inputStr[index] = inputTmp;
return true;
}
return false;
}
public static void lightPath(Point p){
Mosaic.setColor(p.a, p.b, Color.BLUE);
}
public static void showSolution(){
for (int i = 0; i < route.length; i++){
Mosaic.setColor(route[i][0], route[i][1], Color.GREEN);
Mosaic.delay(100);
}
}
}