title: Snake Collision with self and apple
body: <p>So for my class I need to program this snake game as part of a project, my partner and I have run into an error where our code doesn't collide with itself or the apple. So for example if I go up, and press down it reverses itself and we're not quite sure how to fix that. We have an if statement testing to see if snake[1].y-1 = snake[0].y and we're wondering why the apple does not move when our statement collides with the snake.</p>
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;ncurses.h&gt;
  
  //Defines the rate at which the world refreshes
  #define TICKRATE 100
  //Defines the width of the world
  #define WIDTH 50
  //Defines the height of the world
  #define HEIGHT 20
  //Defines the length of the snake
  #define SNAKE_LNGTH 10
  
  //Defines an enum with the directions
  enum direction { UP, DOWN, RIGHT, LEFT };
  
  //Defines the structure &quot;Snake Part&quot; for the parts of the snake
  typedef struct spart {
      int x;
      int y;
  } snakepart;
  
  //Initalizes the function for moving the snake
  int move_snake(WINDOW *win, int direction,
                  snakepart snake[], snakepart app);
  
  //Initalizes the function for checking where the snake is moving
  int collision(snakepart snake[], snakepart app);
  
  //Main argument
  int main(int argc, char *argv[]) {
  
      //Establishes the Window for the snake world
      WINDOW *snake_wrld;
      //
      int offsetx, offsety, i, ch;
  
      initscr();
      noecho();
      cbreak();
      timeout(TICKRATE);
      keypad(stdscr, TRUE);
  
      printw(&quot;Classic Snake (Optimized for Linux/Unix Only)  -  Press q to quit...&quot;);
  
      refresh();
  
      //Offsets the screen to center of the window
      offsetx = (COLS - WIDTH) / 2;
      offsety = (LINES - HEIGHT) / 2;
  
      //Establishes windows height and width, with offset
      snake_wrld = newwin(HEIGHT,WIDTH,offsety,offsetx);
  
      //Creates snake with (x,y) cordinates and SNAKE_LNGTH
      snakepart snake[SNAKE_LNGTH];
      snakepart app;
  
      //Establishes apple with random
      app.x = rand() % (WIDTH-1);
      app.y = rand() % (HEIGHT-1);
  
      //Establishes snake creation location
      int sbegx = (WIDTH - SNAKE_LNGTH) / 2;
      int sbegy = (HEIGHT - 1) / 2;
  
      //Establishes each piece of the snake array
      for (i = 0; i &lt; SNAKE_LNGTH; i++) {
          snake[i].x = sbegx + i;
          snake[i].y = sbegy;
      }
  
      int cur_dir = RIGHT;
  
      while ((ch = getch()) != 'q') {
          //Calls the move_snake function
          move_snake(snake_wrld, cur_dir, snake, app);
          //collision();
          if(ch != ERR) {
              switch(ch) {
                  case KEY_UP:
                      if(snake[1].y-1 == snake[0].y) break;
                      cur_dir = UP;
                      break;
                  case KEY_DOWN:
                      if(snake[1].y+1 == snake[0].y) break;
                      cur_dir = DOWN;
                      break;
                  case KEY_RIGHT:
                      if(snake[1].x+1 == snake[0].y) break;
                      cur_dir = RIGHT;
                      break;
                  case KEY_LEFT:
                      if(snake[1].x-1 == snake[0].y) break;
                      cur_dir = LEFT;
                      break;
                  default:
                      break;
              }
  
          }
      }
  
      delwin(snake_wrld);
  
      endwin();
  
      return 0;
  
  }
  
  int move_snake(WINDOW *win, int direction,
                  snakepart snake[], snakepart app) {
  
      wclear(win);
  
      mvwaddch(win, app.y, app.x, '@');
      //collision(snake, app);
  
      for (int i = 0; i &lt; SNAKE_LNGTH - 1; i++) {
        if(snake[i].y == app.y &amp;&amp; snake[i].x == app.x){
          app.x = rand() % (WIDTH-1);
          app.y = rand() % (HEIGHT-1);
          mvwaddch(win, app.y, app.x, '@');
          //SnakeLength ++
        }
  
      for (int i = 0; i &lt; SNAKE_LNGTH - 1; i++) {
          snake[i] = snake[i + 1];
          mvwaddch(win, snake[i].y, snake[i].x, '$');
  
      }
  
      int x = snake[SNAKE_LNGTH - 1].x;
      int y = snake[SNAKE_LNGTH - 1].y;
      switch (direction) {
          case UP:
              y - 1 == 0 ? y = HEIGHT - 2 : y--;
              break;
          case DOWN:
              y + 1 == HEIGHT - 1 ? y = 1 : y++;
              break;
          case RIGHT:
              x + 1 == WIDTH - 1 ? x = 1 : x++;
              break;
          case LEFT:
              x - 1 == 0 ? x = WIDTH - 2 : x--;
              break;
          default:
              break;
      }
  
      snake[SNAKE_LNGTH - 1].x = x;
      snake[SNAKE_LNGTH - 1].y = y;
  
      mvwaddch(win, y, x, '$');
  
      box(win, 0 , 0);
  
      wrefresh(win);
  
      return 0;
  }
  
  int collision(snakepart snake[], snakepart app){
  
    }
  }
  </code></pre>