马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
因为是规则自编,说好了不好玩不要骂我哈
#include <stdio.h>
#include <stdbool.h>
#include <math.h>
#include <stdlib.h> // 用于随机数生成
#include <time.h> // 用于种子初始化
#define BOARD_SIZE 5
#define COLOR_NONE "\033[0m" //表示清除前面设置的格式
#define RED "\033[1;31;40m" //40表示背景色为黑色, 1 表示高亮
#define YELLOW "\033[1;33;40m"
// 定义棋盘
char board[BOARD_SIZE][BOARD_SIZE];
// 初始化棋盘
void initializeBoard() {
int i,j;
for (i = 0; i < BOARD_SIZE; i++) {
for (j = 0; j < BOARD_SIZE; j++) {
board[i][j] = '*'; // 初始化为*
}
}
// 设置玩家初始棋子位置
board[0][0] = board[0][1] = board[0][3] = board[0][4] = '1'; // 玩家1的兵
board[0][2] = '0'; // 玩家1的王
board[4][0] = board[4][4] = board[4][1] = board[4][3] = 's'; // 玩家0的兵
board[4][2] = 'k'; // 玩家0的王
}
// 打印棋盘
void printBoard() {
int i,j;
printf(RED" 0 1 2 3 4\n"COLOR_NONE);
for (i = 0; i < BOARD_SIZE; i++) {
printf(RED"%d "COLOR_NONE,i);
for (j = 0; j < BOARD_SIZE; j++) {
printf("%c ", board[i][j]);
}
printf("\n");
}
}
// 判断是否游戏结束
bool isGameOver() {
// 遍历棋盘,查找王的位置
int king0_row = -1, king0_col = -1;
int king1_row = -1, king1_col = -1;
int i,j;
for (i = 0; i < BOARD_SIZE; i++) {
for (j = 0; j < BOARD_SIZE; j++) {
if (board[i][j] == '0') {
king0_row = i;
king0_col = j;
} else if (board[i][j] == 'k') {
king1_row = i;
king1_col = j;
}
}
}
// 检查游戏结束条件
if ((king0_row == -1 && king0_col == -1) || (king1_row == -1 && king1_col == -1)) {
// 王相遇,游戏结束
return true;
} else if ((king0_row == 4 && king0_row == 2) ||
(king1_row == 0 && king1_row == 2)) {
// 玩家0或玩家1的王到达对方的初始位置,游戏结束
return true;
}
return false;
}
int main() {
initializeBoard();
int currentPlayer,count_0 = 0,count_1 = 0; // 0表示玩家0,1表示玩家1
int row, col,x,y;
printf("游戏规则:\n棋盘:5*5\n棋子:兵(玩家0:1 or 玩家1:s)和 王(玩家0:0 or 玩家1:k)\n移动条件:根据你的回合数决定,奇数只能直行,偶数只能斜行\n胜利条件:你的王击杀对面的王或者你的王走到对面王的初始位置\n");
printf(RED"!!!\n请输入整数谢谢\n!!!\n"COLOR_NONE);
srand(time(NULL));
currentPlayer = rand() % 2;//随机决定先手
while (!isGameOver()) {
if (currentPlayer)
{
count_1 ++;
printf(YELLOW"玩家1的回合数:%d\n"COLOR_NONE,count_1);
}
else
{
count_0++;
printf(YELLOW"玩家0的回合数:%d\n"COLOR_NONE,count_0);
}//更新个人回合数
printf("当前棋盘状态:\n");
printBoard();
printf("玩家 %d,请输入您要移动的棋子的坐标 (row col): ", currentPlayer);
scanf("%d %d", &row, &col);
// 在这里实现棋子移动的逻辑,根据规则更新棋盘状态。
//检查输入
if (row >= BOARD_SIZE || col >= BOARD_SIZE || board[row][col] == '*')
{
printf("您的输入有误!\n");
if (currentPlayer)
{
count_1--;
}
else
{
count_0--;
}
continue;
}
if ((currentPlayer == 0 && (board[row][col] == 's' || board[row][col] == 'k')) || currentPlayer == 1 && (board[row][col] == '1' || board[row][col] == '0'))
{
printf("您的输入有误!\n");
if (currentPlayer)
{
count_1--;
}
else
{
count_0--;
}
continue;
}
printf("玩家 %d,请输入您要移动到的位置的坐标 (row col):",currentPlayer);
scanf("%d %d",&x,&y);
if (x < 0 || x >= BOARD_SIZE || y < 0 || y >= BOARD_SIZE) {
printf("无效的坐标!请重新输入:");
if (currentPlayer)
{
count_1--;
}
else
{
count_0--;
}
continue; // 输入无效,重新循环
}
// 检查移动是否符合规则
int dx = x - row;
int dy = y - col;
if (currentPlayer)
{
if (count_1 % 2)
{
if ((dx == 0 && dy == -1) || (dx == 0 && dy == 1) || (dx == 1 && dy == 0) || (dx == -1 && dy == 0))
{
;
}
else
{
printf("移动不符合规则!请重新输入:");
if (currentPlayer)
{
count_1--;
}
else
{
count_0--;
}
continue; // 移动不符合规则,重新循环
}
}
else
{
if ((dx == -1 && dy == -1) || (dx == 1 && dy == 1) || (dx == -1 && dy == 1) || (dx == 1 && dy == -1))
{
;
}
else
{
printf("移动不符合规则!请重新输入:");
if (currentPlayer)
{
count_1--;
}
else
{
count_0--;
}
continue; // 移动不符合规则,重新循环
}
}
}
else
{
if (count_0 % 2)
{
if ((dx == 0 && dy == -1) || (dx == 0 && dy == 1) || (dx == 1 && dy == 0) || (dx == -1 && dy == 0))
{
;
}
else
{
printf("移动不符合规则!请重新输入:");
if (currentPlayer)
{
count_1--;
}
else
{
count_0--;
}
continue; // 移动不符合规则,重新循环
}
}
else
{
if ((dx == -1 && dy == -1) || (dx == 1 && dy == 1) || (dx == -1 && dy == 1) || (dx == 1 && dy == -1))
{
;
}
else
{
printf("移动不符合规则!请重新输入:");
if (currentPlayer)
{
count_1--;
}
else
{
count_0--;
}
continue; // 移动不符合规则,重新循环
}
}
}
// 检查是否有自己的棋子或对方的王
if ((currentPlayer == 0 && (board[x][y] == '1' || board[x][y] == '0')) ||
(currentPlayer == 1 && (board[x][y] == 's' || board[x][y] == 'k'))) {
printf("该位置有自己的棋子!请重新输入\n");
if (currentPlayer)
{
count_1--;
}
else
{
count_0--;
}
continue; // 位置上有自己的棋子,重新循环
}
//棋子的移动
if (board[x][y] == '*')
{
board[x][y] = board[row][col];
board[row][col] = '*';
//切换玩家
currentPlayer = 1 - currentPlayer;
continue;
}
//判断兵之间的击杀,如果不能击杀,就返回输入错误
if ((currentPlayer == 0 && board[row][col] == '1') || (currentPlayer == 1 && board[row][col] == 's'))
{
if (currentPlayer == 0 && board[x][y] == 's')//可以击杀
{
board[x][y] = '1';
board[row][col] = '*';
currentPlayer = 1 - currentPlayer;
continue;
}
if (currentPlayer == 1 && board[x][y] == '1')
{
board[x][y] = 's';
board[row][col] = '*';
currentPlayer = 1 - currentPlayer;
continue;
}
if (currentPlayer == 0 && board[x][y] == 'k')
{
printf("抱歉,根据规则你不能击杀这枚棋子,请重新输入!\n");
if (currentPlayer)
{
count_1--;
}
else
{
count_0--;
}
continue;
}
if (currentPlayer == 1 && board[x][y] == '0')
{
printf("抱歉,根据规则你不能击杀这枚棋子,请重新输入!\n");
if (currentPlayer)
{
count_1--;
}
else
{
count_0--;
}
continue;
}
}
//判断王之间的击杀
if ((currentPlayer == 0 && board[row][col] == '0') || (currentPlayer == 1 && board[row][col] == 'k'))
{
if (currentPlayer == 0 && board[x][y] == 'k')//可以击杀
{
board[x][y] = board[row][col];
board[row][col] = '*';
currentPlayer = 1 - currentPlayer;
}
if (currentPlayer == 1 && board[x][y] == '0')
{
board[x][y] = board[row][col];
board[row][col] = '*';
currentPlayer = 1 - currentPlayer;
}
if (currentPlayer == 0 && board[x][y] == 's')
{
printf("抱歉,根据规则你不能击杀这枚棋子,请重新输入!\n");
if (currentPlayer)
{
count_1--;
}
else
{
count_0--;
}
continue;
}
if (currentPlayer == 1 && board[x][y] == '1')
{
printf("抱歉,根据规则你不能击杀这枚棋子,请重新输入!\n");
if (currentPlayer)
{
count_1--;
}
else
{
count_0--;
}
continue;
}
}
}
printf("游戏结束!玩家 %d 获胜!\n", 1-currentPlayer);
return 0;
}
|