VC + + Source Analysis-Chinese chess source analysis

Source: Internet
Author: User
Tags drawtext

Download from

Http://www.newxing.com/Code/VC/game/1750.html


The operating interface is as follows;


Look at the class diagram;


Resources


Main dialog box;



Source Code Description:

I machine chess program using a variety of search algorithms. The following are the main class descriptions for this program:
Category 1.CEveluation: Valuation class, valuation of a given board.


2.CMoveGenerator class: Go to the method generator, to the given checkerboard situation to search out all possible way.


Category 3.CSearchEngine: Search engine base class.
4.CNegaMaxEngine class: Negative Maxima search engine.
Category 5.CAlphaBetaEngine: Search engine with Alpha-beta pruning technology.
Category 6.CFAlphaBetaEngine: Fail-softalpha-beta search engine.
Category 7.CHistoryHeuristic: Historical inspiration class.
8.calphabeta_hhengine class: Alpha-beta search engine with historical inspiration.
9.CAspirationSearch Category: Eager search engine.
10.CIDAlphabetaEngine class: Iterative deepening search engine.
11.cmtd_fengine class: MTD (f) search engine.
12.CTranspositionTable class: Displacement table.
13.calphabeta_ttengine class: Alpha-beta search engine with substitution table.
14.cpvs_engine class: Minimal window search engine.
15.cnegascout_tt_hh class: A negascout search engine that uses a permutation table and historical inspiration.
The program also has undo, restore function, can also record the way.


Then the source code can be referenced to write all kinds of search engines;


Below is a look at some of its code, the main dialog box class;








The most functional categories you can see are the message map functions of various menus, as follows;

Begin_message_map (Cchessdlg, CDialog)//{{afx_msg_map (CCHESSDLG) On_wm_syscommand () On_wm_paint () ON_WM_ Querydragicon () On_wm_lbuttondown () On_wm_lbuttonup () On_wm_mousemove () On_command (Idm_setchessboard, Onsetchessboard) On_command (Idm_set, OnSet) on_command (idm_about, Onabout) on_command (Idm_openfile, OnOpenfile) ON_ COMMAND (Idm_savefile, Onsavefile) on_command (Idm_scbover, Onscbover) on_command (Idm_rpawn, Onrpawn) ON_COMMAND (IDM_ Rcanon, Onrcanon) on_command (Idm_rcar, Onrcar) on_command (Idm_rhorse, Onrhorse) on_command (IDM_RELEPHANT, OnRelephant ) On_command (Idm_rbishop, Onrbishop) on_command (idm_rking, onrking) on_command (Idm_bpawn, OnBpawn) ON_COMMAND (IDM_ Bcanon, Onbcanon) on_command (Idm_bcar, Onbcar) on_command (Idm_bhorse, Onbhorse) on_command (IDM_BELEPHANT, OnBelephant ) On_command (Idm_bbishop, Onbbishop) on_command (idm_bking, onbking) on_command (Idm_delete, OnDelete) ON_WM_ Rbuttondown () on_wm_lbuttondblclk () On_wm_close () on_command (IDM_CLEARCB, ONCLEARCB) on_command (IDM_NEWGAME, Onnewgame) on_bn_clicked (Idc_btncoMputer, Onbtncomputer) on_bn_clicked (Idc_btnundo, Onbtnundo) on_bn_clicked (Idc_btnredo, OnBtnredo) ON_LBN_DBLCLK (IDC _listchessrecord, Ondblclklistchessrecord) on_bn_clicked (Idc_btn_stop, Onbtnstop) On_lbn_selchange (IDC_ Listchessrecord, Onselchangelistchessrecord) on_command (Idm_preview, Onpreview) on_command (IDM_PREVIEWOVER, Onpreviewover) On_command (Idm_help, onhelp) on_command (IDM_INVERSECB, ONINVERSECB)//}}afx_msg_mapend_message_map ()


Loading the chessboard into the dialog initialization function;

BITMAP Bitmap;m_boardbmp.loadbitmap (Idb_chessboard); M_boardbmp.getbitmap (&BITMAP); Take BitMap object m_nboardwidth=bitmap.bmwidth;  Checkerboard Width m_nboardheight=bitmap.bmheight;//Checkerboard Height


As above; a chessboard is a bitmap;

Create three important objects to think about in a dialog constructor:

M_pse=new cnegamaxengine;//Create a negative maximum value search engine m_pmg=new cmovegenerator;//Create a walk-through generator m_pevel=new ceveluation; Create valuation Core

When the left mouse button bounces, the user is pushed to the stack, that is to say, the user goes with the stack storage;

---------the user to move the stack---------m_cmbestmove.from.x=m_ptmovechess.x;m_cmbestmove.from.y=m_ptmovechess.y;m_ Cmbestmove.to.x=x;m_cmbestmove.to.y=y;m_cmbestmove.nchessid=m_movechess.nchessid;m_umundomove.cmchessmove=m_ Cmbestmove;m_umundomove.nchessid=m_bychessboard[y][x];m_stackundomove.push (m_umundomove);//------------------- -------------


......
typedef struct
{
Short Nchessid; It shows what the pawn is.
Chessmanpos from;//Start position
Chessmanpos to; Where to go?
int score; The score of taking the law
}chessmove;

......

Chessmove M_cmbestmove;

Chessmove is a structural body;

......

Stack<undomove> m_stackundomove;//Record the stack of the way, easy to undo

......

M_stackundomove is a stack;


Take the method generator;



According to the different sub-judgment of the landing point is in line with Chinese chess rules; for example, go in four directions:

void Cmovegenerator::gen_elephantmove (BYTE position[10][9],int i,int j,int nply) {int x,y;//inserted in the lower right of the valid walk X=j+2;y=i+2;if ( X<9 && y<10 && isvalidmove (position,j,i,x,y,m_nuserchesscolor)) Addmove (j,i,x,y,nply,position I [j]); /insert effective walk x=j+2;y=i-2;if at upper right (x<9 && y>=0 && isvalidmove (Position,j,i,x,y,m_nuserchesscolor)) Addmove (J,i,x,y,nply,position[i][j]);//Insert the effective x=j-2;y=i+2;if at the lower left (x>=0 && y<10 && isvalidmove ( Position,j,i,x,y,m_nuserchesscolor) Addmove (J,i,x,y,nply,position[i][j]);//Insert the upper left side of the effective walk x=j-2;y=i-2;if (x>=0 && y>=0 && isvalidmove (position,j,i,x,y,m_nuserchesscolor) addmove (J,i,x,y,nply,position[i][j] );}

Search engine base class; Other search engines inherit this class;

Download by http://www. newxing.com//Searchengine.h:interface for the Csearchengine class.//////////////////////////////////////////////// #if!defined (afx_searchengine_h__7a7237b9_0908_45d8_b102_94e342b174a5__included_) # Define afx_searchengine_h__7a7237b9_0908_45d8_b102_94e342b174a5__included_#if _msc_ver > 1000#pragma once#endif/ /_msc_ver > 1000#include "Eveluation.h" #include "MoveGenerator.h" #include "GradientProgressCtrl.h"//Search engine base class Csearchengine {public:csearchengine (); virtual ~csearchengine ();p ublic:virtual searchagoodmove (BYTE position[10][9]        ) = 0; Take the next step chessmove getbestmove () {return m_cmbestmove;};/ /Get the best walk Method Undomove Getundomove () {return m_umundomove;};/ /Get Undo Go Method void setsearchdepth (int ndepth) {m_nsearchdepth=ndepth;};/  /set maximum search depth void Seteveluator (ceveluation* peval) {m_peval=peval;}; Set valuation engine void Setmovegenerator (cmovegenerator* pMG) {M_PMG =pmg;};/ /Set Go generator void setthinkprogress (cgradientprogressctrl* pthinkprogress) {M_PTHINKPROGRESS=PTHINKProgress;};/ /Set progress bar to show thinking Progress Setuserchesscolor (int nuserchesscolor) {m_nuserchesscolor=nuserchesscolor;};/ /set user for Black or red void Undochessmove (byte position[10][9],chessmove* move,byte nchessid);//Undo void Redochessmove (Byte              position[10][9],chessmove* move);    Restore Protected:int isgameover (byte position[10][9],int ndepth);//Determine if the outcome is a byte makemove (chessmove* move); The Checkerboard void Unmakemove (chessmove* move,byte nchessid) After a walk was produced;    Revert to the chessboard before passing Public:int m_nuserchesscolor;protected:cgradientprogressctrl* m_pthinkprogress; A progress bar pointer that shows the progress of the thinking byte curposition[10][9];//the array chessmove used to record the current node's checkerboard state m_cmbestmove;//records the best way undomove m_umundomove; cmovegenerator* m_pmg;//ceveluation* m_peval;//Valuation core int m_nsearchdepth;//maximum search depth int m_nmaxdepth;//The maximum search depth of the current search}; #endif//!defined (AFX_SEARCHENGINE_H__7A7237B9_0908_45D8_B102_94E342B174A5__INCLUDED_)

Coolbutton class; Four buttons for drawing the lower left corner of the main interface; You can refer to writing your own Cool button class;

Download by http://www. newxing.com//CoolButton.cpp:implementation file//#include "stdafx.h" #include "CoolButton.h" #ifdef _debug#define New Debug_new#undef this_filestatic char this_file[] = __file__; #endif///////////////////////////////////////////////// Ccoolbuttonccoolbutton::ccoolbutton () {#ifdef xs_flat_buttonm_mouseonbutton=false; #endifm_hIcon =null;m_cyicon=0;m_cxicon=0;} Ccoolbutton::~ccoolbutton () {}begin_message_map (Ccoolbutton, CButton)//{{afx_msg_map (Ccoolbutton) ON_WM_MOUSEMOVE () On_wm_killfocus ()//}}afx_msg_mapend_message_map ()/////////////////////////////////////////////////////////// Ccoolbutton message handlersvoid Ccoolbutton::D rawitem (lpdrawitemstruct lpdis) {//Todo:add your Code to draw the specified ITEMCDC * pdc=cdc::fromhandle (LPDIS-&GT;HDC); unsigned int ispressed = (lpdis->itemstate&amp ; ods_selected); unsigned int IsFocused = (lpdis->itemstate&ods_focus); unsigned int isdisabled = (lpdis-> Itemstate& ods_disabled); CRect itemrect = lpdis->rcitem; #ifndef xs_flat_buttonif (IsFocused) {CBrush br (RGB (0,0,0));pD C->framerect (& ITEMRECT,&AMP;BR); Itemrect.deflaterect (n);} #endif//fill with Bkcolorcbrush br (GetSysColor (color_btnface));pD c->fillrect (&AMP;ITEMRECT,&AMP;BR);//Is Pressed?if (ispressed) {#ifdef xs_flat_button//shallow border pen CPen penbtnhilight (Ps_solid,0,getsyscolor (color_btnhilight));// Shadow Pen CPen Penbtnshadow (Ps_solid,0,getsyscolor (Color_btnshadow));//Draw Boundary Shadow Pdc->selectobject (Penbtnshadow);pD C- >moveto (itemrect.left,itemrect.bottom-1);pD C->lineto (itemrect.left,itemrect.top);pD C->lineto ( Itemrect.right,itemrect.top);//Draw Shallow border Pdc->selectobject (penbtnhilight);pD C->moveto (Itemrect.left, itemrect.bottom-1);pD C->lineto (itemrect.right-1,itemrect.bottom-1);pD C->lineto (ItemRect.right-1, ITEMRECT.TOP-1); #elseCBrush Brbtnshadow (GetSysColor (Color_btnshadow));pD C->framerect (&itemRect,& Brbtnshadow); #endif}else//did not press {CPen penbtnhilight (Ps_solid,0,getsyscolor (Color_btnhiligHT)); CPen pen3dlight (Ps_solid,0,getsyscolor (color_3dlight)); CPen Penbtnshadow (Ps_solid,0,getsyscolor (Color_btnshadow)); CPen Pen3ddkshadow (Ps_solid,0,getsyscolor (Color_3ddkshadow)); #ifdef xs_flat_buttonif (m_mouseonbutton==true) {pDC- >selectobject (penbtnhilight);pD C->moveto (itemrect.left,itemrect.bottom-1);pD C->lineto (ItemRect.left, Itemrect.top);pD C->lineto (itemrect.right,itemrect.top),//pdc->selectobject (Penbtnshadow);pD C->moveto ( itemrect.left,itemrect.bottom-1);pD C->lineto (itemrect.right-1,itemrect.bottom-1);pD C->lineto ( ITEMRECT.RIGHT-1,ITEMRECT.TOP-1);} #elsepDC->selectobject (penbtnhilight);pD C->moveto (itemrect.left,itemrect.bottom-1);pD C->lineto ( Itemrect.left,itemrect.top);pD C->lineto (itemrect.right,itemrect.top);pD c->selectobject (pen3DLight);pD C- >moveto (itemrect.left+1,itemrect.bottom-1);pD C->lineto (itemrect.left+1,itemrect.top+1);pD C->lineto ( itemrect.right,itemrect.top+1);pD c->selectobject (Pen3ddkshadow);pD C->moveto (Itemrect.left,itemrect.bottom-1);pD C->lineto (itemrect.right-1,itemrect.bottom-1);pD C->lineto (ItemRect.right-1, itemrect.top-1);pD c->selectobject (Penbtnshadow);pD C->moveto (itemrect.left+1,itemrect.bottom-2);pD c-> LineTo (itemrect.right-2,itemrect.bottom-2);pD C->lineto (itemrect.right-2,itemrect.top); #endif} #ifndef XS_FLAT _button//if (IsFocused) {CRect focusrect = Itemrect;focusrect.deflaterect (3,3);pD C->drawfocusrect (&focusrect );} #endif//Get text CString title; GetWindowText (title);//Draw icon if (m_hicon!=null) {CRect Iconrect = lpdis->rcitem;//sets a different icon position depending on whether the title exists if (title). IsEmpty () ==true) {iconrect.left+= ((Iconrect.width ()-m_cxicon)/2);} else{iconrect.left+=6;} iconrect.top+= ((Iconrect.height ()-m_cyicon)/2), if (ispressed) iconrect.offsetrect (;pD C->drawicon ( Iconrect.topleft (), m_hicon);} Paint caption CRect captionrect = lpdis->rcitem;captionrect.left+=m_cxicon;if (title. IsEmpty () ==false) {Pdc->setbkmode (TRANSPARENT); Captionrect.offsetrect (0,-1); if (ispressed) Captionrect.offsetrect (isdisable);d) {captionrect.offsetrect ();pD C->settextcolor (GetSysColor (color_btnhilight));pD C->drawtext (Title,-1, captionrect,dt_singleline| dt_center| Dt_vcenter) Captionrect.offsetrect ( -1,-1);pD C->settextcolor (GetSysColor (Color_btnshadow));pD C->drawtext ( title,-1,captionrect,dt_singleline| dt_center| Dt_vcenter);} Else{pdc->drawtext (title,-1,captionrect,dt_singleline| dt_center| Dt_vcenter);}}}  #ifdef xs_flat_buttonvoid ccoolbutton::onmousemove (UINT nflags, CPoint point) {//Todo:add your message handler code here and/or call defaultcwnd* pWnd; cwnd* pparent; Cbutton::onmousemove (nflags, point);p Wnd=getactivewindow ();pP arent=getowner (); if (M_mouseonbutton==false) & & (GetCapture ()!=this) && ((pwnd!=null) && (Pwnd->m_hwnd==pparent->m_hwnd))) {setcapture () ; SetFocus (); M_mouseonbutton=true;invalidate (); UpdateWindow ();} Else{crect RC; GetClientRect (&AMP;RC); if (!RC. PtInRect (point)) {m_mouseonbutton=false;invalidate (); UpdateWindow (); ReleaseCapture ();}}} #endif #ifdef Xs_flat_buttOnvoid Ccoolbutton::onkillfocus (cwnd* pnewwnd) {cbutton::onkillfocus (Pnewwnd);//Todo:add your message Handler code HEREif (m_mouseonbutton==true) {m_mouseonbutton=false;invalidate (); UpdateWindow ();}} #endifvoid Ccoolbutton::seticon (Hicon hicon, Byte CX, byte cy) {m_hicon = Hicon;m_cxicon = Cx;m_cyicon = cy;} BOOL Ccoolbutton::subclassdlgitem (UINT NID, CWnd *pparent) {bool Retvalue=cbutton::subclassdlgitem (nid,pparent); LONG Bs=::getwindowlong (M_hwnd,gwl_style); Bs|=bs_ownerdraw;::setwindowlong (M_hwnd,gwl_style,bs); return retValue;}

The rest of the free time to analyze it;


A search algorithm is a method that uses the high performance of a computer to have a purposeful, exhaustive problem of solving some or all of the possible situations of a space, thus finding the solution to the problem.
The search algorithm is actually the process of constructing a "solution tree" based on initial conditions and extension rules and finding nodes that match the target State.

The Minimax algorithm, also known as the Minimax algorithm, is an algorithm that finds the minimum value of the maximum probability of failure. The minimax algorithm is often used in chess and other games and programs by two parties, such a program by two players take turns, each time a step. Our well-known gobang, chess and so on belong to this kind of program, so say Minimax algorithm is based on search game algorithm. The algorithm is a zero-sum algorithm, in which one party chooses the option to maximize its advantages, while the other chooses to minimize the opponent's advantage.

VC + + Source Analysis-Chinese chess source analysis

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.