简易版三子棋(c语言版)

简易版三子棋(c语言版),第1张

简易版三子棋(c语言版)

小游戏的代码块分为三个部分,测试函数(test.c):运行小游戏和实现游戏逻辑部分,game.h:引头文件,函数声明,game.c:实现游戏中的函数

test.c

#define _CRT_SECURE_NO_WARNINGS
#include "game.h"

void menu() {
	printf("***************************n");
	printf("*******    1.play     *****n");
	printf("*******    0.exit     *****n");
	printf("***************************n");

}

void game() {
	//创建二维数组,保存数据
	char board[ROW][COL];

	//初始化棋盘
	InitBoard(board, ROW, COL);
	//打印棋盘
	DisplayBoard(board, ROW, COL);

	//下棋
	//返回'*'表示玩家赢了
	//返回'#'表示电脑赢了
	//返回'Q'表示平局
	// 返回‘C’表示游戏继续

	char ret = 0;
	while (1)
	{
		//玩家下
		PlayerMove(board, ROW, COL);
		DisplayBoard(board, ROW, COL);
		//走完后判断输赢
		ret=IsWin(board, ROW, COL);
		if (ret != 'C')
			break;
		
		//电脑下
		ComputerMove(board, ROW, COL);
		DisplayBoard(board, ROW, COL);
		ret=IsWin(board, ROW, COL);

		if (ret != 'C')
			break;
	}
	if (ret == '*') {
		printf("玩家赢了n");
		DisplayBoard(board, ROW, COL);
	}
	else if (ret == '#') {
		printf("电脑赢了n");
		DisplayBoard(board, ROW, COL);
	}
	else {
		printf("平局n");
		DisplayBoard(board, ROW, COL);
	}
		
		
	
}
int main()
{
	srand((unsigned int)time(NULL));//产生随机数
	int input = 0;
	do
	{
		menu();
		printf("请输入你的选择>:");
		scanf("%d", &input);
		switch (input)
		{
		case 1:
			game();
			break;
		case 0:
			printf("退出游戏n");
			break;
		default:
			printf("输入错误,请重新输入n");
			break;
		}
	} while (input);
	return 0;
}

game.h

#pragma once

//引用头文件
#include 
#include 
#include 

#define ROW 3
#define COL 3

//函数声明
void InitBoard(char board[ROW][COL],int row,int col);

void DisplayBoard(char board[ROW][COL], int row, int col);

void PlayerMove(char board[ROW][COL], int row, int col);

void ComputerMove(char board[ROW][COL], int row, int col);

char IsWin(char board[ROW][COL], int row, int col);

game.c

#define _CRT_SECURE_NO_WARNINGS

#include "game.h"

void InitBoard(char board[ROW][COL], int row, int col)
{
	int i = 0;
	for (i = 0; i < row; i++)
	{
		int j = 0;
		for (j = 0;j < col;j++) {
			board[i][j] = ' ';
		}
	}
}

void DisplayBoard(char board[ROW][COL], int row, int col) {
	int i = 0;
	for (i = 0;i < row;i++) {
		int j = 0;
		for (j = 0;j < col;j++) {
			printf(" %c ", board[i][j]);
			if (j < col - 1)
				printf("|");
		}
		printf("n");

		if (i < row - 1) {
			int j = 0;
			for (j = 0;j < col;j++) {
				printf("---");
				if (j < col - 1)
					printf("|");
			}
			printf("n");
		}
	}
}


void PlayerMove(char board[ROW][COL], int row, int col) {
	int x = 0;
	int y = 0;
	printf("玩家走>n");
	while (1)
	{
		printf("请输入玩家下棋的坐标>:");
		scanf("%d %d", &x, &y);
		if (x >= 1 && x <= row && y >= 1 && y <= col) {
			if (board[x - 1][y - 1] == ' ') {
				board[x - 1][y - 1] = '*';
				break;
			}
			else
			{
				printf("坐标被占用,请重新输入n");
			}
		}
		else
		{
			printf("坐标非法请重新输入n");
		}
	}
	
}


void ComputerMove(char board[ROW][COL], int row, int col) {
	
	printf("电脑走n");
	while (1)
	{
		int x = rand() % row;
		int y = rand() % col;
		if (board[x][y] == ' ')
		{
			board[x][y] = '#';
			break;
		}
	}
	
}
int IsFull(char board[ROW][COL], int row, int col) {
	int i = 0;
	for (i = 0;i < row;i++) {
		int j = 0;
		for (j = 0;j < col;j++) {
			if (board[i][j] == ' ')
				return 0;
		}
	}
	return 1;
}

char IsWin(char board[ROW][COL], int row, int col) {
	//3行的情况
	int i = 0;
	for (i = 0;i < row;i++) {
		if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][1] != ' ') {
			return board[i][1];
		}
	}

	//3列的情况
	
	for (i = 0;i < col;i++) {
		if (board[0][i] == board[1][i] && board[1][i] == board[2][i] && board[1][i] != ' ') {
			return board[1][i];
		}
	}


	//两条对角线
	if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[1][1] != ' ') {
		return board[1][1];
	}
	if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[1][1] != ' ') {
		return board[1][1];
	}

	//判断平局
	int ret = 0;
	ret = IsFull(board, ROW, COL);
	if (ret == 1) {
		//棋局满了返回1,没满返回0
		return 'Q';
	}
	else
	{
		return 'C';
	}
}

测试截图:

 

 

 

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5699009.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存