Hey! do you know the game snake? well my professor asked us to develop it in c++!
It`s crazy!Although he said write a simple program but I`m still confused!Can someone pleassssssssse help me with it?
The snake should eat and Its length and score should increase and also do not stick with walls!!!
I have already write something! It just increases Its length!

>I have already write something!
If you have some code, then post it so we can help.

#include <stdlib.h>

#include <stdio.h>

#include <conio.h>

#include<time.h>

void draw_line(int col, int row);

void show_score();

void add_segment();

void setup_level();

int randomize(int i);

void speed(int sec);

/* constants */

const int maxrow=20, maxcol=75;

const int snake_start_col=5,snake_start_row=5;

const char up_key='w', down_key='s', left_key='a', right_key='d';

/* global variables */

int score, snake_length  ;

char screen_grid[maxrow][maxcol];

char direction = right_key;

struct snake_segment {

 int row,col;

} snake[50];

int main()
{

       setup_level();
       draw_line(1,1);

 /* Variable declarations within main() only */

 char keypress;



 do /* restart game loop */
 {

         score=0;

        /* main loop */

        do
        {


               /* If key has been hit, then check it is a direction key - if so,

                       change direction */

               if (kbhit())
               {

                 keypress=(char)getch();

                 if((keypress==right_key)||(keypress==left_key)||

                         (keypress==up_key)||(keypress==down_key))

                        direction = keypress;

               }



               /* Add a segment to the end of the snake */

               add_segment();



               /* Blank last segment of snake */



               /* ... and remove it from the array */

                 for (int s=1;s<=snake_length;s++)

                        snake[s-1]=snake[s];


               for (int j=0;j<=snake_length;j++)
               {
                        speed(1);
                        for(int g=0;g<=j;g++)
                        {
                        if(direction==right_key)
                       {
                               gotoxy(snake[g+1].col+1,snake[g].row);
                               printf("O");
                               gotoxy(snake[g-1].col-1,snake[g].row);
                               printf(" ");
                       }


                       else if(direction== down_key)
                       {
                               gotoxy(snake[g].col,snake[g+1].row+1);
                               printf("O");
                               gotoxy(snake[g].col,snake[g-1].row-1);
                               printf(" ");

                }

                        else if (direction==up_key)
                        {
                               gotoxy(snake[g].col,snake[g-1].row-1);
                               printf("O");
                               gotoxy(snake[g].col,snake[g+1].row+1);
                               printf(" ");

                        }

                else if (direction==left_key)
                {
                       gotoxy(snake[g-1].col-1,snake[g].row);
                       printf("O");
                       gotoxy(snake[g+1].col+1,snake[g].row);
                       printf(" ");

                }
                 }

               if ( (snake[j].row==maxrow)||(snake[j].row==1)||(snake[j].col==maxcol)||(snake[j].col==1)||
 (snake[snake_length].row)==(snake[j].row) &&(snake[snake_length].col)==(snake[j].col))

               {

                       break;

                       printf("break");
               }

                 if (screen_grid[snake[j].row][snake[j].col]=='X')
               {


                 score+=250;
                 show_score();
                 snake_length++;
                 add_segment();


               }

        }


               /* Collision detection - snake (bad!) */



               /* Collision detection - food (good!) */


        } while (keypress!='x');

        show_score();

 } while(keypress!='x');

}


 void setup_level()
{



 /* variables local to setup_level() */

 int row,col;



 /* Set up global variables for new level */

 snake_length='4';




 /* Fill grid with Xs  */

 for(int i=0;i< score+2 ;i++)
 {

       row= randomize(i++);

       col= randomize(i+3);

       gotoxy(col,row++);

       printf("X");

 }



 /* Create snake array of length snake_length */

 for(int m=0;m<snake_length;m++)
 {

        snake[m].row=snake_start_row;

        snake[m].col=snake_start_col;

 }



 /* Draw playing board */


 show_score();


}






void draw_line(int colm, int row)
{

 for (colm=0;colm<maxcol;colm++)
 {
               gotoxy(colm,1);
               printf("-");
       }

 for (colm=0;colm<maxcol;colm++)
 {
               gotoxy(colm,maxrow);
               printf("-");
       }

 for(row=0;row<maxrow;row++)
 {
         gotoxy(1,row);
         printf("|");
       }

        for(row=0;row<maxrow;row++)
       {
                gotoxy(maxcol,row);
                printf("|");
        }
}



void show_score()
{



 gotoxy(10,maxrow+4);

 printf("up_key='w', down_key='s', left_key='a', right_key='d'");

 gotoxy(28,maxrow+5);

 printf("press 'x' to stop");

 gotoxy(30,maxrow+2);

 printf("Score: %05d",score);

}
void add_segment()
{

 switch(direction)
 {

        case(right_key): snake[snake_length].row=snake[snake_length-1].row;

                                                       snake[snake_length].col=snake[snake_length-1].col+1;

                                                       break;

        case(left_key) : snake[snake_length].row=snake[snake_length-1].row;

                                                       snake[snake_length].col=snake[snake_length-1].col-1;

                                                       break;

        case(up_key)   : snake[snake_length].row=snake[snake_length-1].row-1;

                                                       snake[snake_length].col=snake[snake_length-1].col;

                                                       break;

        case(down_key) : snake[snake_length].row=snake[snake_length-1].row+1;

                                                       snake[snake_length].col=snake[snake_length-1].col;

                                                       break;
 }

}

int randomize(int i)
{
       return i*i+4;
}


void speed(int sec)
        {
        clock_t timesec;
        timesec = clock();
        while((clock() - timesec) < sec);
        }

Even after formatting this code, I can't make much sense of it. Just to get it to compile was hard enough, as you've used many non-standard functions. Here is the code I have (green parts are what I added to allow it to compile)

#include <windows.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <time.h>

void draw_line(int col, int row);
void show_score();
void add_segment();
void setup_level();
int randomize(int i);
void speed(int sec);

void gotoxy(int x, int y) {
  static HANDLE hOut = GetStdHandle( STD_OUTPUT_HANDLE );
  COORD c = {x, y};
  SetConsoleCursorPosition( hOut, c );
}

/* constants */
const int maxrow = 20,
          maxcol = 75;

const int snake_start_col = 5,
          snake_start_row = 5;

const char up_key    ='w',
           down_key  ='s',
           left_key  ='a',
           right_key ='d';

/* global variables */
int score, snake_length;
char screen_grid[maxrow][maxcol];
char direction = right_key;

struct snake_segment {
  int row, col;
} snake[50];

int main()
{
  setup_level();
  draw_line(1, 1);

  /* Variable declarations within main() only */
  char keypress = 0;

  do /* restart game loop */
  {
    score = 0;

    do /* main loop */
    {
      /* If key has been hit, then check it is a direction key - if so, change direction */
      if ( GetAsyncKeyState(VK_LEFT)  ) keypress = left_key;  else
      if ( GetAsyncKeyState(VK_RIGHT) ) keypress = right_key; else
      if ( GetAsyncKeyState(VK_UP)    ) keypress = up_key;    else
      if ( GetAsyncKeyState(VK_DOWN)  ) keypress = down_key;


      /* Add a segment to the end of the snake */
      add_segment();

      /* Blank last segment of snake */
      /* ... and remove it from the array */

      for (int s = 1; s <= snake_length; s++)
        snake[s-1] = snake[s];

      for (int j = 0; j <= snake_length; j++) {
        speed(1);
        for (int g = 0; g <= j; g++) {
          if ( direction == right_key ) {
            gotoxy(snake[g+1].col+1, snake[g].row);
            printf("O");
            gotoxy(snake[g-1].col-1, snake[g].row);
            printf(" ");
          }

          else if ( direction == down_key ) {
            gotoxy(snake[g].col, snake[g+1].row+1);
            printf("O");
            gotoxy(snake[g].col, snake[g-1].row-1);
            printf(" ");

          }

          else if ( direction == up_key ) {
            gotoxy(snake[g].col, snake[g-1].row-1);
            printf("O");
            gotoxy(snake[g].col, snake[g+1].row+1);
            printf(" ");

          }

          else if ( direction == left_key ) {
            gotoxy(snake[g-1].col-1, snake[g].row);
            printf("O");
            gotoxy(snake[g+1].col+1, snake[g].row);
            printf(" ");

          }
        }

        if ( (snake[j].row == maxrow) || (snake[j].row == 1)||
             (snake[j].col == maxcol) || (snake[j].col == 1)||
             (snake[snake_length].row) == (snake[j].row) &&
             (snake[snake_length].col) == (snake[j].col) ) {
          break;
          printf("break");
        }

        if ( screen_grid[snake[j].row][snake[j].col] == 'X' )
        {
          score += 250;
          show_score();
          snake_length++;
          add_segment();
        }
      }


      /* Collision detection - snake (bad!) */
      /* Collision detection - food (good!) */


    } while (keypress != 'x');

    show_score();
  } while(keypress!='x');

}


void setup_level()
{
  /* variables local to setup_level() */
  int row,col;

  /* Set up global variables for new level */
  snake_length='4';

  /* Fill grid with Xs  */
  for (int i = 0; i < score + 2; i++) {
    row = randomize(i++);
    col = randomize(i+3);

    gotoxy(col,row++);

    printf("X");
  }

  /* Create snake array of length snake_length */
  for (int m = 0; m < snake_length; m++) {
    snake[m].row=snake_start_row;
    snake[m].col=snake_start_col;
  }

  /* Draw playing board */
  show_score();
}

void draw_line(int colm, int row)
{
  for (colm = 0; colm < maxcol; colm++) {
    gotoxy(colm, 1);
    printf("-");
  }

  for (colm = 0; colm < maxcol; colm++) {
    gotoxy(colm, maxrow);
    printf("-");
  }

  for(row = 0; row < maxrow; row++) {
    gotoxy(1, row);
    printf("|");
  }

  for(row=0;row<maxrow;row++) {
    gotoxy(maxcol, row);
    printf("|");
  }
}

void show_score()
{
  gotoxy(10, maxrow + 4);
  printf("up_key='w', down_key='s', left_key='a', right_key='d'");

  gotoxy(28, maxrow + 5);
  printf("press 'x' to stop");

  gotoxy(30, maxrow + 2);
  printf("Score: %05d", score);

}
void add_segment()
{
  switch(direction) {
    case right_key:
      snake[snake_length].row = snake[snake_length-1].row;
      snake[snake_length].col = snake[snake_length-1].col + 1;
      break;

    case left_key:
      snake[snake_length].row = snake[snake_length-1].row;
      snake[snake_length].col = snake[snake_length-1].col - 1;
      break;

    case up_key:
      snake[snake_length].row=snake[snake_length-1].row - 1;
      snake[snake_length].col=snake[snake_length-1].col;
      break;

    case down_key:
      snake[snake_length].row=snake[snake_length-1].row + 1;
      snake[snake_length].col=snake[snake_length-1].col;
      break;
  }
}

int randomize(int i)
{
  return i * i + 4;
}


void speed(int sec)
{
  clock_t timesec;
  timesec = clock();
  while ((clock() - timesec) < sec);
}

Either way, the output is way off for me, so I don't know what exact problems your having, so I can't help. The best I can do is to say you start over, use standard functions, and structure the game well.

This article has been dead for over six months. Start a new discussion instead.