C语言实现井子棋(多子棋)游戏

将游戏实现分文件。防止文件臃肿,使其目录结构清晰

头文件(game.h),主函数文件(主入口),和函数实现文件(game.c)



前言

当然电脑是随机的没有相应算法实现
电脑计算下棋判断下哪里赢,所以这里电脑是随机下的都是随机值


一、主函数文件(主入口)

#include <stdio.h>
#include "game.h"


void meun() {
	printf("@@@@@@@@@@@@@@@@@@@@\n");
	printf("***1.play**2.exit**\n");
	printf("@@@@@@@@@@@@@@@@@@@@\n");

}
void game() {
	char ret = 0;

	//数组存放的棋盘的信息
	char board[Row][Col] = {0};//开始全部都为空格
	
	//初始化棋盘
	IntBad(&board,Row,Col);
	//建立棋盘
	DpBord(board,Row,Col);
	//下棋
	while (1)
	{
		//玩家先手
	playMove(board, Row, Col);
		
		DpBord(board, Row, Col);
		//判读输赢
	ret=	isWin(board, Row, Col);
		if (ret !='C')

		{
			break;
		}
		//电脑
	    compMove(board, Row, Col);
		
		//判读输赢
		
		DpBord(board, Row, Col);
		ret = isWin(board, Row, Col);
		if (ret != 'C')

		{
			break;
		}
	}
	if (ret == '*')

	{
		printf("玩家赢");
	}
	else if(ret=='#')
	{
		printf("电脑赢");
	}
	else
	{
		printf("平局!");
	}
}

void play() {
      int put = 0;
	  srand((unsigned int)time(NULL));
		do
{
			meun();

			printf("请选择\n");
			scanf("%d", &put);
			
		switch (put)
		{
		case 1:
			printf("三子棋\n");
			game();
			break;
		case  2:
			printf("退出游戏\n");
			break;

		default:
			printf("选择错误\n");
			break;
		}

	} while (put);
}
int main()
{
	//三子棋游戏

	play();

}

二、头文件(game.h)

1.引入库

代码如下(示例):

#pragma once
#define Row 3
#define Col 3
#include <stdio.h>
#include <stdlib.h>
#include <time.h>


void IntBad(char* board[Row][Col], int row, int col);
void  DpBord(char board[Row][Col], int row, int col);
void playMove(char board[Row][Col], int row, int col);
void compMove(char board[Row][Col], int row, int col);

char  isWin(char board[Row][Col], int row, int col);

三,头文件函数实现文件(game.c)

代码如下(示例):

#include "game.h"

void IntBad(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  DpBord(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++) {
			//1.打印- -行的数据
			printf(" %C ", board[i][j]);
			if (j < col - 1)
				printf("|");
		}
		printf("\n");
		//2.打印分割行
		if (i < row - 1) {
			for (j = 0;j < col;j++) {
				printf("---");

				if (j < col - 1)
					printf("|");
			}
	 }

	printf("\n");
	}
}

void playMove(char board[Row][Col], int row, int col) {
	int x = 0;
	int y = 0;
	printf("玩家走,输入坐标\n");
	
	while (1)
	{
		scanf("%d%d", &x,& y);
//判断合法性
	if (x >= 1 && x <= row && y >= 1, y <= col)
	{
		if (board[x - 1][y - 1] == ' ')
		{
			board[x - 1][y - 1] = '*';
			break;
		}
		else{ printf("该坐标已经下过了"); }
	
	}
	else
	{
		printf("非法坐标");
		
	}
	}
	
}
void compMove(char board[Row][Col], int row, int col)
{
	int x = 0;
	int y = 0;
	printf("电脑走\n");
	
		while (1)
		{
    x = rand() % row;//生成0-2的值
	y = rand() % col;
	if (board[x][y] == ' ') {
		board[x ][y ] = '#';
		break;
		}
	}
}
//1满了0没有

int IsFull(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++)
			if (board[i][j] == ' ')
			{
				return 0;
	  }
	}
	
		return 1;
		

}
char isWin(char board[Row][Col], int row, int col) {

	int i = 0;
	//横三行
	for ( i = 0; i < row; i++)
	{
		if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][1] != ' ')
			
			return board[i][1];

	}
	//竖三行
	for (i = 0; i < col; i++){
		if (board[0][i] == board[1][i] && board[1][i] == board[2][i] && board[1][i] != ' '){
		return board[1][i];}

  }
	//两个对角线
	if (board[0][0]==board[1][1]  &&  board[1][1] == board[2][2] && board[1][1] != ' ') {
		return board[1][1];
	}
	if (board[2][0] == board[1][1] && board[1][1] == board[0][2] && board[1][1] != ' '){
		return board[1][1];}
	//判断平局
	//判断是否平局
	if (1 == IsFull(board, Row, Col))
	{
		return 'Q';
	}
	return 'C';


}
	

横三行,竖三行都是。还有对角线均为判断输赢的地方。

srand((unsigned int)time(NULL));

库函数,srand 可以查找相关用法

x = rand() % row;//srand 生成值太大的时候%,使其生成0-2的随机值
y = rand() % col;


![在这里插入图片描述](https://img-blog.csdnimg.cn/20200824112338257.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0FsaWNlc2E=,size_16,color_FFFFFF,t_70#pic_center)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值