Commit 5f66f756 authored by ufebl's avatar ufebl Committed by uwdkn

Add more library classes

parent 5110816d
pushq %rbp
movq %rsp, %rbp
subq $816, %rsp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $8, %rsp
\ No newline at end of file
import lib.BufferedReader;
class Echo {
public static void main(String[] args) throws Exception {
BufferedReader reader = new BufferedReader().init();
Str str = reader.readLine();
while (str != null) {
str.println();
str = reader.readLine();
}
}
}
\ No newline at end of file
GET / HTTP/1.1
Host: localhost:8080
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:63.0) Gecko/20100101 Firefox/63.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
DNT: 1
Connection: keep-alive
Upgrade-Insecure-Requests: 1
Cache-Control: max-age=0
GET /favicon.ico HTTP/1.1
Host: localhost:8080
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:63.0) Gecko/20100101 Firefox/63.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
DNT: 1
Connection: keep-alive
import lib.HttpServer;
class EchoServer {
public static void main(String[] args) throws Exception {
Application app = new Application();
new HttpServer().init(app).run();
}
}
class Application {
public HttpServer server;
public void init(HttpServer server) {
this.server = server;
}
public void processRequest(Request request) {
Str str1 = new Str().init11(89, 111, 117, 32, 109, 97, 100, 101, 32, 97, 32); /* You made a */
Str str2 = new Str().init12(32, 114, 101, 113, 117, 101, 115, 116, 32, 116, 111, 32); /* request to */
Str content = str1.concat(request.getMethod()).concat(str2).concat(request.getUrl());
Response response = new Response().init(200, content);
server.sendResponse(response);
}
}
\ No newline at end of file
GET / HTTP/1.0
GET /13 HTTP/1.0
GET /21 HTTP/1.0
\ No newline at end of file
import lib.HttpServer;
import lib.Str;
import lib.HtmlBuilder;
class FibServer {
public static void main(String[] args) throws Exception {
Application app = new Application();
new HttpServer().init(app).run();
}
}
class Application {
public HttpServer server;
public int count;
public void init(HttpServer server) {
this.server = server;
}
public void processRequest(Request request) {
HtmlBuilder builder = new HtmlBuilder();
HtmlElement html = builder.html();
HtmlElement header = html.head();
HtmlElement body = html.body();
Str fibonacci = new Str().init9(70, 105, 98, 111, 110, 97, 99, 99, 105); /* Fibonacci */
header.title().text(fibonacci);
if (request.getUrl().equals(new Str().init1(47))) { /* / */
body.h(1).text(fibonacci);
body.p().text(new Str().init20(87, 104, 105, 99, 104, 32, 70, 105, 98, 111, 110, 97, 99, 99, 105, 32, 110, 117, 109, 98)
.concat(new Str().init20(101, 114, 32, 100, 111, 32, 121, 111, 117, 32, 119, 97, 110, 116, 32, 116, 111, 32, 107, 110)
.concat(new Str().init3(111, 119, 63)))); /* Which Fibonacci number do you want to know? */
int nr = 0;
while (nr <= 40) {
Str intStr = new Str().fromInt(nr);
HtmlElement a = body.a(new Str().init1(47).concat(intStr));
a.text(intStr);
body.br();
nr = nr + 1;
}
} else {
/* parse the part behind the url as a number */
Str numberStr = request.getUrl().substring(1, request.getUrl().length);
int number = numberStr.toInt();
Str title = new Str().init20(84, 104, 101, 32, 70, 105, 98, 111, 110, 97, 99, 99, 105, 32, 110, 117, 109, 98, 101, 114)
.concat(new Str().init4(32, 111, 102, 32)); /* The fibonacci number of */
body.h(1).text(title.concat(numberStr).concat(new Str().init4(32, 105, 115, 58))); /* is: */
body.text(new Str().fromInt(new Fib().fib(number)));
body.br();body.br();body.br();
HtmlElement a = body.a(new Str().init1(47));
a.text(new Str().init20(67, 97, 108, 99, 117, 108, 97, 116, 101, 32, 97, 110, 111, 116, 104, 101, 114, 32, 111, 110).concat(new Str().init1(101))); /* Calculate another one */
}
Str content = html.buildToString();
Response response = new Response().init(200, content);
server.sendResponse(response);
}
}
class Fib {
public int fib(int n) {
if(n < 2) {
return n;
}
return fib(n - 1) + fib(n - 2);
}
}
File mode changed from 100755 to 100644
import lib.Str;
class HelloWord {
public static void main(String[] args) {
new Str().init12(72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 33).println(); /* Hello World! */
}
}
\ No newline at end of file
import lib.AnsiTerminal;
import lib.TerminalInput;
import lib.BooleanMatrix;
import lib.LCG;
import lib.Math;
import lib.Str;
/**
* A simple snake game.
*
* Use the wrapper to get the full experience:
* https://git.scc.kit.edu/ufebl/mjtest-scripts/blob/master/mj_terminal.sh
*/
class Game {
public AnsiTerminal terminal;
public TerminalInput input;
public int x;
public int y;
public BooleanMatrix snakeField;
public SnakeElement tail;
public BooleanMatrix appleField;
public LCG random;
public int score;
public int direction;
public int nextDirection;
public int gameOver; /* game over counter */
public Game init(AnsiTerminal terminal) {
this.terminal = terminal;
return this;
}
public void setInput(TerminalInput input) {
this.input = input;
}
public void setup() {
x = 1;
y = 1;
direction = 1;
nextDirection = 1;
terminal.set24BitBgColor(0, 0, 10);
terminal.clearScreen();
terminal.conceal();
random = new LCG().init(input.getColumns() * input.getLines());
snakeField = new BooleanMatrix().init(input.getColumns(), input.getLines());
appleField = new BooleanMatrix().init(input.getColumns(), input.getLines());
tail = new SnakeElement().init(x, y);
printSnakeHead();
right();
printSnakeHead();
right();
printSnakeHead();
int i = 0;
int appleCount = new Math().min(input.getColumns(), input.getLines()) / 2;
while (i < appleCount) {
spawnApple();
i = i + 1;
}
printScore();
terminal.flush();
}
public void printSnakeHead() {
snakeField.set(x - 1, y - 1, true);
terminal.setCursor(x, y);
terminal.set24BitBgColor(0, 230, 0);
System.out.write(78);
terminal.setCursor(x, y);
tail.append(new SnakeElement().init(x, y));
}
public void eraseSnakeTail() {
snakeField.set(tail.getX() - 1, tail.getY() - 1, false);
terminal.setCursor(tail.getX(), tail.getY());
terminal.set24BitBgColor(0, 0, 10);
System.out.write(78);
tail = tail.getNext(); /* eleminate one element */
}
public void spawnApple() {
int x = random.next() % input.getColumns() + 1;
int y = random.next() % input.getLines() + 1;
if (!snakeField.get(x - 1, y - 1)) {
/* place free to spawn here */
appleField.set(x - 1, y - 1, true);
terminal.bold();
terminal.notConceal();
terminal.set24BitFgColor(230, 70, 0);
terminal.set24BitBgColor(0, 0, 10);
terminal.setCursor(x, y);
System.out.write(111); /* o */
terminal.setCursor(x, y); /* set cursor back to snake position */
terminal.conceal();
terminal.normalWeight();
terminal.fgDefault();
}
}
public void printScore() {
terminal.setCursor(2, 2);
terminal.fgDefault();
terminal.set24BitBgColor(0, 0, 10);
terminal.notConceal();
new Str().fromInt(score).print();
terminal.conceal();
}
public void keyPressed(int key) {
if (key == input.LEFT) {
if (direction != 1) {
nextDirection = 0;
}
} else if (key == input.RIGHT) {
if (direction != 0) {
nextDirection = 1;
}
} else if (key == input.UP) {
if (direction != 3) {
nextDirection = 2;
}
} else if (key == input.DOWN) {
if (direction != 2) {
nextDirection = 3;
}
} else if (key == input.ESCAPE) {
quit();
return;
} else if (key == input.TICK) {
if (gameOver > 0) {
gameOver = gameOver + 1;
if (gameOver > 20) {
quit();
}
return;
}
direction = nextDirection;
if (direction == 0) {
left();
} else if (direction == 1) {
right();
} else if (direction == 2) {
up();
} else {
down();
}
if (appleField.get(x - 1, y - 1)) {
appleField.set(x - 1, y - 1, false);
/* don't erase the tail -> the snake's size increases by one */
score = score + 1;
printScore();
spawnApple();
} else {
eraseSnakeTail();
}
if (random.next() % 100 == 0) {
spawnApple();
}
if (snakeField.get(x - 1, y - 1)) {
/* GAME OVER */
gameOver = 1;
terminal.set24BitBgColor(255, 0, 0);
terminal.setCursor(x, y);
System.out.write(32);
terminal.setCursorToOrigin();
terminal.flush();
return;
}
printSnakeHead();
terminal.flush();
}
}
public void left() {
x = x - 1;
if (x <= 0) {
x = input.getColumns();
}
}
public void right() {
x = x + 1;
if (x > input.getColumns()) {
x = 1;
}
}
public void up() {
y = y - 1;
if (y <= 0) {
y = input.getLines();
}
}
public void down() {
y = y + 1;
if (y > input.getLines()) {
y = 1;
}
}
public void quit() {
terminal.resetColor();
terminal.clearScreen();
terminal.setCursorToOrigin();
terminal.flush();
input.quit();
}
public static void main(String[] args) throws Exception {
AnsiTerminal terminal = new AnsiTerminal();
Game game = new Game().init(terminal);
InputReciever inputReciever = new InputReciever().initGame(game);
new TerminalInput().init(terminal, inputReciever).handleInput();
}
}
class SnakeElement {
public int x;
public int y;
public SnakeElement next;
public SnakeElement init(int x, int y) {
this.x = x;
this.y = y;
return this;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public SnakeElement getNext() {
return next;
}
public void append(SnakeElement snakeElement) {
if (next == null) {
next = snakeElement;
} else {
next.append(snakeElement);
}
}
}
class InputReciever {
public Game game;
public InputReciever initGame(Game game) {
this.game = game;
return this;
}
public void init(TerminalInput terminalInput) {
game.setInput(terminalInput);
game.setup();
}
public void keyPressed(int key) {
game.keyPressed(key);
}
}
This diff is collapsed.
This diff is collapsed.
import lib.AsciiPgmImage;
class Test {
public static void main(String[] args) throws Exception {
AsciiPgmImage image = new AsciiPgmImage().readFromStdin();
new GaussianBlur().init().applyToImage(image).printToStdout();
}
}
class GaussianBlur {
public int kernelSize;
public int[][] kernel;
public int kernelDivisor;
public GaussianBlur initSmall() {
kernelSize = 5;
kernelDivisor = 256;
kernel = new int[kernelSize][];
kernel[0] = line5(1, 4, 6, 4, 1);
kernel[1] = line5(4, 16, 24, 16, 4);
kernel[2] = line5(6, 24, 36, 24, 6);
kernel[3] = line5(4, 16, 24, 16, 4);
kernel[4] = line5(1, 4, 6, 4, 1);
return this;
}
public int[] line5(int a, int b, int c, int d, int e) {
int[] result = new int[5];
result[0] = a;
result[1] = b;
result[2] = c;
result[3] = d;
result[4] = e;
return result;
}
public GaussianBlur init() {
kernelSize = 7;
kernelDivisor = 4096;
kernel = new int[kernelSize][];
kernel[0] = line7( 1, 6, 15, 20, 15, 6, 1);
kernel[1] = line7( 6, 36, 90, 120, 90, 36, 6);
kernel[2] = line7(15, 90, 225, 300, 225, 90, 15);
kernel[3] = line7(20, 120, 300, 400, 300, 120, 20);
kernel[4] = line7(15, 90, 225, 300, 225, 90, 15);
kernel[5] = line7( 6, 36, 90, 120, 90, 36, 6);
kernel[6] = line7( 1, 6, 15, 20, 15, 6, 1);
return this;
}
public int[] line7(int a, int b, int c, int d, int e, int f, int g) {
int[] result = new int[7];
result[0] = a;
result[1] = b;
result[2] = c;
result[3] = d;
result[4] = e;
result[5] = f;
result[6] = g;
return result;
}
public AsciiPgmImage applyToImage(AsciiPgmImage image) {
AsciiPgmImage newImage = new AsciiPgmImage();
newImage.width = image.width;
newImage.height = image.height;
newImage.maxValue = image.maxValue;
newImage.data = new int[image.width * image.height];
int y = 0;
while (y < image.height) {
int x = 0;
while (x < image.width) {
newImage.data[x + y * image.width] = calcValueAtPixel(image, x, y);
x = x + 1;
}
y = y + 1;
}
return newImage;
}
public int calcValueAtPixel(AsciiPgmImage image, int x, int y) {
int val = 0;
int cY = 0;
while (cY < kernelSize) {
int cX = 0;
while (cX < kernelSize) {
val = val + kernel[cY][cX] * get(image, x + cX - kernelSize/2, y + cY - kernelSize/2);
cX = cX + 1;
}
cY = cY + 1;
}
return val / kernelDivisor;
}
public int get(AsciiPgmImage image, int x, int y) {
if (x < 0) {
x = 0;
}
if (y < 0) {
y = 0;
}
if (x >= image.width) {
x = image.width - 1;
}
if (y >= image.height) {
y = image.height - 1;
}
return image.get(x, y);
}
}
package lib;
/**
* This helps you output ANSI codes to make your terminal output fancy.
* see https://en.wikipedia.org/wiki/ANSI_excape_code#Escape_sequences
*/
public class AnsiTerminal {
public void flush() {
System.out.flush();
}
public void esc() {
System.out.write(27); /* ESC */
}
public void csi() {
esc();
System.out.write(91); /* [ */
}
public void reset() {
esc();
System.out.write(99); /* c */
}
public void cursorUp() {
cursorUpN(1);
}
public void cursorUpN(int n) {
csi();
if (n != 1) {
new Str().fromInt(n).print();
}
System.out.write(65); /* A */
}
public void cursorDown() {
cursorDownN(1);
}
public void cursorDownN(int n) {
csi();
if (n != 1) {
new Str().fromInt(n).print();
}
System.out.write(66); /* B */
}
public void cursorForward() {
cursorForwardN(1);
}
public void cursorForwardN(int n) {
csi();
if (n != 1) {
new Str().fromInt(n).print();
}
System.out.write(67); /* C */
}
public void cursorBack() {
cursorBackN(1);
}
public void cursorBackN(int n) {
csi();
if (n != 1) {
new Str().fromInt(n).print();
}
System.out.write(68); /* D */
}
public void cursorNextLine() {
cursorNextLineN(1);
}
public void cursorNextLineN(int n) {
csi();
if (n != 1) {
new Str().fromInt(n).print();
}
System.out.write(69); /* E */
}
public void cursorPreviousLine() {
cursorPreviousLineN(1);
}
public void cursorPreviousLineN(int n) {
csi();
if (n != 1) {
new Str().fromInt(n).print();
}
System.out.write(70); /* F */
}
public void cursorSetColumn(int n) {
csi();
if (n != 1) {
new Str().fromInt(n).print();
}
System.out.write(71); /* G */
}
public void setCursorToOrigin() {
setCursor(1, 1);
}
/**
* Set the position of the cursor. Top left is 1,1.
*/
public void setCursor(int x, int y) {
csi();
if (y != 1) {
new Str().fromInt(y).print();
}
if (x != 1 || y != 1) {
System.out.write(59); /* ; */
}
if (x != 1) {
new Str().fromInt(x).print();
}
System.out.write(72); /* H */
}
public void eraseInDisplay(int n) {
if (n < 4) {
csi();
System.out.write(48 + n);
System.out.write(74); /* J */
}
}
public void clearScreen() {
eraseInDisplay(2);
}
public void eraseInLine(int n) {
if (n < 3) {
csi();
System.out.write(48 + n);
System.out.write(75); /* K */
}
}
public void scrollUp(int n) {
csi();
if (n != 1) {
new Str().fromInt(n).print();
}
System.out.write(83); /* S */
}
public void scrollDown(int n) {