Quantcast
Channel: Форум программистов и сисадминов Киберфорум
Viewing all articles
Browse latest Browse all 514805

игра "Змея" - Visual C++

$
0
0
Как добавить логику добавления нового яблока после поедания предыдущего, в случайную клетку, не являющуюся клеткой с телом змейки?
:

#include <iostream>
#include <conio.h>
#include <utility>
#include <deque>
#include <vector>
using namespace std;

const int N = 30, M = 15;
char Field[M][N];
vector<pair <int, int> > apples;

// Направление движения
enum Dir{
        Right,
        Left,
        Down,
        Up
};

void EatApple(pair<int, int> coord){
        for (auto iter = apples.begin(); iter != apples.end(); iter++){
                if (*iter == coord){
                        apples.erase(iter);
                        break;
                }
        }
}

class Snake{
public:
        Snake(int X = 0, int Y = 0)        {
                m_Body.push_back(make_pair(Y, X));
                m_Body.push_back(make_pair(Y, X + 1));
                m_Body.push_back(make_pair(Y, X + 2));
                m_Body.push_back(make_pair(Y + 1, X + 2));
                m_Body.push_back(make_pair(Y + 2, X + 2));
                m_Body.push_back(make_pair(Y + 3, X + 2));

                m_Direction = Dir::Right;
        }

        bool Move(Dir direction){
                // Попытка пойти вправо
                if (m_Direction != Dir::Left && direction == Dir::Right){
                        pair<int, int> newHead = m_Body.back();
                        if (newHead.second < N - 2){
                                newHead.second++;

                                char cell = Field[newHead.first][newHead.second];
                                if (cell != ' ' && cell != '@')
                                        return false;

                                m_Body.push_back(newHead);
                                if (cell == '@'){
                                        EatApple(newHead);
                                }
                                else
                                        m_Body.pop_front();
                        }
                        m_Direction = direction;
                }
                ////////////////////////////////////////////////////
                else if (m_Direction != Dir::Right && direction == Dir::Left){
                        pair<int, int> newHead = m_Body.back();
                        if (newHead.second > 1){
                                newHead.second--;

                                char cell = Field[newHead.first][newHead.second];
                                if (cell != ' ' && cell != '@')
                                        return false;

                                m_Body.push_back(newHead);
                                if (cell == '@'){
                                        EatApple(newHead);
                                }
                                else
                                        m_Body.pop_front();
                        }
                        m_Direction = direction;
                }
                else if (m_Direction != Dir::Down && direction == Dir::Up){
                        pair<int, int> newHead = m_Body.back();
                        if (newHead.first > 1){
                                newHead.first--;

                                char cell = Field[newHead.first][newHead.second];
                                if (cell != ' ' && cell != '@')
                                        return false;

                                m_Body.push_back(newHead);
                                if (cell == '@'){
                                        EatApple(newHead);
                                }
                                else
                                        m_Body.pop_front();
                        }
                        m_Direction = direction;
                }
                else if (m_Direction != Dir::Up && direction == Dir::Down){
                        pair<int, int> newHead = m_Body.back();
                        if (newHead.first < M - 2){
                                newHead.first++;

                                char cell = Field[newHead.first][newHead.second];
                                if (cell != ' ' && cell != '@')
                                        return false;

                                m_Body.push_back(newHead);
                                if (cell == '@'){
                                        EatApple(newHead);
                                }
                                else
                                        m_Body.pop_front();
                        }
                        m_Direction = direction;
                }
                return true;
        }

public:
        // Координаты всех клеток змейки
        deque<pair<int, int> > m_Body;
        // Направление предыдущего шага
        Dir m_Direction;
};

Snake snake(2, 2);

void DrawField(){
        system("cls");
        for (int i = 0; i < M; i++)        {
                for (int j = 0; j < N; j++)        {
                        if (j == 0 || j == N - 1){
                                if (i == 0 || i == M - 1)
                                        Field[i][j] = '+';
                                else
                                        Field[i][j] = '|';
                        }
                        else if (i == 0 || i == M - 1)
                                Field[i][j] = '-';
                        else
                                Field[i][j] = ' ';
                }
        }

        // Отрисовка самой змейки
        for (auto iter = snake.m_Body.begin(); iter != snake.m_Body.end(); iter++){
                if (*iter == snake.m_Body.back()){
                        auto prev = (iter - 1);

                        int yDif = prev->first - iter->first;
                        int xDif = prev->second - iter->second;

                        if (xDif == -1)
                                Field[iter->first][iter->second] = '>';
                        if (xDif == 1)
                                Field[iter->first][iter->second] = '<';

                        if (yDif == 1)
                                Field[iter->first][iter->second] = '^';
                        if (yDif == -1)
                                Field[iter->first][iter->second] = 'v';
                }
                else if (*iter == snake.m_Body.front())        {
                        auto prev = (iter + 1);

                        int yDif = prev->first - iter->first;
                        int xDif = prev->second - iter->second;

                        if (xDif == 1)
                                Field[iter->first][iter->second] = '>';
                        if (xDif == -1)
                                Field[iter->first][iter->second] = '<';

                        if (yDif == -1)
                                Field[iter->first][iter->second] = '^';
                        if (yDif == 1)
                                Field[iter->first][iter->second] = 'v';
                }
                else
                        Field[iter->first][iter->second] = '*';
        }

        // Отрисовка яблок
        for (auto iter = apples.begin(); iter != apples.end(); iter++){
                Field[iter->first][iter->second] = '@';
        }

        cout << endl << endl << endl;
        for (int i = 0; i < M; i++){
                cout << "\t\t";
                for (int j = 0; j < N; j++)
                        cout << Field[i][j] << " ";
                cout << endl;
        }
}

int main(){
        apples.push_back(make_pair(5, 5));
       
        while (true){
                DrawField();
                char a = _getch();
                if ((int)a == -32){
                        a = _getch();
                        bool resultOFMove = true;
                        // Вправо
                        if ((int)a == 77){
                                resultOFMove = snake.Move(Dir::Right);
                        }
                        // Вниз
                        else if ((int)a == 80){
                                resultOFMove = snake.Move(Dir::Down);
                        }
                        // Влево
                        else if ((int)a == 75){
                                resultOFMove = snake.Move(Dir::Left);
                        }
                        // Вверх
                        else if ((int)a == 72){
                                resultOFMove = snake.Move(Dir::Up);
                        }
                        if (resultOFMove == false)
                                break;
                }
        }
        cout << "You lost, looser!!!!!!";
        _getch();
        return 0;
}


Viewing all articles
Browse latest Browse all 514805

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>