简单的五子棋游戏(C语言)

                                           五子棋游戏

五子棋小程序对于初学C语言的码农来说,是一个不错的锻炼项目。下面我来简单介绍一下我写五子棋的简单程序。

头文件(game.h)

此部分主要是头文件还有调用函数的头文件,不管怎么样,都是头文件。

#ifndef __GAME_H__  
#define __GAME_H__  

enum OPTION     //枚举
{
	EXIT,
	PLAY
};

#include <stdio.h>  
#include <stdlib.h>  
#include <string.h>  
#include <time.h>  

#define ROWS 5  
#define COLS 5  

void init_board(char board[ROWS][COLS], int row, int col);  //初始化棋盘
void display_board(char board[ROWS][COLS], int row, int col);  //展示棋盘
void player_move(char board[ROWS][COLS], int row, int col);  //玩家走的函数
void computer_move(char board[ROWS][COLS], int row, int col);  //电脑走的函数
static int is_full(char board[ROWS][COLS], int row, int col);  // 判断棋盘是否已满函数
char check_win(char board[ROWS][COLS], int row, int col);  //判断输赢函数

#endif  

游戏的函数(game.c)

这部分是游戏的几乎所以需要调用的函数。包括初始化五子棋棋盘的函数,打印五子棋棋盘函数,玩家走的函数,电脑走的函数,判断棋盘是否已满函数,判断输赢函数。

在初始化棋盘的时候用到memset(),引用百度百科的介绍。

void *memset(void *s, int ch,  size_t n);
函数解释:将s中当前位置后面的n个字节 (typedef unsigned int size_t )用 ch 替换并返回 s 。
memset:作用是在一段内存块中填充某个给定的值,它是对较大的 结构体数组进行清零操作的一种最快方法。
棋盘的大小可以通过更改ROWS和COLS的值来改变行和列。
#include "game.h"  


void init_board(char board[ROWS][COLS], int row, int col)
{
	memset(board, ' ', col*row*sizeof(board[0][0]));  //初始化棋盘,将board中当前位置后面的n个字节 (col*row*sizeof(board[0][0])用space 替换并返回 board
}
void display_board(char board[ROWS][COLS], int row, int col)
{
	int i = 0;
	for (i = 0; i<row; i++)
	{
		printf(" %c | %c | %c | %c | %c \n", board[i][0], board[i][1], board[i][2], board[i][3], board[i][4]); 
		if (i != 4)
			printf("---|---|---|---|---\n");
	}
}
void player_move(char board[ROWS][COLS], int row, int col)
{
	int x = 0;
	int y = 0;
	while (1)
	{
		printf("玩家下棋,输入坐标(中间加空格):");
		scanf_s("%d%d", &x, &y);//scanf_s是vs要求,别的编译器用scanf。
		x--;
		y--;
		if (((x >= 0) && (x<row) && (y >= 0) && (y<col)))
		{
			if (board[x][y] == ' ')
			{
				board[x][y] = '#';
				break;
			}
			else
			{
				printf("输入错误,请重新输入!");
			}
		}
		else
		{
			printf("输入错误,请重新输入!");
		}
	}
}

void computer_move(char board[ROWS][COLS], int row, int col)
{
	printf("电脑走!\n");
	while (1)
	{
		int x = rand() % row;
		int y = rand() % col;
		if (board[x][y] == ' ')
		{
			board[x][y] = '*';
			break;
		}
	}
}

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

char check_win(char board[ROWS][COLS], 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][2] == board[i][3])
			&& (board[i][3] == board[i][4])
			&& (board[i][4] != ' '))
			return board[i][1];
	}
	for (i = 0; i<col; i++)
	{
		if ((board[0][i] == board[1][i])
			&& (board[1][i] == board[2][i])
			&& (board[2][i] == board[3][i])
			&& (board[3][i] == board[4][i])
			&& (board[4][i] != ' '))
			return board[1][i];
	}

	if ((board[0][0] == board[1][1])
		&& (board[1][1] == board[2][2])
		&& (board[2][2] == board[3][3])
		&& (board[3][3] == board[4][4])
		&& (board[4][4] != ' '))
		return board[1][1];

	if ((board[0][4] == board[1][3])
		&& (board[1][3] == board[2][2])
		&& (board[2][2] == board[3][1])
		&& (board[3][1] == board[4][0])
		&& (board[4][0] != ' '))
		return board[1][1];
	if (is_full(board, row, col))
	{
		return 'q';
	}
	return ' ';
}

主函数(test.c)

此部分主要就一个main()主函数。程序开始的入口。

#include "game.h"  

void game()
{
	char board[ROWS][COLS] = { 0 };
	char ret = 0;
	init_board(board, ROWS, COLS);
	display_board(board, ROWS, COLS);
	srand((unsigned int)time(NULL));

	while (1)
	{
		player_move(board, ROWS, COLS);
		if ((ret = check_win(board, ROWS, COLS)) != ' ')
			break;

		display_board(board, ROWS, COLS);
		computer_move(board, ROWS, COLS);
		if ((ret = check_win(board, ROWS, COLS)) != ' ')
			break;

		display_board(board, ROWS, COLS);
	}

	if (ret == '#')
	{
		printf("玩家赢\n");
	}
	else if (ret == '*')
	{
		printf("电脑赢\n");
	}
	else if (ret == 'q')
	{
		printf("平局\n");
	}
	display_board(board, ROWS, COLS);
}

void menu()
{
	printf("***************五子棋游戏**************\n");
	printf("***************记得选择哦**************\n");
	printf("***********(1.play  0.exit)**********\n");
	printf("**************祝您玩的愉快*************\n");
}


int main()
{
	int input = 0;
	do
	{
		menu();
		printf("请输入1或者0:");
		scanf_s("%d", &input);
		switch (input)
		{
		case 1:
			game();
			break;
		case 0:
			break;
		default:
			printf("选择错误\n");
			break;
		}
	} while (input);
	return 0;
}

这个五子棋游戏最大的缺陷就是电脑下棋是随机的。因此没有难度,也没有意思。还望各位大神多多指教。



猜你喜欢

转载自blog.csdn.net/qq_40421919/article/details/79855190
今日推荐