我正在创建一个程序,用户可以在打印到屏幕的数组中移动他们的角色(数字1)。当我试图检查下一个位置是否在我的moveRight函数中打开时,我遇到了麻烦。
我想返回数组部分的值,这是1的右边一个空格。我试图返回数组下一个点的值的原因是因为我想返回该值我的drawBoard函数,所以我可以使用该值重新打印板,使一个在该位置。我如何将mB [i + 1] - (1的右边的下一个值)返回到我的drawBoard函数?
#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;
}
答案 0 :(得分: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]; } } }
我认为您需要以下内容(假设主板为4x4):
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