This repository was archived by the owner on Jul 24, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSnaik.java
185 lines (183 loc) · 4.37 KB
/
Snaik.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
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
import java.util.Timer;
import java.util.TimerTask;
import java.util.Scanner;
import java.util.function.*;
import java.util.ArrayList;
import java.awt.Point;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
public class Snaik {
private static int N = 20;
private static int W = N * 2;
private static String TL = "┌";
private static String TR = "┐";
private static String BL = "└";
private static String BR = "┘";
private static String V = "│";
private static String H = "─";
private static String F = ".";
private static String B = "█";
private static Point P;
private static String foond = "%";
private static String D = "N";
private static int X = N;
private static int Y = N;
private static int L = 3;
private static ArrayList<Point> A = new ArrayList(L);
private static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
private static Runnable row = () -> {
System.out.print(V);
prepeat(F, W);
System.out.println(V);
};
private static void bean() {
beanEnd();
overWrite(X, Y, "#");
System.exit(1);
}
private static Runnable doupdate = () -> {
wDir();
if (D == "N") {
move(0, -1);
if (Y == 0) {
bean();
}
} else if (D == "E") {
move(1, 0);
if (X == W) {
bean();
}
} else if (D == "S") {
move(0, 1);
if (Y == N) {
bean();
}
} else if (D == "W") {
move(-1, 0);
if (X == 0) {
bean();
}
}
if (X == (int) P.getX() && Y == (int) P.getY()) {
// I'm too lazy to invert this
} else {
beanEnd();
}
try {
String s = in.readLine();
if (s.equalsIgnoreCase("quit")) {
System.exit(0);
} else if (s.equalsIgnoreCase("w")) {
D = "N";
} else if (s.equalsIgnoreCase("d")) {
D = "E";
} else if (s.equalsIgnoreCase("s")) {
D = "S";
} else if (s.equalsIgnoreCase("a")) {
D = "W";
}
} catch (IOException lASDKJASLKDALJSKDASLJDK) {
System.exit(1);
}
};
private static Timer timer = new Timer();
public static void main(String args[]) throws InterruptedException {
clear();
top();
dO(row, N);
bottom();
cPOS(N, 2);
move(0, -1);
move(0, -1);
move(0, -1);
PlaceFood();
timer.schedule(wrap(doupdate), 100, 400);
}
private static TimerTask wrap(Runnable r) {
return new TimerTask() {
@Override
public void run() {
r.run();
}
};
}
public static void prepeat(String s, int i) {
String res = "";
while (i --> 0) {
res += s;
}
System.out.print(res);
}
public static void lf() {
System.out.println();
}
public static void top() {
System.out.print(TL);
prepeat(H, W);
System.out.println(TR);
}
public static void bottom() {
System.out.print(BL);
prepeat(H, W);
System.out.println(BR);
}
public static void makeBox() {
System.out.println();
}
public static void dO(Runnable it, int n) {
while(n --> 0) {
it.run();
}
}
public static void pvt100(String str) {
System.out.print(String.format("\033" + str));
}
public static void clear() {
pvt100("[2J");
}
public static void cTL() {
pvt100("[H");
}
public static void cPOS(int y, int x) {
pvt100("[" + (x + 1) + ";" + (y + 1) + "H");
}
public static void cBL() {
pvt100("[H");
}
public static void resetCPOS() {
cPOS(0, N + 3);
}
public static void overWrite(int x, int y, String s) {
// it overwrites things
cPOS(x, y);
System.out.print(s);
resetCPOS();
}
// public static void underWrite(int dx, int dy, String s) {
// overWrite(X + dx, Y + dy, s);
// }
public static void move(int dx, int dy) {
X = X + dx;
Y = Y + dy;
A.add(new Point(X, Y));
overWrite(X, Y, B);
}
public static void beanEnd() {
Point P = A.get(0);
A.remove(0);
int cx = (int) Math.round(P.getX());
int cy = (int) Math.round(P.getY());
overWrite(cx, cy, F);
}
public static void wDir() {
overWrite(W, N + 2, D);
}
public static void PlaceFood() {
int fx = (int) Math.floor(Math.random() * W);
int fy = (int) Math.floor(Math.random() * N);
P = new Point(fx, fy);
System.out.print(String.valueOf(fx) + String.valueOf(fy));
overWrite(fx, fy, foond);
}
}