三子棋游戏

游戏分成三个模块来写

1.test.c(游戏测试):主要实现棋盘的打印,玩家下棋,电脑下棋,最后判断游戏结果

#define _CRT_SECURE_NO_WARNINGS 1
#include 
#include "game.h"
#include 
#include 
void menu()
{
	printf("*************************************************\n");
	printf("********************* 1.play ********************\n");
	printf("********************* 0.exit ********************\n");
	printf("*************************************************\n");
}
void game()
{
	char borad[ROW][COL];
	char ret = 0;
	//初始化棋盘
	init_borad(borad, ROW, COL);
	print_borad(borad, ROW, COL);
	while (1)
	{
		player_move(borad, ROW, COL);
		print_borad(borad, ROW, COL);
		//判断输赢,一定要告诉我们是谁赢了,或是平局,平局游戏继续
		//*玩家赢
		//#电脑赢
		//q平局
		//游戏继续c
		ret = is_win(borad, ROW, COL);
		if (ret != 'c')
		{
			break;
		}
		computer_move(borad, ROW, COL);
		print_borad(borad, ROW, COL);
		//判断输赢
		ret = is_win(borad, ROW, COL);
		if (ret != 'c')
		{
			break;
		}
	}
	if (ret == '*')
		printf("玩家赢了!恭喜发财\n");
	else if (ret == '#')
	{
		printf("你被人机打败了,你真菜,你个大菜狗\n");
	}
	else if (ret == 'q')
		printf("你也就和人机能打个平局,你的菜狗\n");
}
void test()
{
	srand(time(NULL));
	int input=0;
	do
	{
		menu();
		printf("请选择->");
		scanf("%d", &input);
		switch (input)
		{
		case 1:
			printf("游戏开始\n");
			game();
			break;
		case 0:
			printf("退出游戏\n");
			break;
		default:
			printf("选择错误!!!!\n");
			break;
		}
	} while (input);
}
int main()
{
	test();
	return 0;
}

game.h 

#pragma once
#define ROW 3
#define COL 3
#include 
#include 
#include 
//声明函数
void init_borad(char borad[ROW][COL], int row, int col);//创建一个棋盘
void print_borad(char borad[ROW][COL], int row, int col); //打印棋盘
void player_move(char borad[ROW][COL], int row, int col);//玩家
void computer_move(char borad[ROW][COL], int row, int col);//电脑下棋
char is_win(char borad[ROW][COL],int row,int col);//判断结果

game.c

#define _CRT_SECURE_NO_WARNINGS 1
#include "game.h"
void init_borad(char borad[ROW][COL], int row, int col)
{
	int i = 0;
	for (i = 0; i < row; i++)
	{
		for (int j = 0; j < col; j++)
			borad[i][j] = ' ';
	}
}

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

}
void player_move(char borad[ROW][COL], int row, int col)
{
	printf("玩家开始下棋:\n");
	while (1)
	{
		printf("请输入要下棋的坐标:\n");
		int x=0, y=0;
		scanf("%d %d", &x, &y);
		if (x >= 1 && x <= row && y >= 1 && y <= col)
		{
			if (borad[x - 1][y - 1] == ' ')
			{
				borad[x - 1][y - 1] = '*';
				break;
			}
			else
			{
				printf("这个棋格已经被占用了,请重新选择\n");
			}
		}
		else
		{
			printf("输入不合法,请重新输入\n");
		}
	}
}
void computer_move(char borad[ROW][COL], int row, int col) //电脑下棋,只要坐标没有被占用,就可以下棋
{
	printf("电脑开始下棋:\n");
	while (1)
	{
		int x = rand() % row;
		int y = rand() % col;
		if (borad[x][y] == ' ')
		{
			borad[x][y] = '#';
			break;
		}

	}
}
static int is_full(char borad[ROW][COL], int row, int col)
{
	int i,j;
	for (i = 0; i < row; i++)
	{
		for (j = 0; j < col; j++)
		{
			if (borad[i][j] == ' ')
				return 0;
		}
	}
	return 1;
}
char is_win(char borad[ROW][COL], int row, int col)
{
	int i=0;
	for (i = 0; i < row; i++)//行
	{
		if (borad[i][0] == borad[i][1] && borad[i][0]== borad[i][2] && borad[i][0]!= ' ')
		{
			return borad[i][0];
		}
	}
	for (i = 0; i < col; i++)//列
	{
		if (borad[0][i] == borad[1][i] && borad[1][i] == borad[2][i] && borad[0][i] != ' ')
		{
			return borad[0][i];
		}
	}
	//主对角线
	if (borad[0][0] == borad[1][1] && borad[1][1] == borad[2][2] && borad[0][0] != ' ')
	{
		return borad[0][0];
	}
	if (borad[0][2] == borad[1][1] && borad[1][1] == borad[2][0] && borad[1][1] != ' ')//副对角线
	{
		return borad[1][1];
	}
	if (is_full(borad, row, col)==1)
	{
		return 'q';
	}
	return 'c';
}

你可能感兴趣的:(前端)