2014-01-07 51 views
0

我正在制作一个程序,用户可以在打印到屏幕的阵列中移动其字符(数字1)。当我尝试检查下一个位置是否在我的moveRight函数中打开时,我遇到了麻烦。从函数返回值到另一个函数

我想返回数组部分的值,它是1的右边的一个空格。我试图返回数组的下一个点的值的原因是因为我想返回价值到我的drawBoard函数,所以我可以使用该值重新打印制作该位置的板子。我如何将mB [i + 1] - (1的右边的下一个值)返回到我的绘图板函数?

#include "stdafx.h" 
#include <iostream> 
#include "PlayerM.h" 

using namespace std; 
class Player { 
public: 
    Player::Player(int b[]) //create a constructer to copy the values of b into mB 
    { 
     // copy b into the member array 
     for (int i = 0; i < 16; i++) { 
     mB[i] = b[i]; 
     } 
    } 

    int moveUp(); 
    void moveDown(); 
    int moveRight(); 
    void moveLeft(); 
private: 
    int mB[16]; 

}; 
int Player::moveUp() { 
    return 0; 
} 
void Player::moveDown() { 

} 
int Player::moveRight() { 
    for (int i = 0; i < 16; i++) //find the players pos on aray 
    { 
     if (mB[i] == 1 && i < 3) //if the player is eligible to move 
     { 
     mB[i] = 0; 
     mB[i + 1] = 1; 
     return mB[i + 1]; 
     } 
    } 
} 
void Player::moveLeft() { 
} 
int drawBoard(int boardArray[16]) //draw the game board 
{ 
    for (int i = 0; i < 16; i++) //use a for loop to simply draw the game board (4x4) 
    { 
     cout << boardArray[i]; //ouput the storage id of the array 
     if (i == 3 || i == 7 || i == 11 || i == 15) //every 4 lines begin new line 
     { 
     cout << "\n"; 
     } 
    } 
    return 0; 
} 
int main() { 
    int bArray[16] = {0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; //create an array [16] 
    drawBoard(bArray); //send the aray to drawBoard() 
    Player p(bArray); //send bArray to the p constructer 
    char m; 
    cin >> m; 

    if (m == 'W') { 
     p.moveRight(); 
    } 

    char f; 
    cin >> f; 
} 
+2

请收拾格式/缩进 –

+0

对不起。应该现在好吗? @EdHeal – user3150762

+0

“mB [i + 1] - (1的右边的下一个值)”对我来说就是“mB [i + 1] -mB [i + 2]” –

回答

0
int Player::moveRight() { 
    for (int i = 0; i < 16; i++) //find the players pos on aray 
    { 
     if (mB[i] == 1 && i < 3) //if the player is eligible to move 
     { 
     mB[i] = 0; 
     mB[i + 1] = 1; 
     return mB[i + 1]; 
     } 
    } 
} 

我想你想以下(考虑到板是4×4):

int Player::moveRight() { 
    for (int i = 0; i < 16; i++) //find the players pos on aray 
    { 
     if (mB[i] == 1 && (i%4) < 3) //if the player is eligible to move 
     { 
     mB[i] = 0; 
     mB[i + 1] = 1; 
     return mB[i + 1]; 
     } 
    } 
} 

是什么改变:i < 3(i%4) < 3