【c语言】——三子棋小游戏

目录

前言

一、实现的思路

二、代码实现

1.游戏菜单(menu)

2.定义游戏函数

 3.主函数

4.棋盘初始化 

5.打印棋盘 

6.玩家下棋 电脑下棋

7.判断输赢 

 IsFull函数

 三、模块化代码的实现

 1.game.h

2.game.c 

 3.test.h

 四、游戏运行结果



前言

三子棋游戏是一款益智小游戏相信大家都玩过吧,本篇文章将通过c语言来实现这款游戏


一、实现的思路

游戏的实现思路其实很简单就是

1.初始化棋盘

2.打印棋盘

3.玩家下棋

4.判断输赢

5.电脑下棋

6.判断输赢

二、代码实现

既然已经有了游戏的实现思路那么现在可以根据思路来实现代码了

1.游戏菜单(menu)

首先我们需要一个进入游戏的菜单

void menu() {
	printf("****************\n");
	printf("*****1.piay*****\n");
	printf("*****0.exit*****\n");
	printf("****************\n");
}

2.定义游戏函数

三子棋游戏的实现

void game() {
	char ret = '0';
	char board[Row][Col];
	InitBoard(board,Row,Col);
	DisplayBoard(board, Row, Col);
	while (1) {
		PlayerMove(board, Row, Col);
		DisplayBoard(board, Row, Col);
		//判断输赢
		 ret=Iswin(board,Row,Col);
		 if (ret != 'c') {
			 break;
		 }
		ComputerMove(board, Row, Col);
		DisplayBoard(board, Row, Col);
		//判断输赢
		ret = Iswin(board, Row, Col);
		if (ret != 'c') {
			break;
		}
	}
	if (ret == '*') {
		printf("玩家赢\n");
	}
	else if (ret == '#') {
		printf("电脑赢\n");
	}
	else {
		printf("平局");
	}

}

 3.主函数

通过do while语句和switch语句来实现这个游戏整体逻辑

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

4.棋盘初始化 

初始化棋盘将棋盘全部初始化为“空格”

void InitBoard(char board[Row][Col], int row, int col) {
	int i = 0;
	int j = 0;
	for (i = 0; i < row; i++) {
		for (j = 0; j < row; j++) {
			board[i][j] = ' ';
		}
	}
}

5.打印棋盘 

将棋盘打印出来

void DisplayBoard(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 < row - 1)
					printf("|");
			}
		}
		printf("\n");
	}
	
}

6.玩家下棋 电脑下棋

棋盘初始化和打印完成后玩家和电脑就可以下棋了

void PlayerMove(char board[Row][Col], int row, int col) {
	int x = 0;
	int y = 0;
	printf("玩家下棋\n");
	while (1) {
		printf("请输入坐标:");
		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 ComputerMove(char board[Row][Col], int row, int col) {
	int x = 0;
	int y = 0;

	printf("电脑下棋\n");
	while (1) {
		x = rand() % row;
		y = rand() % col;
		if (board[x][y] == ' ') {
			board[x][y] = '#';
			break;
		}

	}

	
}

7.判断输赢 

玩家赢时返回----“*”

电脑赢时返回----“#”

平局时返回----“Q”

游戏继续返回----“c”

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][0] != ' ') {
			return board[i][0];
		}
	}
	for (i = 0; i < col; i++) {
		if (board[0][i] == board[1][i] && board[1][i] == board[2][i] && board[0][i] != ' ') {
			return board[0][i];
		}
	}
	if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[1][1] != ' ') {
		return board[1][1];
	}
	if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[1][1] != ' ') {
		return board[1][1];
	}
	if (IsFull(board, row, col)) 
	{
		return 'Q';
	}
	return 'c';
}

 IsFull函数

判断游戏是否继续(判断平局)

  static  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;
}

 三、模块化代码的实现

 模块化编程的思想是:根据功能将工程划分为不同模块。主函数只调用函数,而不定义函数。在各模块文件中定义功能函数,并将要用到的函数利用同名头文件申明外部函数供其他文件调用

 1.game.h

头文件:关于游戏包含的函数声明,符号声明头文件的包含以及宏定义

#define _CRT_SECURE_NO_WARNINGS 1
#pragma once
#include <stdio.h>
#include<time.h>
#include<stdlib.h>

#define Row 3
#define Col 3

void InitBoard(char board[Row][Col], int row, int col);
void DisplayBoard(char board[Row][Col], int row, int col);
void PlayerMove(char board[Row][Col], int row, int col);
void ComputerMove(char board[Row][Col], int row, int col);
char Iswin(char board[Row][Col], int row, int col);

2.game.c 

 游戏相关的函数定义

#include "game.h"
void InitBoard(char board[Row][Col], int row, int col) {
	int i = 0;
	int j = 0;
	for (i = 0; i < row; i++) {
		for (j = 0; j < row; j++) {
			board[i][j] = ' ';
		}
	}
}
void DisplayBoard(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 < row - 1)
					printf("|");
			}
		}
		printf("\n");
	}
	
}
void PlayerMove(char board[Row][Col], int row, int col) {
	int x = 0;
	int y = 0;
	printf("玩家下棋\n");
	while (1) {
		printf("请输入坐标:");
		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 ComputerMove(char board[Row][Col], int row, int col) {
	int x = 0;
	int y = 0;

	printf("电脑下棋\n");
	while (1) {
		x = rand() % row;
		y = rand() % col;
		if (board[x][y] == ' ') {
			board[x][y] = '#';
			break;
		}

	}

	
}
  static  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][0] != ' ') {
			return board[i][0];
		}
	}
	for (i = 0; i < col; i++) {
		if (board[0][i] == board[1][i] && board[1][i] == board[2][i] && board[0][i] != ' ') {
			return board[0][i];
		}
	}
	if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[1][1] != ' ') {
		return board[1][1];
	}
	if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[1][1] != ' ') {
		return board[1][1];
	}
	if (IsFull(board, row, col)) 
	{
		return 'Q';
	}
	return 'c';
}

 3.test.h

三子棋游戏的整体逻辑实现

#include "game.h"
void game() {
	char ret = '0';
	char board[Row][Col];
	InitBoard(board,Row,Col);
	DisplayBoard(board, Row, Col);
	while (1) {
		PlayerMove(board, Row, Col);
		DisplayBoard(board, Row, Col);
		//判断输赢
		 ret=Iswin(board,Row,Col);
		 if (ret != 'c') {
			 break;
		 }
		ComputerMove(board, Row, Col);
		DisplayBoard(board, Row, Col);
		//判断输赢
		ret = Iswin(board, Row, Col);
		if (ret != 'c') {
			break;
		}
	}
	if (ret == '*') {
		printf("玩家赢\n");
	}
	else if (ret == '#') {
		printf("电脑赢\n");
	}
	else {
		printf("平局");
	}

}
void menu() {
	printf("****************\n");
	printf("*****1.piay*****\n");
	printf("*****0.exit*****\n");
	printf("****************\n");
}
int main() {
	int input = 0;
	srand((unsigned int)time(NULL));
	do
	{
		menu();
		printf("请选择:");
		scanf("%d", &input);
		switch (input) {
		case 1:
			game();
			break;
		case 0:
			printf("退出游戏\n");
			break;
		default:
			printf("输入错误,请重新输入");
			break;
		}
	} while (input);
	return 0;
}

 四、游戏运行结果

  • 5
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值