人人对战
chunli@Linux:~$ cat main.c
// main.c
// 象棋
// 車马相仕帅仕相马車
// 十十十十十十十十十
// 十炮十十十十十炮十
// 兵十兵十兵十兵十兵
// 十十十十十十十十十
// --楚河-汉界--
// 十十十十十十十十十
// 卒十卒十卒十卒十卒
// 十炮十十十十十炮十
// 十十十十十十十十十
// 車马象士将士象马車
//
#include <stdio.h>
#include <stdbool.h>
#include <math.h>
#include <stdlib.h>
#define R(piece) "\033[31m"#piece"\033[0m"//红色棋子
//#define B(piece) "\033[30m"#piece"\033[0m"//黑色棋子
#define B(piece) "\033[32m"#piece"\033[0m"//黑色棋子
#define CROSS "\033[33m十\033[0m"
//定义外部变量,棋盘坐标
char* array[11][9];
int xi,yi;//要移动的棋子
int xj,yj;//移动的目标位置
bool isStandard = 1;//是否符合规则,初始值1,符合
bool gameOverSign = 1;//游戏是否结束,0结束
bool restart = 0;
//生成棋盘
void chessboardBuilding();
//打印棋盘
void printChessboard();
//判断是红棋还是黑棋,红旗返回1,黑棋返回-1,否则返回0
int redOrBlack(int x,int y);
//红棋移动
void redMove();
//黑棋移动
void blackMove();
//每种棋子的规则
void rulesOfAllKindsOfChessPieces();
//判断游戏结束
void isGameOver();
//**************************主函数******************************
int main()
{
//生成棋盘
chessboardBuilding();
//打印棋盘
printChessboard();
//开始下棋
int turn = -1;
while (gameOverSign) {
isStandard = 1;
turn *= (-1);//双方交替下棋
switch (turn) {
case 1:
redMove();
turn = (restart) ? (turn*-1) : turn;
break;
case -1:
blackMove();
turn = (restart) ? (turn*-1) : turn;
break;
}
isGameOver();
}
printf("游戏结束!\n");
}//主函数结束
//*************************自定义函数*****************************
//生成棋盘
void chessboardBuilding()
{
for (int i = 0; i < 11; i ++) {
for (int j = 0; j < 9 ; j ++) {
array[i][j] = CROSS;
}
printf("\n");
}
array[5][0] = array[5][1] = array[5][4] = array[5][7] = array[5][8] = "-";
array[5][2] = B(楚);
array[5][3] = B(河);
array[5][5] = B(汉);
array[5][6] = B(界);
//布置红棋
array[0][0] = array[0][8] = R(車);
array[0][1] = array[0][7] = R(马);
array[0][2] = array[0][6] = R(相);
array[0][3] = array[0][5] = R(仕);
array[0][4] = R(帅);
array[2][1] = array[2][7] = R(炮);
array[3][0] = array[3][2] = array[3][4] = array[3][6] = array[3][8] = R(兵);
//布置黑棋
array[10][0] = array[10][8] = B(車);
array[10][1] = array[10][7] = B(马);
array[10][2] = array[10][6] = B(相);
array[10][3] = array[10][5] = B(仕);
array[10][4] = B(将);
array[8][1] = array[8][7] = B(炮);
array[7][0] = array[7][2] = array[7][4] = array[7][6] = array[7][8] = B(卒);
}
//打印棋盘
void printChessboard()
{
system("clear");
//显示
printf(" \033[43;30m中国象棋欢迎您\033[0m\n\n");
for (int i = 0; i < 11; i ++) {
for (int j = 0; j < 9; j ++) {
printf("%s",array[i][j]);
}
printf("\n");
}
}
//判断是红棋还是黑棋,红旗返回1,黑棋返回-1,否则返回0
int redOrBlack(int x,int y)
{
if (array[x][y] == R(車) || array[x][y] == R(马) || array[x][y] == R(相) || array[x][y] == R(仕) || array[x][y] == R(帅) || array[x][y] == R(炮) || array[x][y] == R(兵))
{
return 1;
}
else if (array[x][y] == B(車) || array[x][y] == B(马) || array[x][y] == B(象) || array[x][y] == B(仕) || array[x][y] == B(将) || array[x][y] == B(炮) || array[x][y] == B(卒))
{
return -1;
}
else
return 0;
}
//红棋移动
void redMove()
{
if (restart) {
printf("违反游戏规则,请重新输入\n");
restart = 0;
}
printf("[红棋]请输入你要移动的棋子:\n");
scanf("%d %d",&xi,&yi);
printf("[红棋]请输入你要放置的位置:\n");
scanf("%d %d",&xj,&yj);
rulesOfAllKindsOfChessPieces();
printChessboard();
}
//黑棋移动
void blackMove()
{
if (restart) {
printf("违反游戏规则,请重新输入\n");
restart = 0;
}
printf("[黑棋]请输入你要移动的棋子:\n");
scanf("%d %d",&xi,&yi);
printf("[黑棋]请输入你要放置的位置:\n");
scanf("%d %d",&xj,&yj);
rulesOfAllKindsOfChessPieces();
printChessboard();
}
//判断游戏结束
void isGameOver()
{
bool sign_r = 0;
bool sign_b = 0;
for (int i = 0; i < 11; i ++) {
for (int j = 0; j < 9; j ++) {
if (array[i][j] == R(帅)) {
sign_r = 1;
}
else if (array[i][j] == B(将))
{
sign_b = 1;
}
}
}
if ((sign_r == 0)||(sign_b == 0)) {
gameOverSign = 0;
}
}
//每种棋子的规则
void rulesOfAllKindsOfChessPieces()
{
//R(車)----------------------------------------
if (array[xi][yi] == R(車))
{
if (yi == yj)//列坐标不变,同列移动
{
for (int i = xi+1; i < xj; i ++)
{
if (i == 5)
continue;//如果行等于5,跳过
if (array[i][yi] != CROSS)
isStandard = 0;//如果初始位置和目标位置之间有棋子,则不符合规则
}
for (int i = xi-1; i > xj; i --)
{
if (i == 5)
continue;//如果行等于5,跳过
if (array[xi][yi] != CROSS)
isStandard = 0;
}
}
else if (xi == xj)//行坐标不变,同行移动
{
for (int i = yi+1; i < yj; i ++)
if (array[xi][i] != CROSS)
isStandard = 0;
for (int i = yi-1; i > yj; i --)
if (array[xi][i] != CROSS)
isStandard = 0;
}
if ((xi == xj || yi == yj)&& isStandard && (redOrBlack(xj, yj) != 1))//如果棋子直行、没有犯规且落点不是红棋,可以移动
{
array[xi][yi] = CROSS;
array[xj][yj] = R(車);
}
else
{
restart = 1;
}
}
//B(車)----------------------------------------
else if (array[xi][yi] == B(車))
{
if (yi == yj)//列坐标不变,同列移动
{
for (int i = xi+1; i < xj; i ++)
{
if (i == 5)
continue;//如果行等于5,跳过
if (array[i][yi] != CROSS)
isStandard = 0;//如果初始位置和目标位置之间有棋子,则不符合规则
}
for (int i = xi-1; i > xj; i --)
{
if (i == 5)
continue;//如果行等于5,跳过
if (array[i][yi] != CROSS)
isStandard = 0;
}
}
else if (xi == xj)//行坐标不变,同行移动
{
for (int i = yi+1; i < yj; i ++)
if (array[xi][i] != CROSS)
isStandard = 0;
for (int i = yi-1; i > yj; i --)
if (array[xi][i] != CROSS)
isStandard = 0;
}
if ((xi == xj || yi == yj)&& isStandard && redOrBlack(xj, yj) != -1)//如果棋子直行、没有犯规且落点不是红棋,可以移动
{
array[xi][yi] = CROSS;
array[xj][yj] = B(車);
}
else
{
restart = 1;
}
}
//R(马)----------------------------------------
else if (array[xi][yi] == R(马))
{
if ((redOrBlack(xj, yj) != 1) && ((xj == xi-2 && yj == yi-1 &&redOrBlack(xi-1, yi) == 0) || (xj == xi-2 && yj == yi+1 &&redOrBlack(xi-1, yi) == 0) || (xj == xi-1 && yj == yi-2 &&redOrBlack(xi, yi-1) == 0) || (xj == xi-1 && yj == yi+2 &&redOrBlack(xi, yi+1) == 0) || (xj == xi+1 && yj == yi-2 &&redOrBlack(xi, yi-1) == 0) || (xj == xi+1 && yj == yi+2 &&redOrBlack(xi, yi+1) == 0) || (xj == xi+2 && yj == yi-1 &&redOrBlack(xi+1, yi) == 0) || (xj == xi+2 && yj == yi+1 &&redOrBlack(xi+1, yi) == 0)))
{
array[xi][yi] = CROSS;
array[xj][yj] = R(马);
}
else
{
restart = 1;
}
}
//B(马)----------------------------------------
else if (array[xi][yi] == B(马))
{
if ((redOrBlack(xj, yj) != -1) && ((xj == xi-2 && yj == yi-1 &&redOrBlack(xi-1, yi) == 0) || (xj == xi-2 && yj == yi+1 &&redOrBlack(xi-1, yi) == 0) || (xj == xi-1 && yj == yi-2 &&redOrBlack(xi, yi-1) == 0) || (xj == xi-1 && yj == yi+2 &&redOrBlack(xi, yi+1) == 0) || (xj == xi+1 && yj == yi-2 &&redOrBlack(xi, yi-1) == 0) || (xj == xi+1 && yj == yi+2 &&redOrBlack(xi, yi+1) == 0) || (xj == xi+2 && yj == yi-1 &&redOrBlack(xi+1, yi) == 0) || (xj == xi+2 && yj == yi+1 &&redOrBlack(xi+1, yi) == 0)))
{
array[xi][yi] = CROSS;
array[xj][yj] = B(马);
}
else
{
restart = 1;
}
}
//R(炮)----------------------------------------
else if (array[xi][yi] == R(炮))
{
int count = 0;//起始位置间棋子的个数
if (yi == yj)//列坐标不变,同列移动
{
for (int i = xi+1; i < xj; i ++)
{
if (i == 5)
continue;//如果行等于5,跳过
if (redOrBlack(i, yi) != 0)
count++;
}
for (int i = xi-1; i > xj; i --)
{
if (i == 5)
continue;//如果行等于5,跳过
if (redOrBlack(i, yi) != 0)
count++;
}
}
else if (xi == xj)//行坐标不变,同行移动
{
for (int i = yi+1; i < yj; i ++)
if (redOrBlack(xi, i) != 0)
count++;
for (int i = yi-1; i > yj; i --)
if (redOrBlack(xi, i) != 0)
count++;
}
if ((xi == xj || yi == yj)&& (count <= 1) && redOrBlack(xj, yj) != 1)//如果棋子直行、没有犯规且落点不是红棋,可以移动
{
array[xi][yi] = CROSS;
array[xj][yj] = R(炮);
}
else
{
restart = 1;
}
}
//B(炮)----------------------------------------
else if (array[xi][yi] == B(炮))
{
int count = 0;//起始位置间棋子的个数
if (yi == yj)//列坐标不变,同列移动
{
for (int i = xi+1; i < xj; i ++)
{
if (i == 5)
continue;//如果行等于5,跳过
if (redOrBlack(i, yi) != 0)
count++;
}
for (int i = xi-1; i > xj; i --)
{
if (i == 5)
continue;//如果行等于5,跳过
if (redOrBlack(i, yi) != 0)
count++;
}
}
else if (xi == xj)//行坐标不变,同行移动
{
for (int i = yi+1; i < yj; i ++)
if (redOrBlack(xi, i) != 0)
count++;
for (int i = yi-1; i > yj; i --)
if (redOrBlack(xi, i) != 0)
count++;
}
if ((xi == xj || yi == yj)&& (count <= 1) && redOrBlack(xj, yj) != -1)//如果棋子直行、没有犯规且落点不是红棋,可以移动
{
array[xi][yi] = CROSS;
array[xj][yj] = B(炮);
}
else
{
restart = 1;
}
}
//R(兵)----------------------------------------
else if (array[xi][yi] == R(兵))
{
if (xi > xj)
isStandard = 0;//如果倒退,则不符合规范
if (xi == 3)
if ((xj != xi+1) || (yi != yj))
isStandard = 0;//第3行时只能前进一步
if (xi == 4)
if ((xj != xi+2) || (yi != yj))
isStandard = 0;//第4行时只能前进两步
if (xi > 4) {
if ((xj == xi+1 && yi ==yj)|| (xj == xi && yi ==yj+1)||(xj == xi && yi ==yj-1))
{
}
else
isStandard = 0;
}
if ((xi == xj || yi == yj)&& isStandard && redOrBlack(xj, yj) != 1)//
{
array[xi][yi] = CROSS;
array[xj][yj] = R (兵);
}
else
{
restart = 1;
}
}
//B(卒)----------------------------------------
else if (array[xi][yi] == B(卒))
{
if (xi < xj)
isStandard = 0;//如果倒退,则不符合规范
if (xi == 7)
if ((xj != xi-1) || (yi != yj))
isStandard = 0;//第3行时只能前进一步
if (xi == 6)
if ((xj != xi-2) || (yi != yj))
isStandard = 0;//第4行时只能前进两步
if (xi < 4) {
if ((xj == xi-1 && yi ==yj)|| (xj == xi && yi ==yj+1)||(xj == xi && yi ==yj-1))
{
}
else
isStandard = 0;
}
if (isStandard && redOrBlack(xj, yj) != -1)//
{
array[xi][yi] = CROSS;
array[xj][yj] = R (卒);
}
else
{
restart = 1;
}
}
//R(相)----------------------------------------
else if (array[xi][yi] == R(相))
{
if ((xj <= 4)&&(redOrBlack(xj, yj) != 1) && ((xj == xi-2 && yj == yi-2 &&redOrBlack(xi-1, yi-1) == 0) || (xj == xi-2 && yj == yi+2 &&redOrBlack(xi-1, yi+1) == 0) || (xj == xi+2 && yj == yi-2 &&redOrBlack(xi+1, yi-1) == 0) || (xj == xi+2 && yj == yi+2 &&redOrBlack(xi+1, yi+1) == 0)))
{
array[xi][yi] = CROSS;
array[xj][yj] = R(相);
}
else
{
restart = 1;
}
}
//B(象)----------------------------------------
else if (array[xi][yi] == B(象))
{
if ((xj >= 6)&&(redOrBlack(xj, yj) != -1) && ((xj == xi-2 && yj == yi-2 &&redOrBlack(xi-1, yi-1) == 0) || (xj == xi-2 && yj == yi+2 &&redOrBlack(xi-1, yi+1) == 0) || (xj == xi+2 && yj == yi-2 &&redOrBlack(xi+1, yi-1) == 0) || (xj == xi+2 && yj == yi+2 &&redOrBlack(xi+1, yi+1) == 0)))
{
array[xi][yi] = CROSS;
array[xj][yj] = B(象);
}
else
{
restart = 1;
}
}
//R(仕)----------------------------------------
else if (array[xi][yi] == R(仕))
{
if ((xj <= 2)&&(redOrBlack(xj, yj) != 1) && ((xj == xi-1 && yj == yi-1 ) || (xj == xi-1 && yj == yi+1 ) || (xj == xi+1 && yj == yi-1 ) || (xj == xi+1 && yj == yi+1 )))
{
array[xi][yi] = CROSS;
array[xj][yj] = R(仕);
}
else
{
restart = 1;
}
}
//B(士)----------------------------------------
else if (array[xi][yi] == B(士))
{
if ((xj >= 8)&&(redOrBlack(xj, yj) != 1) && ((xj == xi-1 && yj == yi-1 ) || (xj == xi-1 && yj == yi+1 ) || (xj == xi+1 && yj == yi-1 ) || (xj == xi+1 && yj == yi+1 )))
{
array[xi][yi] = CROSS;
array[xj][yj] = B(士);
}
else
{
restart = 1;
}
}
//R(帅)----------------------------------------
else if (array[xi][yi] == R(帅))
{
if ((xj <= 2 && yj <= 5 && yj >=3)&&(redOrBlack(xj, yj) != 1) && (((xj == xi)&&(yj == yi + 1 || yj == yi - 1))||((yj == yi)&&(xj == xi + 1 || xj == xi - 1))))
{
array[xi][yi] = CROSS;
array[xj][yj] = R(帅);
}
else
{
restart = 1;
}
}
//B(将)----------------------------------------
else if (array[xi][yi] == B(将))
{
if ((xj >= 8 && yj <= 5 && yj >=3)&&(redOrBlack(xj, yj) != -1) && (((xj == xi)&&(yj == yi + 1 || yj == yi - 1))||((yj == yi)&&(xj == xi + 1 || xj == xi - 1))))
{
array[xi][yi] = CROSS;
array[xj][yj] = B(将);
}
else
{
restart = 1;
}
}
else {
restart = 1;
}
}
chunli@Linux:~$
编译运行:
chunli@Linux:~$ gcc -std=c99 main.c && ./a.out
中国象棋欢迎您
車马相仕帅仕相马車
十十十十十十十十十
十炮十十十十十炮十
兵十兵十兵十兵十兵
十十十十十十十十十
--楚河-汉界--
十十十十十十十十十
卒十卒十卒十卒十卒
十炮十十十十十炮十
十十十十十十十十十
車马相仕将仕相马車
[红棋]请输入你要移动的棋子:
0 0
[红棋]请输入你要放置的位置:
1 0
中国象棋欢迎您
十马相仕帅仕相马車
車十十十十十十十十
十炮十十十十十炮十
兵十兵十兵十兵十兵
十十十十十十十十十
--楚河-汉界--
十十十十十十十十十
卒十卒十卒十卒十卒
十炮十十十十十炮十
十十十十十十十十十
車马相仕将仕相马車
[黑棋]请输入你要移动的棋子:
^C
chunli@Linux:~$
转载于:https://blog.51cto.com/990487026/1792623