不以物喜,不以己悲

EasyX学习-井字棋

使用EasyX制作的井字棋

点击查看代码
#include <iostream>
#include<Windows.h>
#include<graphics.h>
const int fps = 60;
static const int row = 3;
static const int col = 3;
char board_data[row][col] =
{
	{'-', '-', '-'},
	{'-', '-', '-'},
	{'-', '-', '-'},
};
char current_piece = 'O';
/// <summary>
/// 检测指定棋子的玩家是否获胜
/// </summary>
/// <param name="c"></param>
/// <returns></returns>
bool CheckWin(char c)
{
	if (board_data[0][0] == c && board_data[0][1] == c && board_data[0][2] == c)
		return true;
	if (board_data[1][0] == c && board_data[1][1] == c && board_data[1][2] == c)
		return true;
	if (board_data[2][0] == c && board_data[2][1] == c && board_data[2][2] == c)
		return true;
	if (board_data[0][0] == c && board_data[1][0] == c && board_data[2][0] == c)
		return true;
	if (board_data[0][1] == c && board_data[1][1] == c && board_data[2][1] == c)
		return true;
	if (board_data[0][2] == c && board_data[1][2] == c && board_data[2][2] == c)
		return true;
	if (board_data[0][0] == c && board_data[1][1] == c && board_data[2][2] == c)
		return true;
	if (board_data[0][2] == c && board_data[1][1] == c && board_data[2][0] == c)
		return true;
	return false;
}
/// <summary>
/// 检测当前是否出现平局
/// </summary>
/// <returns></returns>
bool CheckDraw()
{
	for (size_t i = 0; i < row; i++)
	{
		for (size_t j = 0; j < col; j++)
		{
			if (board_data[i][j] == '-')//如果数组中有没下的棋子则说明不是平局
			{
				return false;
			}
		}
	}
	return true;
}
/// <summary>
/// 绘制棋盘网格
/// </summary>
void DrawBoard()
{
	line(0, 200, 600, 200);
	line(0, 400, 600, 400);
	line(200, 0, 200, 600);
	line(400, 0, 400, 600);
}
/// <summary>
/// 绘制棋子
/// </summary>
void DrawPiece()
{
	for (size_t i = 0; i < row; i++)
	{
		for (size_t j = 0; j < col; j++)
		{
			switch (board_data[i][j])
			{
			case 'O': //绘制O
			{
				circle(200 * j + 100, 200 * i + 100, 100);
			}
				break;
			case 'X': //绘制X
			{
				line(200 * j, 200 * i, 200 * (j + 1), 200 * (i + 1));
				line(200 * (j + 1), 200 * i, 200 * j, 200 * (i + 1));
			}
				break;
			case '-':
				break;
			default:
				break;
			}
		}
	}
}
/// <summary>
/// 绘制提示信息
/// </summary>
void DrawTipText()
{
	static TCHAR str[64] = {};
	_stprintf_s(str, _T("当前棋子类型:%c"), current_piece);
	settextcolor(RGB(255, 175, 45));
	outtextxy(0, 0, str);
}
int main()
{
	int w = 600;
	int h = 600;
	HWND hwnd = initgraph(w, h);
	bool running = true;
	BeginBatchDraw();
	ExMessage msg = {};
	while (running)
	{
		ULONGLONG start = GetTickCount64();
		while (peekmessage(&msg))
		{
			if (msg.message == WM_LBUTTONDOWN)
			{
				//计算点击位置
				int x = msg.x;
				int y = msg.y;
				int index_x = x / 200;
				int index_y = y / 200;

				//尝试落子
				if (board_data[index_y][index_x] == '-')
				{
					board_data[index_y][index_x] = current_piece;
					if (current_piece == 'O')
						current_piece = 'X';
					else
						current_piece = 'O';
				}
			}
		}
		
		cleardevice();
		DrawBoard();
		DrawPiece();
		DrawTipText();
		FlushBatchDraw();
		if (CheckWin('X'))
		{
			MessageBox(hwnd, _T("X 玩家获胜"), _T("游戏结束"), MB_OK);
			running = false;
		}
		else if (CheckWin('O'))
		{
			MessageBox(hwnd, _T("O 玩家获胜"), _T("游戏结束"), MB_OK);
			running = false;
		}
		else if (CheckDraw())
		{
			MessageBox(hwnd, _T("平局!"), _T("游戏结束"), MB_OK);
			running = false;
		}

		ULONGLONG end = GetTickCount64();
		ULONGLONG delta = end - start;
		if (delta < 1000 / fps)
		{
			Sleep(1000 / fps - delta);//延时
		}
	}
	EndBatchDraw();
	return 0;
}

学习视频

posted @ 2024-07-18 21:19  这种人  阅读(13)  评论(0编辑  收藏  举报