C语言写的五子棋(供参考)

瞎几把的写五子棋

#include <stdio.h>
#include <string.h>
#define CHESSBOARDROW 19
#define CHESSBOARDCOL 19
char chessboard[CHESSBOARDROW][CHESSBOARDCOL];

void initchessboard()
{
	memset(chessboard,'0',CHESSBOARDROW*CHESSBOARDROW);
	int k1=1,k2=1;
	chessboard[0][0]=' ';
	for(int j=1;j<CHESSBOARDROW;j++)
	{
			if(j%2 == 0)
			{	
				chessboard[0][j]=(k1+'0');
				k1++;
			}
		}
		for(int i=1;i<CHESSBOARDROW;i++)
		{	
				if(i%2 == 0)
			{
				chessboard[i][0]=(k2+'0');
				k2++;
			}
		}
	for(int i=1;i<CHESSBOARDROW;i++)
	{
		for(int j=1;j<CHESSBOARDCOL;j++)
			if(i%2==0 && j%2 ==0)
				chessboard[i][j] = '.';
	}
}

void showchessboard()
{
		
		for(int i=0;i<CHESSBOARDROW;i++)
		{
			
			for(int j=0;j<CHESSBOARDCOL;j++)
				if(i%2==0 && j%2 ==0)
					printf("%3c",chessboard[i][j]);
				else
					printf("%c",' ');
			printf("\n");
		}
}
int is_vaild(int x,int y)
{
	//出了边界违法,这个地方有旗子也违法
	if(x<1 || x>9 || y<1 || x>9)
		return 0;
	if(chessboard[x*2][y*2]!='.')
		return 0;
	chessboard[x*2][y*2] = 'x';
	return 1;
}
int is_again(int ret)
{
	if(ret == 0)
		return -1;
	if(ret == 3)
		printf("平局!\n");
	else
		printf("玩家%d获胜!\n",ret);
	int again = -1;
	do
	{
		printf("是否继续玩:(0:退出,1:继续) ");
		scanf("%d",&again);
	}while(again!=0 && again!=1);
	return again;
}

int Judge_who_win(int play,int x,int y)
{
	char chess[2] = {0};
	chess[0] = 'x';
	if(play != 1)
		chess[0] = 'o';
	int flag = 0,is_full = 0;
	chessboard[x*2][y*2]=chess[0];
	for(int k = 0;k<4;k++) //四个方向的判断
	{
		int step=8,is_win =0;
	for(int i=0;i<5;i++) //5个位置的移动
	{
		for(int j=0;j<9;j+=2) //判断五个旗子
		{
			int px,py;
			if(k==0)
			{
				px = x*2-step+j;
				py = y*2-step+j;
			}
			else if(k==1)
			{
				px = x*2;
				py = y*2-step+j;
			}
			else if(k==2)
			{
				px = x*2+step-j;
				py = y*2-step+j;
			}
			else if(k==3)
			{
				px = x*2-step+j;
				py = y*2;
			}
			if((px > 1 && px < CHESSBOARDROW) && (py > 1 && py < CHESSBOARDCOL) && chessboard[px][py] == chess[0])
				is_win++;
			else
			{
				is_win = 0;
				break;
			}
		}
		if(is_win == 5)
		{
			flag = 1;
			break;
		}
		step-=2;
		is_win=0;
	}
	if(flag)
		break;
		}
	if(flag)
	{
		if(play!=1)
			play = 2;
		return play;
	}
end:
	for(int i =2;i<CHESSBOARDROW;i+=2)
	{
		for(int j =2;i<CHESSBOARDCOL;j+=2)
		{
			if(chessboard[i][j] == '.')
			{
				is_full = 0;
				break;
			}
		}
	}
	if(is_full)
		return 3;
	return 0;
}

int main()
{
	//玩家1是x,玩家2是o
	//平局是3
	int playx,playy;
	int again = 1,vaildret,play,ret =0;
	do
	{
		if(again == 1)
		{
			initchessboard();
			play = 1,vaildret=1,ret=0,again = 0;
			showchessboard();
		}
		do
		{
			if(!vaildret)
				printf("落子位置违法,");
			if(play<0)
				printf("请玩家%d落子(x y): ",2);
			else
				printf("请玩家%d落子(x y): ",play);
			scanf("%d %d",&playx,&playy); //非法输入会死循环
			vaildret = is_vaild(playx,playy);
		}while(!vaildret);
		ret = Judge_who_win(play,playx,playy);
		showchessboard();
		again = is_again(ret);
		play*=-1;
	}while (again);
	printf("退出游戏!\n");
	return 0;
}

懒死了----------------------

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
SQLAlchemy 是一个 SQL 工具包和对象关系映射(ORM)库,用于 Python 编程语言。它提供了一个高级的 SQL 工具和对象关系映射工具,允许开发者以 Python 类和对象的形式操作数据库,而无需编大量的 SQL 语句。SQLAlchemy 建立在 DBAPI 之上,支持多种数据库后端,如 SQLite, MySQL, PostgreSQL 等。 SQLAlchemy 的核心功能: 对象关系映射(ORM): SQLAlchemy 允许开发者使用 Python 类来表示数据库表,使用类的实例表示表中的行。 开发者可以定义类之间的关系(如一对多、多对多),SQLAlchemy 会自动处理这些关系在数据库中的映射。 通过 ORM,开发者可以像操作 Python 对象一样操作数据库,这大大简化了数据库操作的复杂性。 表达式语言: SQLAlchemy 提供了一个丰富的 SQL 表达式语言,允许开发者以 Python 表达式的方式编复杂的 SQL 查询。 表达式语言提供了对 SQL 语句的灵活控制,同时保持了代码的可读性和可维护性。 数据库引擎和连接池: SQLAlchemy 支持多种数据库后端,并且为每种后端提供了对应的数据库引擎。 它还提供了连接池管理功能,以优化数据库连接的创建、使用和释放。 会话管理: SQLAlchemy 使用会话(Session)来管理对象的持久化状态。 会话提供了一个工作单元(unit of work)和身份映射(identity map)的概念,使得对象的状态管理和查询更加高效。 事件系统: SQLAlchemy 提供了一个事件系统,允许开发者在 ORM 的各个生命周期阶段插入自定义的钩子函数。 这使得开发者可以在对象加载、修改、删除等操作时执行额外的逻辑。
以下是一个简单的五子棋游戏的C语言代码,仅供参考: ```c #include <stdio.h> #include <stdlib.h> #define SIZE 15 #define EMPTY 0 #define BLACK 1 #define WHITE 2 int board[SIZE][SIZE]; void init_board() { int i, j; for (i = 0; i < SIZE; i++) { for (j = 0; j < SIZE; j++) { board[i][j] = EMPTY; } } } void print_board() { int i, j; printf(" "); for (i = 0; i < SIZE; i++) { printf("%2d", i); } printf("\n"); for (i = 0; i < SIZE; i++) { printf("%2d", i); for (j = 0; j < SIZE; j++) { switch (board[i][j]) { case EMPTY: printf(" ."); break; case BLACK: printf(" *"); break; case WHITE: printf(" O"); break; } } printf("\n"); } } int is_valid(int x, int y) { if (x < 0 || x >= SIZE || y < 0 || y >= SIZE) { return 0; } if (board[x][y] != EMPTY) { return 0; } return 1; } int check(int x, int y, int color) { int i, j, t; int count = 1; // 横向 for (i = x + 1; i < SIZE && board[i][y] == color; i++, count++); for (i = x - 1; i >= 0 && board[i][y] == color; i--, count++); if (count >= 5) { return 1; } // 纵向 for (i = y + 1, count = 1; i < SIZE && board[x][i] == color; i++, count++); for (i = y - 1; i >= 0 && board[x][i] == color; i--, count++); if (count >= 5) { return 1; } // 斜向(左上右下) for (i = x + 1, j = y + 1, count = 1; i < SIZE && j < SIZE && board[i][j] == color; i++, j++, count++); for (i = x - 1, j = y - 1; i >= 0 && j >= 0 && board[i][j] == color; i--, j--, count++); if (count >= 5) { return 1; } // 斜向(左下右上) for (i = x + 1, j = y - 1, count = 1; i < SIZE && j >= 0 && board[i][j] == color; i++, j--, count++); for (i = x - 1, j = y + 1; i >= 0 && j < SIZE && board[i][j] == color; i--, j++, count++); if (count >= 5) { return 1; } return 0; } int main() { int x, y; int color = BLACK; init_board(); print_board(); while (1) { printf("请输入落子位置(x y): "); scanf("%d%d", &x, &y); if (!is_valid(x, y)) { printf("无效的位置!\n"); continue; } board[x][y] = color; print_board(); if (check(x, y, color)) { printf("恭喜,%s方胜利!\n", color == BLACK ? "黑" : "白"); break; } color = 3 - color; // 切换对手 } return 0; } ``` 此代码实现了一个简单的五子棋游戏,规则如下: 1. 棋盘大小为15x15; 2. 玩家以黑色(*)和白色(O)两种颜色进行对弈; 3. 每次玩家输入落子位置,如果该位置已经有棋子或者越界,则提示无效位置; 4. 当一方落子后,判断是否胜利,如果胜利则输出胜利信息并结束游戏; 5. 如果没有胜利,则切换对手,继续游戏。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值