Rock paper scissors game (C language)

Table of contents

1. Game rules

2. Game idea

3. Code implementation

1. Create header file game.h, source files game.c and test.c respectively

2. Create a menu and process the input selection

3. Implement the rock-paper-scissors game

3.1 Print the menu to help the player punch and process the player's choice

3.2 The computer punches randomly

3.3 Judging winning or losing

 3.4 Print game results

3.5 Write the result to a file

 4. View game records

5. Delete game records

4. Complete code


1. Game rules

Rock beats scissors, scissors beats paper, paper beats rock

1 represents scissors, 2 represents paper, and 3 represents rock. The player simulates punching by selecting.

 

2. Game idea

1. Create a menu to help players make choices

2. Implement the rock-paper-scissors game

  (1) The player simulates punching by selecting

  (2) The computer punches randomly

  (3) Judging the outcome

  (4) Write the result to a file

3. View game records

4. Delete history

 

3. Code implementation

1. Create header file game.h, source files game.c and test.c respectively

game.h: stores declarations of custom functions, referenced standard library header files, etc.

game.c: store the definition of the function

test.c: for program testing

2. Create a menu and process the input selection

//主界面菜单
void menu()
{
	printf("******************************\n");
	printf("******  1.进入游戏     *******\n");
	printf("******  2.查看游戏记录 *******\n");
	printf("******  3.删除游戏记录 *******\n");
	printf("******  0.游戏退出     *******\n");
	printf("******************************\n");
}

int main()
{
	//选择选项
	int input = 0;
	do//循环至少执行一次
	{
		menu();
		printf("请选择:>");
		scanf("%d", &input);
		switch (input)
		{
		case 1:
			//玩游戏
			break;
		case 2:
			//查看比赛记录
			break;
		case 3:
			//删除比赛记录
			break;
		case 0:
			printf("游戏已退出\n");
			break;
		default:
			printf("选择错误,请重新选择!\n");
			break;
		}
	} while (input);
	return 0;
}

 

3. Implement the rock-paper-scissors game

3.1 Print the menu to help the player punch and process the player's choice

//游戏菜单
void RPSmenu()
{
	printf("******************************\n");
	printf("******  0.回到主界面   *******\n");
	printf("******  1.剪刀   2.布  *******\n");
	printf("******      3.石头     *******\n");
	printf("******************************\n");
}

//玩游戏
void RPS()
{
	//玩家出拳
	int choose = 0;
	do
	{
		//打印游戏菜单
		RPSmenu();
		printf("请选择:>");
		scanf("%d", &choose);
		switch (choose)
		{
		case 0:
			printf("已退回主界面!\n");
			break;
		case 1:
		case 2:
		case 3:
			//电脑出拳
			break;
		default:
			printf("选择错误,请重新选择!\n");
			break;
		}
	} while (choose);
}

 

3.2 The computer punches randomly

Realize the computer's random punching by generating random numbers

Use the rand function to generate pseudo-random numbers, but before using the rand function, you should use the srand function to set the random number seed, otherwise the random number sequence obtained each time the program runs is the same

//电脑出拳
int ComputerRPS()
{
	//产生随机数
	//在使用rand()函数时,要引用对应头文件
	//要使用srand((unsigned int)time(NULL)来进行初始化
	int num = 0;
	num = rand() % 3 + 1;
	return num;
}

 

3.3 Judging winning or losing

//判断输赢
//平局返回0
//玩家赢返回1
//电脑赢返回-1
int IsWin(int UserChoose, int ComputerChoose)
{
	//平局
	if (UserChoose == ComputerChoose)
	{
		return 0;
	}
	//玩家赢
	else if (UserChoose == 1 && ComputerChoose == 2 ||
			 UserChoose == 2 && ComputerChoose == 3 ||
			 UserChoose == 3 && ComputerChoose == 1)
	{
		return 1;
	}
	//电脑赢
	else
	{
		return -1;
	}
}

 

 3.4 Print game results

Convert numbers to corresponding punches

//数字对应的出拳
char* Outcome(int number)
{
	switch (number)
	{
	case 1:
		return "剪刀";
	case 2:
		return "布";
	case 3:
		return "石头";
	}
}

print game result

//打印结果
void Print(char* user, char* computer, int ret)
{
	printf("这一局,你选择出%s,电脑选择出%s\n", user, computer);
	if (ret == 0)
	{
		printf("平局!\n");
	}
	else if (ret == 1)
	{
		printf("恭喜你,你赢了!\n");
	}
	else
	{
		printf("很遗憾,你输了!\n");
	}
}

 

3.5 Write the result to a file

Define variables in game.h

static int win = 0;
static int lose = 0;
static int draw = 0;
static int count = 0;
//保存数据
void SaveRPS(int ret)
{

	FILE* pf = fopen("test.txt", "w");
	if (pf == NULL)//判断文件是否打开成功
	{
		perror("SaveRPS");//打印错误信息
		return;
	}
	//分别统计胜利、输、平局次数
	if (ret == 0)
	{
		draw++;
	}
	else if (ret > 0)
	{
		win++;
	}
	else
	{
		lose++;
	}
	count++;
	//写文件
	fprintf(pf, "%d %d %d %d", count, win, lose, draw);
	//关闭文件
	fclose(pf);
	pf = NULL;
}

 

 4. View game records

The history is saved in the file, and each time the program runs, the data in the file should be read first

//加载上次记录
void LoadRPS()
{
	//打开文件
	FILE* pf = fopen("test.txt", "r");
	//文件未创建
	if (pf == NULL)
	{
		return;
	}

	//读文件
	fscanf(pf, "%d %d %d %d",&count, &win, &lose, &draw);
	//关闭文件
	fclose(pf);
	pf = NULL;
}

view game history

//查看记录
void ViewRPS()
{
	//统计胜利、输、平局次数
	if (count == 0)
	{
		printf("无历史记录!\n");
		return;
	}
	//打开文件
	FILE* pf = fopen("test.txt", "r");
	if (pf == NULL)
	{
		perror("ViewRPS");
	}
	//读文件
	fscanf(pf, "%d %d %d %d",&count, &win, &lose, &draw);;

	//关闭文件
	fclose(pf);
	pf = NULL;

	//打印
	printf("******************************\n");
	printf("*****     游戏局数:%d   *****\n",count);
	printf("*******    胜利:%d   ********\n", win);
	printf("*******    输: %d    ********\n", lose);
	printf("*******    平局:%d   ********\n", draw);
	printf("******************************\n");
	printf("\n");
}

 

5. Delete game records


//删除所有记录
void DeleteRPS()
{
	//查看是否有历史记录
	if (count == 0)
	{
		printf("无历史记录,无需删除!\n");
		return;
	}

	//确认是否删除所有记录
	int flag = 0;

	while (1)
	{
		printf("是否确认删除所有记录?(是:1/否:0)");
		scanf("%d", &flag);
		if (flag == 0)
		{
			return;
		}
		else if (flag == 1)
		{
			//打开文件
			FILE* pf = fopen("test.txt", "w");
			if (pf == NULL)
			{
				perror("Delete");
			}
			//写文件
			win = 0;
			lose = 0;
			draw = 0;
			count = 0;
			fprintf(pf, "%d %d %d %d",count, win, lose, count);
			//关闭文件
			fclose(pf);
			pf = NULL;

			//打印提示
			printf("历史记录删除成功\n");
			return;
		}
		else
		{
			printf("选择错误,请重新选择!\n");
			continue;
		}
	}
}

 

4. Complete code

game.h

#pragma once

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

void RPS();//石头剪刀布游戏
int ComputerRPS();//电脑出拳
int IsWin(int UserChoose, int ComputerChoose);//判断输赢
void Print(char* user, char* computer, int ret);//打印结果
char* Outcome(int number);//出拳结果
void SaveRPS(int ret);//保存记录
void LoadRPS();//加载记录
void ViewRPS();//查看记录
void DeleteRPS();//删除记录

static int win = 0;
static int lose = 0;
static int draw = 0;
static int count = 0;

game.c

#define _CRT_SECURE_NO_WARNINGS 1

#include "game.h"

//游戏菜单
void RPSmenu()
{
	printf("******************************\n");
	printf("******  0.回到主界面   *******\n");
	printf("******  1.剪刀   2.布  *******\n");
	printf("******      3.石头     *******\n");
	printf("******************************\n");
}

//玩游戏
void RPS()
{
	//玩家出拳
	int choose = 0;
	//电脑随机出拳
	int ret = 0;
	do
	{
		//打印游戏菜单
		RPSmenu();
		printf("请选择:>");
		scanf("%d", &choose);
		switch (choose)
		{
		case 0:
			printf("已退回主界面!\n");
			break;
		case 1:
		case 2:
		case 3:
			//电脑出拳
			ret = ComputerRPS();

			//判断输赢
			//平局返回0
			//用户赢返回1
			//电脑赢返回-1
			int result = IsWin(choose, ret);

			//打印游戏结果
			char* user = Outcome(choose);
			char* computer = Outcome(ret);
			Print(user, computer, result);

			//将结果写入文件
			SaveRPS(result);
			break;
		default:
			printf("选择错误,请重新选择!\n");
			break;
		}
	} while (choose);
}

//电脑出拳
int ComputerRPS()
{
	//产生随机数
	//在使用rand()函数时,要引用对应头文件
	//要使用srand((unsigned int)time(NULL)来进行初始化
	int num = 0;
	num = rand() % 3 + 1;
	return num;
}

//判断输赢
int IsWin(int UserChoose, int ComputerChoose)
{
	//平局
	if (UserChoose == ComputerChoose)
	{
		return 0;
	}
	//玩家赢
	else if (UserChoose == 1 && ComputerChoose == 2 ||
			 UserChoose == 2 && ComputerChoose == 3 ||
			 UserChoose == 3 && ComputerChoose == 1)
	{
		return 1;
	}
	//电脑赢
	else
	{
		return -1;
	}
}

//打印结果
void Print(char* user, char* computer, int ret)
{
	printf("这一局,你选择出%s,电脑选择出%s\n", user, computer);
	if (ret == 0)
	{
		printf("平局!\n");
	}
	else if (ret == 1)
	{
		printf("恭喜你,你赢了!\n");
	}
	else
	{
		printf("很遗憾,你输了!\n");
	}
}

//数字对应的出拳
char* Outcome(int number)
{
	switch (number)
	{
	case 1:
		return "剪刀";
	case 2:
		return "布";
	case 3:
		return "石头";
	}
}

//保存数据
void SaveRPS(int ret)
{

	FILE* pf = fopen("test.txt", "w");
	if (pf == NULL)//判断文件是否打开成功
	{
		perror("SaveRPS");//打印错误信息
		return;
	}
	//分别统计胜利、输、平局次数
	if (ret == 0)
	{
		draw++;
	}
	else if (ret > 0)
	{
		win++;
	}
	else
	{
		lose++;
	}
	count++;
	//写文件
	fprintf(pf, "%d %d %d %d", count, win, lose, draw);
	//关闭文件
	fclose(pf);
	pf = NULL;
}

//加载上次记录
void LoadRPS()
{
	//打开文件
	FILE* pf = fopen("test.txt", "r");
	//文件未创建
	if (pf == NULL)
	{
		return;
	}

	//读文件
	fscanf(pf, "%d %d %d %d",&count, &win, &lose, &draw);
	//关闭文件
	fclose(pf);
	pf = NULL;
}

//查看记录
void ViewRPS()
{
	//统计胜利、输、平局次数
	if (count == 0)
	{
		printf("无历史记录!\n");
		return;
	}
	//打开文件
	FILE* pf = fopen("test.txt", "r");
	if (pf == NULL)
	{
		perror("ViewRPS");
	}
	//读文件
	fscanf(pf, "%d %d %d %d",&count, &win, &lose, &draw);;

	//关闭文件
	fclose(pf);
	pf = NULL;

	//打印
	printf("******************************\n");
	printf("*****     游戏局数:%d   *****\n",count);
	printf("*******    胜利:%d   ********\n", win);
	printf("*******    输: %d    ********\n", lose);
	printf("*******    平局:%d   ********\n", draw);
	printf("******************************\n");
	printf("\n");
}

//删除所有记录
void DeleteRPS()
{
	//查看是否有历史记录
	if (count == 0)
	{
		printf("无历史记录,无需删除!\n");
		return;
	}

	//确认是否删除所有记录
	int flag = 0;

	while (1)
	{
		printf("是否确认删除所有记录?(是:1/否:0)");
		scanf("%d", &flag);
		if (flag == 0)
		{
			return;
		}
		else if (flag == 1)
		{
			//打开文件
			FILE* pf = fopen("test.txt", "w");
			if (pf == NULL)
			{
				perror("Delete");
			}
			//写文件
			win = 0;
			lose = 0;
			draw = 0;
			count = 0;
			fprintf(pf, "%d %d %d %d",count, win, lose, count);
			//关闭文件
			fclose(pf);
			pf = NULL;

			//打印提示
			printf("历史记录删除成功\n");
			return;
		}
		else
		{
			printf("选择错误,请重新选择!\n");
			continue;
		}
	}
}

test.c

#define _CRT_SECURE_NO_WARNINGS 1

#include "game.h"

//主界面菜单
void menu()
{
	printf("******************************\n");
	printf("******  1.进入游戏     *******\n");
	printf("******  2.查看游戏记录 *******\n");
	printf("******  3.删除游戏记录 *******\n");
	printf("******  0.游戏退出     *******\n");
	printf("******************************\n");
}

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

	//加载上次数据
	LoadRPS();

	//选择选项
	int input = 0;
	do//循环至少执行一次
	{
		menu();
		printf("请选择:>");
		scanf("%d", &input);
		switch (input)
		{
		case 1:
			//玩游戏
			RPS();
			break;
		case 2:
			//查看比赛记录
			ViewRPS();
			break;
		case 3:
			//删除比赛记录
			DeleteRPS();
			break;
		case 0:
			printf("游戏已退出\n");
			break;
		default:
			printf("选择错误,请重新选择!\n");
			break;
		}
	} while (input);
	return 0;
}

Guess you like

Origin blog.csdn.net/2301_76161469/article/details/131650431