c语言实现三子棋/五子棋

最近呢,在学习之余搞了个五子棋,使用纯c语言实现的,虽然简单,但是也挺有意思的。

分享出来让大家看看

这是test.c

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


void test() {
	//游戏整体逻辑
	int input = 1;
	while (input) {
		menu1();
		scanf("%d",&input);
		switch (input) {
		case 1:
			game1();
			break;
		case 0:
			printf("退出游戏!\n");
			break;
		default :
			printf("输入错误\n");
			break;
		}
	}
}



int main() {
	test();
	return 0;
}

game1.h

#include <stdio.h>

#define WEIGHT 15
#define HEIGHT 10
#define WIN 5

char arr[WEIGHT][HEIGHT];

void menu1();
void game1();
void chessboard();
void initArr();
int win(int x, int y);

game1.c

#include "game1.h"

//游戏逻辑函数
void game1() {
	int x = 0;
	int y = 0;
	//初始化棋盘数组
	initArr();
	//打印棋盘
	chessboard();
	while (1) {
		
		int tag = 1;
		while (tag) {
			printf("玩家1落子(x,y):>");
			scanf("%d%d", &x, &y);
			if ((x < 1 || x > WEIGHT) && (y < 1 || y > HEIGHT)) {
				printf("落子坐标不合理!\n");
				continue;
			}
			if (arr[x - 1][y - 1] == ' ') {
				arr[x - 1][y - 1] = 'O';
				tag = 0;
			}
			else
				printf("落子坐标不合法\n");
			chessboard();
		}
		//判断函数!
		if (win(x - 1,y - 1)) {
			printf("玩家一胜利!\n");
			break;
		}
		tag = 1;
		while (tag) {
			printf("玩家2落子(x,y):>");
			scanf("%d%d", &x, &y);
			if ((x < 1 || x > WEIGHT) && (y < 1 || y > HEIGHT)) {
				printf("落子坐标不合理!\n");
				continue;
			}
			if (arr[x - 1][y - 1] == ' ') {
				arr[x - 1][y - 1] = 'X';
				tag = 0;
			}
			else
				printf("落子坐标不合法\n");
			chessboard();
		}
		//判断函数!
		if (win(x - 1, y - 1)) {
			printf("玩家二胜利!\n");
			break;
		}
		tag = 1;
	}

}

int win(int x, int y) {
	char c = arr[x][y];//O,X
	int count1 = 1;//横向的寻找标记
	int count2 = 1;//纵向的寻找标记
	int count3 = 1;//一三坐标的寻找标记
	int count4 = 1;//二四坐标的寻找标记
	int _tag = 1;
	int tag_ = 1;
	for (int i = 1; i <= WIN; i++) {
		if (count1 != 0) {
			if (_tag ) {
				if ((x - i) >= 0) {
					if (arr[x - i][y] == c)
						count1++;
					else
						_tag = 0;
				}else
					_tag = 0;
			}
			if (tag_ ) {
				if ((x + i) < WEIGHT) {
					if (arr[x + i][y] == c)
						count1++;
					else
						tag_ = 0;
				}
				else
					tag_ = 0;
			}
			if (((_tag == 0) && (tag_ == 0)) && (count1 < WIN)) {
				count1 = 0;
			}
		}
		_tag = 1;
		tag_ = 1;
		if (count2 != 0) {
			if (_tag) {
				if ((y - i) >= 0) {
					if (arr[x][y - i] == c)
						count2++;
					else
						_tag = 0;
				}
				else
					_tag = 0;
			}
			if (tag_) {
				if (y + i < HEIGHT) {
					if (arr[x][y + i] == c)
						count2++;
					else
						tag_ = 0;
				}
				else
					tag_ = 0;
			}
			if (((_tag == 0) && (tag_ == 0)) && (count2 < WIN)) {
				count2 = 0;
			}
		}
		_tag = 1;
		tag_ = 1;
		if (count3 != 0) {
			if (_tag) {
				if (x - i >= 0 && y - i >= 0) {
					if (arr[x - i][y - i] == c)
						count3++;
					else
						_tag = 0;
				}
				else
					_tag = 0;
			}
			if (tag_) {
				if (x + i < WEIGHT && y + i < HEIGHT){
					if (arr[x + i][y + i] == c)
						count3++;
					else
						tag_ = 0;
				}
				else
					tag_ = 0;
			}
			if (((_tag == 0) && (tag_ == 0)) && (count3 < WIN)) {
				count3 = 0;
			}
		}
		_tag = 1;
		tag_ = 1;
		if (count4 != 0) {
			if (_tag) {
				if (x + i < WEIGHT && y - i >= 0) {
					if (arr[x + i][y - i] == c)
						count4++;
					else
						_tag = 0;
				}
				else
					_tag = 0;
			}
			if (tag_) {
				if (x - i >= 0 && y + i < HEIGHT) {
					if (arr[x - i][y + i] == c)
						count4++;
					else
						tag_ = 0;
				}
				else
					tag_ = 0;
			}
			if (((_tag == 0) && (tag_ == 0)) && (count4 < WIN)) {
				count4 = 0;
			}
		}
	}
	if (count1 == WIN || count2 == WIN || count3 == WIN || count4 == WIN)
		return 1;
	else
		return 0;
}

//打印棋盘
void chessboard() {
	for (int i = 0; i < HEIGHT; i++) {
		printf("  ");
		for (int j = 0; j < WEIGHT; j++) {
			printf("+---");
		}
		printf("+\n%2d",i+1);
		for (int j = 0; j < WEIGHT; j++) {
			printf("| %c ", arr[j][i]);
		}
		printf("|\n");
	}
	printf("  ");
	for (int j = 0; j < WEIGHT; j++) {
		printf("+---");
	}
	printf("+\n ");
	for (int i = 1; i <= WEIGHT; i++) {
		if (i <= 10) {
			printf("   %d", i);
		}
		else {
			printf("  %d", i);
		}
	}
	printf("\n");
}
void initArr() {
	for (int i = 0; i < HEIGHT; i++) {
		for (int j = 0; j < WEIGHT; j++) {
			arr[j][i] = ' ';
		}
	}
}

//游戏开始的菜单
void menu1() {
	printf("**************************************\n");
	printf("*************  1、开始游戏 ************\n");
	printf("*************  0、退出游戏 ************\n");
	printf("**************************************\n");

}

哈哈,这是花了几十分钟搞出来的小玩意,棋盘的宽度,和获胜的条件都是可以通过game1.h中的常量进行调整的,好了,今天的分享就到这了

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

孟婆的cappucino

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值