Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/css/34.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 将值从一个函数返回到另一个函数_C++_Function - Fatal编程技术网

C++ 将值从一个函数返回到另一个函数

C++ 将值从一个函数返回到另一个函数,c++,function,C++,Function,我正在制作一个程序,用户可以在打印到屏幕上的整个数组中移动他们的字符(数字1)。当我试图检查moveRight函数中的下一个位置是否打开时,我遇到了麻烦 我想返回数组中1右边一个空格部分的值。我之所以尝试返回数组下一个点的值,是因为我想将该值返回到drawBoard函数,以便可以使用该值重新打印位于该位置的板。如何将mB[i+1](1右边的下一个值)返回到drawBoard函数 #include "stdafx.h" #include <iostream> #include "Pla

我正在制作一个程序,用户可以在打印到屏幕上的整个数组中移动他们的字符(数字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;
}
#包括“stdafx.h”
#包括
#包括“PlayerM.h”
使用名称空间std;
职业选手{
公众:
Player::Player(int b[])//创建一个构造函数将b的值复制到mB中
{
//将b复制到成员数组中
对于(int i=0;i<16;i++){
mB[i]=b[i];
}
}
int moveUp();
void moveDown();
int moveRight();
void moveLeft();
私人:
int-mB[16];
};
int Player::moveUp(){
返回0;
}
void Player::moveDown(){
}
int播放器::moveRight(){
for(int i=0;i<16;i++)//在aray上查找玩家位置
{
if(mB[i]==1&&i<3)//如果玩家有资格移动
{
mB[i]=0;
mB[i+1]=1;
返回mB[i+1];
}
}
}
void Player::moveLeft(){
}
int drawBoard(int boardArray[16])//绘制游戏板
{
for(int i=0;i<16;i++)//使用for循环简单地绘制游戏板(4x4)
{
库特m;
如果(m='W'){
p、 moveRight();
}
字符f;
cin>>f;
}
int播放器::moveRight(){
for(int i=0;i<16;i++)//在aray上查找玩家位置
{
if(mB[i]==1&&i<3)//如果玩家有资格移动
{
mB[i]=0;
mB[i+1]=1;
返回mB[i+1];
}
}
}
我认为您需要以下内容(考虑到电路板为4x4):

int播放器::moveRight(){
for(int i=0;i<16;i++)//在aray上查找玩家位置
{
if(mB[i]==1&&(i%4)<3)//如果玩家有资格移动
{
mB[i]=0;
mB[i+1]=1;
返回mB[i+1];
}
}
}

更改内容:
i<3
更改为
(i%4)<3

请整理格式/缩进对不起。现在应该可以了吗?@EdHeal“mB[i+1](1右侧的下一个值)”对我来说就是“mB[i+1]-mB[i+2]”作为参考,此代码中几乎所有的注释都是无用的——它们只告诉您通过查看代码可以立即知道的内容。保留不明显的内容的注释,例如您这样做的原因。@user3150762-您喜欢您的空行:-)
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];
      }
   }
}
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];
      }
   }
}