我正处于为学校开发C ++游戏的初期阶段,我遇到了一个问题。我已经开发了10x10阵列,我能够自由地在棋盘上移动一块,但是当我将第二块添加到网格中并尝试移动它时,这里没有任何一块移动是我的代码,当我有一个角色时板。
#include "stdafx.h"
#include <iostream>
using namespace std;
void displayArray(int, int);
void moveBoat(int, int);
int main()
{
int x=7;
int y=6;
displayArray(x,y);
moveBoat(x,y);
system("pause");
return 0;
}
void displayArray(int x, int y)
{
//0 1 2 3 4 5 6 7 8 9
char Array[10][10]={{' ',' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},//0
{' ',' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},//1
{' ',' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},//2
{' ',' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},//3
{' ',' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},//4
{' ',' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},//5
{' ',' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},//6
{' ',' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},//7
{' ',' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},//8
{' ',' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '}//9
};
Array[x][y]='d';
for(int i=0; i<10; i++) //This loops on the rows.
{
for(int j=0; j<10; j++) //This loops on the columns
{
cout << Array[i][j] << " ";
}
cout << endl;
}
}
void moveBoat(int x, int y)
{
int movement=0;
do{
cout<<"Boat Location X "<<x<<" Y "<<y<<endl;
cout<<"1.move up"<<endl;
cout<<"2.move down"<<endl;
cout<<"3.move left"<<endl;
cout<<"4.move right"<<endl;
cin>>movement;
switch(movement)
{
case 1:
if(x>0)
{
x=x-1;
system("cls");
displayArray(x,y);
}
else
{
cout<<"where are you going your leaving the battlefield"<<endl;
cout<<"please make another selection"<<endl;
}
break;
case 2:
if(x<9)
{
x=x+1;
system("cls");
displayArray(x,y);
}
else
{
cout<<"where are you going your leaving the battlefield"<<endl;
cout<<"please make another selection"<<endl;
}
break;
case 3:
if(y>0)
{
y=y-1;
system("cls");
displayArray(x,y);
}
else
{
cout<<"where are you going your leaving the battlefield"<<endl;
cout<<"please make another selection"<<endl;
}
break;
case 4:
if(y<9)
{
y=y+1;
system("cls");
displayArray(x,y);
}
else
{
cout<<"where are you going your leaving the battlefield" <<endl;
cout<<"please make another selection"<<endl;
}
break;
}
}while(movement>-1);
}
这是我的两件代码
#include "stdafx.h"
#include <iostream>
using namespace std;
void displayArray(int, int,int,int);
void moveBoat(int, int, int);
void moveEnemy(int, int, int);
int main()
{
int x=7;
int y=6;
int a=3;
int b=4;
int turn=1;
do{
displayArray(x,y,a,b);
moveBoat(x,y,turn);
displayArray(x,y,a,b);
moveEnemy(a,b,turn);
}while(turn<3);
system("pause");
return 0;
}
void displayArray(int x, int y,int a, int b)
{
//0 1 2 3 4 5 6 7 8 9
char Array[10][10]={{' ',' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},//0
{' ',' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},//1
{' ',' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},//2
{' ',' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},//3
{' ',' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},//4
{' ',' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},//5
{' ',' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},//6
{' ',' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},//7
{' ',' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},//8
{' ',' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '}//9
};
Array[x][y]='d';
Array[a][b]='s';
for(int i=0; i<10; i++) //This loops on the rows.
{
for(int j=0; j<10; j++) //This loops on the columns
{
cout << Array[i][j] << " ";
}
cout << endl;
}
}
void moveBoat(int x, int y,int turn)
{
int movement=0;
cout<<"Boat Location X "<<x<<" Y "<<y<<endl;
cout<<"1.move up"<<endl;
cout<<"2.move down"<<endl;
cout<<"3.move left"<<endl;
cout<<"4.move right"<<endl;
cin>>movement;
switch(movement)
{
case 1:
if(x>0)
{
x=x-1;
system("cls");
}
else
{
cout<<"where are you going your leaving the battlefield"<<endl;
cout<<"please make another selection"<<endl;
}
break;
case 2:
if(x<9)
{
x=x+1;
system("cls");
}
else
{
cout<<"where are you going your leaving the battlefield"<<endl;
cout<<"please make another selection"<<endl;
}
break;
case 3:
if(y>0)
{
y=y-1;
system("cls");
}
else
{
cout<<"where are you going your leaving the battlefield"<<endl;
cout<<"please make another selection"<<endl;
}
break;
case 4:
if(y<9)
{
y=y+1;
system("cls");
}
else
{
cout<<"where are you going your leaving the battlefield"<<endl;
cout<<"please make another selection"<<endl;
}
break;
}
turn++;
}
void moveEnemy(int a, int b,int turn)
{
int eMovement=0;
cout<<"Enemy Location X "<<a<<" Y "<<b<<endl;
cout<<"1.move up"<<endl;
cout<<"2.move down"<<endl;
cout<<"3.move left"<<endl;
cout<<"4.move right"<<endl;
cin>>eMovement;
switch(eMovement)
{
case 1:
if(a>0)
{
a=a-1;
system("cls");
}
else
{
cout<<"where are you going your leaving the battlefield"<<endl;
cout<<"please make another selection"<<endl;
}
break;
case 2:
if(a<9)
{
a=a+1;
system("cls");
}
else
{
cout<<"where are you going your leaving the battlefield"<<endl;
cout<<"please make another selection"<<endl;
}
break;
case 3:
if(b>0)
{
b=b-1;
system("cls");
}
else
{
cout<<"where are you going your leaving the battlefield"<<endl;
cout<<"please make another selection"<<endl;
}
break;
case 4:
if(b<9)
{
b=b+1;
system("cls");
}
else
{
cout<<"where are you going your leaving the battlefield"<<endl;
cout<<"please make another selection"<<endl;
}
break;
}
turn=1;
}
答案 0 :(得分:3)
你工作太辛苦了。以下是可能会有所帮助的事情。管理Eaiser的代码越少。
使用一个函数来检查移动是否有效。
bool is_move_on_the_board(x, y, max_x, max_y) { //assume 0 as the starting points
return x > = 0 && x < max_x && y > = 0 && y < max_y;
}
有一个移动功能
void move(x, y, a, b) {
if(!is_move_on_the_board(a, b, 10, 10)) return; //invalid move
//check if d and s collide if needed
std::swap(Array[x][y], Array[a][b]);
}
答案 1 :(得分:2)
在2人版本中,moveBoat
和moveEnemy
功能没有达到你期望的效果。他们正在修改输入参数(例如y=y+1
),但这些参数是按值传递的。因此displayArray
函数永远不会获得更新的值。
如果您通过引用传递位置参数,您应该看到正确的行为。例如:
void moveBoat(int& x, int& y, int turn);