C++EasyX之井字棋

视频链接

井字棋

用EasyX和C++实现井字棋小游戏

源码及注释

#include<graphics.h>

char board_data[3][3] =
{
	{'-','-','-'},
	{'-','-','-'},
	{'-','-','-'},
};

char current_piece = 'O';

//检测指定棋子的玩家是否获胜
bool CheckWin(char c)
{
	// 检查每一行
	for (int i = 0; i < 3; i++)
	{
		if (board_data[i][0] == c && board_data[i][1] == c && board_data[i][2] == c)
			return true;
	}
	// 检查每一列
	for (int j = 0; j < 3; j++)
	{
		if (board_data[0][j] == c && board_data[1][j] == c && board_data[2][j] == 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;
	// 如果没有匹配,则返回 false
	return false;
}

//检测当前是否出现平局
bool CheckDraw()
{
	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()
{
	line(0, 200, 600, 200);
	line(0, 400, 600, 400);
	line(200, 0, 200, 600);
	line(400, 0, 400, 600);

}

//棋子
void DrawPiece()
{
	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);
					break;
			case '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;
			}
		}
	}
}

//提示
void DrawTipText()
{
	static TCHAR str[64];
	_stprintf_s(str, _T("当前棋子类型: %c"), current_piece);

	settextcolor(RGB(225, 175, 45));
	outtextxy(0, 0, str);
}

int main()
{
	initgraph(600, 600);
	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 (CheckWin('X'))
		{
			MessageBox(GetHWnd(), _T("X 玩家获胜"), _T("游戏结束"), MB_OK);
			running = false;
		}
		else if (CheckWin('O'))
		{
			MessageBox(GetHWnd(), _T("Y 玩家获胜"), _T("游戏结束"), MB_OK);
			running = false;
		}
		else if (CheckDraw())
		{
			MessageBox(GetHWnd(), _T("平局!"), _T("游戏结束"), MB_OK);
			running = false;
		}

		DWORD end_time = GetTickCount();//自系统启动以来经历的毫秒数
		DWORD delta_time = end_time - start_time;//作差的到这次循环所消耗的时间

		if (delta_time < 1000 / 60) // 为了确保60hz刷新
		{
			Sleep(1000 / 60 - delta_time);//控制每次循环的时间消耗,而进行线程休眠
		}
		
	}
	EndBatchDraw();
	return 0;
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面我给你讲一下如何使用EASYX库设计井字棋游戏的思路。 首先,我们需要在程序中引入EASYX库,然后通过 `initgraph` 函数初始化窗口。接着,我们需要设计界面,可以使用 `rectangle` 函数绘制出棋盘和棋子。其中,棋盘可以用一个 3x3 的矩形数组来表示,每个矩形代表一个棋子的位置,初始时可以将数组中的值全部设为 0。 然后,我们需要设计游戏逻辑。每当玩家点击某个位置时,程序需要判断该位置是否已经被占据。如果已经被占据,则提示玩家重新选择;否则,将该位置设置为当前玩家的棋子(可以用 1 表示玩家 1 的棋子,用 2 表示玩家 2 的棋子),然后判断是否满足胜利条件(即同一行、同一列或同一对角线上有三个相同的棋子)。如果满足胜利条件,则游戏结束,否则交换玩家并进入下一轮。 最后,我们需要在程序中加入音效和背景音乐,可以使用 `mciSendString` 函数来实现。 下面是一份简单的代码示例,帮助你更好地理解上述思路: ```c #include <graphics.h> #include <conio.h> #include <stdio.h> #include <stdlib.h> #include <windows.h> #include <mmsystem.h> #pragma comment(lib,"Winmm.lib") #define ROW 3 #define COL 3 int chessboard[ROW][COL] = {0}; // 棋盘数组 int player = 1; // 当前玩家 void drawChessboard() // 绘制棋盘 { for(int i = 0; i < ROW; i++) { for(int j = 0; j < COL; j++) { rectangle(j * 50 + 50, i * 50 + 50, j * 50 + 100, i * 50 + 100); } } } void drawChessman(int x, int y) // 绘制棋子 { if(chessboard[x][y] == 1) { setfillcolor(BLACK); fillellipse(y * 50 + 75, x * 50 + 75, 20, 20); } else if(chessboard[x][y] == 2) { setfillcolor(WHITE); fillellipse(y * 50 + 75, x * 50 + 75, 20, 20); } } int checkWin() // 判断胜利条件 { for(int i = 0; i < ROW; i++) { if(chessboard[i][0] == chessboard[i][1] && chessboard[i][1] == chessboard[i][2] && chessboard[i][0] != 0) { return chessboard[i][0]; } } for(int j = 0; j < COL; j++) { if(chessboard[0][j] == chessboard[1][j] && chessboard[1][j] == chessboard[2][j] && chessboard[0][j] != 0) { return chessboard[0][j]; } } if(chessboard[0][0] == chessboard[1][1] && chessboard[1][1] == chessboard[2][2] && chessboard[0][0] != 0) { return chessboard[0][0]; } if(chessboard[0][2] == chessboard[1][1] && chessboard[1][1] == chessboard[2][0] && chessboard[0][2] != 0) { return chessboard[0][2]; } return 0; } int main() { initgraph(400, 400); // 初始化窗口 drawChessboard(); // 绘制棋盘 PlaySound(TEXT("bgm.wav"), NULL, SND_FILENAME | SND_ASYNC | SND_LOOP); // 播放背景音乐 int x, y; while(1) { if(player == 1) { settextcolor(BLACK); outtextxy(250, 100, "Black's turn"); } else { settextcolor(WHITE); outtextxy(250, 100, "White's turn"); } if(kbhit()) // 判断是否有键盘输入 { int key = getch(); if(key == 27) // 按下 ESC 键退出游戏 { break; } else if(key == 32) // 按下空格键重新开始游戏 { cleardevice(); drawChessboard(); memset(chessboard, 0, sizeof(chessboard)); player = 1; continue; } x = (GetCursorPos().y - 50) / 50; y = (GetCursorPos().x - 50) / 50; if(x >= 0 && x < ROW && y >= 0 && y < COL && chessboard[x][y] == 0) // 判断是否是有效位置 { chessboard[x][y] = player; drawChessman(x, y); int result = checkWin(); if(result != 0) // 判断游戏是否结束 { if(result == 1) { settextcolor(BLACK); outtextxy(250, 200, "Black wins!"); } else { settextcolor(WHITE); outtextxy(250, 200, "White wins!"); } Sleep(1000); cleardevice(); drawChessboard(); memset(chessboard, 0, sizeof(chessboard)); player = 1; continue; } player = 3 - player; // 交换玩家 } } } closegraph(); // 关闭窗口 return 0; } ``` 上述代码仅供参考,你可以根据自己的需求进行修改和优化。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值