俄罗斯方块C++实现(基于QT)(9)– board代码

  • A+
所属分类:C++

俄罗斯方块C++实现(基于QT)(9)-- board代码

J型方块代码实现(board.h, board.cpp)

board.h代码:

// board.h
#include <QBasicTimer>

class Tetris;

class Board
{

public:
    Board(int const &, int const &);
    void new_board(int const &, int const &); // 新建存储池
    void del_board(); // 删除存储池
    bool is_lost(); // 是否游戏失败
    int clear_line(); // 执行消行操作
    bool check_line(int const &) const; // 检查该行是否可以删除
    void move(Tetris *te); // 移动方块,将临时坐标置为当前坐标
    void add_line(); // 增加一行
    void add_lines(int const&); // 增加行
    ~Board();

private:
    int **canvas; // 保存存储池状态
    int bhigh; // 存储池高度
    int bwide; // 存储池宽度
    int clearlines; // 一次清除的行数
    bool win;
    bool fail;

public:
    friend class Player;

};

#endif // BOARD_H

board.cpp代码:

// board.cpp
#include "board.h"
#include "tetris.h"

Board::Board(int const &w, int const &h) :win(false), fail(false)
{
    bwide = w;
    bhigh = h;
    new_board(bwide,bhigh);
}

void Board::new_board(int const &w, int const &h)
{
    canvas = new int* [h];
    for (int i = 0; i != h; ++i)
    {
        canvas[i] = new int [w];
    }
    for (int i = 0; i != h; ++i)
        for (int j = 0; j != w; ++j)
            canvas[i][j] = 0;
}

void Board::del_board()
{
    for (int i = 0; i != bhigh; ++i)
    {
        delete [] canvas[i];
    }
    canvas = 0;
}

bool Board::is_lost()
{
    for (int i = 0; i != bwide; ++i)
    {
        if (canvas[0][i] || canvas[1][i] || canvas[2][i])
        {
            win = false;
            fail = true;
            return true;
        }
    }
    return false;
}

bool Board::check_line(int const &j) const
{
    for (int i = 0; i != bwide; ++i)
    {
        if (!canvas[j][i]) return false;
    }
    return true;
}

int Board::clear_line()
{
    int nums = 0; // 表示一次消除的行数
    for (int i = bhigh-1; i != -1; --i)
    {
        if (check_line(i))
        {
            ++nums;
            for (int j = i; j != 0; j--)
            {
                for (int k = 0; k != bwide; ++k)
                {
                    canvas[j][k] = canvas[j-1][k];
                }
            }
            for (int k = 0; k != bwide; ++k)
            {
                canvas[0][k] = 0;
            }
            ++i;
        }
    }
    clearlines = nums; // 一次消除的行数
    switch (nums)
    {
    case 1: return 100; // 消除1行+100分
    case 2: return 200; // 消除2行+200分
    case 3: return 400; // 消除3行+400分
    case 4: return 800; // 消除4行+800分
    default: return 0;
    }
}

void Board::move(Tetris *te) // 移动方块,将临时坐标置为当前坐标
{
    te->tmpToCurposi();
}

void Board::add_line() // 在最下一行增加方块
{
    for (int i = 0; i != bhigh-1; i++)
    {
        for (int j = 0; j != bwide; ++j)
            canvas[i][j] = canvas[i+1][j];
    }
    int i = bhigh-1;
    for (int j = 0; j != bwide; ++j)
    {
        canvas[i][j] = qrand() % 8;
    }
    canvas[i][qrand()%bwide] = 0; // 保证至少有一个是空格
    canvas[i][qrand()%bwide] = 0;
}

void Board::add_lines(int const &x) // 在最底端增加x行方块
{
    for (int i = 0; i != x; ++i)
    {
        add_line();
    }
}

Board::~Board()
{
    if (canvas)
    {
        del_board();
    }
}

查看上一篇文章俄罗斯方块C++实现(基于QT)(8)

百分购

发表评论

您必须才能发表评论!