恳请大家帮我这个初学者解决一下C++关于下棋的问题,看错在哪儿

2024-12-15 06:56:19
推荐回答(1个)
回答1:

我按照你的思路修正了一下,你看看行不行
#include
#include
#include
using namespace std;

void display(char board[][8]);
bool make_move(char board[][8],char templayer,int x,int z);
char play(char player1);

char player = '@';//黑棋先走
char info[256] = "游戏开始";
int main()
{
//初始化 棋盘
char board[8][8];
memset(board,' ',sizeof(board));
board[3][3] = 'O';
board[4][4] = 'O';
board[3][4] = '@';
board[4][3] = '@';
bool exit = true;

int x,z;
char y;
for(;;)
{
system("cls");
display(board);

for(int i=0;i<8;i++)
for(int j=0;j<8;j++)
if(board[i][j]==' ')
exit = false;//有空位置exit = false

if(!exit)//棋子未满
{
cin>>x>>y;
z=tolower(y)-'a';
if(board[--x][z]==' ')//空白处落棋子
{
char t_player = player;
board[x][z] = player;///确定放置的字符
make_move(board,t_player,x,z);///根据规则改变棋子分布
}
else
{

strcpy(info,"落棋错误,此处有棋子");
continue;

}
}
else
break;

}
strcpy(info,"游戏结束");
display(board);
return 0;
}

void display(char board[][8])
{
int row=0;
int col=0;
char col_label='a';
cout< cout<<" "< for(col=1;col<8;col++)///显示字母
cout<<" "<<(char)(col_label+col);
cout< for(row=0;row<8;row++)///构造格子
{
cout<<" +";
for(col=0;col<8;col++)
cout<<"---+";

cout<
for(col=0;col<8;col++)////显示棋子
cout<<" "< cout< }
cout<<" +";
for(col=0;col<8;col++)///补足最后一行
cout<<"---+";
cout<
cout<<" "< for(col=1;col<8;col++)///显示字母
cout<<" "<<(char)(col_label+col);
cout< }

char play(char player1)///返回棋子种类
{

char qizi = (player1=='@')?'O':'@';
player = qizi;

return qizi;
}

bool make_move(char board[][8],char templayer,int row,int col)
{
int x;
int y;
char opponent;
int rowdelta;
int coldelta;
opponent=(templayer=='@')?'O':'@';
bool flag = false;//判断落棋子是否错误

for(rowdelta=-1;rowdelta<=1;rowdelta++)//for1
for(coldelta=-1;coldelta<=1;coldelta++)//for2
{
if(((rowdelta+row)<0)||((rowdelta+row)>=8)||
((coldelta+col)<0)||((coldelta+col)>=8)||
((rowdelta==0)&&(coldelta==0)))
continue;//越界

if(board[row+rowdelta][col+coldelta]==opponent)
{

x=row+rowdelta;
y=col+coldelta;
for(;;)//for3
{
x+=rowdelta;
y+=coldelta;
if((x<0)||(x>=8)||(y<0)||(y>=8))
break;
if(board[x][y]==' ')
continue;
if(board[x][y]==templayer)
{
flag = true;
int tem_x = x;
int tem_y = y;
while(board[tem_x-=rowdelta][tem_y-=coldelta]==opponent)
board[tem_x][tem_y]=templayer;
break;
}
}//end for3
} //end if
} //end for2
if(!flag)//落棋子错误,周围木有相反的棋子
{
strcpy(info,"落棋错误,周围木有相反的棋子");
board[row][col] = ' ';

}
else
play(player);//仍是现在的

}//end func