C实现---三子棋游戏

三子棋游戏,即在3*3的矩阵上下棋,一个人画叉一个人画圈,谁先出现成行或成列或成对角线三个相同的棋子就算谁赢。用编程实现,输入对应的坐标,判断给定棋局的状态。

下面这个游戏由一个test.c测试源文件

                          一个game.c玩游戏的源文件

                         其对应的头文件game.h完成的

这写代码的好处有利于测试, 方便管理和以后的更改和优化游戏


game.h   //头文件

#ifndef __GAME_H__
#define __GAME_H__

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

#define Rows 3
#define Cols 3

void is_board(char a[Rows][Cols], int rows, int cols);        //初始化棋盘
void display_board(char a[Rows][Cols], int rows, int cols);  //打印棋盘
void play_person(char a[Rows][Cols], int rows, int cols); //玩家下棋
void play_computer(char a[Rows][Cols]);                   //电脑下棋
char dcide(char a[Rows][Cols], int rows, int cols);      //判断谁赢


#endif  //__GAME_H__

test.c   //用于调用和测试三子棋各个部分功能的源文件

# define _CRT_SECURE_NO_WARNINGS 1

#include"game.h"

enum G                          //枚举类型  
{
	EXIT,
	PLAY
};
void menu()          //主界面
{
	printf("!!!**************************************!!!\n");
	printf("  !!!***********  PLAY   1 ************!!!\n");
	printf("  !!!***********  EXIT   0 ************!!!\n");
	printf("!!!**************************************!!!\n");
}

void game()     //玩游戏函数
{
	char set = 0;
	char a[Rows][Cols] = { 0 };
	is_board(a, Rows, Cols);
	system("cls");
	display_board(a, Rows, Cols);

	while (1)
	{

		play_person(a, Rows, Cols);    //玩家下棋
		display_board(a, Rows, Cols);   //打印棋盘
		set = dcide(a, Rows, Cols);   //判断谁赢
		if (set != ' ')
		{
			break;
		}
		play_computer(a);                 //电脑下棋
		display_board(a, Rows, Cols);    //打印棋盘
		set = dcide(a, Rows, Cols);    //判断谁赢
		if (set != ' ')
		{
			break;
		}
	}
	if (set == 'X')
	{
		printf("%c%c%c 666 你赢了 %c%c%c\n", 001, 001, 001, 001, 001, 001);
		system("pause");
		system("cls");
	}
	else if (set == 'O')
	{
		printf("%c%c%c 电脑赢了 %c%c%c\n",001,001,001,001,001,001);
		system("pause");
		system("cls");
	}
	else
	{
		printf("%c%c%c 平局了 %c%c%c\n", 001, 001, 001, 001, 001, 001);
		system("pause");
		system("cls");
	}
}

int main()
{
	int key = 0;
	char arr[Rows][Cols] = { 0 };
	do
	{
		menu();
		printf("请输入1或者0:");
		scanf("%d", &key);
		fflush(stdin);
		switch(key)
		{
		case EXIT:system("cls");      //退出游戏
			   printf("!!谢谢使用!!\n");
			   system("pause");
			   exit(0);
		case PLAY:                  //玩游戏
		    {
				  game();
		    }
			break;
		default: printf("<!!无效选项!!>\n");
			     Sleep(1000); 
				 system("cls");  
				 break;
		}

	} while (key);
	return 0;
 } 


game.c   //用于实现三子棋各个功能的源文件

# define _CRT_SECURE_NO_WARNINGS 1
#include"game.h"


void is_board(char a[Rows][Cols],int rows, int cols)   //初始化棋盘
{
	int i = 0;
	int j = 0;
	for (i = 0; i < rows; i++)
	{
		for (j = 0; j < cols; j++)
			a[i][j] = ' ';
	}
	return 0;
}

void display_board(char a[Rows][Cols], int rows, int cols) //打印棋盘
{
	int i = 0;
	int j = 0;
	printf("<游戏进行中>\n");
	printf("  1   2   3\n");
	for (i = 0; i < rows-1; i++)
	{
		printf("%d_%c_|_%c_|_%c_\n",i+1, a[i][0], a[i][1], a[i][2]);
	}
	printf("3 %c | %c | %c \n",a[i][0], a[i][1], a[i][2]);
}

void play_person(char a[Rows][Cols], int rows, int cols)        //玩家下棋
{
	int x = 0;
	int y = 0;
	printf("该你下棋了,请输入对应坐标:\n");
	while (1)
	{
		scanf("%d%d", &x, &y);
		fflush(stdin);
		x--;
		y--;
		if ((x >= 0) && (x <= rows) && (y >= 0) && (y <= cols))
		{
			if (a[x][y] == ' ')
			{
				a[x][y] = 'X';
				system("cls");
				break;
			}
			else
			{
				printf("看准了在下\a\a\a\n");
			}
		}
		else
		{
			printf("请重新输入对应坐标:\a\a\a");
		}
	}
}
void play_computer(char a[Rows][Cols])  //电脑下棋
{
	srand((unsigned int)time(NULL));
	while (1)
	{
		int x = rand() % 3;
		int y = rand() % 3;
		if (a[x][y] == ' ')
		{
			a[x][y] = 'O';
			printf("电脑计算中....\n");
			Sleep(1500);
			system("cls");
			break;
		}
	}
}
static int full(char a[Rows][Cols], int rows, int cols)    //检查是否填满
{
	int i = 0, j = 0;
	for (i = 0; i < rows; i++)
	{
		for (j = 0; j < cols; j++)
		{
			if (a[i][j] == ' ')
				return 0;  //没有满
		}
	}
	return 1; //满了
}

char dcide(char a[Rows][Cols], int rows, int cols)    //判断谁赢
{
	int i = 0;
	int j = 0;
	for (i = 0; i < rows; i++) //判断三行
	{
		if ((a[i][0] == a[i][1]) && (a[i][1] == a[i][2]) && (a[i][0] != ' '))
		{
			return a[i][0];
		}
	}
	for (i = 0; i < cols; i++)  //判断三列
	{
		if ((a[0][i] == a[1][i]) && (a[1][i] == a[2][i]) && (a[0][i]!= ' '))
		{
			return a[0][i];
		}
	}
	if ((a[0][0] == a[1][1]) && (a[1][1] == a[2][2]) && (a[1][1] != ' '))  //判官对角线
	{
		return a[1][1];
	}
	if ((a[0][2] == a[1][1]) && (a[1][1] == a[2][0]) && (a[1][1] != ' '))  //判官对角线
	{
		return a[1][1];
	}
	if (full(a,rows, cols))
	{
		return 'T';  //平局
	}
	return ' ';
}



  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值