C语言实现三子棋和猜数字游戏

三子棋和猜数字游戏

实现的思路如下:

头文件

#ifndef _GAME_H_
#define _GAME_H_

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

#define CORRECT_ID "Bit"
#define CORRECT_PASSWARD "Bits"
#define OPPORTUNITY 3

#define COL 3
#define ROW 3
#pragma warning(disable:4996)

void ShowNumberMenu();//显示猜数字游戏的菜单
void GameNumber();//猜数字游戏函数
int LoginNumber();//登陆函数

void ShowGameMenu();//显示游戏选择菜单
void ShowChessMenu();//显示三子棋菜单
void GameChess();//三子棋游戏函数

#endif

在头文件中写了全部的头文件调用,以及函数的声明。

函数文件

#include"game.h"
void ShowGameMenu() {
	printf("###################################\n");
	printf("### 1.Play Number Guessing Game ###\n");
	printf("### 2.Play Three Chess Game     ###\n");
	printf("### 3.Exit                      ###\n");
	printf("###################################\n");
	printf("Plese enter your select# ");
}



int LoginNumber() {
	char id[64];
	char pw[64];
	int opportunity = OPPORTUNITY;
	printf("!!! You only have 3 opportunity to login in!!!\n");
	for (opportunity; opportunity > 0; opportunity--) {
		printf("Please enter ID# ");
		scanf("%s", id);
		printf("Please enter PassWord# ");
		scanf("%s", pw);
		if ((strcmp(CORRECT_ID, id) || strcmp(CORRECT_PASSWARD, pw)) == 0) {
			return 1;
		}
		else {
			printf("Login failed!Your have %d opportunities left!\n", opportunity - 1);
		}
	}
	return 0;
}

void ShowNumberMenu() {
	printf("############################\n");
	printf("##  1.Play        2.Exit  ##\n");
	printf("############################\n");
}

void GameNumber() {
	int your_num = 0;
	printf("Welcome to the game!\n");
	srand((unsigned int)time(NULL));
	int random_number = rand() % 100 + 1;
	while (1) {
		printf("Please enter your number# ");
		scanf("%d", &your_num);
		if (your_num == random_number) {
			printf("Congratulations on your guess!\n");
			break;
		}
		else if (your_num < random_number) {
			printf("Sorry, your guess is a little small!\n");
		}
		else {
			printf("Sorry, your guess is a little big!\n");
		}
	}
}



void ShowBoard(char ChessBoard[][COL], int row, int col) {//显示整个棋盘现在的状态
	printf("  | 1 | 2 | 3 |\n");
	printf("---------------\n");
	for (int i = 0; i < row; i++){
		printf("%d |", i + 1);
		for (int j = 0; j < col; j++) {
			printf(" %c |", ChessBoard[i][j]);
		}
		printf("\n---------------\n");
	}
}

void PlayerGo(char ChessBoard[][COL], int row, int col) {//玩家走
	int x = 0;
	int y = 0;
	while (1) {
		printf("Please enter you post# ");
		scanf("%d %d", &x, &y);
		if (ChessBoard[x - 1][y - 1] != ' ') {
			printf("Post error, please enter again!\n");
		}
		else {
			ChessBoard[x - 1][y - 1] = 'X';
			return;
		}
	}
}
char Judge(char ChessBoard[][COL], int row, int col) {//判断程序,输出'N','P','X','O'分别
//代表继续,平局,玩家赢,电脑赢
	int null = 0;
	for (int i = 0; i < row; i++) {//行上3连
		if (ChessBoard[i][0] == ChessBoard[i][1] &&\
			ChessBoard[i][1] == ChessBoard[i][2] &&\
			ChessBoard[i][0] != ' ') {
			return ChessBoard[i][0];
		}
	}
	for (int i = 0; i < col; i++) {//列上3连
		if (ChessBoard[0][i] == ChessBoard[1][i] &&\
			ChessBoard[1][i] == ChessBoard[2][i] &&\
			ChessBoard[0][i] != ' ') {
			return ChessBoard[0][i];
		}
	}
	if (ChessBoard[0][0] == ChessBoard[1][1] &&\
		ChessBoard[1][1] == ChessBoard[2][2] &&\
		ChessBoard[0][0] != ' ') {//对角线1
		return ChessBoard[0][0];
	}
	if (ChessBoard[0][2] == ChessBoard[1][1] &&\
		ChessBoard[1][1] == ChessBoard[2][0] &&\
		ChessBoard[0][2] != ' ') {//对角线2
		return ChessBoard[0][2];
	}

	for (int i = 0; i < row; i++) {
		for (int j = 0; j < col; j++) {
			if (ChessBoard[i][j] == ' ') {
				null = 1;
			}
		}
	}
	if (null) {
		return 'N';//继续游戏,继续输入
	}
	return 'P';//平局
}
void ComputerGO(char ChessBoard[][COL], int row, int col) {//电脑走
	
	while (1) {
		int x = rand() % row;
		int y = rand() % col;
		if (ChessBoard[x][y] == ' ') {
			ChessBoard[x][y] = 'O';
			return;
		}
	}
}

void ShowChessMenu() {
	printf("######################\n");
	printf("## 1.Play    2.Exit ##\n");
	printf("######################\n");
	printf("Plese enter your select# ");
}

int PorC() {//判断玩家还是电脑先下,return 1是玩家先下,return 0是电脑先下
	char p[10];
	int _p;
	int _c;
	char stone[10] = "石头";
	char scissors[10] = "剪刀";
	char cloth[10] = "布";

START:
	_c = rand() % 3;
	while (1) {
		printf("Please enter 石头 or 剪刀 or 布# ");
		scanf("%s", &p);
		if (!(strcmp(stone, p))) {
			_p = 0;
			break;
		}
		else if (!(strcmp(scissors, p))) {
			_p = 1;
			break;
		}
		else if (!(strcmp(cloth, p))) {
			_p = 2;
			break;
		}
		else {
			printf("Enter error,please enter again!\n");
		}
	}
	if (_p > _c) {
		return 1;
	}
	else if (_p < _c) {
		return 0;
	}
	else {
		printf("Common , enter again!\n");
		goto START;
	}
}

void GameChess() {
	srand((unsigned int)time(NULL));
	char ChessBoard[ROW][COL];
	memset(ChessBoard, ' ', ROW * COL);
	
	int who = PorC();//1:Player 2:Computer
	ShowBoard(ChessBoard, ROW, COL);
	while (1) {
		char win = ' ';
		//ShowBoard(ChessBoard, ROW, COL);
		if (who) {
			printf("Because you win,so you go !\n");
			PlayerGo(ChessBoard, ROW, COL);//如果who=1,玩家先走,否则机器走
			ShowBoard(ChessBoard, ROW, COL);
		}
		else {
			printf("Because you lose,so computer go !\n");
			ComputerGO(ChessBoard, ROW, COL);
			ShowBoard(ChessBoard, ROW, COL);
		}
		//ShowBoard(ChessBoard, ROW, COL);
		win = Judge(ChessBoard, ROW, COL);//reurn 'X' Player win; return 'O' Computer win; return 'P' 平局; return 'N' 继续
		if (win != 'N') {
			switch (win) {
			case'X':
				printf("You win!\n");
				break;
			case'O':
				printf("Computer win!\n");
				break;
			case'P':
				printf("No winner!\n");
				break;
			default:
				break;
			}
			return;
		}
		if (who) {//如果who=1,说明玩家先走,所以现在机器走
			ComputerGO(ChessBoard, ROW, COL);
			ShowBoard(ChessBoard, ROW, COL);
		}
		else {
			PlayerGo(ChessBoard, ROW, COL);
			ShowBoard(ChessBoard, ROW, COL);
		}
		win = Judge(ChessBoard, ROW, COL);
		if (win != 'N') {
			switch (win) {
			case'X':
				printf("You win!\n");
				break;
			case'O':
				printf("Computer win!\n");
				break;
			case'P':
				printf("No winner!\n");
				break;
			default:
				break;
			}
			return;
		}
	}
}

主函数文件

#include"game.h"

int main() {
	int gameselect = 0;
	int chessselect = 0;
	int numberselect = 0;
	int chessquit = 0;
	int numberquit = 0;
	int quit = 0;
	while (!quit) {
		ShowGameMenu();
		scanf("%d", &gameselect);
		switch (gameselect) {
				case 1: {//猜数字游戏
					while (!numberquit) {
						if (!LoginNumber()) {//登陆函数(用户名:Bit;密码:Bits)
							printf("Login failed!\n");
							break;
						}

						ShowNumberMenu();
						printf("Please enter your select# ");
						scanf("%d", &numberselect);
						while (1) {
							switch (numberselect) {
							case 1:
								GameNumber();
								break;
							case 2:
								printf("Quit number guess game successfully\n");
								numberquit = 1;
								break;
							default:
								printf("Your select is illegal ! please enter 1 or 2.\n");
								break;
							}
							printf("Do you want to play again?\n");
							ShowNumberMenu();
							printf("Please enter your select# ");
							scanf("%d", &numberselect);
							if (numberselect == 2) {
								printf("Welcome to play again next time, quit successfully!\n");
								numberquit = 1;	
								break;
							}
						}
					}
				}
				break;
			case 2://三子棋游戏
				while (!chessquit) {
					ShowChessMenu();
					scanf("%d", &chessselect);
					switch (chessselect) {
					case 1:
						GameChess();
						printf("Dou you want to play again?\n");
						break;
					case 2:
						printf("Quit three chess successful!\n");
						chessquit = 1;
						break;
					default:
						printf("Select error,please enter again!");
						break;
					}
				}
				break;
			case 3://退出
				printf("Quit successful!\n");
				quit = 1;
				break;
			default://输入不合法
				printf("Selectgame error,please enter again!");
				break;
		}
	}
	return 0;
}

本文没有过多的文字描述,只是单纯的源代码
作者博客: https://blog.csdn.net/qq_44025641.

©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页