Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ Console Application Creating a Snake game Your project must make use of all

ID: 3776234 • Letter: C

Question

C++ Console Application

Creating a Snake game

Your project must make use of all of the following:

• Functions – the program needs to follow the standards from decomposition. You will divide the program up into separate pieces. Some goals:

divide and conquer

intellectual manageability

facilitate change and reuse

Functions are the characteristics of a good decomposition.

You must display passing by reference, and are required to pass your files or arrays by reference at a minimum in one of your functions.

• Arrays/Vectors OR File processing – the program should use some form of vector/array processing or input/output file processing, or both. It may be part of the structure for your in-memory database, it may be the display grid for your game, or you may weave the use of an array or file into your design in some clever way. Good ideas can be found fromour book, Herb Sutter’s "Exceptional C++: 47 Engineering Puzzles, Programming Problems and Solutions", 1999; "More Exceptional C++: 40 New Engineering Puzzles, Programming Problems and Solution", 2001, or even Google, etc… If you are not sure, please ask.

• Your program must implement the use of sorting or searching AND pointer processing. Dynamically allocating your array, for example, would serve as pointer processing. You could even combine both (such as sorting or searching using pointers). There are examples in the book, and of course beyond...

4. You may not use any other external libraries (e.g. for graphical interfaces). Generally a good rule of thumb is that if something was not covered in this course, you should probably not use it (if in doubt, email us!). Stick to a text interface.

5. Follow the guidelines from the RubricProgrammingforAssignments on Springboard. As part of that rubric remember that your program will follow the CSI 3460:209 guidelines as described:

Constants are declared with ALL CAPITAL LETTERS.

Each curly bracket is on a line by itself

Each level of indentation (such as the body of a function or the body of a loop) is indented either 3 or 4 extra spaces

Blank lines are used in a meaningful way

Formulas are nicely spaced with a space around every operator (except *, / and %), and a space after every comma in an argument list.

6. Include a section in your header comments with some basic documentation and instructions on how to use your program. Also, include in this documentation what problems you had during the design, testing, and development of yourproject, what the challenges were, and what would you have done differently if you could do it again.

Please write a professional project that you can be proud of. Follow the good practices that you have learned and make certain to test your program thoroughly.

Explanation / Answer

program class Snake
{
   public:
int snakeX[201];
       int snakeY[201];
       int color_background, color_border , color_apple ,
       color_normal , color_snake ;
       int snake, prvSnake, snake_Enlongment ;
       int rX, rY, apple ;
       int level ;
       int score ;
       char playAgain;
       int i, j;
       int centerX, centerY;
       int x, y, oldX, oldY;
       int lines, columns;
       char c;
       int direction, difficulty;
   public:
       Snake();
       Snake( int i);
       ~Snake();
   public:
   void Settings();
   void Game_Main();
   void game_Canvas();
   void initsnake();
   void check_collision();
   void keyPressed();
   void Display_snake();
   void check_Snake_Location();
   void Create_Apples();
   void Check_Apples();
   void Remove_Apple();
   void Game_Over();
   void Display();
       void gotoxy(int x, int y);
       void clrscr();
       void setcolor(WORD color);
       void clrbox(unsigned char x1,unsigned char y1,
           unsigned char x2,unsigned char y2,unsigned char bkcol);
       void box(unsigned x,unsigned y,unsigned sx,unsigned sy,
           unsigned char col,unsigned char col2,char text_[]);
       void putbox(unsigned x1,unsigned y1,unsigned x2,unsigned y2,
           unsigned char texcol,unsigned char frcol,
           unsigned char bkgcol,char bheader[]);
};
void Snake::Create_Apples()
{
setcolor(color_apple);
rX= ( rand()% columns )+ centerX ;
rY= ( rand()% lines)+ centerY ;
for(i=1;i<=snake;i++)
{
if((rX==snakeX[i])&&(rY==snakeY[i])) Create_Apples();
}
gotoxy(rX,rY); printf("%c",770);
if(score==1)getch();
}
void Snake::Check_Apples()
{
   if((snakeX[1]==rX)&&(snakeY[1]==rY))
   {
    apple++;
   Remove_Apple();
   snake+=snake_Enlongment;
   Create_Apples();
   }
}
void Snake::Display_snake()
{
for(i=snake;i>=0;i--)
{
gotoxy(snakeX[i],snakeY[i]);
if(i==0)
   {
setcolor(color_background);
printf("%c",177);
}else setcolor(color_snake);
if(i==1) printf("%c",178);
if((i!=0)&&(i!=1)) printf("%c",219);
}
setcolor (color_normal);
gotoxy(centerX-1,centerY+lines+2);
printf("level: %2.d",level);
gotoxy(centerX-1,centerY+lines+2+1);
printf(" apple(s) :%2.d/%2.d ",apple,(((lines*columns)/30)+6));
setcolor (color_background);
}
   void Snake::keyPressed()
{
if(kbhit())
{   
c=getch();
switch(c)
   {
case RIGHT :
   if(direction!=3)
{
x++ ;
direction=1;
}
else x-- ;
   break;
case LEFT:
   if(direction!=1)
{
x-- ;
direction=3;
}
else x++ ;
   break;
case DOWN :
   if(direction!=2)
   {
y++ ;
direction=4;
}
else y-- ;
   break;
case UP :
   if(direction!=4)
   {
y-- ;
direction=2;
}
else y++ ;
   break;
case SPACE :
getch() ;
break;
}
}
}
#include "Snake.h"
int main()
{
Snake *snake = new Snake(1);
return 0;
}