先说一下我的思路:
1、将象棋分为两个二位数组,第一个二维数组放的是哪个位置有棋子,并且用1代表黑方,用-1代表红方,第二个数组存放该棋子是什么
2、通过输入两个坐标来移动棋子
3、定义每一个棋子的规则,车的规则,炮的规则,兵的规则,当输入坐标时需满足规则才能移动否则报错
4、判定胜负条件,当没有将帅时,或者老将见面时,即为其中一方胜利
定义一个菜单
void mune()
{
printf("******************************\n");
printf("********* 1. play ************\n");
printf("********* 0. exit ************\n");
printf("******************************\n");
}
选择是否玩游戏
void text()
{
int input=1;
do
{
mune();
scanf("%d", &input);
switch (input)
{
case 1:
game();
break;
case 0:
printf("退出游戏\n");
break;
}
} while (input);
}
其中game()为
void game()
{
int i = 0;
int a[10][9] = { 1, 1, 1, 1, 1, 1, 1, 1, 1,
0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 1, 0, 0, 0, 0, 0, 1, 0,
1, 0, 1, 0, 1, 0, 1, 0, 1,
0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0,
-1, 0,-1, 0,-1, 0,-1, 0,-1,
0,-1, 0, 0, 0, 0, 0,-1, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0,
-1,-1,-1,-1,-1,-1,-1,-1,-1};
int b[10][9] = { 1, 2, 3, 4, 5, 4, 3, 2, 1, //1 2 3 4 5 6 7
0, 0, 0, 0, 0, 0, 0, 0, 0, //车马象士将炮卒
0, 6, 0, 0, 0, 0, 0, 6, 0,
7, 0, 7, 0, 7, 0, 7, 0, 7,
0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0,
7, 0, 7, 0, 7, 0, 7, 0, 7,
0, 6, 0, 0, 0, 0, 0, 6, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0,
1, 2, 3, 4, 5, 4, 3, 2, 1, };
system("cls");
myprint(a, b);
while (1)
{
i=yidongqianzhi(a, b);
myprint(a, b);
if (i == -1)
{
break;
}
}
}
```
输出判断
数组二 if(b[0][0] == 1) printf("车");
```java
void panduan(int arr1[10][9], int arr2[10][9],int i,int j)
{
if (arr1[i][j] == 0 && arr2[i][j] == 0)
printf("%s", " ");
if (arr1[i][j] == 1 && arr2[i][j] == 1)
printf("%s", "车");
if (arr1[i][j] == 1 && arr2[i][j] == 2)
printf("%s", "马");
if (arr1[i][j] == 1 && arr2[i][j] == 3)
printf("%s", "象");
if (arr1[i][j] == 1 && arr2[i][j] == 4)
printf("%s", "士");
if (arr1[i][j] == 1 && arr2[i][j] == 5)
printf("%s", "将");
if (arr1[i][j] == 1 && arr2[i][j] == 6)
printf("%s", "炮");
if (arr1[i][j] == 1 && arr2[i][j] == 7)
printf("%s", "卒");
if (arr1[i][j] == -1 && arr2[i][j] == 1)
printf("%s", "俥");
if (arr1[i][j] == -1 && arr2[i][j] == 2)
printf("%s", "馬");
if (arr1[i][j] == -1 && arr2[i][j] == 3)
printf("%s", "相");
if (arr1[i][j] == -1 && arr2[i][j] == 4)
printf("%s", "仕");
if (arr1[i][j] == -1 && arr2[i][j] == 5)
printf("%s", "帅");
if (arr1[i][j] == -1 && arr2[i][j] == 6)
printf("%s", "炮");
if (arr1[i][j] == -1 && arr2[i][j] == 7)
printf("%s", "兵");
}
输出一下,同时美观一下
void myprint(int arr1[10][9], int arr2[10][9])
{
int i, j;
printf(" ");
for (i = 1; i < 10; i++)
printf(" %d ",i);
printf("\n");
printf(" --------------------------\n");
for (i = 0; i < 10; i++)
{
printf("%-2d |", i+1);
for (j = 0; j < 9; j++)
{
panduan(arr1, arr2,i,j);
printf(" ");
}
printf("| %-2d", i + 1);
printf("\n");
}
printf(" --------------------------\n");
printf(" ");
for (i = 1; i < 10; i++)
printf(" %d ", i);
printf("\n");
}
有的地方需要绝对值
int jueduizhi(int x, int y)
{
int n=0;
if (x > y)
n= x - y;
if (x < y)
n= y - x;
return n;
}
来设置一下移动棋子的条件
i j 是 想要移动的棋子
x y 是 想要移动的位置
移动前移动后如果都是自己的棋子,就会报错,不能吃掉自己的子
没棋子的地方也不能走
int yidongqianzhi(int arr1[10][9], int arr2[10][9])
{
int i, j, x, y;
int n=0;
int m;
printf("想让什么子移动到什么位置\n");
scanf("%d %d %d %d", &i, &j, &x, &y);
if (i >= 1 && i <= 10 && j >= 1 && j <= 9 && x >= 1 && x <= 10 && y >= 1 && y <= 9 )
{
if ((arr1[x - 1][y - 1] == 1 && arr1[i - 1][j - 1] == 1) || (arr1[x - 1][y - 1] == -1 && arr1[i - 1][j - 1] == -1))
{
printf("小伙子你会不会下棋啊? \n");
return 0;
}
if (arr1[i - 1][j - 1] == 0)
{
printf("没有棋子,不能移动\n");
return 0;
}
if (arr1[x - 1][y - 1] == 0 && arr1[i - 1][j - 1] == 0)
{
printf("什么事情都没有发生,我就当没看见\n");
}
n = yidong(arr1, arr2,i,j,x,y);
}
else
{
printf("错误输入,请从新输入\n");
}
if (n == 0)
{
printf("错误输入,请从新输入\n");
}
if (n == -1)
{
printf("游戏结束\n");
return -1;
}
if (arr1[x - 1][y - 1] == 1 && n)
{
printf("换红方移动\n");
}
if (arr1[x - 1][y - 1] == -1 && n)
{
printf("换黑方移动\n");
}
}
移动棋子
int yidong(int arr1[10][9], int arr2[10][9], int i, int j, int x, int y)
{
int m=0;
int jcbing,jcpao,jcshi,jcju,jcxiang,jcjiang,jcma;
jcbing = bing(arr1, arr2, i, j, x, y);
jcpao = pao(arr1, arr2, i, j, x, y);
jcshi = shi(arr1, arr2, i, j, x, y);
jcju = ju(arr1, arr2, i, j, x, y);
jcxiang = xiang(arr1, arr2, i, j, x, y);
jcjiang = jiang(arr1, arr2, i, j, x, y);
jcma = ma(arr1, arr2, i, j, x, y);
if (jcbing == 1 && jcpao == 1 && jcshi == 1 && jcju == 1 && jcxiang == 1 && jcjiang == 1 && jcma == 1)
{
arr1[x - 1][y - 1] = arr1[i - 1][j - 1];
arr1[i - 1][j - 1] = 0;
arr2[x - 1][y - 1] = arr2[i - 1][j - 1];
arr2[i - 1][j - 1] = 0;
m= shengli(arr1, arr2, i, j, x, y);
if (m==1)
{
printf("黑方胜利\n");
return -1;
}
if (m == -1)
{
printf("红方胜利\n");
return -1;
}
if (m==0)
{
return 1;
}
}
else
return 0;
}
然后来定义每一个棋子的规则
先来个兵的
让默认条件通过,然后来找茬,不符合规则就报错
兵一次走一格,不能往回走,过河才能左右走
卒要反过来写,兵是- - ,卒是++
int bing(int arr1[10][9], int arr2[10][9], int i, int j, int x, int y)
{
int n=1;
if((arr1[i - 1][j - 1] == 1 && arr2[i - 1][j - 1] == 7) || (arr1[i - 1][j - 1] == -1 && arr2[i - 1][j - 1] == 7) )
{
if ((arr1[i - 1][j - 1] == 1 && i>x) || (arr1[i - 1][j - 1] == -1 && i<x))
{
n = 0;
return 0;
}
if (arr1[i - 1][j - 1] == 1 && x<i)
{
n = 0;
return 0;
}
if (arr1[1 - 1][j - 1] == -1 && x>i)
{
n = 0;
return 0;
}
if (arr1[i - 1][j - 1] == 1 && (i) < 6)
{
if (j - y != 0)
{
printf("输入错误,请从新输入\n");
n = 0;
}
}
if (arr1[i - 1][j - 1] == -1 && (i) > 5)
{
if (j - y != 0)
{
printf("输入错误,请从新输入\n");
n = 0;
}
}
if ((jueduizhi(x, i) != 1 && jueduizhi(y, j) == 0) || (jueduizhi(y, j) != 1 && jueduizhi(x, i) == 0) || (jueduizhi(y, j) + jueduizhi(x, i))>1)
{
n = 0;
return n;
}
}
return n;
}
炮的
可以随意走但是只能沿着一条线走
不能斜着走,中间隔着一个字才能吃
int pao(int arr1[10][9], int arr2[10][9], int i, int j, int x, int y)
{
int n = 1;
int count = 0;
int m;
if ((arr1[i - 1][j - 1] == 1 && arr2[i - 1][j - 1] == 6) || (arr1[i - 1][j - 1] == -1 && arr2[i - 1][j - 1] == 6))
{
if (x - i != 0 && y - j != 0)
{
n = 0;
}
if (y - j == 0 && arr1[x - 1][y - 1] == 0 && x>i)
{
for (int p = 1; p < x-i; p++)
{
if (arr1[i + p - 1][j - 1] == 1 || arr1[i + p - 1][j - 1] == -1)
count++;
}
if (count != 0)
{
n = 0;
}
}
if (y - j == 0 && arr1[x - 1][y - 1] == 0 && x<i)
{
for (int p = 1; p < i-x; p++)
{
if (arr1[i - p - 1][j - 1] == 1 || arr1[i - p - 1][j - 1] == -1)
count++;
}
if (count != 0)
{
n = 0;
}
}
if (x - i == 0 && arr1[x - 1][y - 1] == 0 && y<j)
{
for (int p = 1; p < j-y; p++)
{
if (arr1[i - 1][j + p - 1] == 1 || arr1[i - 1][j + p - 1] == -1)
count++;
}
if (count != 0)
{
n = 0;
}
}
if (y - j == 0 && (arr1[x - 1][y - 1] == 1 || arr1[x - 1][y - 1] == -1) && x>i)
{
for (int p = 1; p < x-i; p++)
{
if (arr1[i + p - 1][j - 1] == 1 || arr1[i + p - 1][j - 1] == -1)
count++;
}
if (count != 1)
{
n = 0;
}
}
if (y - j == 0 && (arr1[x - 1][y - 1] == 1 || arr1[x - 1][y - 1] == -1) && x<i)
{
for (int p = 1; p < i-x; p++)
{
if (arr1[i - p - 1][j - 1] == 1 || arr1[i - p - 1][j - 1] == -1)
count++;
}
if (count != 1)
{
n = 0;
}
}
if (x - i == 0 && (arr1[x - 1][y - 1] == 1 || arr1[x - 1][y - 1] == -1) && y>j)
{
for (int p = 1; p < y-j; p++)
{
if (arr1[i - 1][j + p - 1] == 1 || arr1[i - 1][j + p - 1] == -1)
count++;
}
if (count != 1)
{
n = 0;
}
}
if (x - i == 0 && (arr1[x - 1][y - 1] == 1 || arr1[x - 1][y - 1] == -1) && y<j)
{
for (int p = 1; p < j-y; p++)
{
if (arr1[i - 1][j - p - 1] == 1 || arr1[i - 1][j - p - 1] == -1)
count++;
}
if (count != 1)
{
n = 0;
}
}
}
return n;
}
车和炮很像
炮要判定中间有一个子才能吃
车中间没有子才能吃
int ju(int arr1[10][9], int arr2[10][9], int i, int j, int x, int y)
{
int n = 1;
int count = 0;
int m;
if ((arr1[i - 1][j - 1] == 1 && arr2[i - 1][j - 1] == 1) || (arr1[i - 1][j - 1] == -1 && arr2[i - 1][j - 1] == 1))
{
if (x - i != 0 && y - j != 0)
{
n = 0;
}
if (y - j == 0 && arr1[x - 1][y - 1] == 0 && x>i)
{
for (int p = 1; p < x - i; p++)
{
if (arr1[i + p - 1][j - 1] == 1 || arr1[i + p - 1][j - 1] == -1)
count++;
}
if (count != 0)
{
n = 0;
}
}
if (y - j == 0 && arr1[x - 1][y - 1] == 0 && x<i)
{
for (int p = 1; p < i - x; p++)
{
if (arr1[i - p - 1][j - 1] == 1 || arr1[i - p - 1][j - 1] == -1)
count++;
}
if (count != 0)
{
n = 0;
}
}
if (x - i == 0 && arr1[x - 1][y - 1] == 0 && y<j)
{
for (int p = 1; p < j - y; p++)
{
if (arr1[i - 1][j + p - 1] == 1 || arr1[i - 1][j + p - 1] == -1)
count++;
}
if (count != 0)
{
n = 0;
}
}
if (y - j == 0 && (arr1[x - 1][y - 1] == 1 || arr1[x - 1][y - 1] == -1) && x>i)
{
for (int p = 1; p < x - i; p++)
{
if (arr1[i + p - 1][j - 1] == 1 || arr1[i + p - 1][j - 1] == -1)
count++;
}
if (count != 0)
{
n = 0;
}
}
if (y - j == 0 && (arr1[x - 1][y - 1] == 1 || arr1[x - 1][y - 1] == -1) && x<i)
{
for (int p = 1; p < i - x; p++)
{
if (arr1[i - p - 1][j - 1] == 1 || arr1[i - p - 1][j - 1] == -1)
count++;
}
if (count != 0)
{
n = 0;
}
}
if (x - i == 0 && (arr1[x - 1][y - 1] == 1 || arr1[x - 1][y - 1] == -1) && y>j)
{
for (int p = 1; p < y - j; p++)
{
if (arr1[i - 1][j + p - 1] == 1 || arr1[i - 1][j + p - 1] == -1)
count++;
}
if (count != 0)
{
n = 0;
}
}
if (x - i == 0 && (arr1[x - 1][y - 1] == 1 || arr1[x - 1][y - 1] == -1) && y<j)
{
for (int p = 1; p < j - y; p++)
{
if (arr1[i - 1][j - p - 1] == 1 || arr1[i - 1][j - p - 1] == -1)
count++;
}
if (count != 0)
{
n = 0;
}
}
}
return n;
}
象就很简单了
只允许在规定的格子走就行
int xiang(int arr1[10][9], int arr2[10][9], int i, int j, int x, int y)
{
int n = 1;
int m = jueduizhi(x, i);
int q = jueduizhi(y, j);
if ((arr1[i - 1][j - 1] == 1 && arr2[i - 1][j - 1] == 3) || (arr1[i - 1][j - 1] == -1 && arr2[i - 1][j - 1] == 3))
{
if (arr1[i - 1][j - 1] == 1 && ((x != 1 && x != 3 && x != 5) || (y != 1 && y != 3 && y != 5 && y != 7)) || (m != 2 || q != 2))
{
n = 0;
return n;
}
if (arr1[i - 1][j - 1] == -1 && ((x != 10 && x != 8 && x !=6) || (y != 1 && y != 3 && y != 5 && y != 7)) || (m != 2 || q != 2))
{
n = 0;
return n;
}
m = (x - i) / 2;
q = (y - j) / 2;
if (arr1[i + m-1][j + q-1] == 1 || arr1[i + m-1][j + q-1] == -1)
{
n = 0;
return n;
}
}
return n;
}
士也一样
int shi(int arr1[10][9], int arr2[10][9], int i, int j, int x, int y)
{
int n = 1;
if ((arr1[i - 1][j - 1] == 1 && arr2[i - 1][j - 1] == 4) || (arr1[i - 1][j - 1] == -1 && arr2[i - 1][j - 1] == 4))
{
if (arr1[i - 1][j - 1] == 1 && (x > 3 || y<4 || y>6))
{
n = 0;
return n;
}
if (arr1[i - 1][j - 1] == -1 && (x < 8 || y<4 || y>6))
{
n = 0;
return n;
}
if ((i == 2 && j == 5) && ((x != 1 && x != 3) || (y != 4 && y != 6)))
{
n = 0;
return n;
}
if ((i == 9 && j == 5) && ((x != 8 && x != 10) || (y != 4 && y != 6)))
{
n = 0;
return n;
}
if ((i == 1 || i == 3) && (j == 4 || j == 6) && (x !=2 || y != 5))
{
n = 0;
return n;
}
if ((i == 8 || i == 10) && (j == 4 || j == 6) && (x != 9 || y != 5))
{
n = 0;
return n;
}
}
return n;
}
马走日,注意可能蹩马腿
int ma(int arr1[10][9], int arr2[10][9], int i, int j, int x, int y)
{
int n = 1;
int m = jueduizhi(x, i);
int q = jueduizhi(y, j);
int z;
if ((arr1[i - 1][j - 1] == 1 && arr2[i - 1][j - 1] == 2) || (arr1[i - 1][j - 1] == -1 && arr2[i - 1][j - 1] == 2))
{
if ((m != 2 || q != 1) && (m != 1 || q != 2))
{
n = 0;
return n;
}
z = (x - i) / 2;
if (!(m != 2 || q != 1) && (arr1[i + z - 1][j - 1] == 1 || arr1[i + z - 1][j - 1] == -1))
{
n = 0;
return 0;
}
z = (y - j) / 2;
if (!(m != 1 || q != 2) && (arr1[i - 1][j + z - 1] == 1 || arr1[i - 1][j + z - 1] == -1))
{
n = 0;
return 0;
}
}
return n;
}
老将先不判定输赢,只先能走就行
int jiang(int arr1[10][9], int arr2[10][9], int i, int j, int x, int y)
{
int n = 1;
if ((arr1[i - 1][j - 1] == 1 && arr2[i - 1][j - 1] == 5) || (arr1[i - 1][j - 1] == -1 && arr2[i - 1][j - 1] == 5))
{
if (arr1[i - 1][j - 1] == 1 && ((x>3) || (y<4 || y>6)))
{
n = 0;
return n;
}
if (arr1[i - 1][j - 1] == -1 && ((x<8) || (y<4 || y>6)))
{
n = 0;
return n;
}
if ((jueduizhi(x, i) != 1 && jueduizhi(y, j) == 0) || (jueduizhi(y, j) != 1 && jueduizhi(x, i) == 0) || (jueduizhi(y, j) + jueduizhi(x, i))>1)
{
n = 0;
return n;
}
}
return n;
}
现在判定胜负,当一方走的子,在符合规则的同时,走向将或者帅
则为胜利,或者两个老将见面,也为一方胜利
int shengli(int arr1[10][9], int arr2[10][9], int i, int j, int x, int y)
{
int count = 0;
int p, q,p1,q1;
int l, k,l1,k1;
if (arr1[i - 1][j - 1] == 1 && arr1[x - 1][y - 1] == -1 && arr2[x - 1][y - 1] == 5)
{
return 1;
}
if (arr1[i - 1][j - 1] == -1 && arr1[x - 1][y - 1] == 1 && arr2[x - 1][y - 1] == 5)
{
return -1;
}
for ( p = 1; p <= 10; p++)
{
for ( q = 1; q <= 9; q++)
{
if (arr1[p-1][q-1] == 1 && arr2[p-1][q-1] == 5)
{
p1 = p;
q1 = q;
break;
}
}
}
for (l = 1; l <= 10; l++)
{
for (k = 1; k <= 9; k++)
{
if (arr1[l-1 ][k-1 ] == -1 && arr2[l-1 ][k -1] == 5)
{
l1 = l;
k1 = k;
break;
}
}
}
for (q = 1; q < l1 - p1; q++)
{
if (arr1[l1 - q - 1][k1 - 1] == 1 || arr1[l1 - q - 1][k1 - 1] == -1)
count++;
}
if ((q1 - k1) == 0 && count == 0 && arr1[x-1][y-1] == 1)
{
return -1;
}
if ((q1 - k1) == 0 && count == 0 && arr1[x-1][y-1] == -1)
{
return 1;
}
return 0;
}
加上一个mian()
int main()
{
game();
system("pause");
return 0;
}
头文件的定义与声明
#include"stdio.h"
#include"stdlib.h"
#include "string.h"
int jueduizhi(int x, int y);
void myprint(int arr1[10][9],int arr2[10][9]);
void panduan(int arr1[10][9], int arr2[10][9],int i,int j);
int yidongqianzhi(int arr1[10][9], int arr2[10][9]);
int yidong(int arr1[10][9], int arr2[10][9], int i, int j, int x, int y);
int bing(int arr1[10][9], int arr2[10][9], int i, int j, int x, int y);
int pao(int arr1[10][9], int arr2[10][9], int i, int j, int x, int y);
int shi(int arr1[10][9], int arr2[10][9], int i, int j, int x, int y);
int ju(int arr1[10][9], int arr2[10][9], int i, int j, int x, int y);
int xiang(int arr1[10][9], int arr2[10][9], int i, int j, int x, int y);
int ma(int arr1[10][9], int arr2[10][9], int i, int j, int x, int y);
int jiang(int arr1[10][9], int arr2[10][9], int i, int j, int x, int y);
int shengli(int arr1[10][9], int arr2[10][9], int i, int j, int x, int y);