移入二维阵列板并阻止玩家离开

时间:2017-10-29 10:37:27

标签: c++ multidimensional-array

我正试图通过网上发现的练习制作基本游戏。我使用2D数组作为地下城地图。现在对于玩家运动,我想阻止玩家离开棋盘,所以我做了playerPositionLimiter这个功能。这很有效,玩家X和玩家Y没有超过9。board

void playerPositionLimiter(){
    //The if condition below prevents the player to move off the board
    if((playerX>9||playerY>9)||(playerX<0||playerY<0)){
        if(playerX>9) playerX=9;
        else if(playerX<0) playerX=0;
        else if(playerY>9) playerY=9;
        else if(playerY<0) playerY=0;
    }
}

现在我想使用相同的功能来阻止敌人X和Y位置移动离开,所以我将代码更改为

void playerPositionLimiter(int posX, int posY){
//Prevents the player from moving off the board
if((posX>9||posY>9)||(posX<0||posY<0)){
    if(posX>9) posX=9;
    else if(posX<0) posX=0;
    else if(posY>9) posY=9;
    else if(posY<0) posY=0;
}}

现在如果我继续向右移动,X值会继续增加,并在超过9后移动到下面一行。second

完整代码:

#include <iostream>
#include <conio.h>
#include <stdlib.h>
using namespace std;
/* Dungeon Crawl*/
/*Player variables*/
int turn=0;
int playerXprev;
int playerYprev;
int playerX=0;
int playerY=0;
int enemyX=rand()%10+1;
int enemyY=rand()%10+1;
/*Game variables*/
bool gameOver=false;
//Declaring functions
void gameScreenUpdate();
void playerMovementHandling();
//
// Char array for dungeon map
char dungeonMap[10][10]={
{'.','.','.','.','.','.','.','.','.','.'},
{'.','.','.','.','.','.','.','.','.','.'},
{'.','.','.','.','.','.','.','.','.','.'},
{'.','.','.','.','T','.','.','T','.','.'},
{'.','.','.','.','.','.','.','.','.','.'},
{'.','.','.','.','.','.','T','.','.','.'},
{'.','.','.','.','T','.','.','.','.','.'},
{'.','.','.','.','.','.','.','T','.','.'},
{'.','.','.','.','.','.','.','.','.','.'},
{'.','.','.','.','.','.','.','.','.','X'},
};
// Function for printing the map
void printDungeonMap(){
    int dR=0;
    while(dR<10){
        for(int dC=0;dC<10;dC++){
            cout << dungeonMap[dR][dC];
        }
        cout << endl;
        dR++;
    }
}
// Sets the position of player in array and clears the previous position
void playerPositionPrint(){
    dungeonMap[playerYprev][playerXprev]='.';
    dungeonMap[playerY][playerX]='P';
}
// Prevents the player to move off the grid
/*
void playerPositionLimiter(){
    //The if condition below prevents the player to move off the board
    if((playerX>9||playerY>9)||(playerX<0||playerY<0)){
        if(playerX>9) playerX=9;
        else if(playerX<0) playerX=0;
        else if(playerY>9) playerY=9;
        else if(playerY<0) playerY=0;
    }
}
*/

void playerPositionLimiter(int posY, int posX){
    //Prevents the player from moving off the board
        if(posX>9) posX=9;
        if(posX<0) posX=0;
        if(posY>9) posY=9;
        if(posY<0) posY=0;
}

// Game Conditions status | checking for winning / losing condition
void checkPlayerCondition(){
    string gameMessage;
/*
switch (dungeonMap[playerX][playerY]){
case 'X':
gameMessage = "You win!";
gameOver=true;
break;
case 'E':
gameMessage = "You ran into an enemy and died!";
gameOver=true;
break;
case 'T':
gameMessage = "You ran into a trap and died!";
gameOver=true;
break;
case '.':
gameOver=false;
break;
default:
gameOver=false;
}*/

    if(dungeonMap[playerY][playerX]=='X'){
        //gameScreenUpdate();
        gameMessage = "You win!";
        gameOver=true;
    }else if(dungeonMap[playerY][playerX]=='E'){
        //gameScreenUpdate();
        gameMessage = "You ran into an enemy and died!";
        gameOver=true;
    }else if(dungeonMap[playerY][playerX]=='T'){
        //gameScreenUpdate();
        gameMessage = "You ran into a trap and died!";
        gameOver=true;
    }else if(dungeonMap[playerY][playerX]=='.'){
        //gameScreenUpdate();
        gameOver=false;
    }
    gameScreenUpdate();
    cout << gameMessage;
}
// Takes user input and changes the X Y co-ordinates of the player
void userInputHandling(){
    playerXprev=playerX;
    playerYprev=playerY;
    char playerChar;
    cout << "Next move: ";
    playerChar=_getch();
    switch (playerChar){
    case 'w':
    playerY--;
    break;
    case 's':
    playerY++;
    break;
    case 'd':
    playerX++;
    break;
    case 'a':
    playerX--;
    break;
    default:
    cout << "(w/a/s/d)" << endl;
    userInputHandling();
}
}
// Takes user input -> checks whether player position is in limits -> checks player conditions(game conditions)
void playerPositionHandling(){
    userInputHandling();
    playerPositionLimiter(playerX,playerY);
    checkPlayerCondition();
}
//gameScreenUpdate clears and updates game screen
void gameScreenUpdate(){
    system("cls");
    playerPositionPrint();
    printDungeonMap();
}
// Moving enemies
void enemyPosition(){
    dungeonMap[enemyX][enemyY] = 'E';
}
// Move enemy
/*
void moveEnemy(){
    if(turn%5){
        dungeonMap[enemyY][enemyX]='.';
        enemyX=enemyX+(rand()%1+(-1));
        enemyY=enemyY+(rand()%1+(-1));
    }
    enemyPosition();
}*/
// Prints the game screen for the first time then loops till game over is true
int main(){
    srand(632);
    enemyPosition();
    gameScreenUpdate();
    while(!gameOver){
    playerPositionHandling();
    //moveEnemy();
    turn++;
    //cout << turn;
    cout << "X:" << playerX;
    cout << "Y:" << playerY;
    }
}

1 个答案:

答案 0 :(得分:-1)

再看看你的代码:

if((posX>9||posY>9)||(posX<0||posY<0)){
    if(posX>9) posX=9;
    else if(posX<0) posX=0;
    else if(posY>9) posY=9;
    else if(posY<0) posY=0;
}}

在所有有问题的情况下,它会进入if,但如果有两个不同的问题,例如x&lt; 0和y>在同一时间,posX将设置为0,posY将保持不变。修正建议:

if(posX>9) posX=9;
if(posX<0) posX=0;
if(posY>9) posY=9;
if(posY<0) posY=0;

外部if被移除,单独的if解决了每个问题。

修改

另一个问题是posXposY是本地参数,您需要传递对该函数的引用,因为posXposY参数将停止在函数运行后存在,即使您在参数中存储了正确的值,外部值也将保持不变。