三子棋小游戏(C语言版)

头文件

#ifndef __GAME_H__
#define __GAME_H__
#include
#include
#include
#include
//良好的代码风格建议使用宏定义,有效减少代码后序的维护成本
#define ROW 3 //行
#define COL 3 //列
#define INIT ' '
//游戏有四种结果:Player win ,Computer win,平局,继续
#define WHITE 'X'//Player win
#define BLACK 'O'//Computer win
#define DRAW 'D'//平局
#define NEXT 'N'//继续
#pragma warning(disable:4996)
extern void Game();
#endif

主要函数

#include"game.h"
//初始化面板,初始化为全部是空格
static void InitBoard(char board[][COL], 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[][COL], 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);//格式控制字符默认是左对齐,加-变为右对齐
		for (int j = 0; j < col; j++){
			printf("| %c ", board[i][j]);
		}
		printf("\n-------------\n");
	}
}
static void PlayerMove(char board[][COL], int row, int col){//玩家落子
	int x = 0, y = 0;
	while (1){
		printf("Please Enter Postion#");
		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[][COL], int row, int col){//电脑落子
	while (1){
		int x = rand() % row; //[0,row)
		int y = rand() % col;//[0,col)
		if (board[x][y] == INIT){
			board[x][y] = BLACK;
			break;
		}
	}
}
static char IsEnd(char board[][COL], 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][1] != INIT){//判定不为空
			return board[i][0];//棋子颜色就可得出玩家
		}
	}
	for (int j = 0; j < col; j++){//判断列是否三子连线
		if (board[0][j] == board[1][j] && \
			board[1][j] == board[2][j] && \
			board[1][j] != INIT){
			return board[1][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;//平局
}
void Game(){
	char board[ROW][COL];
	InitBoard(board, ROW, COL);
	srand((unsigned long)time(NULL));
	char result = 0;
	while (1){
		ShowBoard(board, ROW, COL);
		PlayerMove(board, ROW, COL);
		result = IsEnd(board, ROW, COL);
		if (result != NEXT){
			break;
		}
		ShowBoard(board, ROW, COL);
		ComputerMove(board, ROW, COL);
		result = IsEnd(board, ROW, COL);
		if (result != NEXT){
			break;
		}
	}
	ShowBoard(board, ROW, COL);
	switch (result){
	case WHITE:
		printf("You Win!\n");
		break;
	case BLACK:
		printf("You Defeat!\n");
		break;
	case DRAW:
		printf("You == Computer!\n");
		break;
	default:
		printf("BUG!\n");//如果出现这个,前面的代码有误
		break;
	}
}

主函数

#include"game.h"
//职业化:(自顶向下的设计方法)先不考虑细节,假设所有的接口已经存在;
//switch-case中,频率最高的case放在前面
void Menu(){
	printf("+--------------------------+\n");
	printf("| 1.Play            0.Exit |\n");
	printf("+--------------------------+\n");
}
int main(){
	int select = 0;
	int quit = 0;
	while (!quit){
		Menu();
		printf("Please Select#");
		scanf("%d", &select);
		switch (select){
		case 1:
			Game();
			break;
		case 0:
			quit = 1;
			break;
		default:
			printf("Enter Error,Try Again!\n");
			break;
		}
}
	printf("ByeBye!\n");
	system("pause");
	return 0;
}

运行结果

三子棋小游戏(C语言版)_第1张图片

你可能感兴趣的:(c语言)