码迷,mamicode.com
首页 > 其他好文 > 详细

amz

时间:2019-12-19 19:08:12      阅读:90      评论:0      收藏:0      [点我收藏+]

标签:注释   reference   title   全局   patch   frame   def   back   struct   

// Gm.cpp : 定义应用程序的入口点。
//
#include <string>
#include "framework.h"
#include "Gm.h"
#include "ai.h"

int openAI=2;
vector<Move> playmoves;

int gameover(int p) {
    int r=0;
    for (int i=11;i<89;++i) {
        if (broad[i]==p) {
            r+=(broad[i-11]*broad[i-10]*broad[i-9]*broad[i-1]*broad[i+1]*broad[i+11]*broad[i+10]*broad[i+9]!=0);
        }
    }
    return r>=4;
}

int GG() {
    if(playmoves.size()%2) MessageBox(NULL,L"Black win!",L"Black win!",0);
    else MessageBox(NULL,L"White win!",L"White win!",0);
    memset(broad,0,sizeof(broad));
    init();
    playmoves.clear();
    return 0;
}


const int windowSize=924,gridSize=88,chessSize=34;

int player_from=0,player_to=0,player_ar=0;

int chess_color[4]={0,RGB(0,0,0),RGB(255,255,255),RGB(32,32,205)};

void draw(HDC hdc){
    HPEN hpen1 = CreatePen(PS_SOLID,1,RGB(139,90,43));
    HBRUSH hbrush1 = CreateSolidBrush(RGB(139,90,43));
    SelectObject(hdc,hpen1);
    SelectObject(hdc,hbrush1);
    Rectangle(hdc,0,0,windowSize,windowSize);
    hpen1 = CreatePen(PS_SOLID,1,RGB(239,90,43));
    hbrush1 = CreateSolidBrush(RGB(239,90,43));
    SelectObject(hdc,hpen1);
    SelectObject(hdc,hbrush1);
    if (player_from) {
        int x=player_from%10,y=player_from/10;
        Rectangle(hdc,x*gridSize-gridSize/2,y*gridSize-gridSize/2,(x+1)*gridSize-gridSize/2,(y+1)*gridSize-gridSize/2);
    }
    if (player_to) {
        int x=player_to%10,y=player_to/10;
        Rectangle(hdc,x*gridSize-gridSize/2,y*gridSize-gridSize/2,(x+1)*gridSize-gridSize/2,(y+1)*gridSize-gridSize/2);
    }
    hpen1 = CreatePen(PS_SOLID,1,RGB(0,0,0));
    hbrush1 = CreateSolidBrush(RGB(0,0,0));
    SelectObject(hdc,hpen1);
    SelectObject(hdc,hbrush1);
    for (int i=0;i<8;++i) {
        MoveToEx(hdc,gridSize*(i+1),gridSize,NULL);
        LineTo(hdc,gridSize*(i+1),gridSize*8);
        MoveToEx(hdc,gridSize,gridSize*(i+1),NULL);
        LineTo(hdc,gridSize*8,gridSize*(i+1));
    }
    for (int c=1;c<4;++c) {
        hpen1 = CreatePen(PS_SOLID,1,chess_color[c]);
        hbrush1 = CreateSolidBrush(chess_color[c]);
        SelectObject(hdc,hpen1);
        SelectObject(hdc,hbrush1);
        for (int i=1;i<9;++i) {
            for (int j=1;j<9;++j) {
                if (broad[j*10+i]==c) {
                    Ellipse(hdc,gridSize*(i)-chessSize,gridSize*(j)-chessSize,
                        gridSize*(i)+chessSize,gridSize*(j)+chessSize);
                }
            }
        }
    }
}

using namespace std;

#define MAX_LOADSTRING 100

// 全局变量:
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);

HWND hWnd;

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

    // TODO: 在此处放置代码。
    srand(GetTickCount64());
    // 初始化全局字符串
    LoadStringW(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
    LoadStringW(hInstance, IDC_GM, szWindowClass, MAX_LOADSTRING);
    MyRegisterClass(hInstance);

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

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

    MSG msg;
    init();
    // 主消息循环:
    while (GetMessage(&msg, nullptr, 0, 0))
    {
        int p=playmoves.size()%2+1;
        if (gameover(p)) {
            GG();
        }
        if (openAI&p) {
            playmoves.push_back(searchStep(p));
            doStep(playmoves.back());
            InvalidateRect(hWnd,NULL,TRUE);
            //Sleep(500);
        }
        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_GM));
    wcex.hCursor        = LoadCursor(nullptr, IDC_ARROW);
    wcex.hbrBackground  = (HBRUSH)(COLOR_WINDOW+1);
    wcex.lpszMenuName   = MAKEINTRESOURCEW(IDC_GM);
    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 = CreateWindowW(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
      CW_USEDEFAULT, 0,windowSize,windowSize, 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 reachable[100];

int legal_reach(int from,int to) {
    return 0;
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    static int from=0,to=0,ar=0;
    POINT pt;
    wstring s;
    switch (message)
    {
    case WM_PAINT:
        {
            GetCursorPos(&pt);
            PAINTSTRUCT ps;
            HDC hdc = BeginPaint(hWnd, &ps);
            draw(hdc);
            ReleaseDC(hWnd,hdc);
            EndPaint(hWnd, &ps);
            //InvalidateRect(hWnd,NULL,TRUE);
        }
        break;
    case WM_LBUTTONDOWN:
    {
        int p=playmoves.size()%2+1;
        InvalidateRect(hWnd,NULL,TRUE);
        GetCursorPos(&pt);
        ScreenToClient(hWnd,&pt);
        int x=(pt.y-gridSize/2)/gridSize+1,y=(pt.x-gridSize/2)/gridSize+1;
        int wrong=0;
        if (x>0&&x<9&&y>0&&y<9) {
            if (player_from==0) {
                player_from=x*10+y;
                memset(reachable,0,sizeof(reachable));
                for (int dr: drs) {
                    for (int j=player_from+dr;broad[j]==0;j+=dr) {
                        reachable[j]=1;
                    }
                }
                if (broad[player_from]!=p) wrong=1;
            } else if (player_to==0) {
                player_to=x*10+y;
                if (broad[player_to]||!reachable[player_to]) wrong=1;
                broad[player_from]=0;
                memset(reachable,0,sizeof(reachable));
                for (int dr: drs) {
                    for (int j=player_to+dr;broad[j]==0;j+=dr) {
                        reachable[j]=1;
                    }
                }
                broad[player_from]=p;
            } else {
                player_ar=x*10+y;
                if (!reachable[player_ar]) wrong=1;
            }
        }
        if (wrong) {
            player_from=player_to=player_ar=0;
        }
        if (player_ar) {
            doStep({player_from,player_to,player_ar});
            playmoves.push_back({player_from,player_to,player_ar});
            player_from=player_to=player_ar=0;
        }
        InvalidateRect(hWnd,NULL,TRUE);
        GetCursorPos(&pt);
        PAINTSTRUCT ps;
        HDC hdc = BeginPaint(hWnd,&ps);
        draw(hdc);
        ReleaseDC(hWnd,hdc);
        EndPaint(hWnd,&ps);
        break;
    }
    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    case WM_RBUTTONDOWN:
    {
        if (playmoves.size()) {
            undoStep(playmoves.back());
            playmoves.pop_back();
            InvalidateRect(hWnd,NULL,TRUE);
        }
        if (playmoves.size()) {
            undoStep(playmoves.back());
            playmoves.pop_back();
            InvalidateRect(hWnd,NULL,TRUE);
        }
    }
    default:
        return DefWindowProc(hWnd, message, wParam, lParam);
    }
    return 0;
}

\

#pragma once
#include <string>
#include <stack>
#include <iostream>
#include <vector>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <ctime>
using namespace std;

int broad[100];
int turn;
time_t max_time;

double firstadv =0,PVS_width=1.2;

double f5[23]={0.2300,0.2300,0.2159,0.2067,0.2000,0.1933,0.1841,0.1700,0.1496,0.1254,0.1010,0.0800,0.0652,0.0557,0.0500,0.0464,0.0436,0.0400,0.0346,0.0274,0.0190,0.0097,0},
f1[23]={0.1080,0.1080,0.1235,0.1332,0.1400,0.1468,0.1565,0.1720,0.1949,0.2217,0.2476,0.2680,0.2800,0.2884,0.3000,0.3208,0.3535,0.4000,0.4613,0.5350,0.6181,0.7075,0.8000},
f2[23]={0.3940,0.3940,0.3826,0.3753,0.3700,0.3647,0.3574,0.3460,0.3294,0.3098,0.2903,0.2740,0.2631,0.2559,0.2500,0.2430,0.2334,0.2200,0.2020,0.1800,0.1550,0.1280,0.1000},
f3[23]={0.1160,0.1160,0.1224,0.1267,0.1300,0.1333,0.1376,0.1440,0.1531,0.1640,0.1754,0.1860,0.1944,0.1995,0.2000,0.1950,0.1849,0.1700,0.1510,0.1287,0.1038,0.0773,0.0500},
f4[23]={0.1160,0.1160,0.1224,0.1267,0.1300,0.1333,0.1376,0.1440,0.1531,0.1640,0.1754,0.1860,0.1944,0.1995,0.2000,0.1950,0.1849,0.1700,0.1510,0.1287,0.1038,0.0773,0.0500};

int OP(int p) {
    return 3-p;
}

void init() {
    for (int i=0; i<10; ++i) {
        broad[i]=broad[90+i]=7;
        broad[10*i]=broad[10*i+9]=7;
    }
    broad[13]=broad[31]=broad[16]=broad[38]=1;
    broad[61]=broad[83]=broad[68]=broad[86]=2;
}

int drs[8]={1,-9,-10,-11,-1,9,10,11};

int bq[100],wq[100],bk[100],wk[100];

int qsz,qi,pubq[64],blackc[4],whitec[4];

struct Move {
    int st,to,ar;
};

void findAmz() {
    int bi=0,wi=0;
    for (int i=11;i<89;++i) {
        if (broad[i]==1) blackc[bi++]=i;
        else if (broad[i]==2) whitec[wi++]=i;
    }
}

void kmove() {
    memset(bk,127,sizeof(broad));
    memset(wk,127,sizeof(broad));
    qi=0;qsz=4;
    int nz=0,step=0;
    memcpy(pubq,blackc,4*sizeof(int));
    while (nz<qsz) {
        ++step;
        for (nz=qsz;qi<nz;++qi) {
            int now=pubq[qi];
            for (int &dr : drs) {
                if (broad[now+dr]==0&&bk[now+dr]>step) {
                    bk[now+dr]=step;
                    pubq[qsz++]=now+dr;
                }
            }
        }
    }
    qsz=4;qi=nz=step=0;
    memcpy(pubq,whitec,4*sizeof(int));
    while (nz<qsz) {
        ++step;
        for (nz=qsz;qi<nz;++qi) {
            int now=pubq[qi];
            for (int &dr : drs) {
                if (broad[now+dr]==0&&wk[now+dr]>step) {
                    wk[now+dr]=step;
                    pubq[qsz++]=now+dr;
                }
            }
        }
    }
}

void qmove() {
    memset(bq,127,sizeof(broad));
    memset(wq,127,sizeof(broad));
    qi=0;qsz=4;
    int nz=0,step=0;
    memcpy(pubq,blackc,4*sizeof(int));
    while (nz<qsz) {
        ++step;
        for (nz=qsz; qi<nz; ++qi) {
            int now=pubq[qi];
            for (int dr: drs) {
                for (int j=now+dr;broad[j]==0&&bq[j]>=step;j+=dr) {
                    if (bq[j]>step) {
                        bq[j]=step;
                        pubq[qsz++]=j;
                    }
                }
            }
        }
    }
    qsz=4;qi=nz=step=0;
    memcpy(pubq,whitec,4*sizeof(int));
    while (nz<qsz) {
        ++step;
        for (nz=qsz; qi<nz; ++qi) {
            int now=pubq[qi];
            for (int dr: drs) {
                for (int j=now+dr;broad[j]==0&&wq[j]>=step;j+=dr) {
                    if (wq[j]>step) {
                        wq[j]=step;
                        pubq[qsz++]=j;
                    }
                }
            }
        }
    }
}

int emp[100];

inline int Pow2(int n) {
    if (n>=31) {
        return 2147483647;
    }
    return 1<<n;
}

double eval(int p,int cturn) {
    findAmz();
    kmove();
    qmove();
    double c1=0,c2=0,t1=0,t2=0,bm=0,wm=0;
    memset(emp,0,sizeof(emp));
    for (int i=11;i<89;++i) {
        if (broad[i]||(bq[i]>666&&wq[i]==bq[i])) continue;
        for (int dr : drs) {
            ++emp[i+dr];
        }
        if (wq[i]==bq[i]) t1-=firstadv;
        else if (wq[i]<bq[i]) t1+=1;
        else t1-=1;
        if (bk[i]==wk[i]) t2-=firstadv;
        else if (wk[i]<bk[i]) t2+=1;
        else t2-=1;
        c1+=2.0/Pow2(wq[i])-2.0/Pow2(bq[i]);
        c2 += min(1.0,max(-1.0,(1.0 / 6.0) * (bk[i] - wk[i])));
    }
    for (int i=0;i<4;++i) {
        int now= blackc[i],now2=whitec[i];
        for (int dr : drs) {
            for (int step=1,nx=now+dr; broad[nx]==0; ++step,nx+=dr) {
                bm+= double(emp[nx])/step;
            }
            for (int step=1,nx=now2+dr; broad[nx]==0; ++step,nx+=dr) {
                wm+= double(emp[nx])/step;
            }
        }
    }
    double val;
    cturn+=turn;
    if (cturn<22) {
        val=f1[cturn]*t1+f2[cturn]*t2+f3[cturn]*c1+f4[cturn]*c2+f5[cturn]*(wm-bm);
    } else {
        val = f1[22]*t1+f2[22]*t2+f3[22]*c1+f4[22]*c2+f5[22]*(wm-bm);
    }
    if (p==1) val=-val;
    return val;
}

void doStep(Move mv) {
    broad[mv.to]=broad[mv.st];
    broad[mv.st]=0;
    broad[mv.ar]=3;
}
void undoStep(Move mv) {
    broad[mv.ar]=0;
    broad[mv.st]=broad[mv.to];
    broad[mv.to]=0;
}

double pvs(int p,double alpha,double beta,int depth,int d) {
    if (depth==0 || clock()>=max_time) {
        return eval(p,(d-depth)/2);
    }
    Move moves[1500];
    memset(moves,0,sizeof(moves));
    int pos=0;
    double value = 0;
    for (int i=11;i<89;++i) {
        if (broad[i]!=p) continue;
        broad[i]=0;
        for (auto dr:drs) {
            for (int to=i+dr;broad[to]==0;to+=dr) {
                for (auto d2 : drs) {
                    for (int ar=to+d2;broad[ar]==0;ar+=d2) {
                        moves[pos]={i,to,ar};
                        ++pos;
                    }
                }
            }
        }
        broad[i]=p;
    }
    if (pos==0) {
        return eval(p,(d-depth)/2);
    }
    double best;
    doStep(moves[0]);
    best = -pvs(3-p,-beta,-alpha,depth-1,d);
    undoStep(moves[0]);
    if (best>alpha) alpha = best;

    for (int i=1;i<pos;i++) {
        doStep(moves[i]);
        value = -pvs(3-p,-best-PVS_width,-best,depth-1,d);
        if (value>=best+1 && value<beta) best = -pvs(3-p,-beta,-value,depth-1,d);//以value作为下界再次搜索

        else if (value>best) best = value;
        undoStep(moves[i]);
        //更新alpha以及剪枝
        if (best>alpha) alpha = best;
        if (alpha>=beta) break;//下界大于等于上界 剪枝 
    }
    return best;
}

struct valmove {
    Move mv;
    double val;
};

valmove moves[1232];
Move searchStep(int p) {
    max_time=clock()+CLOCKS_PER_SEC;
    int pos=0;
    for (int i=11;i<89;++i) {
        if (broad[i]!=p) continue;
        broad[i]=0;
        for (auto dr:drs) {
            for (int to=i+dr;broad[to]==0;to+=dr) {
                broad[to]=p;
                for (auto d2 : drs) {
                    for (int ar=to+d2;broad[ar]==0;ar+=d2) {
                        moves[pos].mv={i,to,ar};
                        broad[ar]=3;
                        moves[pos].val=eval(p,0);
                        broad[ar]=0;
                        ++pos;
                    }
                }
                broad[to]=0;
            }
        }
        broad[i]=p;
    }
    sort(moves,moves+pos,[](valmove &a,valmove &b) {return a.val>b.val;});
    for (int d=1,t;d<10;d++) {
        for (t=0;t<pos;t++) {
            if (clock()>=max_time) break;
            doStep(moves[t].mv);
            moves[t].val = -pvs(3-p,-1e6,1e6,d,d);
            undoStep(moves[t].mv);
        }
        sort(moves,moves+t,[](valmove &a,valmove &b) {return a.val>b.val;});
        if (clock()>=max_time)
            break;
    }
    //cout<<moves[0].val<<endl;
    return moves[0].mv;
}

 

amz

标签:注释   reference   title   全局   patch   frame   def   back   struct   

原文地址:https://www.cnblogs.com/TheName/p/12069357.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!