C ++在2d数组中移动char [英] C++ Moving a char in a 2d array

查看:157
本文介绍了C ++在2d数组中移动char的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须为大学项目做一个游戏。在这个游戏中,我必须有一个字符,可以通过使用键盘输入在2d网格中移动。我只能得到字符移动一个块向左,右,上,下和所有。



这是我的代码:

  void Opstical(char ** array,int row,int col,int direction){
int a(0);
int b(1);
//这里是我的问题,我不能让它移动
//离原始位置多一个块。
// char 2我想移动

数组[a] [b] = 2;
if(direction == 4){
array [a] [b] = array [a] [b-1] = 2;
}
if(direction == 6){
array [a] [b] = array [a] [b + 1] = 2;
}
if(direction == 8){
array [a] [b] = array [a-1] [b] = 2;
}
if(direction == 2){
array [a] [b] = array [a + 1] [b] = 2;
}

for(int i = 0; i for(int c = 0; c cout < array [i] [c]<<;
}
cout<<< endl<< endl;
}
}

谢谢。


<因为你正在初始化 a b 每个时间输入功能。



我假设 a b 是当前坐标的字符? (你可以用一些更好的描述性变量名)。



同样我不太确定什么是赋值2试图完成,但它看起来错误。 / p>

I have to make a game for a university project. In this game i have to have a character that can be moved in the 2d grid by use of keyboard input. I can only get the character to move one block to the left, right, up,down and that all. it does not allow me for example to move two positions to the left from the original.

Here is my code:

void Opstical(char**array,int row,int col,int direction) {
    int a(0);
    int b(1);
    //here is my problem where i cant get it to move 
    //more than one block away from the original spot.
    //char 2 i want to move

    array[a][b] = 2;
    if(direction == 4) {
       array[a][b]=array[a][b-1]=2;
    }
    if(direction == 6) {
       array[a][b]=array[a][b+1]=2;
    }
    if(direction == 8) {
       array[a][b]=array[a-1][b]=2;
    }
    if(direction == 2) {
       array[a][b]=array[a+1][b]=2;
    }

    for(int i = 0;i < row ; i++) {
        for(int c = 0; c < col; c++) {
            cout << array[i][c]<<" ";
        }
        cout <<endl<<endl;
    }
}

Thanks.

解决方案

Because you are initialising a and b every time you enter the function.

I am assuming a and b are supposed to be the current co-ordinates of the character? (You could do with some better descriptive variable names).

Also I'm not quite sure what assigning 2 to everything is trying to accomplish, but it looks wrong.

这篇关于C ++在2d数组中移动char的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆