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 pathSnhaik.java
210 lines (209 loc) · 5.12 KB
/
Snhaik.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
import java.util.Timer;
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 Snhaik {
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 + 1) {
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
PlaceFood();
} 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);
}
checkL();
};
public static void main(String args[]) throws InterruptedException {
if (args.length == 1) {
System.out.println("Player 1 Controls:");
System.out.println(" w\nasd\n");
System.out.println("Player 2 Controls:");
System.out.println("lol it's just return / enter");
System.out.println("Player 1 should try to get as long as possible");
System.out.println("\nPlayer 2 should try to get Player 1 to commit git");
} else {
clear();
top();
dO(row, N);
bottom();
cPOS(N, 2);
move(0, -1);
move(0, -1);
move(0, -1);
PlaceFood();
while(true) {
doupdate.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();
}
}
/** Prints a VT100 Sequence */
public static void pvt100(String str) {
System.out.print(String.format("\033" + str));
}
/** Clear the terminal */
public static void clear() {
pvt100("[2J");
}
/** Cursor to top left */
public static void cTL() {
pvt100("[H");
}
/** Cursor position */
public static void cPOS(int y, int x) {
pvt100("[" + (x + 1) + ";" + (y + 1) + "H");
}
/** Cursor to bottom left */
public static void cBL() {
pvt100("[H");
}
/** Reset the cursor position */
public static void resetCPOS() {
cPOS(0, N + 3);
}
/** overWrite sets the cursor to a certain position and then prints the String provided*/
public static void overWrite(int x, int y, String s) {
cPOS(x, y);
System.out.print(s);
resetCPOS();
}
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);
}
/** Writes a Food character to the terminal, not used */
public static void PlaceFood() {
int fx = (int) Math.floor(Math.random() * W) + 1;
int fy = (int) Math.floor(Math.random() * N) + 1;
if (checkK(fx, fy)) {
PlaceFood();
} else {
P = new Point(fx, fy);
overWrite(fx, fy, foond);
}
}
public static boolean checkK(int x, int y) {
int s = A.size() - 1;
while(s --> 0) {
Point p = A.get(s);
int px = (int) p.getX();
int py = (int) p.getY();
if (px == x && py == y) {
return true;
}
}
return false;
}
public static void checkL() {
if (checkK(X, Y)) {
bean();
}
}
}