📄 main.cpp
字号:
/* * CChess Copyright (C) 2006 wesley.wang lily.ru * wq1977@gmail.com rujingli@163.com * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA * */#define DEBUGMODE#include "aihomesdk.h"#include "defs14.h"#include "cstdlib"#include "cstring"using namespace std;#define PX 80#define PY 80#define QIZI_WIDTH 50#define PAN_WIDTH 9#define PAN_HEIGHT 10char aicolor[BOARD_SIZE]= { 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 0, 7, 7, 7, 7, 7, 0, 7, 0, 7, 0, 7, 0, 7, 0, 7, 0, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 1, 7, 1, 7, 1, 7, 1, 7, 1, 7, 1, 7, 7, 7, 7, 7, 1, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 1, 1, 1, 1, 1, 1, 1, 1, 1};int aipiece[BOARD_SIZE]= { 5, 3, 2, 1, 6, 1, 2, 3, 5, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 4, 7, 7, 7, 7, 7, 4, 7, 0, 7, 0, 7, 0, 7, 0, 7, 0, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 0, 7, 0, 7, 0, 7, 0, 7, 0, 7, 4, 7, 7, 7, 7, 7, 4, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 5, 3, 2, 1, 6, 1, 2, 3, 5};char aimaterialnumber[2][7] = {{5, 2, 2, 2, 2, 2, 1}, {5, 2, 2, 2, 2, 2, 1}};DATAFILE* data=NULL;char aipointtable[7][2][90] = {{{0, 0, 0, 0, 0, 0, 0, 0, 0, /* PAWN*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 0, 12, 0, 15, 0, 12, 0, 10, 10, 0, 13, 0, 10, 0, 13, 0, 10, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 21, 21, 22, 22, 22, 21, 21, 20, 20, 21, 21, 23, 23, 23, 21, 21, 20, 20, 21, 21, 23, 22, 23, 21, 21, 20, 11, 12, 13, 14, 14, 14, 13, 12, 11}, {11, 12, 13, 14, 14, 14, 13, 12, 11, /* PAWN*/ 20, 21, 21, 23, 22, 23, 21, 21, 20, 20, 21, 21, 23, 23, 23, 21, 21, 20, 20, 21, 21, 22, 22, 22, 21, 21, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 10, 0, 13, 0, 10, 0, 13, 0, 10, 10, 0, 12, 0, 15, 0, 12, 0, 10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}}, {{0, 0, 0, 20, 0, 20, 0, 0, 0, /* BISHOP */ 0, 0, 0, 0, 22, 0, 0, 0, 0, 0, 0, 0, 19, 0, 19, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, /* BISHOP */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 19, 0, 19, 0, 0, 0, 0, 0, 0, 0, 22, 0, 0, 0, 0, 0, 0, 0, 20, 0, 20, 0, 0, 0}}, {{0, 0, 25, 0, 0, 0, 25, 0, 0, /* ELEPHAN */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 23, 0, 0, 0, 28, 0, 0, 0, 23, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 22, 0, 0, 0, 22, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, /* ELEPHAN */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 22, 0, 0, 0, 22, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 23, 0, 0, 0, 28, 0, 0, 0, 23, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 25, 0, 0, 0, 25, 0, 0}}, {{40, 35, 40, 40, 40, 40, 40, 35, 40, /* KNIGHT */ 40, 41, 42, 40, 20, 40, 42, 41, 40, 40, 42, 43, 40, 40, 40, 43, 42, 40, 40, 42, 43, 43, 43, 43, 43, 42, 40, 40, 43, 44, 44, 44, 44, 44, 43, 40, 40, 43, 44, 44, 44, 44, 44, 43, 40, 40, 43, 44, 44, 44, 44, 44, 43, 40, 40, 43, 44, 44, 44, 44, 44, 43, 40, 40, 41, 42, 42, 42, 42, 42, 41, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40}, {40, 40, 40, 40, 40, 40, 40, 40, 40, /* KNIGHT */ 40, 41, 42, 42, 42, 42, 42, 41, 40, 40, 43, 44, 44, 44, 44, 44, 43, 40, 40, 43, 44, 44, 44, 44, 44, 43, 40, 40, 43, 44, 44, 44, 44, 44, 43, 40, 40, 43, 44, 44, 44, 44, 44, 43, 40, 40, 42, 43, 43, 43, 43, 43, 42, 40, 40, 42, 43, 40, 40, 40, 43, 42, 40, 40, 41, 42, 40, 20, 40, 42, 41, 40, 40, 35, 40, 40, 40, 40, 40, 35, 40}}, {{50, 50, 50, 50, 50, 50, 50, 50, 50, /* CANNON */ 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 51, 53, 53, 55, 53, 53, 51, 50, 50, 51, 50, 50, 50, 50, 50, 51, 50, 50, 51, 51, 51, 51, 51, 51, 51, 50, 50, 51, 50, 50, 50, 50, 50, 51, 50, 50, 51, 50, 50, 50, 50, 50, 51, 50, 50, 51, 50, 50, 50, 50, 50, 51, 50, 50, 51, 50, 50, 50, 50, 50, 51, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50}, {50, 50, 50, 50, 50, 50, 50, 50, 50, /* CANNON */ 50, 51, 50, 50, 50, 50, 50, 51, 50, 50, 51, 50, 50, 50, 50, 50, 51, 50, 50, 51, 50, 50, 50, 50, 50, 51, 50, 50, 51, 50, 50, 50, 50, 50, 51, 50, 50, 51, 51, 51, 51, 51, 51, 51, 50, 50, 51, 50, 50, 50, 50, 50, 51, 50, 50, 51, 53, 53, 55, 53, 53, 51, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50}}, {{89, 92, 90, 90, 90, 90, 90, 92, 89, /* ROOK */ 91, 92, 90, 93, 90, 93, 90, 92, 91, 90, 92, 90, 91, 90, 91, 90, 92, 90, 90, 91, 90, 91, 90, 91, 90, 91, 90, 90, 94, 90, 94, 90, 94, 90, 94, 90, 90, 93, 90, 91, 90, 91, 90, 93, 90, 90, 91, 90, 91, 90, 91, 90, 91, 90, 90, 91, 90, 90, 90, 90, 90, 91, 90, 90, 92, 91, 91, 90, 91, 91, 92, 90, 90, 90, 90, 90, 90, 90, 90, 90, 90}, {90, 90, 90, 90, 90, 90, 90, 90, 90, /* ROOK */ 90, 92, 91, 91, 90, 91, 91, 92, 90, 90, 91, 90, 90, 90, 90, 90, 91, 90, 90, 91, 90, 91, 90, 91, 90, 91, 90, 90, 93, 90, 91, 90, 91, 90, 93, 90, 90, 94, 90, 94, 90, 94, 90, 94, 90, 90, 91, 90, 91, 90, 91, 90, 91, 90, 90, 92, 90, 91, 90, 91, 90, 92, 90, 91, 92, 90, 93, 90, 93, 90, 92, 91, 89, 92, 90, 90, 90, 90, 90, 92, 89}}, {{0, 0, 0, 30, 35, 30, 0, 0, 0, /* KING */ 0, 0, 0, 15, 15, 15, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, /* KING */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 15, 15, 15, 0, 0, 0, 0, 0, 0, 30, 35, 30, 0, 0, 0}}};short aioffset[7][8] = {{-1, 1,13, 0, 0, 0, 0, 0}, /* PAWN {for REDSIDE side} */ {-12,-14,12,14,0,0,0,0}, /* BISHOP */ {-28,-24,24,28, 0, 0, 0, 0 }, /* ELEPHAN */ {-11,-15,-25,-27,11,15,25,27}, /* KNIGHT */ {-1, 1,-13,13, 0, 0, 0, 0}, /* CANNON */ {-1, 1,-13,13, 0, 0, 0, 0}, /* ROOK */ {-1, 1,-13,13, 0, 0, 0, 0}}; /* KING */short aimailbox182[182] = {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, -1,-1, 0, 1, 2, 3, 4, 5, 6, 7, 8,-1,-1, -1,-1, 9,10,11,12,13,14,15,16,17,-1,-1, -1,-1,18,19,20,21,22,23,24,25,26,-1,-1, -1,-1,27,28,29,30,31,32,33,34,35,-1,-1, -1,-1,36,37,38,39,40,41,42,43,44,-1,-1, -1,-1,45,46,47,48,49,50,51,52,53,-1,-1, -1,-1,54,55,56,57,58,59,60,61,62,-1,-1, -1,-1,63,64,65,66,67,68,69,70,71,-1,-1, -1,-1,72,73,74,75,76,77,78,79,80,-1,-1, -1,-1,81,82,83,84,85,86,87,88,89,-1,-1, -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1};short aimailbox90[90] = {28, 29, 30, 31, 32, 33, 34, 35, 36, 41, 42, 43, 44, 45, 46, 47, 48, 49, 54, 55, 56, 57, 58, 59, 60, 61, 62, 67, 68, 69, 70, 71, 72, 73, 74, 75, 80, 81, 82, 83, 84, 85, 86, 87, 88, 93, 94, 95, 96, 97, 98, 99,100,101, 106, 107,108,109,110,111,112,113,114, 119, 120,121,122,123,124,125,126,127, 132, 133,134,135,136,137,138,139,140, 145, 146,147,148,149,150,151,152,153};short ailegalposition[90] = {1, 1, 5, 3, 3, 3, 5, 1, 1, 1, 1, 1, 3, 3, 3, 1, 1, 1, 5, 1, 1, 3, 7, 3, 1, 1, 5, 1, 1, 1, 1, 1, 1, 1, 1, 1, 9, 1,13, 1, 9, 1,13, 1, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9};short aimaskpiece[7] = {8, 2, 4, 1, 1, 1, 2}, aiknightcheck[8] = {1,-1,-9,-9,-1,1,9,9}, aielephancheck[8] = {-10,-8,8,10,0,0,0,0}, aikingpalace[2][9] = {{3,4,5,12,13,14,21,22,23},{66,67,68,75,76,77,84,85,86}};class CChessAI{public: int side, xside, computerside; char color[BOARD_SIZE]; int piece[BOARD_SIZE]; move newmove; double tickstart, tickend; unsigned long nodecount, brandtotal, gencount; char mply, follow_pv; char materialnumber[2][7]; move pv[HIST_STACK]; int history[BOARD_SIZE][BOARD_SIZE]; int ply; char pointtable[7][2][90]; short gen_begin[HIST_STACK], gen_end[HIST_STACK]; short offset[7][8]; short mailbox182[182]; short mailbox90[90]; short legalposition[90]; short maskpiece[7],knightcheck[8],elephancheck[8],kingpalace[2][9]; hist_rec hist_dat[HIST_STACK]; short hdp; gen_rec gen_dat[MOVE_STACK]; long capbrandtotal, capgencount; struct node *book, *t1, *t2; short ss[4][52], /* save 4 symmetrical kinds of line */ nm; /* working pointer of line */ bool ReadingBook; CChessAI(); short UpdateNewMove(void); void ComputerThink(void); short AlphaBeta(short alpha, short beta, short depth); short Quiescence(short alpha, short beta); short Eval(void); short GenCapture(void); void Gen_push(short from, short dest); short MoveSave (int from, int dest); short MakeMove(move m); short IsInCheck (int side); short Attack (int pos, int side); int CheckMoveLoop(void); void UnMakeMove(void); short Gen(void); void Check_pv (void); void Sort(void); void Quicksort(short q, short r); int InitGame(bool AiRed); void InitGen(void); short Readbook(void); void DelBook(void); short GetBook(void); short SymmetricConvert(short m, short k); void NextStep(void);};class CChess : public aihomegame{public: virtual bool ProcInput(int keycode); virtual void Draw(); virtual bool Init(); virtual void Quit(); virtual void Update();private: CChessAI *ai; int State; //0:WaitGameStart 1:Gaming 2: Wait Game Restart bool AiTurn; bool AiRed; bool Selected; bool Moving; int FromX,FromY,ToX,ToY,MoveQizi; int HumenMoveTag,from; int Win,Lose,Friend; DATAFILE* data; BITMAP *qizi,*bg; SAMPLE *jiang; void DrawBoard(); void DrawCell(short pos, short mode); void DrawHumenMoveTag(); void DrawFromSelectTag();};short CChessAI::SymmetricConvert(short m, short k){ switch (k) { case 0: return m; case 1: return m+SIZE_X-1-2*(m%SIZE_X); case 2: return m + (SIZE_Y-1 - 2*(m/SIZE_X))*SIZE_X; case 3: return BOARD_SIZE-1-m; } return m;}void CChessAI::NextStep(void){ if (ReadingBook){ if (!GetBook()) { ReadingBook=false; DelBook(); ComputerThink(); } else{ for (int i=0; i<4; i++) { ss[i][nm] = SymmetricConvert(newmove.from, i); ss[i][nm+1] = SymmetricConvert(newmove.dest, i); } nm += 2; } } else{ ComputerThink(); }}short CChessAI::GetBook(void){ short k=0, sk=0, g = 0; /* sk: symmetrical kind */ struct node *t1; t1 = book; if (!book) return 0; /* Computer go first */ if (!nm) { if (computerside==REDSIDE) sk = 2; else sk = 0; k = 0; g = 1; } else while (t1 != NULL) { if (nm < t1->len) { for(sk=0; sk<4; sk++) { for (k=0,g=1; k<nm; k++) if (ss[sk][k]!=t1->line[k]) { g=0; break;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -