Allen_Hu

專題

Mar 29th, 2017
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 20.03 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #include <conio.h>
  3. #include <windows.h>
  4. #define a first
  5. #define b second
  6. #define mp make_pair
  7. #define mp4(i , j , k , l) mp(mp(i , j) , mp(k , l))
  8. /// cksh.h
  9.  
  10. void SetColor(int f,int b)
  11. { SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),b*16+f); }
  12. void gotoxy(int xpos, int ypos)
  13. {
  14.   COORD scrn;
  15.   HANDLE hOuput = GetStdHandle(STD_OUTPUT_HANDLE);
  16.   scrn.X = ypos,scrn.Y = xpos;
  17.   SetConsoleCursorPosition(hOuput,scrn);
  18. }
  19. void show()
  20. {
  21.   HANDLE hcon = GetStdHandle(STD_OUTPUT_HANDLE);
  22.   CONSOLE_CURSOR_INFO cci;
  23.   cci.bVisible = TRUE,cci.dwSize = 25;
  24.   SetConsoleCursorInfo(hcon, &cci);
  25. }
  26. void hide()
  27. {
  28.   HANDLE hcon = GetStdHandle(STD_OUTPUT_HANDLE);
  29.   CONSOLE_CURSOR_INFO cci;
  30.   cci.bVisible = FALSE,cci.dwSize = 25;
  31.   SetConsoleCursorInfo(hcon, &cci);
  32. }
  33. void clrscr() { system("CLS"); }
  34. int getkey()
  35. {
  36.     int ch=getch();
  37.     if(ch==224)
  38.     {
  39.         ch=getch();
  40.         if(ch==72)return 0; //up
  41.         if(ch==75)return 1; //left
  42.         if(ch==77)return 2; //right
  43.         if(ch==80)return 3; //down
  44.     }
  45.     return -1;
  46. }
  47. /// PIC_MAKER
  48. using namespace std;
  49. typedef pair<int,int> PII;
  50. const char U = ' ' , V = '#';
  51. const int HMAX = 10000 , WMAX = 5000;
  52. const int BCOL = 15;         ///地板顏色
  53. const int WCOL = 4;          ///牆壁顏色
  54. const int SCOL = 10;         ///自己顏色
  55. const int MCOL = 2;          ///moving wall顏色
  56. const int TOP = 1;           ///地圖從第幾行開始印
  57. const int BLANK = 2;         ///間距
  58.  int BLOOD = 50;        ///初始血量
  59. const int BAG = 5;           ///血包數量(<=6)
  60. const int SDLAY = 10;        ///動畫速度
  61. const int INF = 10000000 , m[4][2]={{-1,0},{0,-1},{0,1},{1,0}}; ///上 左 右 下
  62. int H = 23,W = 39;
  63. int pit_base = 1 , pit_range = 1; ///最少幾個洞,範圍
  64. int pc_speed = 80; ///電腦移速,越大越快
  65. char p[HMAX][WMAX];
  66. int step[HMAX][WMAX];
  67. int from[HMAX][WMAX];
  68. deque<int> way;
  69. deque<PII> Q;
  70. int minstep_solve , minstep_blood;
  71. const int BLCOL = 9;
  72. map<PII , int> blood_bag;
  73.  
  74. class DJset
  75. {
  76.     public:
  77.     int par[100000];
  78.     int rank[100000];
  79.     void djinit(int n)
  80.     {
  81.         for(int i=0;i<n;i++)
  82.         {
  83.             par[i]=i;
  84.             rank[i]=0;
  85.         }
  86.     }
  87.     int djfind(int x)
  88.     {
  89.         if(par[x]==x)return x;
  90.         return par[x]=djfind(par[x]);
  91.     }
  92.     void unite(int x,int y)
  93.     {
  94.         x=djfind(x);
  95.         y=djfind(y);
  96.         if(x==y)return;
  97.         if(rank[x]<rank[y])par[x]=y;
  98.         else
  99.         {
  100.             par[y]=x;
  101.             if(rank[x]==rank[y])rank[x]++;
  102.         }
  103.     }
  104.     bool same(int x,int y)
  105.     {
  106.         return djfind(x)==djfind(y);
  107.     }
  108. };
  109.  
  110. class N{
  111. public:
  112.     int x , y , blood , dis , bag;
  113.     N (int x_ , int y_ , int blood_ , int dis_ , int bag_){
  114.         x = x_ , y = y_ , blood = blood_ , dis = dis_ , bag = bag_;
  115.     }
  116. };
  117.  
  118. int goodsolverByblood(map<PII , int> blood , int initblood){
  119.  
  120.     way.clear();
  121.  
  122.     assert(blood.size() <= 6); // too much
  123.  
  124.     const int lim = max(H , W) + 1 , bloodlim = 130;
  125.     PII dis[bloodlim][H + 1][W + 1];
  126.     PII pa[bloodlim][H + 1][W + 1];
  127.     memset(dis , 0x3f3f3f3f , sizeof dis);
  128.  
  129.     int val[20] , j = 0;
  130.     for(map<PII , int>::iterator i = blood.begin() ; i != blood.end() ; i ++ , j ++)
  131.         val[j] = i->b , blood[i->a] = j;
  132.  
  133.     queue<N> qu;
  134.  
  135.     // int x , y , blood , dis , bag;
  136.     qu.push(N(1 , 0 , initblood , 0 , 0));
  137.  
  138.     while(qu.size()){
  139.         N now = qu.front(); qu.pop();
  140.         if(now.blood == 0) continue;
  141.         else {
  142.             if(now.x == H - 2 && now.y == W - 1) break;
  143.  
  144.             for(int i = 0 ; i < 4 ; i ++){
  145.                 int qx = now.x + m[i][0] , qy = now.y + m[i][1];
  146.                 if(qx >= 0 && qx < H && qy >= 0 && qy < W && p[qx][qy] == ' '){
  147.                     int tmpb = 0 , nbag;
  148.                     if(blood.find(mp(qx , qy)) != blood.end()){
  149.                         tmpb = blood[mp(qx , qy)];
  150.                         if(now.bag & (1 << tmpb) != 0) tmpb = 0 , nbag = now.bag;
  151.                         else nbag = now.bag | (1 << tmpb) , tmpb = val[tmpb];
  152.                     }
  153.                     else tmpb = 0 , nbag = now.bag;
  154.  
  155.                     if(dis[nbag][qx][qy].a > now.dis + 1){
  156.                         dis[nbag][qx][qy] = mp(now.dis + 1 , now.blood - 1 + tmpb);
  157.                         pa[nbag][qx][qy] = mp(now.bag , i);
  158.                         qu.push(N(qx , qy , dis[nbag][qx][qy].b , dis[nbag][qx][qy].a , nbag));
  159.                     }
  160.                 }
  161.             }
  162.         }
  163.     }
  164.     int ans = INF , po = -1;
  165.     PII now = mp(H - 2 , W - 1);
  166.     for(int i = 0 ; i < bloodlim ; i++) if(dis[i][H - 2][W - 1].a < ans)
  167.         ans = dis[i][H - 2][W - 1].a , po = i;
  168.  
  169.     if(ans == INF) return -1;
  170.     stack<int> cc;
  171.     while(true){
  172.         PII dir = pa[po][now.a][now.b];
  173.         cc.push(dir.b);
  174.         now.a -= m[dir.b][0] , now.b -= m[dir.b][1];
  175.         po = dir.a;
  176.         if(now == mp(1 , 0)) break;
  177.     }
  178.     while(cc.size()) way.push_back(cc.top()) , cc.pop();
  179.     return 0;
  180. }
  181.  
  182. void solve()
  183. {
  184.     way.clear();
  185.     for(int i=0;i<H;i++)for(int j=0;j<W;j++)step[i][j]=INF;
  186.     PII A=mp(1,1),B;
  187.     step[1][1]=0;
  188.     from[1][1]=0;
  189.     Q.push_back(A);
  190.     while(!Q.empty())
  191.     {
  192.         A=Q.front();Q.pop_front();
  193.         for(int i=0;i<4;i++)
  194.         {
  195.             B=mp(A.a+m[i][0],A.b+m[i][1]);
  196.             if(p[B.a][B.b]==V)continue;
  197.             if(step[A.a][A.b]+1>=step[B.a][B.b])continue;
  198.             from[B.a][B.b]=i;
  199.             step[B.a][B.b]=step[A.a][A.b]+1;
  200.             Q.push_back(B);
  201.         }
  202.     }
  203.     PII now=mp(H-2,W-2);
  204.     while(!(now.a==1&&now.b==1))
  205.     {
  206.         way.push_front(from[now.a][now.b]);
  207.         int w=3-from[now.a][now.b];
  208.         now.a+=m[w][0];
  209.         now.b+=m[w][1];
  210.     }
  211.     way.push_front(2);
  212.     way.push_back(2);
  213. }
  214.  
  215.  
  216.  
  217. ///以下為小工具系列===========================================
  218.  
  219. bool inside(int x,int y)
  220. {
  221.     if(x<0||x>=H)return false;
  222.     if(y<0||y>=W)return false;
  223.     return true;
  224. }
  225.  
  226. void print_box(int x, int y,int col)
  227. {
  228.     SetColor(0,col);
  229.     gotoxy(x+TOP,y*2);
  230.     printf(" ");
  231.     SetColor(15,0);
  232. }
  233.  
  234. void print_Nbox(int x, int y, int n, int col)
  235. {
  236.     SetColor(15,col);
  237.     gotoxy(x+TOP,y*2);
  238.     if(n>=10)printf("%d",n);
  239.     else printf("0%d",n);
  240.     SetColor(15,0);
  241. }
  242.  
  243. void show_bar()
  244. {
  245.     printf("===================================迷宮===================================\n");
  246. }
  247.  
  248. void show_maze()
  249. {
  250.     for(int i=0;i<H;i++)
  251.     {
  252.         for(int j=0;j<W;j++)
  253.         {
  254.             if(p[i][j]==U)print_box(i,j,BCOL);
  255.             if(p[i][j]==V)print_box(i,j,WCOL);
  256.         }
  257.         printf("\n");
  258.     }
  259.     SetColor(15,0);
  260. }
  261.  
  262. void show_info(int s,int b)
  263. {
  264.     gotoxy(H+TOP,0);
  265.     cout << "                                   " << endl;
  266.     gotoxy(H+TOP,0);
  267.     //assert(b >= 10);
  268.     if(b!=0)printf("當前血量: %d\n",b);
  269.     printf("使用步數: %d",s);
  270. }
  271.  
  272. void setHW(int _a,int _b,int _M)
  273. {
  274.     H=rand()%_M+_a,W=rand()%_M+_b;
  275.     if(!(H%2))H++;
  276.     if(!(W%2))W++;
  277. }
  278.  
  279. int decide(int x,int y,int w)
  280. {
  281.     vector<PII> v;
  282.     int xx,yy;
  283.     for(int i=0;i<4;i++)
  284.     {
  285.         if(i == 3 - w) continue;
  286.         xx=x+m[i][0],yy=y+m[i][1];
  287.         if(xx >= 0 && xx < H && yy >= 0 && yy < W && p[xx][yy]==U)
  288.             v.push_back(mp(xx + yy , i));
  289.     }
  290.     if(v.empty()) return 3 - w;
  291.     else {
  292.         sort(v.begin() , v.end());
  293.         int tmp = rand() % (int)v.size();
  294.         if(tmp != 0 && rand() % 11 < 1) tmp --;
  295.         return v[tmp].b;
  296.     }
  297. }
  298.  
  299. void wait_for_enter()
  300. {
  301.     while(true)
  302.     {
  303.         if(kbhit())
  304.         {
  305.             char c=getch();
  306.             if(c==13)return;
  307.         }
  308.     }
  309. }
  310.  
  311. void finish_game(int winner,int s)
  312. {
  313.     clrscr();
  314.     show_bar();
  315.     if(winner==0)
  316.     {
  317.         puts("\n\n\t\t\tThank you for playing");
  318.         printf("\n\n\t\t\t銘謝惠顧 你輸了");
  319.         puts("\n\n\t\t\t遊戲結束\n\n");
  320.         wait_for_enter();
  321.         return;
  322.     }
  323.     if(winner==1)printf("\n\t\tYOU WIN :)\n");
  324.     if(winner==2)printf("\n\t\tPC WINS :P\n");
  325.     if(s!=-1)printf("\n\n\t\t\t您一共花了%d步\n",s);
  326.     if(s==way.size())SetColor(2,0),puts("\n\n\t\t\t你使用了最短路徑!!"),SetColor(15,0);
  327.     else SetColor(15,0),printf("\n\n\t\t\t這不是最短路徑(%d步)...\n",way.size()),SetColor(15,0);
  328.     puts("\n\n\t\t\tThank you for playing");
  329.     puts("\n\n\t\t\t遊戲結束\n\n");
  330.     puts("\n\n\t\t\tPRESS ENTER TO CONTINUE\n\n");
  331.     wait_for_enter();
  332. }
  333.  
  334. ///生成===========================================
  335.  
  336. void prim_build(bool anim)
  337. {
  338.     if(anim)show_maze();
  339.     vector<PII> wall;
  340.     for(int i=0;i<H;i++)for(int j=0;j<W;j++)p[i][j]=V;
  341.     p[1][1]=U;
  342.     wall.push_back(make_pair(1,2));
  343.     wall.push_back(make_pair(2,1));
  344.     while(wall.size())
  345.     {
  346.         if(anim)Sleep(SDLAY);
  347.         int randpos=rand()%wall.size();
  348.         PII now=wall[randpos];
  349.         wall.erase(wall.begin()+randpos);
  350.         int count=0;
  351.         for(int i=0;i<4;i++)if(p[now.a+m[i][0]][now.b+m[i][1]]==U)++count;
  352.         if(count>1)continue;
  353.         p[now.a][now.b]=U;
  354.         if(anim)print_box(now.a,now.b,BCOL);
  355.         for(int i=0;i<4;i++)
  356.         {
  357.             int newx=now.a+m[i][0],newy=now.b+m[i][1];
  358.             if(p[newx][newy]==U)continue;
  359.             if(newx>=(H-1)||newx<1)continue;
  360.             if(newy>=(W-1)||newy<1)continue;
  361.             wall.push_back(make_pair(newx,newy));
  362.         }
  363.     }
  364.     p[1][0]=U;p[1][1]=U;p[H-2][W-1]=U;p[H-2][W-2]=U;
  365.     if(anim)Sleep(SDLAY);
  366.     if(anim)print_box(1,0,BCOL),print_box(1,1,BCOL),print_box(H-2,W-1,BCOL),print_box(H-2,W-2,BCOL);
  367.     solve() , minstep_solve = way.size();
  368. }
  369.  
  370. void kruskal_build(bool anim)
  371. {
  372.     if(anim)show_maze();
  373.     DJset djset;
  374.     djset.djinit(H*W);
  375.     vector<PII> wall;
  376.     for(int i=0;i<H;i++)
  377.     {
  378.         for(int j=0;j<W;j++)
  379.         {
  380.             if(anim)Sleep(SDLAY);
  381.             if(i%2==1&&j%2==1)
  382.             {
  383.                 p[i][j]=U;
  384.                 if(anim)print_box(i,j,BCOL);
  385.             }
  386.             else
  387.             {
  388.                 //p[i][j]=V;
  389.                 if(i==0||i==(H-1))continue;
  390.                 if(j==0||j==(W-1))continue;
  391.                 if((i%2)==(j%2))continue;
  392.                 wall.push_back(make_pair(i,j));
  393.             }
  394.         }
  395.     }
  396.     while(wall.size())
  397.     {
  398.         if(anim)Sleep(SDLAY);
  399.         int randpos=rand()%wall.size();
  400.         PII now=wall[randpos];
  401.         wall.erase(wall.begin()+randpos);
  402.         for(int i=0;i<4;i++)
  403.         {
  404.             int ax=now.a+m[i][0],ay=now.b+m[i][1];
  405.             int bx=now.a-m[i][0],by=now.b-m[i][1];
  406.             if(p[now.a][now.b]==U)continue;
  407.             if(p[ax][ay]==V)continue;
  408.             int PA=ax*W+ay,PB=bx*W+by;
  409.             if(djset.same(PA,PB))continue;
  410.             else
  411.             {
  412.                 djset.unite(PA,PB);
  413.                 p[now.a][now.b]=U;
  414.                 if(anim)print_box(now.a,now.b,BCOL);
  415.             }
  416.         }
  417.     }
  418.     p[1][0]=U;p[1][1]=U;p[H-2][W-1]=U;p[H-2][W-2]=U;
  419.     if(anim)Sleep(SDLAY);
  420.     if(anim)print_box(1,0,BCOL),print_box(1,1,BCOL),print_box(H-2,W-1,BCOL),print_box(H-2,W-2,BCOL);
  421.     solve() , minstep_solve = way.size();
  422. }
  423.  
  424. void haha_build(bool anim, int hole){
  425.     if(anim)show_maze();
  426.     typedef pair<PII , PII> PIIII;
  427.     priority_queue<PIIII> qu , ho;
  428.     for(int i = 1 ; i < H - 1 ; i ++){
  429.         for(int j = 1 ; j < W - 1 ; j ++){
  430.             p[i][j] = U;
  431.             if(anim){
  432.                 print_box(i,j,BCOL);
  433.                 Sleep(SDLAY);
  434.             }
  435.         }
  436.     }
  437.  
  438.     for(int i = 2 ; i < W - 1 ; i += 2)
  439.         qu.push(mp4(rand() , 3 , 0 , i)) , qu.push(mp4(rand() , 0 , H - 1 , i));
  440.     for(int i = 2 ; i < H - 1 ; i += 2)
  441.         qu.push(mp4(rand() , 2 , i , 0)) , qu.push(mp4(rand() , 1 , i , W - 1));
  442.  
  443.     while(qu.size()){
  444.         int dir = qu.top().a.b , ran = qu.top().a.a;
  445.         PII now = qu.top().b , ld = mp(m[dir][0] , m[dir][1]) , rd = mp(m[dir][0] , m[dir][1]);
  446.         qu.pop();
  447.  
  448.         ld.a += (dir == 1 || dir == 2) ? -1 : 0;
  449.         ld.b += (dir == 0 || dir == 3) ? -1 : 0;
  450.         rd.a += (dir == 1 || dir == 2) ? 1 : 0;
  451.         rd.b += (dir == 0 || dir == 3) ? 1 : 0;
  452.  
  453.         int x = now.a + m[dir][0] , y = now.b + m[dir][1];
  454.         int lx = x + ld.a , ly = y + ld.b;
  455.         int rx = x + rd.a , ry = y + rd.b;
  456.         int qx = now.a + m[dir][0] * 2 , qy = now.b + m[dir][1] * 2;
  457.  
  458.         if(p[x][y] == U && p[qx][qy] == U && p[lx][ly] == U && p[rx][ry] == U){
  459.  
  460.             if(x % 2 == 0 && y % 2 == 0){
  461.                 for(int i = 0 ; i < 4 ; i ++)
  462.                     qu.push(mp4(rand() , i , x , y));
  463.             }
  464.             else{
  465.                 qu.push(mp4(ran , dir , x , y));
  466.                 ho.push(mp4(rand() , -1 , x , y));
  467.             }
  468.             p[x][y] = V;
  469.             if(anim){
  470.                 print_box(x,y,WCOL);
  471.                 Sleep(SDLAY);
  472.             }
  473.         }
  474.     }
  475.     p[1][0] = U; p[1][1] = U; p[H - 2][W - 1] = U; p[H - 2][W - 2] = U;
  476.     if(anim)Sleep(SDLAY);
  477.     if(anim)print_box(1,0,BCOL),print_box(1,1,BCOL),print_box(H-2,W-1,BCOL),print_box(H-2,W-2,BCOL);
  478.     while(ho.size() && hole > 0) hole -- , p[ho.top().b.a][ho.top().b.b] = U , ho.pop();
  479.     solve() , minstep_solve = way.size();
  480. }
  481.  
  482. void set_blood_bag(int sum){
  483.     do
  484.     {
  485.         blood_bag.clear();
  486.         while(sum --){
  487.             int x = rand() % H , y = rand() % W;
  488.             while(p[x][y] == V || blood_bag.find(mp(x , y)) != blood_bag.end())
  489.                 x = rand() % H , y = rand() % W;
  490.             blood_bag[mp(x , y)] = rand()%20+20+1;
  491.         }
  492.     }while(goodsolverByblood(blood_bag, BLOOD)==-1);
  493.     minstep_blood = way.size();
  494. }
  495.  
  496. void show_blood_bag()
  497. {
  498.     for(int i=0;i<H;i++)
  499.     {
  500.         for(int j=0;j<W;j++)
  501.         {
  502.             if(blood_bag.find(mp(i,j))!=blood_bag.end())print_Nbox(i,j,blood_bag[mp(i,j)],BLCOL);
  503.         }
  504.     }
  505. }
  506.  
  507. ///以下為遊戲模式系列===========================================
  508.  
  509. void user_play()
  510. {
  511.     int nx=TOP,ny=0,s=0;
  512.     hide();
  513.     clrscr();
  514.     show_bar();
  515.     show_maze();
  516.     show_info(s,0);
  517.     print_box(nx,ny,SCOL);
  518.     while(!(nx==(H-2)&&ny==(W-1)))
  519.     {
  520.         if(kbhit())
  521.         {
  522.             int w=getkey();
  523.             if(w==-1)return;
  524.             if(nx+m[w][0] >= 0 && nx+m[w][0] < H && ny+m[w][1] >= 0 && ny+m[w][1] < W &&
  525.                 p[nx+m[w][0]][ny+m[w][1]]==U)
  526.             {
  527.                 print_box(nx,ny,BCOL);
  528.                 nx+=m[w][0],ny+=m[w][1];
  529.                 print_box(nx,ny,SCOL);
  530.                 show_info(++s,0);
  531.             }
  532.         }
  533.     }
  534.     gotoxy(H+1,0);
  535.     finish_game(1,s);
  536. }
  537.  
  538. void pc_play()
  539. {
  540.     int nx=TOP,ny=0,s=0;
  541.     hide();
  542.     clrscr();
  543.     show_bar();
  544.     show_maze();
  545.     show_info(s,0);
  546.     print_box(nx,ny,SCOL);
  547.     while(!(nx==(H-2)&&ny==(W-1)))
  548.     {
  549.         Sleep(200-pc_speed*2);
  550.         int w=way.front();way.pop_front();
  551.         print_box(nx,ny,BCOL);
  552.         nx+=m[w][0],ny+=m[w][1];
  553.         print_box(nx,ny,SCOL);
  554.         show_info(++s,0);
  555.         SetColor(15,0);
  556.     }
  557.     gotoxy(H+1,0);
  558.     finish_game(2,s);
  559. }
  560.  
  561. void user_vs_pc()
  562. {
  563.     int nx=TOP,ny=0,mx=H-2,my=W-1,s=0;
  564.     hide();
  565.     clrscr();
  566.     show_bar();
  567.     show_maze();
  568.     print_box(nx,ny,SCOL);
  569.     print_box(mx,my,MCOL);
  570.     while(true)
  571.     {
  572.         int z=-1;
  573.         if(nx==(H-2)&&ny==(W-1))break;
  574.         if(mx==1&&my==0)break;
  575.         if(kbhit())
  576.         {
  577.             int w=getkey();
  578.             if(w==-1)return;
  579.             if(nx+m[w][0] >= 0 && nx+m[w][0] < H && ny+m[w][1] >= 0 && ny+m[w][1] < W &&
  580.                 p[nx+m[w][0]][ny+m[w][1]]==U)
  581.             {
  582.                 print_box(mx,my,BCOL);
  583.                 int z=3-way.back();way.pop_back();
  584.                 mx+=m[z][0],my+=m[z][1];
  585.                 print_box(mx,my,MCOL);
  586.                 print_box(nx,ny,BCOL);
  587.                 nx+=m[w][0],ny+=m[w][1];
  588.                 print_box(nx,ny,SCOL);
  589.                 show_info(++s,0);
  590.             }
  591.         }
  592.     }
  593.     if(nx==(H-2)&&ny==(W-1))finish_game(1,s);
  594.     else finish_game(2,s);
  595. }
  596.  
  597. void random_go()
  598. {
  599.     int nx=TOP,ny=0,mx=H-2,my=W-2,z=-1,s=0;
  600.     hide();
  601.     clrscr();
  602.     show_bar();
  603.     show_maze();
  604.     print_box(nx,ny,SCOL);
  605.     print_box(mx,my,MCOL);
  606.     while(true)
  607.     {
  608.         if(nx==(H-2)&&ny==(W-1))break;
  609.         if(kbhit())
  610.         {
  611.             int w=getkey();
  612.             if(w==-1)return;
  613.             if(nx+m[w][0] >= 0 && nx+m[w][0] < H && ny+m[w][1] >= 0 && ny+m[w][1] < W &&
  614.                 p[nx+m[w][0]][ny+m[w][1]]==U)
  615.             {
  616.                 if((nx+m[w][0]==mx)&&(ny+m[w][1])==my)continue;
  617.                 print_box(nx,ny,BCOL);
  618.                 nx+=m[w][0],ny+=m[w][1];
  619.                 print_box(nx,ny,SCOL);
  620.                 print_box(mx,my,BCOL);
  621.                 z=decide(mx,my,z);
  622.                 mx+=m[z][0],my+=m[z][1];
  623.                 print_box(mx,my,MCOL);
  624.                 show_info(++s,0);
  625.             }
  626.         }
  627.     }
  628.     finish_game(1,s);
  629. }
  630.  
  631. void user_survive()
  632. {
  633.     int nx=TOP,ny=0,s=0,b=BLOOD;
  634.     map<PII,int> BB=blood_bag;
  635.     hide();
  636.     clrscr();
  637.     show_bar();
  638.     show_maze();
  639.     show_blood_bag();
  640.     show_info(s,b);
  641.     print_box(nx,ny,SCOL);
  642.     while(!(nx==(H-2)&&ny==(W-1)))
  643.     {
  644.         if(kbhit())
  645.         {
  646.             int w=getkey();
  647.             if(w==-1)return;
  648.             if(p[nx+m[w][0]][ny+m[w][1]]==U)
  649.             {
  650.                 print_box(nx,ny,BCOL);
  651.                 nx+=m[w][0],ny+=m[w][1];
  652.                 if(BB.find(mp(nx,ny))!=BB.end())b+=BB[mp(nx,ny)],BB[mp(nx,ny)]=0;
  653.                 print_box(nx,ny,SCOL);
  654.                 if(b<=1){finish_game(0,s);return;}
  655.                 show_info(++s,--b);
  656.             }
  657.         }
  658.     }
  659.     gotoxy(H+1,0);
  660.     finish_game(1,s);
  661. }
  662.  
  663. void pc_survive()
  664. {
  665.     int nx=TOP,ny=0,s=0,b=BLOOD;
  666.     deque<int> ddq = way;
  667.     map<PII,int> BB=blood_bag;
  668.     hide();
  669.     clrscr();
  670.     show_bar();
  671.     show_maze();
  672.     show_blood_bag();
  673.     show_info(s,b);
  674.     print_box(nx,ny,SCOL);
  675.     while(!(nx==(H-2)&&ny==(W-1)))
  676.     {
  677.         Sleep(200-pc_speed*2);
  678.         int w=way.front();way.pop_front();
  679.         print_box(nx,ny,BCOL);
  680.         nx+=m[w][0],ny+=m[w][1];
  681.         if(BB.find(mp(nx,ny))!=BB.end())b+=BB[mp(nx,ny)],BB[mp(nx,ny)]=0;
  682.         print_box(nx,ny,SCOL);
  683.         show_info(++s,--b);
  684.         SetColor(15,0);
  685.     }
  686.     way = ddq;
  687.     gotoxy(H+1,0);
  688.     finish_game(2,s);
  689. }
  690.  
  691. ///以下為選單系列===========================================
  692.  
  693. void spawn()
  694. {
  695.     clrscr();
  696.     int chs;
  697.     show_bar();
  698.     printf("\t1. Prim生成\n");
  699.     printf("\t2. Kruskal生成\n");
  700.     printf("\t3. 呵呵生成\n");
  701.     printf("\t4. BACK\n");
  702.     printf("\n");
  703.     printf("\t->");
  704.     scanf("%d",&chs);
  705.     if(chs==4)return;
  706.     for(int i=0;i<H;i++)for(int j=0;j<W;j++)p[i][j]=V;
  707.     if(chs==1)prim_build(true);
  708.     if(chs==2)kruskal_build(true);
  709.     if(chs==3)haha_build(true,0);
  710.     set_blood_bag(BAG);
  711.     gotoxy(H+TOP,0);
  712.     system("PAUSE");
  713. }
  714.  
  715. void menu()
  716. {
  717.     for(int i=0;i<H;i++)for(int j=0;j<W;j++)p[i][j]=V;
  718.     kruskal_build(false);
  719.     set_blood_bag(BAG);
  720.     while(true)
  721.     {
  722.         show();
  723.         clrscr();
  724.         int chs;
  725.         show_bar();
  726.         printf("\t0. < SPAWN > 迷宮生成\n");
  727.         printf("\t1. <U S E R> 鍵盤走迷宮\n");
  728.         printf("\t2. < P C > 電腦走迷宮\n");
  729.         printf("\t3. < FIGHT > 與電腦競賽\n");
  730.         printf("\t4. < CRAZY > 與亂走電腦競賽\n");
  731.         printf("\t5. <SURVIVE> 生存模式\n");
  732.         printf("\t6. < PCSUR > 電腦生存\n");
  733.         printf("\t7  < RESET > 血包重置\n");
  734.         printf("\t8. < E N D > 離開\n");
  735.         printf("\n\t\t Type What You Need:  ");
  736.         scanf("%d",&chs);
  737.         if(chs==0)spawn();
  738.         if(chs==1)user_play();
  739.         if(chs==2)pc_play();
  740.         if(chs==3)user_vs_pc();
  741.         if(chs==4)random_go();
  742.         if(chs==5)user_survive();
  743.         if(chs==6)pc_survive();
  744.         if(chs==7)set_blood_bag(BAG);
  745.         if(chs==8)return;
  746.     }
  747. }
  748.  
  749. int main()
  750. {
  751.     srand(time(NULL));
  752.     menu();
  753.     return 0;
  754. }
Add Comment
Please, Sign In to add comment