Community challenge - Compact programs!

Compact code challenge

Hi!
I’ve always been obsessed with shortening the code. Even if it becomes unreadable. It was always fun trying to make the code as short as possible!

So I decided to create this post. In this post, someone (usually me, feel free to do the same), will post a challenge. In the challenge, you need to specify what the challenge is and it’s requirements.

Requirements:

  • game must be playable (there is no reason to post a 15 lines long snake that can’t be played and enjoyed)
  • in a visible area of the post, you need to show the number of lines your program has, along with the mode you used to make it (Java mode / Python mode,…)
  • lines are counted AFTER you format the code in default processing editor (Ctrl + t)
  • the code must be provided in a [details=""] </> [/details] format
  • the submission must follow all of the requirements the host (who issued the challenge) specified
  • you can add any feature to the challenge, however, the total number of lines still count
  • you can submit as many tries as you want, but all of them must be a reply to the challenge post

When issuing a challenge:

  • clearly specify the requirements for the challenge
  • provide your take on the challenge (optional)

If you have any suggestions/questions please ask.

I hope you have fun!

#1 - Snake program
image

Requirements
  • Create a snake game
  • The grid must be resizable (default: 10x10)
  • The window must be resizable (scale can be preset)
  • The program will stop if the snake hits its tail / hits a wall
  • Obstacles are not necessary
  • Randomly generated fruit - A red square - that increases the length of the snake by 1, gets eaten if over any part of the snake
My code

Java mode - 23 lines of code
Instructions: you need to press any arrow key to start (the screen will be black) and press them again to move

ArrayList<PVector> snake = new ArrayList<PVector>();
int w = 10, h = 10, scl = 60, len = 3, px = 3, py = 3, x = 5, y = 5, end = -1, screenW = 600, screenH = 600;
void draw() { //press any arrow key to start
  if (width != screenW || height != screenH) surface.setSize(600, 600);
  snake.add(new PVector(x, y, 0));
  background(((end == -1)? color(0) : color(200, 0, 0)));
  for (int i = 0; i < snake.size(); i++) {
    if (snake.get(i).z > len-2) snake.remove(i);
    else snake.set(i, new PVector(snake.get(i).x, snake.get(i).y, snake.get(i).z+1));
    drawSquare(snake.get(i).x, snake.get(i).y, color(map(snake.get(i).z, 0, len, 255, 0)));
    if (px==snake.get(i).x && py == snake.get(i).y) for (int set = 0; set < 1; set++, px = floor(random(w)), py = floor(random(random(h))), len++);
    else drawSquare(px, py, color(255, 0, 0));
    for (int j = 0; j < snake.size(); j++) if (i != j) if (snake.get(i).x == snake.get(j).x && snake.get(i).y == snake.get(j).y) end = 1;
  }
  noLoop();
}
void drawSquare(float x, float y, color clr) {
  fill(clr);
  rect(x*scl, y*scl, scl, scl);
}
void keyPressed() {
  for (int abc = 0; abc < 1; abc++, end = ( ((x + ((((keyCode == LEFT)? -1 : ((keyCode == RIGHT)? 1 : 0)))) != constrain(x+((((keyCode == LEFT)? -1 : ((keyCode == RIGHT)? 1 : 0)))), 0, w-1) || y +((((keyCode == UP)? -1 : ((keyCode == DOWN)? 1 : 0)))) != constrain(y+((((keyCode == UP)? -1 : ((keyCode == DOWN)? 1 : 0)))), 0, h-1))? 1 : end)), x = ((end != 1)? (x+(((keyCode == LEFT)? -1 : ((keyCode == RIGHT)? 1 : 0)))) : x), y = ((end != 1)? (y+(((keyCode == UP)? -1 : ((keyCode == DOWN)? 1 : 0)))) : y)) if (end != 1) loop();
}