From 387040109d9db93e436c5e57f5a65300de337302 Mon Sep 17 00:00:00 2001 From: "dev@dev.com" Date: Fri, 4 Jun 2021 11:35:33 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E6=98=BE=E7=A4=BA=E7=AD=96?= =?UTF-8?q?=E7=95=A5=E4=BA=86=EF=BC=8C=E4=BB=A5=E4=BE=BF=E8=A7=82=E5=AF=9F?= =?UTF-8?q?=E8=B5=B0=E4=BD=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- chesspi/chesspi.h | 2 +- chesspi/chesspi_rules.cpp | 53 +++++++++++++++++++++++++++++---------- chesspi/main.cpp | 30 +++++++++++++--------- 3 files changed, 59 insertions(+), 26 deletions(-) diff --git a/chesspi/chesspi.h b/chesspi/chesspi.h index 6a558f1..c4e7b82 100644 --- a/chesspi/chesspi.h +++ b/chesspi/chesspi.h @@ -42,7 +42,7 @@ bool build_node(const int coordx[/*32*/], const int coordy[/*32*/],const int ali bool build_node(const int coordx[/*32*/], const int coordy[/*32*/],const int alive[/*32*/],const int side,chess_node * node); -void print_node(const chess_node & node); +void print_node(const chess_node & node,const chess_node & old_node); void mirror_coordx(const unsigned char cod1[/*32*/],unsigned char cod2[/*32*/]); diff --git a/chesspi/chesspi_rules.cpp b/chesspi/chesspi_rules.cpp index 9c4b738..5694792 100644 --- a/chesspi/chesspi_rules.cpp +++ b/chesspi/chesspi_rules.cpp @@ -67,10 +67,17 @@ std::vector expand_node(const chess_node & r_root, const int side) //有限层级优先顺序 // * 帅士士相相马马车车炮炮兵兵兵兵兵 將仕仕象象馬馬車車砲砲卒卒卒卒卒 - const int order[16] = {7,8,9,10,5,6,11,12,14,15,1,2,3,4,13,0}; + const int order[5][16] = { + {7,8,9,10,5,6,11,12,14,15,1,2,3,4,13,0}, + {9,10,7,8,5,6,11,12,14,15,1,2,3,4,13,0}, + {5,6,9,10,7,8,11,12,14,15,1,2,3,4,13,0}, + {9,6,5,7,10,8,1,2,3,4,11,12,14,15,13,0}, + {10,8,5,7,9,6,1,2,3,4,11,12,14,15,13,0} + }; + int curr_od = rand() % 5; for (int oi=0;oi<16;++oi) { - const int i = order[oi]; + const int i = order[curr_od][oi]; if (!alive[i]) continue; switch (i) @@ -342,7 +349,7 @@ const int alive[/*32*/],const int side,chess_node * node) return true; } -void print_node(const chess_node & node) +void print_node(const chess_node & node,const chess_node & old_node) { const int side = node.side; printf ("side=%d\n",side); @@ -352,23 +359,36 @@ void print_node(const chess_node & node) int coordy[32]{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}; int alive[32]{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}; + int coord_ox[32]{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}; + int coord_oy[32]{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}; + int alive_o[32]{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}; + + std::string nhash = node2hash(node.coords,node.alive); for (int i=0;i<32;++i) { coordx[i] = node.coords[i] >> 4; coordy[i] = node.coords[i] & 0x0f; + coord_ox[i] = old_node.coords[i] >> 4; + coord_oy[i] = old_node.coords[i] & 0x0f; alive[i] = (node.alive & (1<>4); - printf ("%X",((unsigned char)(nhash[i]))&0x0F); + alive_o[i] = (old_node.alive & (1<>4); + //printf ("%X",((unsigned char)(nhash[i]))&0x0F); } - printf ("\n"); + //printf ("\n"); //制作坐标站位表 int map_coords[11][10]; + int map_oldcoords[11][10]; memset (map_coords,0,sizeof(map_coords)); + memset (map_oldcoords,0,sizeof(map_coords)); for (int i=0;i<32;++i) + { if (alive[i]) map_coords[coordy[i]][coordx[i]] = i+1; + if (alive_o[i]) + map_oldcoords[coord_oy[i]][coord_ox[i]] = i+1; + } if (side) { @@ -381,12 +401,15 @@ void print_node(const chess_node & node) printf ("%2d ", y-1); for (int x=1;x<=9;++x) { + if ((map_coords[y][x]-1)/16 != (map_oldcoords[y][x]-1)/16 + ||(map_coords[y][x]>0)!=(map_oldcoords[y][x]>0)) + printf("\033[1m\033[4m"); if (map_coords[y][x]>0 && map_coords[y][x]<=16) - printf("\33[1m\033[31m%s\033[0m",pstr[map_coords[y][x]-1]); + printf("\033[31m%s\033[0m",pstr[map_coords[y][x]-1]); else if (map_coords[y][x]>16) - printf("\33[1m\033[36m%s\033[0m",pstr[map_coords[y][x]-1]); + printf("\033[36m%s\033[0m",pstr[map_coords[y][x]-1]); else - printf (" "); + printf (" \033[0m"); } printf ("\n"); } @@ -402,12 +425,16 @@ void print_node(const chess_node & node) printf ("%2d ", y-1); for (int x=9;x>0;--x) { + if ((map_coords[y][x]-1)/16 != (map_oldcoords[y][x]-1)/16 + ||(map_coords[y][x]>0)!=(map_oldcoords[y][x]>0)) + printf("\033[1m\033[4m"); + if (map_coords[y][x]>0 && map_coords[y][x]<=16) - printf("\33[1m\033[31m%s\033[0m",pstr[map_coords[y][x]-1]); + printf("\033[31m%s\033[0m",pstr[map_coords[y][x]-1]); else if (map_coords[y][x]>16) - printf("\33[1m\033[36m%s\033[0m",pstr[map_coords[y][x]-1]); + printf("\033[36m%s\033[0m",pstr[map_coords[y][x]-1]); else - printf (" "); + printf (" \033[0m"); } printf ("\n"); } diff --git a/chesspi/main.cpp b/chesspi/main.cpp index 485ba03..9532742 100644 --- a/chesspi/main.cpp +++ b/chesspi/main.cpp @@ -6,6 +6,7 @@ #include #include #include +#include #include #include "chesspi.h" @@ -16,6 +17,7 @@ int main() int coordy[32]{1,1,1,1,1,1,1,1,1,3,3,4,4,4,4,4,10,10,10,10,10,10,10,10,10,8,8,7,7,7,7,7}; int alive[32]{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,1,1,1,1}; + srand(time(0)); printf ("Side(0=RED,1=BLACK):"); chess_node root; @@ -33,15 +35,18 @@ int main() side = 1; size_t round = 0; bool finished = false; - root.side = side; + root.side = side % 2; std::vector history; - history.push_back(root); while (!finished) { + printf ("\nround %d:\n==========\n", round); + if (history.size()) + print_node(root,*history.rbegin()); + else + print_node(root,root); + history.push_back(root); if ((round+side) % 2==0) { - printf ("\nround %d:\n==========\n", round); - print_node(root); //找到棋子 bool ok = false; chess_node new_node; @@ -62,25 +67,26 @@ int main() } } } - else { - print_node(root); + else + { std::vector tree = build_tree(root,round%2,history); size_t best = judge_tree(tree); if (best <1 || best + 1 > tree.size()) break; root = tree[best]; - printf ("Best % d Cost: %f, %f, Weight %f\n",best + printf ("Best % ld Cost: %f, %f, Weight %f\n",best ,root.jump_cost[0],root.jump_cost[1],root.weight ); } - root.side = side; - history.push_back(root); + root.side = side % 2; ++round; if (!((root.alive & 0x00010001)==0x00010001)) - break; - - + { + printf ("\nround %lu:\n==========\n", round); + print_node(root,*history.rbegin()); + finished = true; + } } printf ("FINISHED!\n"); -- GitLab