游戏系统

Posted 语风之

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了游戏系统相关的知识,希望对你有一定的参考价值。

简介:

该游戏由三字棋子(ThreeChess)和扫雷(MineClear)两个小游戏组成,通过switch 语句来完成选玩游戏和退出系统的操作,由于代码数量过多,所以采取了多文件管理的方式,便于代码的管理和使用。

注意:

1、

system函数代表执行系统命令,system("cls")就是执行命令”清屏“的意思,避免了运行界面的冗长,实现了内容的更新。在VC环境下有实现清屏:

#include <windows.h>
system("cls");

2、

头文件 Game.h 中宏定义了ROW、COL的大小,他们代表着扫雷游戏定义的数组的行和列,可以通过改变数值的大小实现扫雷的规模调整,更加方便直接。

#define ROW 10
#define COL 10

朋友们,程序员的快乐就在这里了,用朴实的代码实现童年的快乐。一起打开代码试玩一下吧。


头文件 Game.h:

用于指定模块接口的声明放在文件中

#pragma once//使头文件只被编译一次

#include<stdio.h>
#include<windows.h>
#include<stdlib.h>
#include <time.h>
#pragma warning(disable:4996)

extern void GameHall();//游戏大厅目录,用于选择游戏
extern void Game_1();//游戏1 三子棋代码需要定义调用的函数
extern void ThreeChess();
extern void Game_2();//游戏2 扫雷代码需要定义调用的函数
extern void MineClear();
//ThreeChess
#define FOW 3//FOW、FOL代表三子棋边界的长和宽
#define FOL 3

#define INIT  ' ' //INIT为空格的宏定义
#define WHITE 'X' //X在三字棋代表Player
#define BLACK 'O' //O在三字棋代表Computer
#define DRAW  'D' //DRAW代表平局
#define NEXT  'N' //NEXT代表继续

//MineClearence
#define ROW 10//ROW、COL代表扫雷游戏定义的数组的行和列
#define COL 10

#define STYLE '?'//扫雷界面初始化为?
#define NUM 20//NUM代表扫雷游戏中埋下的雷的个数


源文件  Game.c:

存放自己定义,需要调用的函数

#include "Game.h"


static void MenuChess()

	printf("|-------------ThreeChess-------------|\\n");
	printf("+------------------------------------+\\n");
	printf("| 1. Play                   0. Exit  |\\n");
	printf("+------------------------------------+\\n");


void Game_1()

	system("cls");
	int select = 0;
	int quit = 0;
	while (!quit)
		MenuChess();
		printf("Please Select# ");
		scanf("%d", &select);
		switch (select)
		case 1:
			ThreeChess();
			break;
		case 0:
			quit = 1;
			break;
		default:
			printf("Enter Error, Try Again!\\n");
			break;
		
	
	printf("ByeBye!\\n");
	system("pause");



//ThreeChess
static void InitBoard(char board[][FOL], int row, int col)

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

static void ShowBoard(char board[][FOL], int row, int col)

	system("cls");
	printf(" ");
	for (int i = 0; i < col; i++)
		printf("%4d", i + 1);
	
	printf("\\n--------------\\n");
	for (int i = 0; i < row; i++)
		printf("%-2d", i + 1); //2
		for (int j = 0; j < col; j++)
			printf("| %c ", board[i][j]); //12
		
		printf("\\n--------------\\n");
	


//#define WHITE 'X' //Player
//#define BLACK 'O' //Computer
//#define DRAW 'D'
//#define NEXT 'N'
static char IsEnd(char board[][FOL], int row, int col)

	for (int i = 0; i < row; i++)
		if (board[i][0] == board[i][1] && \\
			board[i][1] == board[i][2] && \\
			board[i][0] != INIT)
			return board[i][0];
		
	

	for (int j = 0; j < FOL; j++)
		if (board[0][j] == board[1][j] && \\
			board[1][j] == board[2][j] && \\
			board[0][j] != INIT)
			return board[0][j];
		
	
	if (board[0][0] == board[1][1] && \\
		board[1][1] == board[2][2] && \\
		board[1][1] != INIT)
		return board[1][1];
	
	if (board[0][2] == board[1][1] && \\
		board[1][1] == board[2][0] && \\
		board[1][1] != INIT)
		return board[1][1];
	
	for (int i = 0; i < row; i++)
		for (int j = 0; j < col; j++)
			if (board[i][j] == INIT)
				return NEXT;
			
		
	
	return DRAW;

static void PlayerMove(char board[][FOL], int row, int col)

	int x = 0;
	int y = 0;
	while (1)
		printf("Please Enter Postion<x,y># ");
		scanf("%d %d", &x, &y);
		if (x < 1 || y < 1 || x > 3 || y > 3)
			printf("Enter Postion Error!\\n");
			continue;
		
		if (board[x - 1][y - 1] == INIT)
			board[x - 1][y - 1] = WHITE;
			break;
		
		else
			printf("Postion Is Not Empty!\\n");
		
	

static void ComputerMove(char board[][FOL], int row, int col)

	while (1)
		int x = rand() % row;
		int y = rand() % col;
		if (board[x][y] == INIT)
			board[x][y] = BLACK;
			break;
		
	


void ThreeChess()

	char board[FOW][FOL];
	InitBoard(board, FOW, FOL);
	srand((unsigned long)time(NULL));
	char result = 0;
	while (1)
		ShowBoard(board, FOW, FOL);
		PlayerMove(board, FOW, FOL);
		result = IsEnd(board, FOW, FOL);
		if (result != NEXT)
			break;
		
		ShowBoard(board, FOW, FOL);
		ComputerMove(board, FOW, FOL);
		result = IsEnd(board, FOW, FOL);
		if (result != NEXT)
			break;
		
	
	ShowBoard(board, FOW, FOL);
	switch (result)
	case WHITE:
		printf("You Win!\\n");
		break;
	case BLACK:
		printf("You Lose!\\n");
		break;
	case DRAW:
		printf("You == Computer!\\n");
		break;
	default:
		printf("BUG!\\n"); //Do Nothing!
		break;
	


//MineClearence
static void MenuClear()

	printf("|-----------MineClear----------|\\n");
	printf("+------------------------------+\\n");
	printf("|***1、Play          0、Exit***|\\n");
	printf("+------------------------------+\\n");


void Game_2()

	system("cls");
	int quit = 0;
	int select = 0;
	while (!quit)
		MenuClear();
		printf("Please Enter# ");
		scanf("%d", &select);
		switch (select)
		case 1:
			MineClear();
			break;
		case 0:
			quit = 1;
			break;
		default:
			printf("Postion Error, Try Again!\\n");
			break;
		
	
	printf("byebye!\\n");
	system("pause");


static void SetMines(char board[][COL], int row, int col)

	int count = NUM;
	while (count)
		int x = rand() % (row - 2) + 1;
		int y = rand() % (col - 2) + 1;
		if (board[x][y] == '0')
			board[x][y] = '1';
			count--;
		
	


static void ShowLine(int col)

	for (int i = 0; i <= (col - 2); i++)
		printf("----");
	
	printf("\\n");

static void ShowBoard(char board[][COL], int row, int col)

	printf("   |");
	for (int i = 1; i <= (col - 2); i++)
		printf("%2d |", i);
	
	printf("\\n");
	ShowLine(col);
	for (int i = 1; i <= (row - 2); i++)
		printf("%-3d|", i);
		for (int j = 1; j <= (col - 2); j++)
			printf(" %c |", board[i][j]);
		
		printf("\\n");
		ShowLine(col);
	


static char CountMines(char board[][COL], int x, int y)

	return board[x - 1][y - 1] + board[x - 1][y] + board[x - 1][y + 1] + \\
		board[x][y + 1] + board[x + 1][y + 1] + board[x + 1][y] + \\
		board[x + 1][y - 1] + board[x][y - 1] - 7 * '0';


void MineClear()

	srand((unsigned long)time(NULL));

	char show_board[ROW][COL];
	char mine_board[ROW][COL];

	memset(show_board, STYLE, sizeof(show_board));
	memset(mine_board, '0', sizeof(mine_board));

	SetMines(mine_board, ROW, COL);

	int count = (ROW - 2)*(COL - 2) - NUM;

	while (count)
		system("cls");
		ShowBoard(show_board, ROW, COL);
		printf("Please Enter Your Postion<x,y># ");
		int x = 0;
		int y = 0;
		scanf("%d %d", &x, &y);
		if (x < 1 || x > 10 || y < 1 || y > 10)
			printf("Postion Error!\\n");
			continue;
		
		if (show_board[x][y] != STYLE)
			printf("Postion Is not *\\n");
			continue;
		
		if (mine_board[x][y] == '1')
			printf("game over!\\n");
			ShowBoard(mine_board, ROW, COL);
			break;
		
		//['0', '8']
		show_board[x][y] = CountMines(mine_board, x, y);
		count--;
	


源文件 mian.c:

#include "Game.h"

void GameHall()
	printf("+-------------GameHall-------------+\\n");
	printf("|----------------------------------|\\n");
	printf("|1、ThreeChess                     |\\n");
	printf("|2、MineClear                      |\\n");
	printf("|3、Exit                           |\\n");
	printf("+----------------------------------+\\n");


int main()
	int flag = 0;
	while (!flag)
		GameHall();
		int select = 0;
		printf("Enter your choice # ");
		scanf("%d", &select);
		switch (select)
		case 1:
			Game_1();
			break;
		case 2:
			Game_2();
			break;
		case 3:
			flag = 1;
			break;
		dafault:
			printf("Enter Error,Try Again!\\n");
			break;
		
	
	printf("You will sign out the GameHall\\n");
	system("pause");
	return 0;

运行结果:

三子棋

 

以上是关于游戏系统的主要内容,如果未能解决你的问题,请参考以下文章

iOS 游戏中心排行榜不允许平局?

AcWing 1801. 蹄子剪刀布(思维+暴力)

一段简单的JS代码,我却一次次收到同样的结果,我想,我可能是做了一个逻辑错误。

0041-猜拳游戏

SG函数学(hua)习(shui)记录

TicTacToe Java 检查抽奖