当前位置: 首页 > 工具软件 > TAC > 使用案例 >

井字棋(tic tac toe)

督建柏
2023-12-01
井字棋 ,英文名叫Tic-Tac-Toe,是一种在3*3格子上进行的连珠游戏,和五子棋类似,由于棋盘一般不画边框,格线排成井字故得名。游戏需要的工具仅为纸和笔,然后由分别代表O和X的两个游戏者轮流在格子里留下标记(一般来说先手者为X),任意三个标记形成一条直线,则为获胜。

//

头文件

#ifndef __GAME_H__
#define __GAME_H__


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

#define ROW 3
#define COL 3

void InitBroad(char broad[ROW][COL], int row, int col);//初始化数组
void OutPut(char broad[ROW][COL], int row, int col);//输出棋盘
char ComputerTurn(char broad[ROW][COL], int row, int col);//电脑下棋
char PlayerTurn(char broad[ROW][COL], int row, int col);//玩家下棋
char IfWin(char broad[ROW][COL], int row, int col);//判断胜负
int Winner(win);

#endif //__GAME_H__

游戏运行函数

#include"Game.h"

void InitBroad(char broad[ROW][COL], int row, int col)//初始化数组,全置为空格
{
	/*int i = 0;
	int j = 0;
	for (i = 0; i < row; i++)
	{
		for (j = 0; j < col; j++)
			broad[i][j] = ' ';
	}*/
	memset(&broad[0][0], ' ', row*col*sizeof(broad[0][0]));
}

void OutPut(char broad[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 (j < col - 1)
				printf(" %c |", broad[i][j]);
			else
				printf(" %c ", broad[i][j]);
		}
		printf("\n");
		if (i < row - 1)
		{
			for (j = 0; j < col; j++)
			{
				if (j < col - 1)
					printf("---|");
				else
					printf("---");
			}
		}
		printf("\n");
	}
}
char ComputerTurn(char broad[ROW][COL], int row, int col)//电脑下棋
{
	int x = 0;
	int y = 0;
	printf("电脑下:\n");
	while (1)
	{
		x = rand() % row;
		y = rand() % col;
		if (broad[x][y] == ' ')
		{
			broad[x][y] = 'X';
			break;
		}
	}
}

char PlayerTurn(char broad[ROW][COL], int row, int col)//玩家下棋
{
	int x = 0;
	int y = 0;
	while (1)
	{
		printf("玩家下:\n");
		scanf("%d%d", &x, &y);

		if (x > 0 && x <= row  && y > 0 && y <= col)
		{
			if (broad[x-1][y-1] == ' ')
			{
				broad[x - 1][y - 1] = 'O';
				break;
			}
			else 
				printf("位置已被占用,请重新输入!\n");
		}
		else
			printf("超出棋盘范围,请重新输入:\n");
			
	}
}
char IfWin(char broad[ROW][COL], int row, int col)//判断胜负
{
	int i = 0;
	int j = 0;
	int count = 0;
	for (i = 0; i < row; i++)
	{
		for (j = 0; j < col; j++)
		{
			if (broad[i][j] == ' ')
				count++;
		}
	}
	for (i = 0; i < row; i++)
	{
		if (broad[i][0] == broad[i][1] &&
			broad[i][1] == broad[i][2] && broad[i][0] != ' ')
			return broad[i][0];
	}
	for (j = 0; j < col; j++)
	{
		if (broad[0][j] == broad[1][j] &&
			broad[0][j] == broad[2][j] && broad[0][j] != ' ')
			return broad[0][j];
	}
	if (broad[0][0] == broad[1][1] &&
		broad[1][1] == broad[2][2] && broad[1][1] != ' ')
		return broad[1][1];
	else if (count == 0)
		return 'P';

	return 0;
}
int Winner(win)
{
	if (win == 'X')
	{
		printf("电脑胜利啦!\n");
		return 1;
	}
	else if (win == 'O')
	{
		printf("玩家胜利啦!\n");
		return 1;
	}
	else if (win == 'P')
	{
		printf("平局!");
		return 1;
	}
	else
		return 0;
}
#include<stdio.h>
#include<windows.h>
#include"Game.h"
#pragma warning (disable:4996)

void menu()//游戏初始界面
{
	printf("*******************************\n");
	printf("*********    1.play    ********\n");
	printf("*********    0.exit    ********\n");
	printf("*******************************\n");
}

int work()//判断游戏是否进行
{
	printf("是否开始一轮新的游戏:");
	while (1)
	{
		int i = 0;
		scanf("%d", &i);
		if (i == 1)
			return 1;
		else if (i == 0)
			return 0;
		else
			printf("输入错误,请重新输入:");
	}
}


void game()
{
	char win = ' ';
	char broad[ROW][COL] = {0};
	InitBroad(broad, ROW, COL);//初始化棋盘
	while (1)
	{
		ComputerTurn(broad, ROW, COL);//电脑下
		OutPut(broad, ROW, COL);//打印棋盘
		win = IfWin(broad, ROW, COL);//判断胜负
		if (win != 0)
		{
			OutPut(broad, ROW, COL);
			break;
		}
		PlayerTurn(broad, ROW, COL);//玩家下
		OutPut(broad, ROW, COL);//打印棋盘
		if (win != 0) //判断胜负
		{
			OutPut(broad, ROW, COL);
			break;
		}
	}
	Winner(win);//输出胜者
}

int main()
{
	srand((unsigned)time(NULL));
	while (1)
	{
		menu();
		if (work())
			game();
		else
			break;
	}
	printf("游戏结束!\n");

	system("pause");
	return 0;
}
 类似资料: