首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >简单无止尽避障游戏

简单无止尽避障游戏
EN

Code Review用户
提问于 2016-02-29 23:29:13
回答 2查看 3.6K关注 0票数 4

我在C++上做了一个快速的游戏,你沿着垂直的轨道跑下去,避开‘X’或障碍物。玩家被表示为'v',分数是根据你在赛道上的距离来计算的。障碍在地图底部随机生成,每次玩家移动时都会向上滑动(使用箭头键)。

我有从另一个源链接的dodgeGame()函数,所以这是程序实际开始的地方。

我怎样才能让它变得更好?

代码语言:javascript
运行
复制
#include <conio.h>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <time.h>
using namespace std;


void restartMap(char map[9][9]) //Sets all values on map to ' '
{
        for(int i = 0; i < 9; i++)
    {
        for(int g = 0; g < 9; g++)
        {
            map[i][g] = ' ';
        }
    }
}

void drawMap(char map[9][9]) //Displays full array, with lines on each side
{
    for(int i = 0; i < 9; i++)
    {
        cout << '|';
        for(int g = 0; g < 9; g++)
        {
            cout << map[i][g];
        }
        cout << '|' << endl;
    }
}

void slideMap(char map[9][9], int player) //Slides each value in the array up, adjusts bottom row
{
    for(int i = 0; i < 8; i++)
    {
        for(int g = 0; g < 9; g++)
        {
            map[i][g] = map[i+1][g];
        }
    }
    map[0][player] = 'v';
    for(int i = 0; i < 9; i++)
    {
        if(rand() % 6 == 0) //One in six chance to place an obstacle on the bottom row, otherwise blank space
        {
            map[8][i] = 'X';
        }
        else
        {
            map[8][i] = ' ';
        }
    }
}

void mapInfo(int player, int count)
{
    cout << "Position: " << player << endl << "Distance Traveled: " << count;
}

void dodgeGame() //MAIN
{
    srand (time(NULL));
    char map[9][9];
    restartMap(map);
    int player = 4;
    map[0][player] = 'v';
    int c = 0, count = 0;
    drawMap(map); mapInfo(player, count);
    bool crash = false;
    while(true)
    {
        c = getch();
        switch(c)
        {
            case 224:   //Issue with error keys
            continue;

            case 80: //down
            if(map[1][player] == 'X') crash = true;   //These test if there is an obstacle where the player is trying to move to
            break;

            case 75: //left
            if(player == 0) continue;
            if(map[1][player-1] == 'X')
            {
                crash = true;
                break;
            }
            map[0][player] = ' ';
            player--;
            map[0][player] = 'v';
            break;

            case 77: //right
            if(player == 8) continue;
            if(map[1][player+1] == 'X')
            {
                crash = true;
                break;
            }
        map[0][player] = ' ';
        player++;
        map[0][player] = 'v';
        break;

        default: continue;
    }
    count++;
    system("cls");
    slideMap(map, player);
    if(crash == true) map[0][player] = '=';
    drawMap(map); mapInfo(player, count);
    if(crash == true) break;

    }
cout << "\n|<|Crash!|>|\nFinal Score: " << count << ".\n";
}
EN

回答 2

Code Review用户

发布于 2016-03-01 00:33:09

我要做的第一个改变是去掉您目前在代码中撒下的大多数9s。

代码语言:javascript
运行
复制
static const int rows = 9;
static const int cols = 9;

这样,如果您决定要10x10网格(或8x11,等等)这样做应该很容易。

第二种可能性是进行优化尝试(可能有点错误)。特别是,与其在每次转弯后复制行,不如随时跟踪哪一行是当前的起始点。每次更新都会提前完成,修改现在是“底部”的行,然后重新绘制。

老实说,只要您只处理一个9x9网格,这可能不会得到足够的注意。对于一个游戏(例如)是绘制2D图形,这可能会带来一个更有意义的差别。

我可能还会在主循环中为807577定义名称:

代码语言:javascript
运行
复制
enum {LEFT = 75, RIGHT = 77, DOWN = 80 };

// ...

switch (ch) { 
    case LEFT:
        if(player == 0) continue;
        if(map[1][player-1] == 'X')
        {
            crash = true;
            break;
        }
        map[0][player] = ' ';
        player--;
        map[0][player] = 'v';
        break;

player的名称可能也应该改为类似于horizontal_positioncurrent_column (至少我认为这就是它所代表的)。

您还可以考虑将代码重新转换为map类,reset (听起来比restart更准确)、drawslideinfo作为成员函数。如果要这样做,还可以考虑完全摆脱reset。相反,在构造函数中进行初始化,当您想清除网格时,只需创建一个新的map对象。

另一个次要细节是:if(crash == true)通常应该像if (crash)那样编写。

票数 4
EN

Code Review用户

发布于 2016-03-01 13:00:35

我建议您将数组通过引用传递给指针。

代码语言:javascript
运行
复制
void restartMap(char (&map)[9][9]);

您编写restartMap()映射的方式会衰减到char (*)[9],这意味着您可以编写以下代码并获得运行时错误。

代码语言:javascript
运行
复制
void restartMap(char map[9][9]);

char foo[3][9];
restartMap(foo);

这还将使您能够对一些更清晰的循环使用基于范围的for循环:

代码语言:javascript
运行
复制
constexpr auto ROWS = std::size_t(9);
constexpr auto COLS = std::size_t(9);

void restartMap(char (&map)[ROWS][COLS])
{    
    for (auto& row : map)
    {
        for (auto& c : row)
        {
            c = ' ';
        }
    }
}

或者,特别是在resetMap()的情况下,您可以使用std::fill来完成您的工作。

代码语言:javascript
运行
复制
void restartMap(char (&map)[ROWS][COLS])
{ 
    std::fill( &map[0][0], &map[0][0] + sizeof(map), 0 );
}
票数 0
EN
页面原文内容由Code Review提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://codereview.stackexchange.com/questions/121493

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档