井字棋(easyX实现)

#include<graphics.h>
#include<iostream>
#define DELTA 50
#define LINE 5
char board_data[3][3] =
{
	{'-','-','-'},
	{'-','-','-'},
	{'-','-','-'},
};

char current_piece = 'O';

//检测是否获胜
bool CheakWin(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;
}

//检测是否平局
bool CheakDraw()
{
	for (size_t i = 0; i < 3; i++)
	{
		for (size_t j = 0; j < 3; j++)
		{
			if (board_data[i][j] == '-')
			{
				return false;
			}
		}
	}
	return true;
}

//绘制棋盘网格
void DrawBoard()
{
	setlinecolor(BLACK);

	solidrectangle(0, 200 + LINE, 600, 200 - LINE);
	solidrectangle(0, 400 + LINE, 600, 400 - LINE);
	solidrectangle(200 + LINE, 0, 200 - LINE, 600);
	solidrectangle(400 + LINE, 0, 400 - LINE, 600);
}

//绘制棋子
void DrawPiece()
{
	//设置画线样式为宽度 10 像素的实线,连接处为斜面
	setlinestyle(PS_SOLID | PS_JOIN_BEVEL, 10);
	for (size_t i = 0; i < 3; i++)
	{
		for (size_t j = 0; j < 3; j++)
		{
			switch (board_data[i][j])
			{
			case 'O':
				circle(200 * j + 100, 200 * i + 100, 100 - DELTA);
				break;
			case 'X':
				line(200 * j + DELTA, 200 * i + DELTA, 200 * (j + 1) - DELTA, 200 * (i + 1) - DELTA);
				line(200 * (j + 1) - DELTA, 200 * i + DELTA, 200 * j + DELTA, 200 * (i + 1) - DELTA);
				break;
			case '-':
				break;
			default:
				break;
			}
		}
	}
}

//绘制提示信息
void DrawTipText()
{
	static TCHAR str[64];
	_stprintf_s(str, _T("当前棋子类型:%c"), current_piece);
	settextstyle(24, 0, _T("华文琥珀"));//设置字号、字体
	settextcolor(RGB(124, 37, 249));
	outtextxy(0, 0, str);
}

int main()
{

    initgraph(600, 600);
	setbkcolor(RGB(113, 240, 247));
	
	bool running = true;

	ExMessage msg;

	BeginBatchDraw();

	while (running)
	{
		DWORD start_time = GetTickCount();

		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 (CheakWin('X'))
		{
			MessageBox(GetHWnd(), _T("X 玩家获胜"), _T("游戏结束"), MB_OK);
			running = false;
		}

		else if (CheakWin('O'))
		{
			MessageBox(GetHWnd(), _T("O 玩家获胜"), _T("游戏结束"), MB_OK);
			running = false;
		}
		
		else if(CheakDraw())
		{
			MessageBox(GetHWnd(), _T("平局"), _T("游戏结束"), MB_OK);
			running = false;
		}

		DWORD end_time = GetTickCount();
		DWORD delta_time = end_time - start_time;

		if (delta_time < 1000 / 60)
		{
			Sleep(1000 / 60 - delta_time);
		}
	}

	EndBatchDraw();
    return 0;
}

  • 9
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
C++井字棋游戏代码可以使用EasyX图形库进行实现,下面是一个简单的示例代码: ``` #include <graphics.h> #include <conio.h> const int kBoardSize = 3; // 棋盘大小 // 绘制棋盘 void DrawBoard() { cleardevice(); // 清空屏幕 int width = getwidth() / kBoardSize; int height = getheight() / kBoardSize; // 绘制横线 for (int i = 1; i < kBoardSize; i++) { line(0, i * height, getwidth(), i * height); } // 绘制竖线 for (int i = 1; i < kBoardSize; i++) { line(i * width, 0, i * width, getheight()); } } // 下棋 void PlayChess(int row, int col, bool is_black) { int x = col * getwidth() / kBoardSize + getwidth() / kBoardSize / 2; int y = row * getheight() / kBoardSize + getheight() / kBoardSize / 2; if (is_black) { setcolor(BLACK); circle(x, y, getwidth() / kBoardSize / 2 - 10); } else { setcolor(WHITE); line(x - getwidth() / kBoardSize / 2 + 10, y - getheight() / kBoardSize / 2 + 10, x + getwidth() / kBoardSize / 2 - 10, y + getheight() / kBoardSize / 2 - 10); line(x - getwidth() / kBoardSize / 2 + 10, y + getheight() / kBoardSize / 2 - 10, x + getwidth() / kBoardSize / 2 - 10, y - getheight() / kBoardSize / 2 + 10); } } int main() { initgraph(600, 600); DrawBoard(); bool is_black = true; // 黑棋先手 while (true) { if (_kbhit()) { // 如果有按键按下 char ch = _getch(); if (ch == 27) break; // 如果是Esc键,退出游戏 int row = -1; int col = -1; bool can_put = false; switch (ch) { case 'q': row = 0; col = 0; can_put = true; break; case 'w': row = 0; col = 1; can_put = true; break; case 'e': row = 0; col = 2; can_put = true; break; case 'a': row = 1; col = 0; can_put = true; break; case 's': row = 1; col = 1; can_put = true; break; case 'd': row = 1; col = 2; can_put = true; break; case 'z': row = 2; col = 0; can_put = true; break; case 'x': row = 2; col = 1; can_put = true; break; case 'c': row = 2; col = 2; can_put = true; break; } if (can_put) { PlayChess(row, col, is_black); is_black = !is_black; } } } closegraph(); return 0; } ``` 此示例中,通过`initgraph`函数初始化图形界面,然后在`DrawBoard`函数中绘制棋盘,`PlayChess`函数用于在棋盘上下棋。在`main`函数中,通过`_kbhit`函数检测键盘是否有按键按下,然后根据按键的不同来确定落子的位置,通过`PlayChess`函数在棋盘上下棋,并通过`is_black`变量来控制黑白棋的交替。当按下Esc键时,游戏退出。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

叶孤程

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值