赞
踩
- /*象棋ai*/
- #pragma GCC optimize(1)
- #pragma GCC optimize(2)
- #pragma GCC optimize(3)
- #pragma GCC optimize("Ofast")
- #pragma GCC optimize("inline")
- #pragma GCC optimize("-fgcse")
- #pragma GCC optimize("-fgcse-lm")
- #pragma GCC optimize("-fipa-sra")
- #pragma GCC optimize("-ftree-pre")
- #pragma GCC optimize("-ftree-vrp")
- #pragma GCC optimize("-fpeephole2")
- #pragma GCC optimize("-ffast-math")
- #pragma GCC optimize("-fsched-spec")
- #pragma GCC optimize("unroll-loops")
- #pragma GCC optimize("-falign-jumps")
- #pragma GCC optimize("-falign-loops")
- #pragma GCC optimize("-falign-labels")
- #pragma GCC optimize("-fdevirtualize")
- #pragma GCC optimize("-fcaller-saves")
- #pragma GCC optimize("-fcrossjumping")
- #pragma GCC optimize("-fthread-jumps")
- #pragma GCC optimize("-funroll-loops")
- #pragma GCC optimize("-freorder-blocks")
- #pragma GCC optimize("-fschedule-insns")
- #pragma GCC optimize("inline-functions")
- #pragma GCC optimize("-ftree-tail-merge")
- #pragma GCC optimize("-fschedule-insns2")
- #pragma GCC optimize("-fstrict-aliasing")
- #pragma GCC optimize("-falign-functions")
- #pragma GCC optimize("-fcse-follow-jumps")
- #pragma GCC optimize("-fsched-interblock")
- #pragma GCC optimize("-fpartial-inlining")
- #pragma GCC optimize("no-stack-protector")
- #pragma GCC optimize("-freorder-functions")
- #pragma GCC optimize("-findirect-inlining")
- #pragma GCC optimize("-fhoist-adjacent-loads")
- #pragma GCC optimize("-frerun-cse-after-loop")
- #pragma GCC optimize("inline-small-functions")
- #pragma GCC optimize("-finline-small-functions")
- #pragma GCC optimize("-ftree-switch-conversion")
- #pragma GCC optimize("-foptimize-sibling-calls")
- #pragma GCC optimize("-fexpensive-optimizations")
- #pragma GCC optimize("inline-functions-called-once")
- #pragma GCC optimize("-fdelete-null-pointer-checks")
- #include<bits/stdc++.h>
- #include<random>
- #include<ctime>
- #include<windows.h>
- #include<omp.h>
- #define pll pair<int,int>
- using namespace std;
- typedef double lf;
- const int N=3e6;
- static default_random_engine e;
- struct Chess {
- int des[10][9];
- };
- int maxnumcore;
- int maxstep=6;
- int maxcal=5000;
- int thinktime=100;
- int nodelimt=6;
- int siz=0;
- vector<int> datree[N];
- Chess chess[N];
- int fa[N],dep[N],mark[N],endpo[N],roval[N],del[N];
- lf n[N],w[N],winrate[N];
-
- int qzw[32] = { -100,-3,-3,-3,-3,-7,-7,-15,-15,-10,-10,-1,-1,-1,-1,-1,100,3,3,3,3,7,7,15,15,10,10,1,1,1,1,1 };
- pair<int, int> limx[16] = { {0,2},{0,2},{0,2},{0,4},{0,4},{0,9},{0,9},{0,9},{0,9},{0,9},{0,9},{0,9},{0,9},{0,9},{0,9},{0,9}};
- pair<int, int> limy[16] = { {3,5},{3,5},{3,5},{0,8},{0,8},{0,8},{0,8},{0,8},{0,8},{0,8},{0,8},{0,8},{0,8},{0,8},{0,8},{0,8}};
- Chess prec;
- class cheess{
- public:
- lf c = 2;
- pair<int, int> POS[32];
- Chess base;
- inline int calval(Chess&u){
- int cn=0;
- for(int i=0;i<10;i++)for(int j=0;j<9;j++)
- if(u.des[i][j]>=0)cn+=qzw[u.des[i][j]];
- return cn;
- }
- void init() {
- POS[0] = { 0,4 }; POS[1] = { 0,3 }; POS[2] = { 0,5 }; POS[3] = { 0,2 }; POS[4] = { 0,6 };
- POS[5] = { 0,1 }; POS[6] = { 0,7 }; POS[7] = { 0,0 }; POS[8] = { 0,8 }; POS[9] = { 2,1 };
- POS[10] = { 2,7 }; POS[11] = { 3,0 }; POS[12] = { 3,2 }; POS[13] = { 3,4 }; POS[14] = { 3,6 };
- POS[15] = { 3,8 }; POS[16] = { 9,4 }; POS[17] = { 9,5 }; POS[18] = { 9,3 }; POS[19] = { 9,6 };
- POS[20] = { 9,2 }; POS[21] = { 9,7 }; POS[22] = { 9,1 }; POS[23] = { 9,8 }; POS[24] = { 9,0 };
- POS[25] = { 7,7 }; POS[26] = { 7,1 }; POS[31] = { 6,0 }; POS[30] = { 6,2 }; POS[29] = { 6,4 };
- POS[28] = { 6,6 }; POS[27] = { 6,8 };
- for (int i = 0; i < 10; i++)for (int j = 0; j < 9; j++)base.des[i][j] = -1;
- for (int i = 0; i < 32; i++) base.des[POS[i].first][POS[i].second] = i;
- //base.des[6][0]=base.des[6][2]=base.des[6][4]=base.des[6][6]=base.des[6][8]=-1;
- //base.des[3][0]=base.des[3][2]=base.des[3][4]=base.des[3][6]=base.des[3][8]=-1;
- }
- inline int Checkend(Chess &u){//查看是否结局
- int x=1;
- for(int i=limx[0].first;i<=limx[0].second;i++)
- for(int j=limy[0].first;j<=limy[0].second;j++)
- if(u.des[i][j]==0)x=0;
- return x;
- }
- inline Chess rev(Chess& a) {//局面旋转
- Chess k;
- for(int i=0;i<10;i++)for(int j=0;j<9;j++)k.des[i][j]=-1;
- for (int i = 0; i < 10; i++)for (int j = 0; j < 9; j++){
- if(a.des[9 - i][8 - j]>15)k.des[i][j] = a.des[9 - i][8 - j]-16;
- else if(a.des[9 - i][8 - j]>=0)k.des[i][j] = a.des[9 - i][8 - j]+16;
- }
- return k;
- }
- inline Chess Creatdes(int posxi, int posyi, int posx, int posy, Chess p) {//根据走子建立新局面
- int i = p.des[posxi][posyi];
- p.des[posx][posy] = i;
- p.des[posxi][posyi] = -1;
- return rev(p);
- }
- inline void creatnode(int u,Chess&son) {//建立节点
- int k=++siz;
- datree[u].push_back(k);
- datree[k].clear();
- fa[k]=u;
- chess[k]=son;
- w[k]=n[k]=winrate[k]=0;
- dep[k]=dep[u] + 1;
- mark[k]=!mark[u];
- endpo[k]=roval[k]=del[k]=0;
- }
- inline int judge(Chess& u, int i, int posx, int posy) {
- if (i == 0 && u.des[posx][posy] == 16)return 1;
- if (!(limx[i].first <= posx && posx <= limx[i].second && limy[i].first <= posy && posy <= limy[i].second))return 0;
- if (!(u.des[posx][posy] == -1 || u.des[posx][posy] > 15))return 0;
- return 1;
- }
- void newrate(int u) {
- winrate[u] = w[u] / (n[u]*100) + (lf)c * sqrt(max(log(n[fa[u]]) / n[u],0.0));
- }
- inline vector<Chess> step(Chess tmp, int sw) {//在tmp的局面里面获取走子可能,sw为1获取所有能
- pair<int, int> poi[32];
- for (int i = 0; i < 32; i++)poi[i] = { -1,-1 };
- for (int i = 0; i < 10; i++)for (int j = 0; j < 9; j++)if (tmp.des[i][j] >= 0)poi[tmp.des[i][j]] = { i,j };
- vector<Chess> ans;
-
- int nposx, nposy, posx, posy;
- auto work = [&](int i)-> void {
- if (judge(tmp, i, nposx, nposy))
- ans.push_back(Creatdes(posx, posy, nposx, nposy, tmp));
- };
- for (int i = 0; i <= 15; i++) {
- if(sw==0)i=e()%16;
- if (poi[i].first == -1)continue;
- posx = poi[i].first, posy = poi[i].second;
- if (i == 0) {
- nposx = posx - 1, nposy = posy;
- work(i);
- nposx = posx + 1, nposy = posy;
- work(i);
- nposx = posx, nposy = posy + 1;
- work(i);
- nposx = posx, nposy = posy - 1;
- work(i);
- if (posy == poi[16].second) {
- nposx = poi[16].first, nposy = poi[16].second;
- int x=0;
- for(int j=posx+1;j<=nposx-1;j++)if(tmp.des[j][posy]!=-1)x=1;
- if(x==0){
- work(i);
-
- }
- }
- }
- else if (i == 1 || i == 2) {
- nposx = posx - 1, nposy = posy - 1;
- work(i);
- nposx = posx + 1, nposy = posy + 1;
- work(i);
- nposx = posx - 1, nposy = posy + 1;
- work(i);
- nposx = posx + 1, nposy = posy - 1;
- work(i);
- }
- else if (i == 3 || i == 4) {
- if (posx - 1 >= 0 && posy - 1 >= 0 && tmp.des[posx - 1][posy - 1] == -1) {
- nposx = posx - 2, nposy = posy - 2;
- work(i);
- }
- if (posx + 1 <= 9 && posy + 1 <= 8 && tmp.des[posx + 1][posy + 1] == -1) {
- nposx = posx + 2, nposy = posy + 2;
- work(i);
- }
- if (posx + 1 <= 9 && posy - 1 >= 0 && tmp.des[posx + 1][posy - 1] == -1) {
- nposx = posx + 2, nposy = posy - 2;
- work(i);
- }
- if (posx - 1 >= 0 && posy + 1 <= 8 && tmp.des[posx - 1][posy + 1] == -1) {
- nposx = posx - 2, nposy = posy + 2;
- work(i);
- }
- }
- else if (i == 5 || i == 6) {
- if (posx - 1 >= 0 && tmp.des[posx - 1][posy] == -1) {
- nposx = posx - 2, nposy = posy - 1;
- work(i);
- nposx = posx - 2, nposy = posy + 1;
- work(i);
- }
- if (posx + 1 <= 9 && tmp.des[posx + 1][posy] == -1) {
- nposx = posx + 2, nposy = posy + 1;
- work(i);
- nposx = posx + 2, nposy = posy - 1;
- work(i);
- }
- if (posy - 1 >= 0 && tmp.des[posx][posy - 1] == -1) {
- nposx = posx - 1, nposy = posy - 2;
- work(i);
- nposx = posx + 1, nposy = posy - 2;
- work(i);
- }
- if (posy + 1 <= 8 && tmp.des[posx][posy + 1] == -1) {
- nposx = posx - 1, nposy = posy + 2;
- work(i);
- nposx = posx + 1, nposy = posy + 2;
- work(i);
- }
- }
- else if (i == 7 || i == 8) {
- for (int j = 1; j <= 9; j++) {
- nposx = posx + j, nposy = posy;
- work(i);
- if (posx + j > 9 || tmp.des[posx + j][posy] != -1)break;
- }
- for (int j = 1; j <= 9; j++) {
- nposx = posx - j, nposy = posy;
- work(i);
- if (posx - j < 0 || tmp.des[posx - j][posy] != -1)break;
- }
- for (int j = 1; j <= 8; j++) {
- nposx = posx, nposy = posy + j;
- work(i);
- if (posy + j > 8 || tmp.des[posx][posy + j] != -1)break;
- }
- for (int j = 1; j <= 8; j++) {
- nposx = posx, nposy = posy - j;
- work(i);
- if (posy - j < 0 || tmp.des[posx][posy - j] != -1)break;
- }
- }
- else if (i == 9 || i == 10) {
- for (int j = 1; j <= 9; j++) {
- if (posx + j > 9)break;
- if (tmp.des[posx + j][posy] == -1) {
- nposx = posx + j, nposy = posy;
- work(i);
- }
- if (tmp.des[posx + j][posy] != -1) {
- j++;
- while (posx + j <= 9 && tmp.des[posx + j][posy] == -1)j++;
- nposx = posx + j, nposy = posy;
- work(i);
- break;
- }
- }
- for (int j = 1; j <= 9; j++) {
- if (posx - j < 0)break;
- if (tmp.des[posx - j][posy] == -1) {
- nposx = posx - j, nposy = posy;
- work(i);
- }
- if (tmp.des[posx - j][posy] != -1) {
- j++;
- while (posx - j >= 0 && tmp.des[posx - j][posy] == -1)j++;
- nposx = posx - j, nposy = posy;
- work(i);
- break;
- }
- }
- for (int j = 1; j <= 8; j++) {
- if (posy + j > 8)break;
- if (tmp.des[posx][posy + j] == -1) {
- nposx = posx, nposy = posy + j;
- work(i);
- }
- if (tmp.des[posx][posy + j] != -1) {
- j++;
- while (posy + j <= 8 && tmp.des[posx][posy + j] == -1)j++;
- nposx = posx, nposy = posy + j;
- work(i);
- break;
- }
- }
- for (int j = 1; j <= 8; j++) {
- if (posy - j < 0)break;
- if (tmp.des[posx][posy - j] == -1) {
- nposx = posx, nposy = posy - j;
- work(i);
- }
- if (tmp.des[posx][posy - j] != -1) {
- j++;
- while (posy - j >= 0 && tmp.des[posx][posy - j] == -1)j++;
- nposx = posx, nposy = posy - j;
- work(i);
- break;
- }
- }
- }
- else {
- nposx = posx + 1, nposy = posy;
- work(i);
- if (posx > 4) {
- nposx = posx, nposy = posy + 1;
- work(i);
- nposx = posx, nposy = posy - 1;
- work(i);
- }
- }
- if(sw==0&&ans.size())return ans;
- }
- return ans;
- }
-
- int rollout(Chess u,int dep) {
- int ans = 0;
- if (Checkend(u)) {
- return 1e9;
- }
- for (int tp = 1; tp <= thinktime; tp++) {
- auto tmp = u;
- int cnt = maxstep - dep;
- int lun=1;
- while (cnt>=0) {
- vector<Chess> mulp;
- while(mulp.size()<1)mulp=step(tmp,0);
- int fe = 1;
- lun=!lun;
- for (auto& v : mulp) {
- if(!Checkend(v))continue;
- if (lun==0)ans -= calval(v);
- else ans+=calval(v);
- fe=0;
- }
- if(fe==0)break;
- random_shuffle(mulp.begin(), mulp.end());
- tmp=mulp.back();
- cnt--;
- if(cnt<0){
- if (lun==0)ans -= calval(tmp);
- else ans+= calval(tmp);
- }
- }
- }
- return ans;
- }
- void creat(int u) {
- auto p=step(chess[u],1);
- vector<pll> snf(p.size());
- #pragma omp parallel for num_threads(maxnumcore)
- for(int i=0;i<p.size();i++){
- auto&v=p[i];
- snf[i]={rollout(v,dep[u]+1),i};
- }
- sort(snf.begin(), snf.end(), greater<pll>());
- for(int i=0;i<nodelimt&&i<snf.size();i++)
- creatnode(u,p[snf[i].second]);
-
- }
- int explor(int u) {
- if (datree[u].empty())creat(u);
- vector<pair<lf,int>> snf;
- for (auto& v : datree[u]) {
- if (!n[v])snf.push_back({ roval[v]+1e8,v });
- else newrate(v),snf.push_back({ winrate[v],v });
- }
- sort(snf.begin(), snf.end());
- return snf.back().second;
- }
- void spreadback(int end) {
- lf cn = 0;
- int v = end;
- while (v != 0) {
- n[v]++;
- if (mark[v] == mark[end])cn+=calval(chess[v]);
- else cn-=calval(chess[v]);
- if (mark[v] == mark[end])w[v] += cn;
- else w[v] -= cn;
- newrate(v);
- v = fa[v];
- }
- }
- inline void sendmessage(int rx,int ry,int px,int py){
- ofstream outFile;
- outFile.open("rec.txt", std::ios::out);
- outFile << rx<<' '<<ry<<'\n';
- outFile << px<<' '<<py<<'\n';
- outFile.close();
- outFile.open("message.txt", std::ios::out);
- outFile<<1<<'\n';
- outFile.close();
- }
- Chess cal(Chess u) {
- siz=0;
- chess[0]=u; datree[0].clear();
- n[0]=w[0]=winrate[0]=0;
- fa[0]=-1; dep[0]=mark[0]=endpo[0]=roval[0]=del[0]=0;
- int cnt=0,root=0;
- while (1) {
- cout<< siz <<"\n";
- cout << cnt <<"\n";
- root = explor(root);
- cnt++;
- if (cnt>= maxcal)break;
- if (endpo[root] || cnt>= maxcal) {
- spreadback(root);
- root = 0;
- if (cnt>= maxcal)break;
- continue;
- }
- else if (dep[root] > maxstep || cnt>= maxcal) {
- spreadback(root);
- root = 0;
- if (cnt>= maxcal)break;
- continue;
- }
- if (cnt>= maxcal)break;
- }
- Chess ans;
- lf maxr = -1e9;
- for (auto& v : datree[0]) {
- if (n[v] == 0)continue;
- newrate(v);
- if (w[v]/n[v] > maxr) {
- maxr = w[v]/n[v];
- ans = chess[v];
- }
- }
- return ans;
- }
- void cp() {
- Chess now=base,tmp;
- prec=base;
- while (1) {
- tmp=now;
- now = cal(now);
- prec=tmp;
- tmp=rev(now);
-
- int readx,ready,px,py;
- for(int i=0;i<10;i++)for(int j=0;j<9;j++)if(prec.des[i][j]!=tmp.des[i][j]){
- if(tmp.des[i][j]!=-1){
- px=i,py=j;
- }
- else readx=i,ready=j;
- }
-
- sendmessage(readx,ready,px,py);
-
- if(Checkend(now)){
- cout<<"ERO!!!!!!!!!!!!\n";
- system("pause");
- break;
- }
-
- tmp=now;
- now = cal(now);
- prec=rev(tmp);
-
- tmp=now;
-
- for(int i=0;i<10;i++)for(int j=0;j<9;j++)if(prec.des[i][j]!=tmp.des[i][j]){
- if(tmp.des[i][j]!=-1){
- px=i,py=j;
- }
- else readx=i,ready=j;
- }
- sendmessage(readx,ready,px,py);
- if(Checkend(now)){
- cout<<"ERO!!!!!!!!!!!!\n";
- system("pause");
- break;
- }
- }
- }
- }tester;
- int main()
- {
- maxnumcore = omp_get_num_procs();
- tester.init();
- e.seed(time(0));
- while (1) {
- tester.cp();
- }
- while(1);
- return 0;
- }
- /*棋盘easyx实现*/
- // includes
- #include<iostream>
- #include<graphics.h>
- #include<bits/stdc++.h>
- using namespace std;
-
- // 使用到的 WCHAR 字符
- class CKind {
- public:
- WCHAR ROOKS = *(_T("车"));
- WCHAR KNIGHTS = *_T("马");
- WCHAR ELEPHANTS = *_T("象");
- WCHAR MINISTERS = *_T("相");
- WCHAR MANDARINS = *_T("士");
- WCHAR GUARDS = *_T("仕");
- WCHAR KING = *_T("将");
- WCHAR GENERALS = *_T("帅");
- WCHAR CANNONS = *_T("炮");
- WCHAR PAWNS = *_T("卒");
- WCHAR SOLDIERS = *_T("兵");
- WCHAR PLAYER_1 = *_T("BLACK");
- WCHAR PLAYER_2 = *_T("RED");
- };
-
- // 落子点
- class Grid {
- int x, y;
- public:
- Grid() {};
- virtual WCHAR get_name() { return NULL; }
- virtual void drowReady(int x, int y) {}
- virtual void setPlayer(WCHAR player) {}
- virtual WCHAR getPlayer() { return NULL; }
- void set_xy(int x, int y)
- {
- this->x = x;
- this->y = y;
- }
- pair<int, int>get_xy()
- {
- pair<int, int>p(x, y);
- return p;
- }
- };
-
- // 棋子
- class Chess :public Grid
- {
- WCHAR player;
- WCHAR name;
- public:
- Chess(WCHAR name)
- {
- this->name = name;
- }
- WCHAR get_name()
- {
- return name;
- }
- void setPlayer(WCHAR player)
- {
- this->player = player;
- }
- WCHAR getPlayer()
- {
- return player;
- }
- void drowReady(int x, int y)
- {
- setfillcolor(RGB(51, 205, 219));
- fillcircle(x, y, 27);
- RECT r = { x - 18, y - 18, x + 18, y + 18 };
- setbkcolor(RGB(51, 205, 219));
- if (player == *_T("BLACK"))
- {
- settextcolor(BLACK);
- }
- else
- settextcolor(RED);
- drawtext(name, &r, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
- }
- };
-
- // 棋盘
- class PaneBoard {
- CKind ckind;
- int PERLENGTH;//单位长度
- Grid* grid[10][9];
- public:
- PaneBoard()
- {
- PERLENGTH = 60;
- }
- //开始游戏
- void startGame()
- {
- initPaneGrid();
- CreateMap();
- repaint();
- messagewhile();
- closegraph();
- }
- //初始化棋盘格
- void initPaneGrid()
- {
- for (int i = 0; i < 9; i++)
- {
- for (int j = 0; j < 10; j++)
- {
- grid[i][j] = NULL;
- }
- }
- //敌方
- grid[0][0] = new Chess(ckind.ROOKS);
- grid[0][1] = new Chess(ckind.KNIGHTS);
- grid[0][2] = new Chess(ckind.ELEPHANTS);
- grid[0][3] = new Chess(ckind.MANDARINS);
- grid[0][4] = new Chess(ckind.KING);
- grid[0][5] = new Chess(ckind.MANDARINS);
- grid[0][6] = new Chess(ckind.ELEPHANTS);
- grid[0][7] = new Chess(ckind.KNIGHTS);
- grid[0][8] = new Chess(ckind.ROOKS);
- grid[2][1] = new Chess(ckind.CANNONS);
- grid[2][7] = new Chess(ckind.CANNONS);
- for (int i = 0; i <= 8; i += 2)
- {
- grid[3][i] = new Chess(ckind.PAWNS);
- }
- //设置棋盘格坐标与属性
- for (int j = 0; j <= 8; j++)
- {
- grid[0][j]->set_xy(PERLENGTH * (j + 1), PERLENGTH);
- grid[0][j]->setPlayer(ckind.PLAYER_1);
- }
- grid[2][1]->set_xy(2 * PERLENGTH, 3 * PERLENGTH);
- grid[2][7]->set_xy(8 * PERLENGTH, 3 * PERLENGTH);
- grid[2][1]->setPlayer(ckind.PLAYER_1);
- grid[2][7]->setPlayer(ckind.PLAYER_1);
- for (int i = 0; i <= 8; i += 2)
- {
- grid[3][i]->set_xy((i + 1) * PERLENGTH, 4 * PERLENGTH);
- grid[3][i]->setPlayer(ckind.PLAYER_1);
- }
- //我方
- grid[9][0] = new Chess(ckind.ROOKS);
- grid[9][1] = new Chess(ckind.KNIGHTS);
- grid[9][2] = new Chess(ckind.MINISTERS);
- grid[9][3] = new Chess(ckind.GUARDS);
- grid[9][4] = new Chess(ckind.GENERALS);
- grid[9][5] = new Chess(ckind.GUARDS);
- grid[9][6] = new Chess(ckind.MINISTERS);
- grid[9][7] = new Chess(ckind.KNIGHTS);
- grid[9][8] = new Chess(ckind.ROOKS);
- grid[7][1] = new Chess(ckind.CANNONS);
- grid[7][7] = new Chess(ckind.CANNONS);
- for (int i = 0; i <= 8; i += 2)
- {
- grid[6][i] = new Chess(ckind.SOLDIERS);
- }
- //设置棋盘格坐标与属性
- for (int j = 0; j <= 8; j++)
- {
- grid[9][j]->set_xy(PERLENGTH * (j + 1), 10 * PERLENGTH);
- grid[9][j]->setPlayer(ckind.PLAYER_2);
- }
- grid[7][1]->set_xy(2 * PERLENGTH, 8 * PERLENGTH);
- grid[7][7]->set_xy(8 * PERLENGTH, 8 * PERLENGTH);
- grid[7][1]->setPlayer(ckind.PLAYER_2);
- grid[7][7]->setPlayer(ckind.PLAYER_2);
- for (int i = 0; i <= 8; i += 2)
- {
- grid[6][i]->set_xy((i + 1) * PERLENGTH, 7 * PERLENGTH);
- grid[6][i]->setPlayer(ckind.PLAYER_2);
- }
- }
- //创建窗口
- void CreateMap()
- {
- initgraph(10 * PERLENGTH, 11 * PERLENGTH);//单位宽度
- setbkcolor(WHITE);
- cleardevice();
- }
-
- //重绘棋盘
- void repaint()
- {
- setbkcolor(WHITE);
- cleardevice();
- setlinecolor(BLACK);
- setlinestyle(PS_SOLID | PS_JOIN_BEVEL, 3);
- line(PERLENGTH - 5, PERLENGTH - 5, PERLENGTH * 9 + 5, PERLENGTH - 5);
- line(PERLENGTH - 5, PERLENGTH - 5, PERLENGTH - 5, PERLENGTH * 10 + 5);
- line(PERLENGTH - 5, PERLENGTH * 10 + 5, PERLENGTH * 9 + 5, PERLENGTH * 10 + 5);
- line(PERLENGTH * 9 + 5, PERLENGTH * 10 + 5, PERLENGTH * 9 + 5, PERLENGTH - 5);
- setlinestyle(PS_SOLID | PS_JOIN_BEVEL, 2);
- for (int i = 1; i <= 10; i++)
- {
- line(PERLENGTH, i * PERLENGTH, 9 * PERLENGTH, i * PERLENGTH);
- }
- for (int i = 1; i <= 9; i++)
- {
- line(i * PERLENGTH, PERLENGTH, i * PERLENGTH, 5 * PERLENGTH);
- line(i * PERLENGTH, 6 * PERLENGTH, i * PERLENGTH, 10 * PERLENGTH);
- }
- line(PERLENGTH, 5 * PERLENGTH, PERLENGTH, 6 * PERLENGTH);
- line(9 * PERLENGTH, 5 * PERLENGTH, 9 * PERLENGTH, 6 * PERLENGTH);
- line(4 * PERLENGTH, PERLENGTH, 6 * PERLENGTH, 3 * PERLENGTH);
- line(4 * PERLENGTH, 3 * PERLENGTH, 6 * PERLENGTH, PERLENGTH);
- line(4 * PERLENGTH, 8 * PERLENGTH, 6 * PERLENGTH, 10 * PERLENGTH);
- line(4 * PERLENGTH, 10 * PERLENGTH, 6 * PERLENGTH, 8 * PERLENGTH);
- settextcolor(RED);
- settextstyle(PERLENGTH * 2 / 3, 0, _T("楷体"));
- RECT r = { 2 * PERLENGTH, 5 * PERLENGTH, 4 * PERLENGTH, 6 * PERLENGTH };
- drawtext(_T("楚河"), &r, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
- r = { 6 * PERLENGTH, 5 * PERLENGTH, 8 * PERLENGTH, 6 * PERLENGTH };
- drawtext(_T("汉界"), &r, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
- for (int i = 3; i <= 7; i += 2)
- {
- //敌方
- line(i * PERLENGTH - 3, 4 * PERLENGTH - 3, i * PERLENGTH - 3, 4 * PERLENGTH - 10);
- line(i * PERLENGTH - 3, 4 * PERLENGTH - 3, i * PERLENGTH - 10, 4 * PERLENGTH - 3);
- line(i * PERLENGTH - 3, 4 * PERLENGTH + 3, i * PERLENGTH - 3, 4 * PERLENGTH + 10);
- line(i * PERLENGTH - 3, 4 * PERLENGTH + 3, i * PERLENGTH - 10, 4 * PERLENGTH + 3);
- line(i * PERLENGTH + 3, 4 * PERLENGTH - 3, i * PERLENGTH + 3, 4 * PERLENGTH - 10);
- line(i * PERLENGTH + 3, 4 * PERLENGTH - 3, i * PERLENGTH + 10, 4 * PERLENGTH - 3);
- line(i * PERLENGTH + 3, 4 * PERLENGTH + 3, i * PERLENGTH + 3, 4 * PERLENGTH + 10);
- line(i * PERLENGTH + 3, 4 * PERLENGTH + 3, i * PERLENGTH + 10, 4 * PERLENGTH + 3);
- //我方
- line(i * PERLENGTH - 3, 7 * PERLENGTH - 3, i * PERLENGTH - 3, 7 * PERLENGTH - 10);
- line(i * PERLENGTH - 3, 7 * PERLENGTH - 3, i * PERLENGTH - 10, 7 * PERLENGTH - 3);
- line(i * PERLENGTH - 3, 7 * PERLENGTH + 3, i * PERLENGTH - 3, 7 * PERLENGTH + 10);
- line(i * PERLENGTH - 3, 7 * PERLENGTH + 3, i * PERLENGTH - 10, 7 * PERLENGTH + 3);
- line(i * PERLENGTH + 3, 7 * PERLENGTH - 3, i * PERLENGTH + 3, 7 * PERLENGTH - 10);
- line(i * PERLENGTH + 3, 7 * PERLENGTH - 3, i * PERLENGTH + 10, 7 * PERLENGTH - 3);
- line(i * PERLENGTH + 3, 7 * PERLENGTH + 3, i * PERLENGTH + 3, 7 * PERLENGTH + 10);
- line(i * PERLENGTH + 3, 7 * PERLENGTH + 3, i * PERLENGTH + 10, 7 * PERLENGTH + 3);
- }
- for (int i = 2; i <= 8; i += 6)
- for (int j = 3; j <= 8; j += 5)
- {
- line(i * PERLENGTH - 3, j * PERLENGTH - 3, i * PERLENGTH - 3, j * PERLENGTH - 10);
- line(i * PERLENGTH - 3, j * PERLENGTH - 3, i * PERLENGTH - 10, j * PERLENGTH - 3);
- line(i * PERLENGTH - 3, j * PERLENGTH + 3, i * PERLENGTH - 3, j * PERLENGTH + 10);
- line(i * PERLENGTH - 3, j * PERLENGTH + 3, i * PERLENGTH - 10, j * PERLENGTH + 3);
- line(i * PERLENGTH + 3, j * PERLENGTH - 3, i * PERLENGTH + 3, j * PERLENGTH - 10);
- line(i * PERLENGTH + 3, j * PERLENGTH - 3, i * PERLENGTH + 10, j * PERLENGTH - 3);
- line(i * PERLENGTH + 3, j * PERLENGTH + 3, i * PERLENGTH + 3, j * PERLENGTH + 10);
- line(i * PERLENGTH + 3, j * PERLENGTH + 3, i * PERLENGTH + 10, j * PERLENGTH + 3);
- }
- for (int j = 4; j <= 7; j += 3)
- {
- line(PERLENGTH + 3, j * PERLENGTH - 3, PERLENGTH + 3, j * PERLENGTH - 10);
- line(PERLENGTH + 3, j * PERLENGTH - 3, PERLENGTH + 10, j * PERLENGTH - 3);
- line(PERLENGTH + 3, j * PERLENGTH + 3, PERLENGTH + 3, j * PERLENGTH + 10);
- line(PERLENGTH + 3, j * PERLENGTH + 3, PERLENGTH + 10, j * PERLENGTH + 3);
- }
- for (int j = 4; j <= 7; j += 3)
- {
- line(9 * PERLENGTH - 3, j * PERLENGTH - 3, 9 * PERLENGTH - 3, j * PERLENGTH - 10);
- line(9 * PERLENGTH - 3, j * PERLENGTH - 3, 9 * PERLENGTH - 10, j * PERLENGTH - 3);
- line(9 * PERLENGTH - 3, j * PERLENGTH + 3, 9 * PERLENGTH - 3, j * PERLENGTH + 10);
- line(9 * PERLENGTH - 3, j * PERLENGTH + 3, 9 * PERLENGTH - 10, j * PERLENGTH + 3);
- }
- for (int i = 0; i < 10; i++)
- {
- for (int j = 0; j < 9; j++)
- {
- if (grid[i][j] != NULL)
- {
- pair<int, int>p = grid[i][j]->get_xy();
- int x = p.first; int y = p.second;
- setfillcolor(RGB(200, 220, 250));
- setbkcolor(RGB(200, 220, 250));
- fillcircle(x, y, 27);
- RECT r = { x - 18, y - 18, x + 18, y + 18 };
- if (grid[i][j]->getPlayer() == *_T("BLACK"))
- {
- settextcolor(BLACK);
- }
- else
- settextcolor(RED);
- drawtext(grid[i][j]->get_name(), &r, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
- }
- }
- }
- }
-
- //进入消息循环
- void messagewhile()
- {
- MOUSEMSG msg;
- pair<int, int>p;
- pair<int, int>readyChess;
- while (1) {
- ifstream data("message.txt", std::ios::in);
- int mm;
- data >> mm;
- if (mm != 1) {
- data.close();
- continue;
- }
- data.close();
-
- ofstream outFile;
- outFile.open("message.txt", std::ios::out);
- outFile << 0<<'\n';
- outFile.close();
-
- ifstream rec("rec.txt", std::ios::in);
- rec >> readyChess.first;
- rec >> readyChess.second;
- rec >> p.first;
- rec >> p.second;
-
- grid[p.first][p.second] = grid[readyChess.first][readyChess.second];
- pair<int, int>change = compute_xy(p.first, p.second);
- grid[p.first][p.second]->set_xy(change.first, change.second);
- grid[readyChess.first][readyChess.second] = NULL;
-
- repaint();
- rec.close();
- continue;
- }
- }
- pair<int, int>compute_xy(int i, int j)
- {
- pair<int, int>p((j + 1) * PERLENGTH, (i + 1) * PERLENGTH);
- return p;
- }
- };
- void main()
- {
- PaneBoard paneBoard;
- paneBoard.startGame();
- }
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。