三子棋你值得拥有(已优化可实现N子棋)


一、三子棋的实现所需文件

1.<test.c>

#include "add.h"
void game()
{
 
	char ret=0;
	char board[ROW][COL];
	inti_board(board,ROW,COL);
	print_board(board, ROW, COL);
	while(1)
	{
		player_move(board, ROW, COL);//玩家下棋
		print_board(board, ROW, COL);//打印棋盘
		ret = is_win(board, ROW, COL);//判断输赢
		if (ret != 'c')
		{
			break;
		}
		computer_move(board, ROW, COL);
		print_board(board, ROW, COL);
		ret = is_win(board, ROW, COL);
		if (ret != 'c')
		{
			break;
		}
	}
	if (ret == '#')
		printf("电脑赢了\n");
	else if (ret == '*')
		printf("玩家赢了\n");
	else if (ret == 'Q')
		printf("平局\n");
}


void menu()
{

printf("****************\n");
printf("**  1.play   ***\n");
printf("**  0.exit   ***\n");
printf("****************\n");

}


void test()
{
	srand((unsigned)time(NULL));
	int a = 0;
	 do
	{
		menu();
		printf("请选择\n");
		scanf("%d", &a);
		switch (a)
		{
			case 1: printf("三子棋\n"); 
					game(); 
					break;
			case 2: printf("退出游戏\n"); 
					break;
			default:printf("重新选择\n");
		}
	 } while (a);
}

int main()
{
	int	a = 0;
	test();

	return 0;
}

2.<game.c>


#include "add.h";
void inti_board(char board[ROW][COL], int row, int col)
{
	int i = 0;

	int j = 0;
	for (i = 0; i < row; i++)
	{
		for (j = 0; j < col; j++)
		{
			board[i][j]=' ';
		}
	}
}


void print_board(char board [ROW] [COL], int row, int col)
{

	
	int i = 0;
	int j = 0;
	for (i = 0; i < row; i++)
	{
		for (j = 0; j < col; j++)
		{
			printf(" %c ", board[i][j]);
			if (j < col-1)
				printf("|");
		}
		printf("\n");
		if (i < row - 1)
		for (j = 0; j < col; j++)
		{
			printf("---");                           
			if (j < col-1)
				printf("|");
		}
		printf("\n");
	}

}


void player_move(char board[ROW][COL], int row, int col)
{
	printf("玩家下棋\n");
	while (1)
	{
		printf("请输入坐标\n");
		int x, y;
		scanf("%d %d", &x, &y);
		if (x - 1 >= 0 && x - 1 < row && y - 1 < col && y - 1 >= 0)
		{
			if (board[x - 1][y - 1] == ' ')
			{
				board[x - 1][y - 1] = '*';
				break;
			}
			else
				printf("该坐标已被占用,请重新输入\n");
		}
		else
			printf("非法坐标重新输入\n");
	}
}

void computer_move(char board[ROW][COL], int row, int col)
{
	printf("电脑输入\n");
	while (1)
	{
		int x = rand() % row;
		int y = rand() % col;
		if (board[x ][y ] == ' ')
		{
			board[x][y] = '#';
			break;
		}	
	}
}



int is_full(char board[ROW][COL], int row, int col)
{
	int i = 0;
	for (i = 0; i < row; i++)
	{
		int j = 0;
		for (j = 0; j < col; j++)
		{
			if (board[i][j] == ' ')
				return 0;
		}
	}
	return 1;
}



char is_win(char board[ROW][COL], int row, int col)
{
	
	int flag = 0;
	int i = 0;
	int j = 0;
	for (i = 0; i < row; i++)
	{
		flag = 0;
	
		for (j = 1; j < col; j++)
		{
			
			if (board[i][j - 1] == board[i][j] && board[i][j - 1] != ' ' && board[i][j] != ' ')
			{
				flag++ ;
			}
		}
		if (flag == col - 1)
			return board[i][j - 1];
	}
	


	
	flag = 0;
	for (j = 0; j< col; j++)
	{
		flag = 0;
		for ( i= 1; i< row; i++)
		{
			if (board[i - 1][j] == board[i][j] && board[i - 1][j] != ' ' && board[i - 1][j] != ' ')
				flag ++;
		}
		if (flag == row - 1)
			return board[i-1][j];
	}



	flag = 0;
	for (i = 0; i < row-1; i++)
	{
		if (board[i][i] != board[i + 1][i + 1] || board[i][i] == ' ' || board[i + 1][i + 1] == ' ')
		{
			flag = 1;
		}
	}
	if (flag == 0)
		return board [i-1][i-1];

	flag = 0;
	for (i = 1; i < row; i++)
	{
		if (board[i-1][row - 1-(i-1) ] != board[i ][row-i-1] || board[i-1][row - 1 -(i-1)] == ' ' || board[i] [row-i-1]== ' ')
			flag = 1;
	}
	if(flag==0)
	return board[row-1][0];


	if (is_full(board, ROW, COL) == 1)
	{
		return	'Q';
	}
	return 'c';
}

3.<game.h>

#define _CRT_SECURE_NO_WARNINGS
#define ROW 3
#define COL 3
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void inti_board(char board[ROW][COL], int row, int col);//小写因为不与ROW COL冲突
void print_board(char board[ROW][COL],int row,int col);
void player_move(char board[ROW][COL], int row, int col);
void computer_move(char board[ROW][COL], int row, int col);
char is_win(char board[ROW][COL], int row, int col);

二、三子棋游戏函数(已优化)

1.test()

#define _CRT_SECURE_NO_WARNINGS
#define ROW 3//N子棋
#define COL 3//
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void inti_board(char board[ROW][COL], int row, int col);//小写因为不与ROW COL冲突
void print_board(char board[ROW][COL],int row,int col);
void player_move(char board[ROW][COL], int row, int col);
void computer_move(char board[ROW][COL], int row, int col);
char is_win(char board[ROW][COL], int row, i


void test()//选择游戏是否开始与结束
{
	srand((unsigned)time(NULL));//该函数为库函数且不能多次使用,用于电脑随机生成数字
	int a = 0;
	 do
	{
		menu();
		printf("请选择\n");
		scanf("%d", &a);
		switch (a)
		{
			case 1: printf("三子棋\n"); 
					game(); 
					break;
			case 2: printf("退出游戏\n"); 
					break;
			default:printf("重新选择\n");
		}
	 } while (a);
}

2.menu()

void menu()//给用户观看的菜单
{

printf("****************\n");
printf("**  1.play   ***\n");
printf("**  0.exit  ***\n");
printf("****************\n");

}




3.inti_board(board,ROW,COL)


void inti_board(char board[ROW][COL], int row, int col)//给棋盘初始化
{
	int i = 0;

	int j = 0;
	for (i = 0; i < row; i++)
	{
		for (j = 0; j < col; j++)
		{
			board[i][j]=' ';
		}
	}
}



4.print_board(board, ROW, COL)


void print_board(char board [ROW] [COL], int row, int col)//打印该棋盘
{

	
	int i = 0;
	int j = 0;
	for (i = 0; i < row; i++)
	{
		for (j = 0; j < col; j++)
		{
			printf(" %c ", board[i][j]);
			if (j < col-1)//每行最后一列不用打印分割行
				printf("|");
		}
		printf("\n");
		if (i < row - 1)//最后一行不用打印分割行
		for (j = 0; j < col; j++)
		{
			printf("---");                           
			if (j < col-1)//每行最后一列不用打印分割行
				printf("|");
		}
		printf("\n");
	}

}


图片:在这里插入图片描述

5.player_move(board, ROW, COL)



void player_move(char board[ROW][COL], int row, int col)//玩家下棋
{
	printf("玩家下棋\n");
	while (1)
	{
		printf("请输入坐标\n");
		int x, y;
		scanf("%d %d", &x, &y);
		if (x - 1 >= 0 && x - 1 < row && y - 1 < col && y - 1 >= 0)//在合法坐标内才可下棋
		{
			if (board[x - 1][y - 1] == ' ')//判断该坐标是否有棋
			{
				board[x - 1][y - 1] = '*';//玩家下棋
				break;
			}
			else
				printf("该坐标已被占用,请重新输入\n");
		}
		else
			printf("非法坐标重新输入\n");
	}
}


6.computer_move(board, ROW, COL);

void computer_move(char board[ROW][COL], int row, int col)//电脑下棋
{
	printf("电脑输入\n");
	while (1)
	{
		int x = rand() % row;//该函数为库函数随机生成坐标
		int y = rand() % col;
		if (board[x ][y ] == ' ')
		{
			board[x][y] = '#';//电脑下棋
			break;
		}	
	}
}


7.is_win(board, ROW, COL)//该函数已优化可以实现N×N子棋;

char is_win(char board[ROW][COL], int row, int col)//判断是否输赢,返回值为一个字符
{
	int flag = 0;
	int i = 0;
	int j = 0;
	for (i = 0; i < row; i++)//共row行
	{
		flag = 0;
	
		for (j = 1; j < col; j++)//对比col-1次
		{
			
			if (board[i][j - 1] == board[i][j] && board[i][j - 1] != ' ' && board[i][j] != ' ')//判断一行中左右坐标是否相等且是否为空
			{
				flag++ ;//如果前后坐标相等且不是空坐标则加1
			}
		}
		if (flag == col - 1)//共对比col-1次,如果flag加了col-1次说明一行中坐标全都相等且不为空坐标
			return board[i][j - 1];返回该坐标的字符
	}
	


	//(同上思路)
	flag = 0;//重新赋值
	for (j = 0; j< col; j++)//循环col次
	{
		flag = 0;
		for ( i= 1; i< row; i++)//外循环一次内循环row-1次
		{
			if (board[i - 1][j] == board[i][j] && board[i - 1][j] != ' ' && board[i - 1][j] != ' ')//判断一列中上下坐标是否相等,且是否为空
				flag ++;//如果上下坐标相等且不是空坐标则加1
		}
		if (flag == row - 1)//共对比row-1次,如果flag加了row-1次说明一列中坐标全都相等且不为空坐标
			return board[i-1][j];返回该坐标的字符
	}



	flag = 0;
	for (i = 0; i < row-1; i++)
	{
		if (board[i][i] != board[i + 1][i + 1] || board[i][i] == ' ' || board[i + 1][i + 1] == ' ')//从左上到右下对角线的两两对比
		{
			flag = 1;
		}
	}
	if (flag == 0)
		return board [i-1][i-1];

	flag = 0;
	for (i = 1; i < row; i++)
	{
		if (board[i-1][row - 1-(i-1) ] != board[i ][row-i-1] || board[i-1][row - 1 -(i-1)] == ' ' || board[i] [row-i-1]== ' ')//从右上到左下的两两对比
			flag = 1;//如果其中有一个对比失败flag为1
	}
	if(flag==0)//说明row-1次对比都相等且不为空坐标
	return board[row-1][0];


	if (is_full(board, ROW, COL) == 1)//在玩家和电脑都没赢的情况下,该函数判断棋盘受否满了,因为该函数为is_win所使用,所以不用在<game.h>中声明
	{
		return	'Q';
	}
	return 'c';
}


图片:在这里插入图片描述

图片:在这里插入图片描述

8.is_full(board, ROW, COL)



int is_full(char board[ROW][COL], int row, int col)//判断棋盘是否满了
{
	int i = 0;
	for (i = 0; i < row; i++)
	{
		int j = 0;
		for (j = 0; j < col; j++)
		{
			if (board[i][j] == ' ')//如果有一个空坐标返回0
				return 0;
		}
	}
	return 1;
}


三. 学习鸡汤

每个人都有完美的梦想,只有坚持,你的梦想才能实现,
为了明天的offer加油,加油!!!

评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值