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

导读:本篇文章讲解 三子棋小游戏(C语言版),希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com

头文件

#ifndef __GAME_H__
#define __GAME_H__
#include<stdio.h>
#include<windows.h>
#include<time.h>
#include<stdlib.h>
//良好的代码风格建议使用宏定义,有效减少代码后序的维护成本
#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<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[][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语言版)

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/110986.html

(0)
小半的头像小半

相关推荐

极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!