C++ Greedy Snake的OOP实现 贪食蛇 STL初次学习

C++ Greedy Snake的OOP实现

    这次实现用到了oop面相对象编程,为了良好的封装性,搞了好多get型函数,然后这次对<LIST>STL也是有了初步探究。

list用法个人小总结

1.list的基本操作和使用
assign() 给list赋值
back() 返回最后一个元素
begin() 返回指向第一个元素的迭代器
clear() 删除所有元素
empty() 如果list是空的则返回true
end() 返回末尾的迭代器
erase() 删除一个元素
front() 返回第一个元素
get_allocator() 返回list的配置器
insert() 插入一个元素到list中
max_size() 返回list能容纳的最大元素数量
merge() 合并两个list
pop_back() 删除最后一个元素
pop_front() 删除第一个元素
push_back() 在list的末尾添加一个元素
push_front() 在list的头部添加一个元素
rbegin() 返回指向第一个元素的逆向迭代器
remove() 从list删除元素
remove_if() 按指定条件删除元素
rend() 指向list末尾的逆向迭代器
resize() 改变list的大小
reverse() 把list的元素倒转
size() 返回list中的元素个数
sort() 给list排序
splice() 合并两个list
swap() 交换两个list
unique() 删除list中重复的元素

2.查找特定元素
例如查找元素a

list<int>::iterator p = find( mylist.begin(), mylist.end(), a ); //查找

用这个代码可以把a放在迭代器p中
然后它就任你处置了哈哈
for example:
从list中去除它

 if ( p != mylist.end() )
     mylist.erase(p);

3.遍历
有一种方法是遍历不过会删除:
因为在蛇中对食物位置的调用后我要保留,所以不能用这种方法

while(!mylist.empty())
       {
            cout<<"remove : " << mylist.front()<<endl;
            mylist.pop_front();
       }

另外一种是迭代器遍历,不删除:
例如在蛇中用到的

list<int>::iterator itor;
       itor = _foods.begin();
        while (itor != _foods.end()) {
            _map[itor->x][itor->y] = FOOD;
            *itor++;
        }

以下贴贪吃蛇代码
game.cpp

//
// main.cpp
// cpp Application Greedy Snake
//
// Created by 邱兆丰 on 16/4/10.
// Copyright © 2016年 邱兆丰. All rights reserved.
//

#include "map.hpp"
#include "global.hpp"
#include <iostream>
#include <list>
#include <algorithm>

using std::cin;
using std::cout;
using std::cerr;
using std::endl;

class InvalidInputException {
public:
    InvalidInputException() { cerr << "Invalid input!" << endl; }
};
class DuplicateInputException : public InvalidInputException {
public:
    DuplicateInputException() { cerr << "Duplicate input!" << endl; }
};

class GameUI {
private:
    map* world;
    point initial_size;
    point initial_head;
    std::list<point> initial_foods;

public:
    GameUI() {
        cout << "please input two positive integers indicates the map size!"
        << endl;
        cin >> initial_size.x >> initial_size.y;
        if (initial_size.x <= 5 || initial_size.y <= 5 || initial_size.x > 15 ||
            initial_size.y > 15) {
            cout << "invalid input" << endl;
            throw InvalidInputException();
        }
        cout << "please input two positive integers(range(0, size_x-1), "
        "range(0,size_y-1)) the initialize snake head position!"
        << endl;
        cin >> initial_head.x >> initial_head.y;
        if (initial_head.x >= initial_size.x || initial_head.x < 0 ||
            initial_head.y >= initial_size.y || initial_head.y < 0) {
            cout << "invalid input" << endl;
            throw InvalidInputException();
        }
        int food_num;
        cout << "please input how many food you will put and then input food "
        "position which is different form each other"
        << endl;
        cin >> food_num;

        if (food_num <= 0) {
            throw InvalidInputException();
        }

        while (food_num > 0) {
            food_num--;
            point temp;
            cin >> temp.x >> temp.y;
            if (temp.x >= 0 && temp.x < initial_size.x && temp.y >= 0 &&
                temp.y < initial_size.y &&
                std::find(initial_foods.begin(), initial_foods.end(), temp) ==
                initial_foods.end() &&
                !(temp.x == initial_head.x && temp.y == initial_head.y)) {
                initial_foods.push_back(temp);
            } else {
                throw DuplicateInputException();
            }
        }

        world = new map(initial_size, initial_head, initial_foods);
    }

    ~GameUI() { delete world; }

    void GameLoop() {
        world->print();
        bool exit = false;
        while (true) {
            char operation = getInput();
            switch (operation) {
                case 'w':
                case 'W':
                    this->world->move(up);
                    break;
                case 's':
                case 'S':
                    this->world->move(down);
                    break;
                case 'a':
                case 'A':
                    this->world->move(left);
                    break;
                case 'd':
                case 'D':
                    this->world->move(right);
                    break;
                case 'q':
                case 'Q':
                    exit = true;
                    break;
                default:
                    this->world->move(freeze);
            }
            world->print();
            if (world->isGameOver()) {
                cout << "Game Over!" << endl;
                break;
            }
            if (exit) {
                cout << "Bye!" << endl;
                break;
            }
        }
    }

    char getInput() {
        char temp;
        cin >> temp;
        return temp;
    }
};

int main() {
    GameUI greedySnake;
    greedySnake.GameLoop();
    return 0;
}

map.hpp

//
// map.hpp
// cpp Application Greedy Snake
//
// Created by 邱兆丰 on 16/4/10.
// Copyright © 2016年 邱兆丰. All rights reserved.
//

#ifndef map_hpp
#define map_hpp

#include "snake.hpp"

class map {
public:
    map(const point& initialSize, const point& initialHead, const std::list<point>& initialFoods) : _size(initialSize), player(initialHead), _foods(initialFoods) {
        snakeStatus = 0;
        _map = new char*[_size.x];
        for (int i = 0; i < _size.x; i++) {
            _map[i] = new char[_size.y];
            memset(_map[i], EMPTY, _size.y);
        }
        changeMap();
    }
    void changeMap() {
        if (snakeStatus == -1) {
            return;
        }
        for (int i = 0; i < _size.x; i++) {
            memset(_map[i], EMPTY, _size.y);
        }
        std::list<point>::iterator itor;
        itor = _foods.begin();
        while (itor != _foods.end()) {
            _map[itor->x][itor->y] = FOOD;
            *itor++;
        }
        _map[player.getHead().x][player.getHead().y] = HEAD;
        for (int i = 0; i < player.getSnakeLength(); i++) {
            _map[player.getBody()[i].x][player.getBody()[i].y] = BODY;
        }
    }
    void print() {
        for (int i = 0; i < _size.x; i++) {
            for (int j = 0; j < _size.y; j++) {
                std::cout << _map[i][j];
            }
            std::cout << std::endl;
        }
        std::cout << std::endl;
    }
    void move(direction d) {
        player.Smove(d);
        collision();
        if (snakeStatus == 1) {
            player.snakeGrowth();
            snakeStatus = 0;
        }
        changeMap();
    }
    bool isGameOver() {
        if (snakeStatus == -1)
            return 1;
        else
            return 0;
    }
    ~map() {
        for (int i = 0; i < _size.x; i++) {
            delete []_map[i];
        }
        delete []_map;
    }
    void collision() {
        for (int i = 0; i < player.getSnakeLength(); i++) {
            if (player.getHead().x == player.getBody()[i].x && player.getHead().y == player.getBody()[i].y) {
                snakeStatus = -1;
                return;
            }
        }
        if (player.getHead().x < 0 || player.getHead().x >= _size.x || player.getHead().y < 0 || player.getHead().y >= _size.y) {
            snakeStatus = -1;
            return;
        }
        else if (_map[player.getHead().x][player.getHead().y] == FOOD) {
            std::list<point>::iterator it = find( _foods.begin(), _foods.end(), player.getHead());
            _foods.erase(it);
            snakeStatus = 1;
            return;
        }
        snakeStatus = 0;
    }
private:
    int snakeStatus;
    char **_map;
    point _size;
    snake player;
    std::list<point> _foods;
};

#endif /* map_hpp */

global.hpp

//
// global.hpp
// cpp Application Greedy Snake
//
// Created by 邱兆丰 on 16/4/10.
// Copyright © 2016年 邱兆丰. All rights reserved.
//

#ifndef _GLOBAL_H_
#define _GLOBAL_H_

#ifndef SYMBOLS
#define HEAD '@'
#define BODY 'X'
#define EMPTY '+'
#define FOOD '$'
#endif // !SYMBOLS

enum direction { up = 0, down = 1, left = 2, right = 4, freeze = 5 };

struct point {
    int x;
    int y;
    point(int x = 0, int y = 0) : x(x), y(y) {}
    point(const point& another) : x(another.x), y(another.y) {}
    point& operator=(const point& other) {
        x = other.x;
        y = other.y;
        return *this;
    }
    friend bool operator==(const point& point1, const point& point2) {
        return point1.x == point2.x && point1.y == point2.y;
    }
    point& move(direction d) {
        switch (d) {
            case up:
                x--;
                break;
            case down:
                x++;
                break;
            case left:
                y--;
                break;
            case right:
                y++;
                break;
            case freeze:
            default:
                break;
        }
        return *this;
    }
};

#endif // !_GLOBAL_H_

snake.hpp

//
// snake.hpp
// cpp Application Greedy Snake
//
// Created by 邱兆丰 on 16/4/10.
// Copyright © 2016年 邱兆丰. All rights reserved.
//

#ifndef snake_hpp
#define snake_hpp

#include <iostream>
#include "global.hpp"
#include <algorithm>
#include <list>
#include <cstring>

class snake {
public:
    snake (point H) {
        _head = H;
        _tail = H;
        for (int i = 0; i < 100; i++) {
            _body[i].x = -1;
            _body[i].y = -1;
        }
        snakeLength = 0;
    }
    point &getHead() {
        return _head;
    }
    point* getBody() {
        return _body;
    }
    point &getTail() {
        return _tail;
    }
    void snakeGrowth() {
        _body[snakeLength] = _tail;
        snakeLength++;
    }
    void Smove(direction d) {
        if (snakeLength != 0) {
            _tail = _body[snakeLength - 1];
        } else {
            _tail = getHead();
        }
        for (int i = snakeLength - 1; i >0; i--) {
            _body[i] = _body[i - 1];
        }
        _body[0] = _head;
        _head.move(d);
    }
    int getSnakeLength() {
        return snakeLength;
    }
private:
    point _head;
    point _tail;
    point _body[100];
    int snakeLength;
};


#endif /* snake_hpp */

你可能感兴趣的:(list,C语言,STL,作业,贪食蛇)