分享 秦时明月人机大战黑白棋

同学的 C高作业终于交上去了,我也可以把这个项目拉到博客上,以记录为初衷,其实更多也是希望能够得到大神的指点。

本人在上大一,靠着在网上公开课渐渐入门可视化编程(不知道这样谈不谈的上入门)。

第一次写Blog,也是第一次写 可视化 的程序。 也在MSDN 和 搜索引擎的帮助下 记住了不少 的API,但是自己真的是不太喜欢用 MFC,感觉 MFC 加 Windows API 真的好乱 。

闲话不多说,希望大家能够谅解程序出现的Bug, 也希望大家可以和 我这个 笨笨的学生 多交流交流,也希望大神能够传授给我这个小白一些经验

(皮肤的接口 用的是Skin# 提供的  资源是在易语言的黑白棋里面抽出来的

窗口创建采用的是CreateDialog函数,然后在回调函数里处理消息,但是有个疑点是在回调函数里处理不到 按键被按下的消息,只好在消息循环里面处理按键被按下的消息。

VOID MessageLoop(VOID)
{
	MSG msg;
	while(GetMessage(&msg, NULL, NULL, NULL))
	{
        if (msg.message == WM_KEYDOWN)
        {
            switch(msg.wParam)
            {
            case VK_ADD:
                /* 更换下一个皮肤 */
                if (SkinID != 72)
                {
                    SkinID++;
                    SkinH_AttachFile(SkinID);
                }
                break;
            case VK_SUBTRACT:
                /* 更换上一个皮肤 */
                if (SkinID != 0)
                {
                    SkinID--;
                    SkinH_AttachFile(SkinID);
                }
                break;
            }
        }
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}
}


分享 秦时明月人机大战黑白棋_第1张图片



分享 秦时明月人机大战黑白棋_第2张图片



分享 秦时明月人机大战黑白棋_第3张图片

游戏的AI算法 本来打算采用 负极大值 搜索,但是自己理解不明白,真希望有人能帮我讲一下  

自己只好采用 一个类似 贪心的一个算法 , 太笨了

INT HashMap[CHESSXNUMB][CHESSYNUMB] = {
    {9999999, -9999999, 0, 0, 0, 0, -9999999, 9999999}, //1
    {-9999999, -9999999, 0, 0, 0, 0, -9999999, -9999999}, //2
    {0, 0, 0, 0, 0, 0, 0, 0}, //3
    {0, 0, 0, 0, 0, 0, 0, 0}, //4
    {0, 0, 0, 0, 0, 0, 0, 0}, //5
    {0, 0, 0, 0, 0, 0, 0, 0}, //6
    {-9999999, -9999999, 0, 0, 0, 0, -9999999, -9999999}, //7
    {9999999, -9999999, 0, 0, 0, 0, -9999999, 9999999} //8
};

POINT AI_GreedyAlgorithm(VOID)
{
    int maxScore = - (1<<31);
    int iScore = 0;
    int EatChessType, ChessType = g_WhoHasChess;
    if (BLACKCHESS == g_WhoHasChess)
    {
        EatChessType = WHITECHESS;
    } else {
        EatChessType = BLACKCHESS;
    }

    for (std::vector::size_type i = 0; i < CanDropChessPoint.size(); i++)
    {
        int Score = 0;
        Score += 50 * GetTransChessNum(CanDropChessPoint[i].x, CanDropChessPoint[i].y);
        g_ChessMap[CanDropChessPoint[i].x][CanDropChessPoint[i].y] = g_WhoHasChess;
        g_WhoHasChess = EatChessType;
        Score += GetCanDropChessNumber() * (-100);
        g_WhoHasChess = ChessType;
        g_ChessMap[CanDropChessPoint[i].x][CanDropChessPoint[i].y] = 0;

        Score += HashMap[CanDropChessPoint[i].x][CanDropChessPoint[i].y];

        if (maxScore < Score)
        {
            maxScore = Score;
            iScore = i;
        }
    }

    return CanDropChessPoint[iScore];
}

游戏的绘图 使用GDI 先在窗口上获取DC,然后创建内存兼容DC,把位图和棋子画到内存DC中,再把内存DC 通过Bitblt 函数画到 窗口DC上。

VOID DrawGame(VOID)
{
    WCHAR Buffer[256];
    memset(Buffer, 0, sizeof Buffer);
    wsprintf(Buffer, L"%d", g_BlackNum);
    SendMessage(GetDlgItem(g_GameHWND, IDC_Score2), WM_SETTEXT, wcslen(Buffer) + 1, (LPARAM)Buffer);
    memset(Buffer, 0, sizeof Buffer);
    wsprintf(Buffer, L"%d", g_WhiteNum);
    SendMessage(GetDlgItem(g_GameHWND, IDC_Score1), WM_SETTEXT, wcslen(Buffer) + 1, (LPARAM)Buffer);
	BitBlt(g_hdcBuf, 0, 0, CHESSBOARDWIDTH, CHESSBOARDHEIGH, g_hdcBoard, 0, 0, SRCCOPY);
    /* 绘制棋盘 */
	for (int x = 0; x < CHESSXNUMB; x++)
	{
		for (int y = 0; y < CHESSYNUMB; y++)
		{
			if (NULL == g_ChessMap[x][y])
			{
				continue;
			}

			if (g_ChessMap[x][y] % 2 == 0)
			{
				TransparentBlt(g_hdcBuf, 32 + x*CHESSWIDTH, 32 + y*CHESSHEIGH, CHESSWIDTH, CHESSHEIGH, g_BlackChess, 0, 0, CHESSWIDTH, CHESSHEIGH, RGB(191, 217, 230));
			} else {
				TransparentBlt(g_hdcBuf, 32 + x*CHESSWIDTH, 32 + y*CHESSHEIGH, CHESSWIDTH, CHESSHEIGH, g_WhiteChess, 0, 0, CHESSWIDTH, CHESSHEIGH, RGB(191, 217, 230));
			}
		}
	}
    /* 选择轮到谁下载的资源图片,绘制可下子的点 */
    HDC *pHDC = nullptr;
    if (BLACKCHESS == g_WhoHasChess)
    {
        pHDC = &g_BlackChessCanDropPoint;
    } else {
        pHDC = &g_WhiteChessCanDropPoint;
    }
    
    for (std::vector::size_type i = 0; i < CanDropChessPoint.size(); i++)
    {
        /* 显示可以下棋的地方 */
        TransparentBlt(g_hdcBuf, 32 + CanDropChessPoint[i].x*CHESSWIDTH, 32 + CanDropChessPoint[i].y*CHESSHEIGH, CHESSWIDTH, CHESSHEIGH, *pHDC, 0, 0, CHESSWIDTH, CHESSHEIGH, RGB(191, 217, 230));
    }
    if (!ChessQueue.empty())
    {
        Chess tChess = ChessQueue.back();
        if (BLACKCHESS == tChess.ChessColor)
        {
            TransparentBlt(g_hdcBuf, 32 + tChess.ChessPoint.x*CHESSWIDTH, 32 + tChess.ChessPoint.y*CHESSHEIGH, CHESSWIDTH, CHESSHEIGH, g_BlackNewDropChess, 0, 0, CHESSWIDTH, CHESSHEIGH, RGB(191, 217, 230));
        } else {
            TransparentBlt(g_hdcBuf, 32 + tChess.ChessPoint.x*CHESSWIDTH, 32 + tChess.ChessPoint.y*CHESSHEIGH, CHESSWIDTH, CHESSHEIGH, g_WhiteNewDropChess, 0, 0, CHESSWIDTH, CHESSHEIGH, RGB(191, 217, 230));
        }
    }
    
	BitBlt(g_hdc, 0, 0, CHESSBOARDWIDTH, CHESSBOARDHEIGH, g_hdcBuf, 0, 0, SRCCOPY);
}


项目下载地址:http://download.csdn.net/detail/li767517488/9551625

你可能感兴趣的:(Windows)