欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

简单的三子棋实现

程序员文章站 2022-04-07 17:33:16
...

game.h

#pragma warning(disable: 4996)

#ifndef __GAME_H__ 
#define __GAME_H__

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


#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 PlayMove(char board[ROW][COL], int row, int col);
void ComputerMove(char board[ROW][COL], int row, int col);
char CheckWin(char board[ROW][COL], int row, int col);

enum OPTION
{
	EXIT,
    PLAY
};
#endif  //__GAME_H__

#ifndef 和 #endif 要一起使用,如果丢失#endif,可能会报错

#ifndef起到的效果是防止一个源文件两次包含同一个头文件,而不是防止两个源文件包含同一个头文件

分析:

当第一次包含test.h时,由于没有定义_TEST_H,条件为真,这样就会包含(执行)#ifndef _TEST_H和

#endif之间的代码,当第二次包含test.h时前面一次已经定义了_TEST_H,条件为假,#ifndef _TEST_H和

#endif之间的代码也就不会再次被包含,这样就避免了重定义了.

game.c

#include"game.h"

void InitBoard(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++)
		{
			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)
		{
			for (j = 0;j < col;j++)
			{
				printf("---");
				if (j < col - 1)
					printf("|");
			}
			printf("\n");
		}
	}
}

void PlayMove(char board[ROW][COL], int row, int col)
{
	int x = 0;
	int y = 0;
	printf("玩家走:\n");
	while (1)
	{
		printf("请输入坐标: \n");
		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] = 'X';
				break;
			 }
			else
				printf("输入坐标被占用!\n");	
		}
		else
			printf("输入坐标非法!\n");
	}
}

void ComputerMove(char board[ROW][COL], int row, int col)
{
	int x = 0;
	int y = 0;
	printf("电脑走: \n");
	while (1)
	{
		 x = rand() % row;
		 y = rand() % col;
		if (board[x][y] == ' ')
		{
			board[x][y] = 'O';
			break;
		}

	}

}

static int IsFull(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++)
		{
			if (board[i][j] == ' ')
				return 0;
		}
	}
	return 1;
}

char CheckWin(char board[ROW][COL], int row, int col)
{
	int i = 0;
	for (i = 0;i < row;i++)
	{
		if (board[i][0] == board[i][1] && board[i][1] == board[i][2])
		{
			return board[i][0];
		}
	}
	for (i = 0;i < col;i++)
	{
		if (board[0][i] == board[1][i] && board[1][i] == board[2][i])
		{
			return board[0][i];
		}
	}
	if (board[0][0] == board[1][1] && board[1][1] == board[2][2])
	{
		return board[1][1];
	}
	if (board[0][2] == board[1][1] && board[1][1] == board[2][0])
	{
		return board[1][1];
	}
	if (IsFull(board, row, col))
	{
		return 'PJ';
	}
	return ' ';
}

可是通过改变宏ROW.COL快速改变棋盘大小,但是判断输赢的条件随之也要修改

#include"game.h"

void menu()
{
	printf("*************************\n");
	printf("***1: play     0:exit****\n");
	printf("*************************\n");
}

void game()
{
	char win = 0;
	char board[ROW][COL] = { 0 };
	InitBoard(board,ROW,COL);
	DisplayBoard(board, ROW, COL);

	while (1)
	{
		PlayMove(board, ROW, COL);
		win = CheckWin(board, ROW, COL);
		DisplayBoard(board, ROW, COL);
		if (win != ' ')
			break;

		ComputerMove(board, ROW, COL);
		win = CheckWin(board, ROW, COL);
		DisplayBoard(board, ROW, COL);
		if (win != ' ')
			break;
	}

		 if(win == 'X')
		 {
			 printf("玩家赢!\n");
		 }
		 else if(win == 'O')
		 {
			 printf("电脑赢!\n");
		 }
		 else 
		 {
			 printf("平局!\n");
		 }

}
void test()
{
	int i = 0;
	srand((unsigned int)time(NULL));//初始化随机数种子
	do 
	{
		menu();
		printf("请输入: \n");
		scanf("%d", &i);
		switch(i)
		{
		case PLAY:
			printf("游戏开始\n");
			game();
			break;
		case EXIT:
			printf("游戏结束\n");
			break;
		default:
			printf("选择错误\n");
			break;
		}
	} while (i);
}

int main()
{
	test();
	system("pause");
	return 0;
}

‘X’玩家赢

‘O’电脑赢

‘PJ’平局

‘ ’未分出结果

简单的三子棋实现

ps:如果开始游戏时选择1 任意数字

能运行游戏,但是会发生错误~

求大神~~

简单的三子棋实现

相关标签: 游戏