C语言-三子棋游戏

C语言中用写头文件的方式写了一个三子棋游戏

1.测试函数text.c

#define _CRT_SECURE_NO_WARNINGS 1

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<time.h>
#include"chess.h"

void menu()
{
	printf("***********************\n");
	printf("******  1.play  *******\n");
	printf("******  0.exit  *******\n");
	printf("***********************\n");
	printf("请选择相应的数字>:");
}

int main()
{
	char board[LINE][LIST];
	int input = 1;
	int a = 0;
	srand((unsigned int)time(NULL));
	
	while (input)
	{
		memset(board, ' ', sizeof(char)*LINE*LIST);
		menu();
		scanf("%d", &input);
		printf("\n");
		switch (input)
		{
		case 1:a = game(board);
			break;
		case 0:break;
		default:printf("你的输入不符合要求,请重新输入>:"); break;
		}
		if (0 == input)
		{
			break;
		}	
	}
	printf("游戏结束,欢迎再次使用!!!\n");
	return 0;
}


2.头文件chess.h

#ifndef __CHESS_H__
#define __CHESS_H__

#define LINE 3
#define LIST 3

int game(char board[LINE][LIST]);
void Player(char board[LINE][LIST]);
void Computer(char board[LINE][LIST]);
void ChessBoard(char board[LINE][LIST]);
int Check(char board[LINE][LIST]);
//void ChessBoard(int LINE,int LIST );       此语句错误,不可以直接用宏作为参数,头文件中不可以这样写,LIST和LINE可以作为实参,不可以作为形参
#endif  //__CHESS_H__


3.函数文件chess.c


#define _CRT_SECURE_NO_WARNINGS 1

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include"chess.h"

void ChessBoard(char board[LINE][LIST])
{
	printf("%c|%c|%c\n", board[0][0], board[0][1], board[0][2]);
	printf("-----\n");
	printf("%c|%c|%c\n", board[1][0], board[1][1], board[1][2]);
	printf("-----\n");
	printf("%c|%c|%c\n", board[2][0], board[2][1], board[2][2]);
	printf("\n\n");
}

int game(char board[LINE][LIST])
{
	ChessBoard(board);//为了使电脑赢了之后可以显示出棋盘来
	while (1)
	{
		Player(board);
		ChessBoard(board);
		if (Check(board))
		{
			return 0;
		}
		Computer(board);
		ChessBoard(board);
		if (Check(board))
		{
			return 0;
		}

	}
}

void Player(char board[LINE][LIST])
{
	int x = 0, y = 0;
	int m = 1;//设置m是为了使输入数据不合法
	printf("玩家玩\n请输入坐标>:");
	while (m)
	{
		scanf("%d%d", &x, &y);
		printf("\n");
		if ((x >= 1 && x <= 3) && (board[x - 1][y - 1] == ' '))
		{
			board[x - 1][y - 1] = 'X';
			m = 0;
		}
		else
		{
			printf("数据不符合要求\n请重新输入>:");
		}
	}
}

void Computer(char board[LINE][LIST])  
{
	int x = 0, y = 0;
	int m = 1;
	printf("电脑玩\n");
	while (m)
	{
		x = rand()% 3;
		y = rand()% 3;
		
		if (board[x][y] == ' ')
		{
			board[x][y] = '#';
			m = 0;
		}
	}
}

int Check(char board[LINE][LIST])   //When you pass an array, you must add the brackets in the back of the array.
{
	int i = 0,j = 0,flag = 0;
	for (i = 0; i <= 2; i++)
	{
		if (((board[i][0] == board[i][1]) && (board[i][1] == board[i][2]) && (board[i][0] == 'X')) || ((board[0][i] == board[1][i]) && (board[1][i] == board[2][i]) && (board[0][i] == 'X')))
		{
			printf("The player wins the game!!!\n\n");
			return 1;
		}
		if (((board[i][0] == board[i][1]) && (board[i][1] == board[i][2]) && (board[i][0] == '#')) || ((board[0][i] == board[1][i]) && (board[1][i] == board[2][i]) && (board[0][i] == '#')))
		{
			printf("The computer wins the game!!!\n\n");
			return 1;
		}
		if (((board[0][0] == board[1][1]) && (board[0][0] == board[2][2]) && (board[0][0] == 'X')) || ((board[0][2] == board[1][1]) && (board[0][2] == board[2][0]) && (board[0][2] == 'X')))
		{
			printf("The player wins the game!!!\n\n");
			return 1;
		}
		if (((board[0][0] == board[1][1]) && (board[0][0] == board[2][2]) && (board[0][0] == '#')) || ((board[0][2] == board[1][1]) && (board[0][2] == board[2][0]) && (board[0][2] == '#')))
		{
			printf("The computer wins the game!!!\n\n");
			return 1;
		}
		for (j = 0; j <= 2; j++)  //平局
		{
			if (' ' != board[i][j])
			{
				flag++;
			}

		}
		if (9 == flag)
		{
			printf("平局\n");
			return 1;
		}
	}
	
	return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值