小游戏--三子棋——N子棋(实现)

三子棋大家应该都不陌生吧

三子棋是一种民间传统游戏,又叫九宫棋、圈圈叉叉棋、一条龙、井字棋等。

我们今天来一个c语言三子棋的进阶,n子棋的实现过程

1.我们首先可以创建一个三子棋.c源文件,从main函数开始,作为代码运行入口是前提开始写代码。

2.在创建一个game.c源文件来实现游戏的具体过程代码。

3再来一个game.h头文件来包含声明自己写的函数。

游戏规则:

在一个棋盘上,两方选手对弈,先将三个棋连成一条线的一方获胜(包括斜线)

下面来具体看下实现代码

三子棋.c

#define _CRT_SECURE_NO_WARNINGS 1

#include"game.h"

void menu(void)
{
	printf("*********************\n");
	printf("*******1. play*******\n");
	printf("*******0. quit*******\n");
	printf("*********************\n");
}

void game()
{
	int cow = 0, rol = 0;
	scanf("%d %d", &cow, &rol);
	char board[COW][ROL];
	init_board(board, cow, rol); //初始化数组(棋盘)
	print_board(board, cow, rol); //打印棋盘	
	while (1)
	{
		play_game(board, cow, rol); //玩家下

		print_board(board, cow, rol);
		
//判断输赢 返回'#'玩家赢 ‘*’电脑赢,‘P’平局,‘C’继续游戏

		 char ret = re_board(board, cow, rol);
		
		if ( ret == '#')
		{
			printf("玩家赢了\n");
			break;//
		}
		else if (ret == 'C')
		{
			printf("电脑下棋\n");
		}
		else if(ret == 'P')
		{
			printf("平局\n");
			break;
		}
		
		comp_game(board, cow, rol); //电脑下

		print_board(board, cow, rol);
		
		if( re_board(board, cow, rol)== '*')
		{
			printf("电脑赢了\n");
			break;
		}
		else if (re_board(board, cow, rol) == 'C')
		{
			printf("玩家下棋\n");
		}
		else if(re_board(board, cow, rol) == 'P')
		{
			printf("平局\n");
			break;
		}

	}
}

int main()
{
	srand(time(NULL));
	int input = 0;
	do
	{
		menu();
		scanf("%d", &input);
		switch (input)
		{
		case 1:
			game();
			break;
		case 0:
			printf("已退出游戏\n");
			break;
	    default:
			printf("输入错误\n");
			break;
		}
	} while (input);

	return 0;
}

game.c

#define _CRT_SECURE_NO_WARNINGS 1

#include"game.h"

void init_board(char board[COW][ROL], int cow, int rol)
{
	for(int i=0; i<cow; i++)
	{
		for(int j=0; j<rol; j++)
		{
			board[i][j] = ' ';
		}
	}
}

void print_board(char board[COW][ROL], int cow, int rol)
{
	for(int i=0; i<cow; i++)
	{
		for(int j=0; j<rol; j++)
		{
			printf(" %c ", board[i][j]);
			if(j < rol-1)
			{
				printf("|");
			}
		}
		printf("\n");
		for(int j=0; j<rol; j++)
		{
			if(i < cow - 1)
			{
				printf("---");//
			}
			if (i < cow - 1 && j < rol - 1)
			{
				printf("|");
			}
		}
		printf("\n");
	}
}

void play_game(char board[COW][ROL], int cow, int rol)
{
	printf("请输入下的坐标\n");
	int x = 0, y = 0;
	while (1)
	{
		scanf("%d %d", &x, &y);
		if (x >= 1 && x <= cow && y >= 1 && y <= rol)
		{
			if (board[x - 1][y - 1] = ' ')
			{
				board[x - 1][y - 1] = '#';
				break;
			}
			else
			{
				printf("坐标被占用,请重新输入\n");
			}
		}
		else
		{
			printf("非法坐标\n");
		}
	}
}

void comp_game(char board[COW][ROL], int cow, int rol)
{
	while (1)
	{
		int x = rand() % cow;
		int y = rand() % rol;
		if (board[x][y] == ' ')
		{
			board[x][y] = '*';
			break;
		}
	}
}

char re_board(char board[COW][ROL], int cow, int rol)
{
	//行判断
	int count1 = 0;
	int count2 = 0;
	for(int i=0; i<cow; i++)
	{
		for(int j=0; j<rol - 1; j++)
		{
			if ((board[i][j]) == (board[i][j + 1]) && board[i][j] == '#')
			{
				count1++;
			}
			if (board[i][j] == board[i][j + 1] && board[i][j] == '*')
			{
				count2++;
			}			
		}		
		if(count1 == (rol - 1))
		{
			return '#';
		}
		if(count2 == (rol - 1))
		{
			return '*';
		}
	}
	//列判断
	int count3 = 0;
	int count4 = 0;
	for(int j=0; j<rol; j++)
	{
		for(int i=0; i<cow - 1; i++)
		{
			if (board[i][j] == board[i + 1][j] && board[i][j] == '#')
			{
				count3++;
			}
			if (board[i][j] == board[i + 1][j] && board[i][j] == '*')
			{
				count4++;
			}			
		}		
		if(count3 == (cow - 1))
		{
			return '#';
		}
		if(count4 == (cow - 1))
		{
			return '*';
		}
	}
	//对角线判断
	int count5 = 0;
	int count6 = 0;
	for(int i=0,j=0; (i < cow-1 && j < rol-1); i++,j++)
	{
		if (board[i][j] == board[i + 1][j + 1] && board[i][j] == '#')
		{
			count5++;
		}
		if (board[i][j] == board[i + 1][j + 1] && board[i][j] == '*')
		{
			count6++;
		}		
	}
	if(count5 == cow-1)
	{
		return '#';
	}
	if(count6 == cow-1)
	{
		return '*';
	}
	//对角线2判断
	int count7 = 0;
	int count8 = 0;
	for(int i=0,j=2; (i < cow-1 && j > 0); i++,j--)
	{
		if (board[i][j] == board[i + 1][j - 1] && board[i][j] == '#')
		{
			count7++;
		}
		if (board[i][j] == board[i + 1][j - 1] && board[i][j] == '*')
		{
			count8++;
		}
	}
	if(count7 == cow-1)
	{
		return '#';	
	}	
	if(count8 == cow-1)
	{
		return '*';	
	}
	//判断平局
	int count = 0;
	for(int i=0; i<cow; i++)
	{
		for(int j=0; j<rol; j++)
		{
			if(board[i][j] != ' ')
			{
				count++;
			}
		}
	}
	if(count == (cow*rol))
	{
		return 'P';
	}
	//继续游戏
	return 'C';
}

game.h


#include<stdio.h>

#define COW 3
#define ROL 3
//

void init_board(char board[COW][ROL], int cow, int rol);

void print_board(char board[COW][ROL], int cow, int rol);

void play_game(char board[COW][ROL], int cow, int rol);

void comp_game(char board[COW][ROL], int cow, int rol);

char re_board(char board[COW][ROL], int cow, int rol);
  • 9
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值