How to resolve the algorithm Snake step by step in the C++ programming language

Published on 7 June 2024 03:52 AM

How to resolve the algorithm Snake step by step in the C++ programming language

Table of Contents

Problem Statement

Snake is a game where the player maneuvers a line which grows in length every time the snake reaches a food source.

Implement a variant of the Snake game, in any interactive environment, in which a sole player attempts to eat items by running into them with the head of the snake. Each item eaten makes the snake longer and a new item is randomly generated somewhere else on the plane. The game ends when the snake attempts to eat himself.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Snake step by step in the C++ programming language

This C++ code is a simple implementation of the classic Snake game. It provides a detailed description of how each component of the game works, from creating the game field and handling user input to updating the snake's position and checking for collisions.

Here's a breakdown of the code:

  1. Header Files: • The code includes necessary header files like <windows.h> for console manipulation, <ctime> for generating random numbers, <iostream> for input/output operations, and <string> for string handling.
  2. Constants: • It defines WID and HEI representing the width and height of the game field, respectively. It also defines MAX_LEN, the maximum length of the snake.
  3. DIR Enum: • This enum defines four possible directions for the snake: NORTH, EAST, SOUTH, and WEST.
  4. snake Class: • This class represents the snake and contains all the game's functionality.
  5. snake Class Constructor: • The constructor initializes the console handle, sets the console title and screen buffer size, and hides the console cursor.
  6. play Method: • This method handles the main game loop. It repeatedly creates game fields, handles user input, moves the snake, and checks for collisions. It also displays the game field and allows users to restart the game if they wish.
  7. createField Method: • This method creates the game field, setting boundaries, generating a random food item (@), and initializing the snake's head and tail positions.
  8. readKey Method: • This method reads user input from the keyboard, checking for arrow keys to change the snake's direction.
  9. drawField Method: • This method draws the game field, including the snake, food, and boundaries. It uses different colors to represent different game elements.
  10. moveSnake Method: • This method updates the snake's position based on the current direction, checks for collisions, and handles snake growth when it eats food. It also updates the snake's head and tail positions.
  11. Member Variables: • alive (bool): Indicates whether the snake is still alive. • brd (char array): Represents the game field. • console (HANDLE): The console handle. • dir (DIR): The current direction of the snake. • snk (COORD array): Stores the positions of the snake's body parts. • head (COORD): Represents the snake's head position. • tailIdx, headIdx, and points (int): Variables used to track the snake's body, head, and points scored.
  12. main Function: • The main function initializes the random seed using the current time and creates an instance of the snake class to start the game.

Source code in the cpp programming language

#include <windows.h>
#include <ctime>
#include <iostream>
#include <string>

const int WID = 60, HEI = 30, MAX_LEN = 600;
enum DIR { NORTH, EAST, SOUTH, WEST };

class snake {
public:
    snake() {
        console = GetStdHandle( STD_OUTPUT_HANDLE ); SetConsoleTitle( "Snake" ); 
        COORD coord = { WID + 1, HEI + 2 }; SetConsoleScreenBufferSize( console, coord );
        SMALL_RECT rc = { 0, 0, WID, HEI + 1 }; SetConsoleWindowInfo( console, TRUE, &rc );
        CONSOLE_CURSOR_INFO ci = { 1, false }; SetConsoleCursorInfo( console, &ci );
    }
    void play() {
        std::string a;
        while( 1 ) {
            createField(); alive = true;
            while( alive ) { drawField(); readKey(); moveSnake(); Sleep( 50 ); }
            COORD c = { 0, HEI + 1 }; SetConsoleCursorPosition( console, c );
            SetConsoleTextAttribute( console, 0x000b );
            std::cout << "Play again [Y/N]? "; std::cin >> a;
            if( a.at( 0 ) != 'Y' && a.at( 0 ) != 'y' ) return;
        }
    }
private:
    void createField() {
        COORD coord = { 0, 0 }; DWORD c;
        FillConsoleOutputCharacter( console, ' ', ( HEI + 2 ) * 80, coord, &c );
        FillConsoleOutputAttribute( console, 0x0000, ( HEI + 2 ) * 80, coord, &c );
        SetConsoleCursorPosition( console, coord );
        int x = 0, y = 1; for( ; x < WID * HEI; x++ ) brd[x] = 0;
        for( x = 0; x < WID; x++ ) {
            brd[x] = brd[x + WID * ( HEI - 1 )] = '+';
        }
        for( ; y < HEI; y++ ) {
            brd[0 + WID * y] = brd[WID - 1 + WID * y] = '+';
        }
        do {
            x = rand() % WID; y = rand() % ( HEI >> 1 ) + ( HEI >> 1 );
        } while( brd[x + WID * y] );
        brd[x + WID * y] = '@';
        tailIdx = 0; headIdx = 4; x = 3; y = 2;
        for( int c = tailIdx; c < headIdx; c++ ) {
            brd[x + WID * y] = '#';
            snk[c].X = 3 + c; snk[c].Y = 2;
        }
        head = snk[3]; dir = EAST; points = 0;
    }
    void readKey() {
        if( GetAsyncKeyState( 39 ) & 0x8000 ) dir = EAST;
        if( GetAsyncKeyState( 37 ) & 0x8000 ) dir = WEST;
        if( GetAsyncKeyState( 38 ) & 0x8000 ) dir = NORTH;
        if( GetAsyncKeyState( 40 ) & 0x8000 ) dir = SOUTH;
    }
    void drawField() {
        COORD coord; char t;
        for( int y = 0; y < HEI; y++ ) {
            coord.Y = y;
            for( int x = 0; x < WID; x++ ) {
                t = brd[x + WID * y]; if( !t ) continue;
                coord.X = x; SetConsoleCursorPosition( console, coord );
                if( coord.X == head.X && coord.Y == head.Y ) {
                    SetConsoleTextAttribute( console, 0x002e );
                    std::cout << 'O'; SetConsoleTextAttribute( console, 0x0000 );
                    continue;
                }
                switch( t ) {
                    case '#': SetConsoleTextAttribute( console, 0x002a ); break;
                    case '+': SetConsoleTextAttribute( console, 0x0019 ); break;
                    case '@': SetConsoleTextAttribute( console, 0x004c ); break;
                }
                std::cout << t; SetConsoleTextAttribute( console, 0x0000 );
            }
        }
        std::cout << t; SetConsoleTextAttribute( console, 0x0007 );
        COORD c = { 0, HEI }; SetConsoleCursorPosition( console, c );
        std::cout << "Points: " << points;
    }
    void moveSnake() {
        switch( dir ) {
            case NORTH: head.Y--; break;
            case EAST: head.X++; break;
            case SOUTH: head.Y++; break;
            case WEST: head.X--; break;
        }
        char t = brd[head.X + WID * head.Y];
        if( t && t != '@' ) { alive = false; return; }
        brd[head.X + WID * head.Y] = '#';
        snk[headIdx].X = head.X; snk[headIdx].Y = head.Y;
        if( ++headIdx >= MAX_LEN ) headIdx = 0;
        if( t == '@' ) {
            points++; int x, y;
            do {
                x = rand() % WID; y = rand() % ( HEI >> 1 ) + ( HEI >> 1 );
            } while( brd[x + WID * y] );
            brd[x + WID * y] = '@'; return;
        }
        SetConsoleCursorPosition( console, snk[tailIdx] ); std::cout << ' ';
        brd[snk[tailIdx].X + WID * snk[tailIdx].Y] = 0;
        if( ++tailIdx >= MAX_LEN ) tailIdx = 0;
    }
    bool alive; char brd[WID * HEI]; 
    HANDLE console; DIR dir; COORD snk[MAX_LEN];
    COORD head; int tailIdx, headIdx, points;
};
int main( int argc, char* argv[] ) {
    srand( static_cast<unsigned>( time( NULL ) ) );
    snake s; s.play(); return 0;
}


  

You may also check:How to resolve the algorithm Greyscale bars/Display step by step in the MAXScript programming language
You may also check:How to resolve the algorithm Floyd-Warshall algorithm step by step in the C# programming language
You may also check:How to resolve the algorithm Hello world/Text step by step in the Creative Basic programming language
You may also check:How to resolve the algorithm Loops/Foreach step by step in the Elixir programming language
You may also check:How to resolve the algorithm Caesar cipher step by step in the Nanoquery programming language