VC五子棋游戏

  VC++的五子棋游戏

运行下面代码

复制代码
// fiveChess.cpp : 定义应用程序的入口点。
//

#include "stdafx.h"
#include "fiveChess.h"
#define BLOCKCOUNTH  10
#define BLOCKCOUNTV  10
#define DIVW  600
#define DIVH  600
#define BLOCKW DIVW/BLOCKCOUNTH
#define BLOCKH DIVH/BLOCKCOUNTV
#define MAX_LOADSTRING 100
#define NULL_CHESS 0
#define WHITE_CHESS 1
#define BLACK_CHESS 2

// 全局变量: 
HINSTANCE hInst;                                // 当前实例
WCHAR szTitle[MAX_LOADSTRING];                  // 标题栏文本
WCHAR szWindowClass[MAX_LOADSTRING];            // 主窗口类名

// 此代码模块中包含的函数的前向声明: 
ATOM                MyRegisterClass(HINSTANCE hInstance);
BOOL                InitInstance(HINSTANCE, int);
LRESULT CALLBACK    WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK    About(HWND, UINT, WPARAM, LPARAM);

int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
                     _In_opt_ HINSTANCE hPrevInstance,
                     _In_ LPWSTR    lpCmdLine,
                     _In_ int       nCmdShow)
{
    UNREFERENCED_PARAMETER(hPrevInstance);
    UNREFERENCED_PARAMETER(lpCmdLine);

    // TODO: 在此放置代码。

    // 初始化全局字符串
    LoadStringW(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
    LoadStringW(hInstance, IDC_FIVECHESS, szWindowClass, MAX_LOADSTRING);
    MyRegisterClass(hInstance);

    // 执行应用程序初始化: 
    if (!InitInstance (hInstance, nCmdShow))
    {
        return FALSE;
    }

    HACCEL hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_FIVECHESS));

    MSG msg;

    // 主消息循环: 
    while (GetMessage(&msg, nullptr, 0, 0))
    {
        if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
    }

    return (int) msg.wParam;
}


//
//  函数: MyRegisterClass()
//
//  目的: 注册窗口类。
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
    WNDCLASSEXW wcex;

    wcex.cbSize = sizeof(WNDCLASSEX);

    wcex.style          = CS_HREDRAW | CS_VREDRAW;
    wcex.lpfnWndProc    = WndProc;
    wcex.cbClsExtra     = 0;
    wcex.cbWndExtra     = 0;
    wcex.hInstance      = hInstance;
    wcex.hIcon          = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_FIVECHESS));
    wcex.hCursor        = LoadCursor(nullptr, IDC_ARROW);
    wcex.hbrBackground  = (HBRUSH)(COLOR_WINDOW+1);
    wcex.lpszMenuName   = MAKEINTRESOURCEW(IDC_FIVECHESS);
    wcex.lpszClassName  = szWindowClass;
    wcex.hIconSm        = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));

    return RegisterClassExW(&wcex);
}

//
//   函数: InitInstance(HINSTANCE, int)
//
//   目的: 保存实例句柄并创建主窗口
//
//   注释: 
//
//        在此函数中,我们在全局变量中保存实例句柄并
//        创建和显示主程序窗口。
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
   hInst = hInstance; // 将实例句柄存储在全局变量中

   HWND hWnd = CreateWindowW(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
      CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, nullptr, nullptr, hInstance, nullptr);

   if (!hWnd)
   {
      return FALSE;
   }

   ShowWindow(hWnd, nCmdShow);
   UpdateWindow(hWnd);

   return TRUE;
}

//
//  函数: WndProc(HWND, UINT, WPARAM, LPARAM)
//
//  目的:    处理主窗口的消息。
//
//  WM_COMMAND  - 处理应用程序菜单
//  WM_PAINT    - 绘制主窗口
//  WM_DESTROY  - 发送退出消息并返回

//棋盘二维数组
int chessPanel[BLOCKCOUNTH][BLOCKCOUNTH];
struct Pos {
    int x;
    int y;
};
//点击下棋
    //左键白棋
    //在事件中写
    //右键黑棋

Pos getIdx(int x, int y) {
    int w = BLOCKW;
    return Pos{y/w , x/w };
    //此法不好
    /*for (int j = 0; j < BLOCKCOUNTV; j++) {
        for (int i = 0; i < BLOCKCOUNTH; i++) {
            if ((i*BLOCKW < x) && x < (i + 1)*BLOCKW ) {
                if ((j*BLOCKH < y) && (y < (j + 1)*BLOCKH)) {
                    return Pos{ i,j };
                }
            };
        }
    }*/
}
//往棋盘中压入棋子
VOID pushWhite( int x, int y, int _chessPanel[BLOCKCOUNTH][BLOCKCOUNTH]) {
    _chessPanel[x][y] = 1;
}
VOID pushBlack(int x, int y, int _chessPanel[BLOCKCOUNTH][BLOCKCOUNTH]) {
    _chessPanel[x][y] = 2;
}
int checkFive(int _chessPanel[BLOCKCOUNTH][BLOCKCOUNTH]) {
    return 0;
}
VOID Create(HWND hWnd) {
    RECT rect;
    int nWinX, nWinY, nClientX, nClientY, nScreenWidth, nScreenHeight, userWidth, userHeight;
    GetWindowRect(hWnd, &rect);
    //获取窗口大小
    nWinX = rect.right - rect.left;
    nWinY = rect.bottom - rect.top;
    //获取客户区大小
    GetClientRect(hWnd, &rect);
    nClientX = rect.right - rect.left;
    nClientY = rect.bottom - rect.top;
    //移动居中
    nScreenWidth = GetSystemMetrics(SM_CXSCREEN);
    nScreenHeight = GetSystemMetrics(SM_CYSCREEN);
    userWidth = DIVW;
    userHeight = DIVH;
    //设置对象在桌面中的位置
    MoveWindow(hWnd, (nScreenWidth - userWidth) / 2, (nScreenHeight - userHeight) / 2, userWidth, userHeight, TRUE);
}
//return 1胜利
//return 0失败
int checkLink(int x, int y, int step_x, int step_y, int _chessPanel[BLOCKCOUNTH][BLOCKCOUNTH], int chess) {
    int i, j, flag = 0;
    //如果有五个连在一起的就跳出去
    for (i = x, j = y; i<10&&j<10&&i>=0&&j>=0; i += step_x, j += step_y) {
        if (_chessPanel[j][i] == chess) {
            flag++;
        }
        else {
            break;
        }
    }
    return flag;
}
VOID checkSuccess(int x, int y, int _chessPanel[BLOCKCOUNTH][BLOCKCOUNTH],int chess, HWND hWnd) {
    //从某一个点出发, 判断周围是否有五个统一颜色的棋子是相连的
    //水平方向
    int state = 0;
    if (checkLink(x, y, 1, 0, _chessPanel,chess) + checkLink(x, y, -1, 0, _chessPanel, chess)-1 >= 5) {
        state = 1;
    }
    //垂直方向
    if (checkLink(x, y, 0, 1, _chessPanel, chess)+ checkLink(x, y, 0, -1, _chessPanel, chess)-1 >= 5) {
        state = 1;
    };
    //左斜方向
    if (checkLink(x, y, -1, -1, _chessPanel, chess) + checkLink(x, y, 1, 1, _chessPanel, chess) - 1 >= 5) {
        state = 1;
    };
    //右斜方向
    if (checkLink(x, y, -1, 1, _chessPanel, chess) + checkLink(x, y, 1, -1, _chessPanel, chess) - 1 >= 5) {
        state = 1;
    };
    if (state == 1) {
        MessageBox(hWnd, L"成功了", L"提示", MB_OK);
    }
}
VOID Paint(HDC hdc, HWND hWnd) {
    for (int i = 0; i < BLOCKCOUNTH; i++) {
        for (int j = 0; j < BLOCKCOUNTV; j++) {
            Rectangle(hdc,
                BLOCKW*i,
                BLOCKH*j,
                BLOCKW*(i + 1),
                BLOCKH*(j + 1));
        }
    }
    HBRUSH hBrush;
    for (int i = 0; i < BLOCKCOUNTV; i++) {
        for (int j = 0; j<BLOCKCOUNTH; j++) {
            if (chessPanel[i][j] == 1) {
                hBrush = (HBRUSH)GetStockObject(GRAY_BRUSH);
                SelectObject(hdc, hBrush);
                Ellipse(hdc, j*BLOCKW, i*BLOCKH, (1 + j)*BLOCKW, (1 + i)*BLOCKH);
            }
            else if (chessPanel[i][j] == 2) {
                hBrush = (HBRUSH)GetStockObject(BLACK_BRUSH);
                SelectObject(hdc, hBrush);
                Ellipse(hdc, j*BLOCKW, i*BLOCKH, (1 + j)*BLOCKW, (1 + i)*BLOCKH);;
            }
        }
    }
}
VOID initGame(int _chessPanel[BLOCKCOUNTH][BLOCKCOUNTH]) {
    memset(_chessPanel, 0, sizeof(_chessPanel));
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)
    {
    case WM_CREATE:
    {
        Create(hWnd); 
    }
    break;
    case WM_LBUTTONDOWN:
    {
        PAINTSTRUCT ps;
        int client_x, client_y;
        client_x = LOWORD(lParam);
        client_y = HIWORD(lParam);
        Pos pos = getIdx(client_x, client_y);
        pushWhite(pos.x, pos.y, chessPanel);
        checkSuccess( pos.y, pos.x, chessPanel, WHITE_CHESS, hWnd);
        InvalidateRect(hWnd, NULL, true);
    }
    break;

    case WM_RBUTTONDOWN:
    {
        PAINTSTRUCT ps;
        int client_x, client_y;
        client_x = LOWORD(lParam);
        client_y = HIWORD(lParam);
        Pos pos = getIdx(client_x, client_y);
        pushBlack(pos.x, pos.y, chessPanel);
        checkSuccess(pos.y, pos.x, chessPanel, BLACK_CHESS, hWnd);
        InvalidateRect(hWnd, NULL, true);
    }
    break;
    case WM_COMMAND:
        {
            int wmId = LOWORD(wParam);
            // 分析菜单选择: 
            switch (wmId)
            {
            case IDM_ABOUT:
                DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
                break;
            case IDM_EXIT:
                DestroyWindow(hWnd);
                break;
            default:
                return DefWindowProc(hWnd, message, wParam, lParam);
            }
        }
        break;
    case WM_PAINT:
        {
            PAINTSTRUCT ps;
            HDC hdc = BeginPaint(hWnd, &ps);
            // TODO: 在此处添加使用 hdc 的任何绘图代码...
            Paint(hdc, hWnd);
            EndPaint(hWnd, &ps);
        }
        break;
    case WM_DESTROY:
    {
        Create(hWnd);
    }
    break;
    default:
        return DefWindowProc(hWnd, message, wParam, lParam);
    }
    return 0;
}

// “关于”框的消息处理程序。
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
    UNREFERENCED_PARAMETER(lParam);
    switch (message)
    {
    case WM_INITDIALOG:
        return (INT_PTR)TRUE;

    case WM_COMMAND:
        if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
        {
            EndDialog(hDlg, LOWORD(wParam));
            return (INT_PTR)TRUE;
        }
        break;
    }
    return (INT_PTR)FALSE;
}
复制代码

 

本文作者:方方和圆圆

本文链接:https://www.cnblogs.com/diligenceday/p/6087651.html

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   方方和圆圆  阅读(276)  评论(0编辑  收藏  举报

再过一百年, 我会在哪里?

💬
评论
📌
收藏
💗
关注
👍
推荐
🚀
回顶
收起
点击右上角即可分享
微信分享提示