Snake C ++, есть предложения по повышению эффективности? - PullRequest
0 голосов
/ 28 октября 2019

Я сделал это в Turbo C ++, поэтому может потребоваться модификация для запуска в других IDE. Я знаю, что он устарел, но это то, что использует моя школа.

Я хочу обновить его, чтобы использовать классы и объекты, файлы для хранения рекордов и, возможно, некоторую графику, которую я не выучил, когда писал изначальноэтот код

Но прежде чем я сделаю это, я хотел бы знать, есть ли какие-либо изменения или предложения, которые вы, ребята, должны повысить эффективность и, возможно, сократить сам код.

#include <iostream.h>
#include <conio.h>
#include <stdlib.h>
#include <dos.h>

unsigned long int score, high_score = 0;

const int no_rows = 22, no_columns = 77, head_position = 0;

char choice, token, token_food, key_press, direction_head, overlay;

char board[no_rows][no_columns];

unsigned int i, j, k, snake_length, grow, crash;

struct position
{
    int row;
    int column;
} head_map[no_rows * no_columns], food, temp1, temp2;

void Initialize()
{
start:
    clrscr();

    cout << "\t\t\t\tWELCOME TO SNAKE GAME\n\n\n";
    cout << "Choose the Body of the Snake(Enter a symbol):\n";
    cin >> token;
    cout << "\nChoose the Food of the Snake(Enter a symbol):\n";
    cin >> token_food;

    if(token_food == token)
    {
        clrscr();
        cout << "\nDon't use the same symbols for food and body of the snake!!!\n\n\n";
        goto start;
    }

    cout << "\n\nUP\t-\tW\nDOWN\t-\tS\nRIGHT\t-\tD\nLEFT\t-\tA\n\n\nARE YOU
         READY(Y / N) ?? \n";
         cin >> choice;

    for (i = 0; i < no_rows; i++)
    {
        for (j = 0; j < no_columns; j++)
            board[i][j] = ' ';
    }

    head_map[0].row = no_rows / 2;
    head_map[0].column = no_columns / 2;

    board[head_map[0].row][head_map[0].column] = token;

    direction_head = 'R';
    snake_length = 1;

    grow = crash = score = 0;
}

void Food()
{
    do
    {
        food.row = rand() % no_rows;
        food.column = rand() % no_columns;
    }
    while(board[food.row][food.column] == token);

    board[food.row][food.column] = token_food;
}

void Display()
{
    clrscr();

    for(k = 0; k < (no_columns + 2); k++)
        cout << "-";

    cout << endl;

    for(i = 0; i < no_rows; i++)
    {
        cout << "|";
        for(j = 0; j < no_columns; j++)
            cout << board[i][j];
        cout << "|" << endl;
    }

    for(k = 0; k < (no_columns + 2); k++)
        cout << "-";

    cout << "\t\t\t\t\t  SCORE :" << score;
}

void Input()
{
    if(kbhit())
    {
        key_press = getch();

        if((key_press == 'd' || key_press == 'D') && (key_press != 'a' || key_press != 'A') && (direction_head != 'L'))
            direction_head = 'R';

        if((key_press == 's' || key_press == 'S') && (key_press != 'w' || key_press != 'W') && (direction_head != 'U'))
            direction_head = 'D';

        if((key_press == 'a' || key_press == 'A') && (key_press != 'd' || key_press != 'D') && (direction_head != 'R'))
            direction_head = 'L';

        if((key_press == 'w' || key_press == 'W') && (key_press != 's' || key_press != 'S') && (direction_head != 'D'))
            direction_head = 'U';

        if(key_press == 'p' || key_press == 'P')
            delay(5000);
    }

    temp1 = head_map[head_position];
    board[head_map[head_position].row][head_map[head_position].column] = ' ';

    switch(direction_head)
    {
    case 'R':
    {
        if(head_map[head_position].column == no_columns)
            crash = 1;

        overlay = board[head_map[head_position].row][++head_map[head_position].column];

        break;
    }

    case 'L':
    {
        if(head_map[head_position].column == 0)
            crash = 1;

        overlay = board[head_map[head_position].row][--head_map[head_position].column];

        break;
    }

    case 'D':
    {
        if(head_map[head_position].row == no_rows)
            crash = 1;

        overlay = board[++head_map[head_position].row][head_map[head_position].column];

        break;
    }

    case 'U':
    {
        if(head_map[head_position].row == 0)
            crash = 1;

        overlay = board[--head_map[head_position].row][head_map[head_position].column];

        break;
    }

    default:
        break;
    }
}

void Move()
{
    board[head_map[head_position].row][head_map[head_position].column] = token;

    for(i = 1; i < snake_length; i++)
    {
        temp2 = head_map[i];
        head_map[i] = temp1;

        board[head_map[i].row][head_map[i].column] = token;
        board[temp2.row][temp2.column] = ' ';

        temp1 = temp2;
    }

    if(grow == 1)
    {
        head_map[i] = temp1;
        board[head_map[i].row][head_map[i].column] = token;

        ++snake_length;
        score += 10;
        Food();
        grow = 0;
    }
}

void Change()
{
    if(overlay == token)
        crash = 1;

    if(overlay == token_food)
        grow = 1;
}

void Game_over()
{
    getch();
    clrscr();

    cout << "\n\n\n\n\n\n\n\n\n\n\t\t\t\tGAME OVER!!!\n\t\t\t\tFINAL SCORE: " << score;

    if(score > high_score)
    {
        high_score = score;
        cout << "\n\t\t\tYOU BEAT THE HIGH SCORE!!!";
    }

    else
    {
        if(score == high_score)
            cout << "\n\t\t\tYOU TIED THE HIGH SCORE!!!";

        else
            cout << "\n\t\t\tHIGH SCORE: " << high_score;
    }
    getch();
    clrscr();

    cout << "\n\n\n\n\n\n\n\n\n\n\n\t\t\tDO YOU WANT TO CONTINUE(Y/N)??\n\t\t\t\t\t";
    cin >> choice;

    getch();
}

int main()
{
    do
    {
        Initialize();
        Food();
        Display();

        do
        {
            Input();
            Change();

            if(crash == 1)
                break;

            Move();
            delay(200);

            Display();

        }
        while(crash != 1);

        Game_over();

    }
    while(choice == 'Y' || choice == 'y');

    return 0;
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...