# The Failure of the Snake

For this week assignment, I decided to build the classic Snake game that used to appear in all Nokia phones before the smartphones were invented. The Idea seemed simple at the beginning to create class Snake with the proper methods that allow the snake to move in a specific direction, eat the apple and increase its size and die if the snake clash against itself.  Creating the methods to move around and eat an apple were simple to make. The struggle came trying to build an increasing size method. When I was planning my algorithm I forgot that arrays cannot increase its size once the code is running, so the solution that I attempted to do was using ArrayList instead of arrays to keep the vector position of the snake. Unfortunately,  I was not able to make ArrayList work the way I wanted, Although I learned how to add and remove elements from ArrayList I did not find a function that Allowed to overwrite the values from the list without altering its size. And when I gave up trying to use ArrayList it was too late to design a whole new algorithm. But I decided to make a piece of art with the leftovers of my snake code.

Credits to this video of the  CodingTrain who helped me to be closer to achieving the game that I wanted to create. But if the purpose was to improve my OOP, it definitely worked.

```int scl = 20;
PVector apple;

////////////////////////////////////OPP
class Snake{
int xspeed, yspeed, xdir, ydir;
int x,y;
int total;
PVector vec ;
ArrayList<PVector> tail;

Snake(){
x = 0;
y = 0;
xspeed = 0;
yspeed = 0;
total = 0;
tail = new ArrayList();
}

void update(){

if (total == tail.size()){
for(int i = 0; i<tail.size();i++){
tail.remove(i);

}
}
vec = new PVector(x,y);

for (int i = 0; i< tail.size();i++){
print("Snake",tail.get(i));
}

x = x+xspeed*scl;
y = y+yspeed*scl;
x = constrain(x,0,width -scl);
y = constrain(y,0,height -scl);
}

void show(){

println("total",total);
println("size",tail.size());
for(int i = 0; i<tail.size();i++){
fill(random(255),random(100),random(50));
rect(tail.get(i).x,tail.get(i).y,scl,scl);
}
fill(random(255),random(100),random(50));
rect(x,y,scl,scl);

}
void setdir(int xpass,int ypass){
xspeed = xpass;
yspeed = ypass;

}

boolean eat(PVector pos){
float dis = dist(x,y,pos.x,pos.y);
if(dis<2){
total++;

return true ;
}
else{
return false ;
}
}

}
////////////////////////Main///////////////////////
Snake snake;
void setup(){
size(640, 480);
snake = new Snake();
frameRate(600);
pickLocation();

}

void pickLocation(){
int cols = int(width/scl);
int rows = int(height/scl);
apple = new PVector(int(random(cols)),int( random(rows)));
apple.mult(scl);
}

void keyPressed(){
int four = int(random(4));
if(four == 0){
snake.setdir(0,-1);
}
else if(four == 1){
snake.setdir(0,1);
}
else if(four == 2){
snake.setdir(-1,0);
}
else if(four == 3){
snake.setdir(1,0);
}
}

void draw(){
background(255);
keyPressed();
snake.update();
snake.show();

if(snake.eat(apple)){
pickLocation();
}

fill(254,0,101);
rect(apple.x, apple.y, scl,scl);
}

```