将值从一个函数返回到另一个函数
Returning a value from a function to another function
我正在制作一个程序,用户可以在打印到屏幕上的数组中移动他们的字符(数字 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;
}
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
相关文章:
- 函数向量_指针有不同的原型,我可以构建一个吗
- 为什么在没有显式默认构造函数的情况下,将另一个结构封装在联合中作为成员的结构不能编译
- 创建一个函数以在输入为负数或零时输出字符串.第一次执行用户定义的函数
- 基于另一个成员参数将函数调用从类传递给它的一个成员
- 如何仅为一个函数添加延迟
- 构造函数正在调用一个使用当前类类型的函数
- C++-试图将函数指针推回到另一个CPP文件中的矢量时出错
- 有一个打印语句的函数是一种糟糕的编程实践吗
- 有没有什么方法可以使用一个函数中定义的常量变量,也可以由c++中同一程序中的其他函数使用
- 输入到文件并输出到另一个文件,并将流文件传递给函数
- 我不明白为什么我声明一个空的内部结构并将其传递给构造函数
- 如何创建函数管道,以便函数一个接一个地运行?
- 如何巧妙地编写两个函数——一个用于检查是否存在解决方案,另一个用于获取所有解决方案
- 在c++中的复制构造函数/一个声明语句中的初始化的延续中使用chain方法
- C :基类调用自己的虚拟函数 - 一个反图案
- 如何在这个交换函数(一个单独的链表)中找到错误
- 两个相同的函数(一个使用模板模式,另一个不使用)
- 你怎么能一次给一个函数一个参数呢
- 为什么要做两个函数?(一个是非const,另一个是const)
- 当代码在其他地方使用时,如何保证函数一个接一个地被调用