zoukankan      html  css  js  c++  java
  • 数对游戏

    要求是编写一个“找一对数字”的游戏,具体的output如下图:

    其他小的细节就是,如果没找到一对,那么就要重新盖住翻开的数字;比如上图的6和3,他们不是一对,那么在下一次找的时候他们都恢复到“*”的状态。 

    如果找到了一对,那么就让这一对数学一直在后面的游戏里面显示出来。

    当全部的“对子”都找到以后,询问玩家还要不要玩,如果要的话,就再重新随机16个数字,然后继续以上的步骤。

    实现没问题,但是有个细节逻辑没处理好。

    View Code
      1 #include<iostream>
    2 #include<ctime>
    3 #include<cstring>
    4 #include<cstdlib>
    5 using namespace std;
    6
    7 int arr[4][4];
    8 bool visible[4][4];
    9 int num;
    10
    11
    12 void init()
    13 {
    14 memset(visible,false,sizeof(visible));
    15 num=0;
    16 // arr[4][4]={{1,1,2,2},{3,3,4,4},{5,5,6,6},{7,7,8,8}};
    17 int value[16]={1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8};
    18 int index=0;
    19 for(int i=0;i<4;i++)
    20 for(int j=0;j<4;j++)
    21 arr[i][j]=value[index++];
    22 srand(time(0));
    23 for(int k=0;k<8;k++)
    24 {
    25 int i=rand()%4;
    26 int j=rand()%4;
    27 if(i!=j) //交换的两个的引用是一样的情况下会导致错误0.
    28 {
    29 arr[i][j]^=arr[j][i];
    30 arr[j][i]^=arr[i][j];
    31 arr[i][j]^=arr[j][i];
    32 }
    33 }
    34 }
    35
    36 void show()
    37 {
    38 cout<<" 1 2 3 4 "<<endl;
    39 cout<<" ============ "<<endl;
    40 for(int i=0;i<4;i++)
    41 {
    42 cout<<i+1<<" |";
    43 for(int j=0;j<4;j++)
    44 {
    45 if(visible[i][j]==false)
    46 cout<<" * ";
    47 else
    48 cout<<""<<arr[i][j]<<"";
    49 }
    50 cout<<"|"<<endl;
    51 }
    52 cout<<" ============ "<<endl;
    53 }
    54
    55 bool flip()
    56 {
    57 if(num==8)
    58 {
    59 cout<<"You Win!"<<endl;
    60 cout<<"Do you want a resume game?(y/n)"<<endl;
    61 char c;
    62 cin>>c;
    63 if(c=='y')
    64 return true;
    65 else
    66 return false;
    67 }
    68 int x1,y1,x2,y2;
    69 cout<<"Enter an X and Y position of the first card to flip."<<endl;
    70 cin>>x1>>y1;
    71 cout<<"Enter an X and Y position of the second card to flip."<<endl;
    72 cin>>x2>>y2;
    73 if(!(x1<5&&x1>0&&y1<5&&y1>0&&x2<5&&x2>0&&y2>0&&y2<5))
    74 {
    75 cout<<"输入数据有误,请重新输出!"<<endl;
    76 show();
    77 flip();
    78 }
    79 else
    80 {
    81 if(visible[x1-1][y1-1]||visible[x2-1][y2-1])
    82 {
    83 cout<<"输入的牌已经被翻开,请重新输入!"<<endl;
    84 show();
    85 flip();
    86 }
    87 else
    88 {
    89 visible[x1-1][y1-1]=true;
    90 visible[x2-1][y2-1]=true;
    91 if(arr[x1-1][y1-1]==arr[x2-1][y2-1])
    92 {
    93 num++;
    94 show();
    95 flip();
    96 }
    97 else
    98 {
    99 show();
    100 visible[x1-1][y1-1]=false;
    101 visible[x2-1][y2-1]=false;
    102 cout<<"猜错了,重新猜!"<<endl;
    103 show();
    104 flip();
    105 }
    106 }
    107 }
    108 }
    109
    110 int main()
    111 {
    112 cout<<"Find all the maching pairs on the board."<<endl;
    113 init();
    114 show();
    115 while(flip())
    116 {
    117 cout<<"Find all the maching pairs on the board."<<endl;
    118 init();
    119 show();
    120 }
    121 return 0;
    122 }
  • 相关阅读:
    用二重循环打印图形--------矩形 三角形 菱形
    数组的应用
    柳暗花明又一村的———for循环
    E-PUCK机器人-开始
    E-PUCK机器人-软件
    E-PUCK机器人-电池使用
    E-PUCK机器人-硬件
    E-PUCK机器人-FAQ
    E-PUCK机器人-Tiny Bootloader和其他开发工具
    E-PUCK机器人-例子
  • 原文地址:https://www.cnblogs.com/YipWingTim/p/2240562.html
Copyright © 2011-2022 走看看