一个简单的三子棋游戏

game.h

#pragma once


#define  ROW  3
#define  COL  3


void menu();
void  game();
void printboard(char board[ROW][COL], int row, int col);
void  boardinit(char board[ROW][COL], int row, int col);//棋盘初始化
int judge_winner(char board[ROW][COL]);
void computermove(char board[ROW][COL]);//电脑落子
void playermove(char board[ROW][COL]);//玩家落子
int blance_judge(char board[ROW][COL]);//平局判定

game.c

#define  _CRT_SECURE_NO_WARNINGS 1
#include
#include"game.h"
#include


void menu()
{
	printf("**********  1.play   0.exit *********\n");
}
void printboard(char board[ROW][COL],int row,int col)
{
	int i = 0;
	int j = 0;
	for (i = 0;i < row;i++)
	{
		for (j = 0;j < col;j++)
		{
			printf(" %c ", board[i][j]);
			if(j");
		scanf("%d %d",&x,&y);
		if (board[(x-1)][(y-1)] == ' ')
		{
			board[(x-1)][(y-1)] = '#';
			break;
		}
		printf("位置错误,重新输入\n");
	} while (1);
}

int blance_judge(char board[ROW][COL])//平局判定
{
	int count = 0;
	int i = 0;
	int j = 0;
	for (i = 0;i < ROW;i++)
	{
		for (j = 0;j < COL;j++)
		{
			if (board[i][j] == ' ')
				count++;
		}
	}
	return count;
}
void game()
{
	int winner = 0;
	char chessboard[ROW][COL] = { ' ' };
	boardinit(chessboard, ROW, COL);
	printboard(chessboard, ROW, COL);
	do
	{
		computermove(chessboard);
		printboard(chessboard, ROW, COL);
		winner = judge_winner(chessboard);
		if (winner == 0)
		{
			printf("电脑胜利\n");
			break;
		}
		if (blance_judge(chessboard) == 0)
		{
			printf("平局\n");
			break;
		}
		playermove(chessboard);
		winner = judge_winner(chessboard);
		if (winner == 1)
		{
			printboard(chessboard, ROW, COL);
			printf("玩家胜利\n");
			break;
		}
		if (blance_judge(chessboard) == 0)
		{
			printf("平局\n");
			break;
		}
	} while (1);
}

main.c

#define  _CRT_SECURE_NO_WARNINGS 1
#include
#include"game.h"
#include
#include

int main()
{
	int input=0;
	srand((unsigned int)time(NULL));
	menu();
	scanf("%d", &input);
	switch (input)
	{
	case 0:
		return 0;
		break;
	case 1:
		game();
		break;
	default:
		printf("输入错误,请重新输入");
		break;
	}

	return 0;
}

你可能感兴趣的:(C语言学习分享,游戏,c语言,蓝桥杯)