May 08

Data Structures Project

Information Comments Off on Data Structures Project

Data Structures Report

Problem Description

We’ve chosen the third choice of how to find the player’s location in game which was snake. A location is required which the snake must find the food in order to grow and the food relocates itself after it’s being eaten. The snake has to eat the food to increase the score, as the score increases, the size of the snake should also increases. If it hits the wall, the game ends.

Objective by using C++:

− Identify the location of snake

− Location of food

− Relocation of the food

Data Structures Comparison

In this Snake project, we build the project with Array data structure for the snake and for the wall.  If we compare to templates, it also uses the types e.g. std:: and also the non type template parameters e.g. integral value, pointer to object/function, reference to object/function, pointer to member. Also we can also use an stl list. Although it’s not easy unlike arrays is that it has to go through all of the templates passed and we have to make a header for it. Another case is linked list. We have to use points and nodes for that. And it will be so much harder if we use linked list because we have to go through all of the nodes and function.

Program Manual

We use the libraries: <cstdlib> and <n.curses.h> i.e. using mac. We use width and height as const int so it measures the size of the maze. Score and fruit position uses int for the random position so after the snake eats and the score increases. The enum eDirection is the main control of the snake. TailX and TailY is the tail of the snake so that it increases after it eats the fruit.

The setup function: Sets game over to false so that the game continues to run. X and Y direction divides by the maze. The initscr() function also causes the first refresh operation to clear the screen. The noecho() routines control whether characters typed by the user are echoed by getch as they are typed. The curs_set() function sets the appearance of the cursor based on the value of visibility. The cbreak() routine disables line buffering and erase/kill character-processing, making characters typed by the user immediately available to the program. FruitX and FruitY locates the fruit randomly. We set the score to 0 as the starting point of the game. The dir direction is to identify whether the snake hits the wall or not. The clear() resets the error flags on a stream.

The draw function draws the game window. Again, we use clear() so that it resets an error. Use FruitX and FruitY  for random fruit placing and using 2d array looping for drawing window of the game; mvprintw to print out the window of the program. After drawing the board we use mvprintw and the score integer for the hud and refresh() for the program.

The logic function explains the process of the game how the snake controls, fruit and the game over condition.  Use switch statement for the control of snake, for loop to make the snake appear on the screen by putting prevx, prevy as an integer and tailx and taily for direction.  If statements explains the game over when the snake touches the tail and the scoring and growing. The input function is the snake’s control. We use getch() case and switch and input the arrow keys so that it can control the snake. We also put gameover true so that if the snake hits the wall, the game ends.

The main function is to put all the functions from the previous and for the game to run.

 

Github code(via partner Rizky):

https://github.com/rizkysuharyadi/Snake

Demo application(Execution of the program):

https://drive.google.com/file/d/1JmvrlAfCvzMgR9m1zDjqjwPyWnPvo6P4/view

Screenshots(execution of program):

Members:

  • Alfi – 2101693574
  • Rizky – 1801450680