2020年4月28日 星期二

樂透猜

回應網友的程式貼文,在這留一份




#include <iostream>
#include <chrono>
#include <random>
#include <map>
#include <vector>
using namespace std;
class Lotto
{
  union 
  {
    int n[49]{ 0 };
    int nn[7][7];
  };  
  // 洗牌
  void Shuffle()
  {
    // obtain a seed from the system clock:
    unsigned seed = std::chrono::system_clock::now().time_since_epoch().count();
    std::default_random_engine generator(seed);
    uniform_int_distribution<int> dis(0, 48);
    for (int &p : n)
    {
      size_t Index = dis(generator);
      int tmp = p;
      p = n[Index];
      n[Index] = tmp;
    }
  }
  void Show() const
  {
    cout << endl << "開出來的號碼是..." << endl;
    cout << "  1 2 3 4 5 6 7";
    vector<char*> Z{"0","1","2","3","4","5","6","7" };
    for (int i = 0; i < 7; ++i)
    {      
      cout << endl << Z[i+1];
      for (int j = 0; j < 7; ++j)
      {
        cout << ' ';
        if (nn[i][j] == 1)
          cout << ((i + 1) * 10 + j + 1);
        else
          cout << "__";
      }
    }
    cout << endl << endl;
  }
  void Run()
  {
    Shuffle();
    Show();
  }
  void Match(map<int,int> &Num)
  {    
    vector<int> N;
    for (int i = 0; i < 7; ++i)
    {
      for (int j = 0; j < 7; ++j)
      {
        if (nn[i][j] == 1)
        {
          int d = ((i + 1) * 10 + j + 1);
          if (Num[d] == d)
            N.push_back(d);
        }
      }
    }
    if (N.size() > 0)
    {
      cout << "恭喜中了 " << N.size() << " 個號碼:";
      for (int n : N)
        cout << n << " ";
      cout << endl;
    }
    else
      cout << "沒中獎,繼續加油" << endl;
  }
  // Constructor
  Lotto()
  {
    n[0] = n[1] = n[2] = n[3] = n[4] = n[5] = 1;
  }
public:
  static void Play(map<int, int> &Num)
  {
    Lotto MyLotto;
    MyLotto.Run();
    MyLotto.Match(Num);
  }
};
class Guess
{
  map<int, int> Num;
  void Show() const
  {
    cout << endl;
    cout << "  1 2 3 4 5 6 7" << endl;
    cout << "1 11 12 13 14 15 16 17" << endl;
    cout << "2 21 22 23 24 25 26 27" << endl;
    cout << "3 31 32 33 34 35 36 37" << endl;
    cout << "4 41 42 43 44 45 46 47" << endl;
    cout << "5 51 52 53 54 55 56 57" << endl;
    cout << "6 61 62 63 64 65 66 67" << endl;
    cout << "7 71 72 73 74 75 76 77" << endl;
  }
  bool Check(int n)
  {    
    int n1 = n % 10;
    int n2 = n / 10;
    if (n1 > 0 && n1 < 8 && n2 > 0 && n2 < 8)
    {      
      if(Num[n] != n)
        return true;
      else
        cout << "已選擇過了!" << endl;
    }
    else
      cout << "輸入錯誤!" << endl;
    return false;
  }
  void GetNum()
  {
    cout << "請選擇 6 組號碼..." << endl;
    int i = 1;
    while (i < 7)
    {
      int n;
      cout << "請輸入第 " << i << " 組號碼:";
      cin >> n;
      if (Check(n))
      {
        Num[n] = n;
        ++i;
      }
    }
  }
  // Constructor
  Guess()
  {}
public:
  static map<int, int> toGuess()
  {
    Guess MyGuess;
    MyGuess.Show();
    MyGuess.GetNum();
    return std::move(MyGuess.Num);
  }
};
int main()
{
  char Key;
  do
  {
    map<int, int> Num = Guess::toGuess();
    Lotto::Play(Num);
    cout << "<R> 再玩一次,其它結束...";
    cin.ignore(INT_MAX, '\n');
    Key = cin.get();
    cin.ignore(INT_MAX, '\n');
  } while (Key == 'R' || Key == 'r');
  return 0;
}






沒有留言:

張貼留言