查表胡牌判断算法 c++

根据 日文胡牌算法资料 JAVA 此篇博文资料 改写的C++ 代码

tbl.h

复制代码
1 #pragma once
2 #include <map>
3 #include <vector>
4 
5 void init1(std::map<int, std::vector<int>>& tbl);
6 void init2(std::map<int, std::vector<int>>& tbl);
7 void init3(std::map<int, std::vector<int>>& tbl);
8 void init4(std::map<int, std::vector<int>>& tbl);
View Code
复制代码

main.cpp

复制代码
  1 // 1111.cpp: 定义控制台应用程序的入口点。
  2 //
  3 
  4 #include "stdafx.h"
  5 #include <map>
  6 #include <vector>
  7 #include <iostream>
  8 #include <thread>
  9 #include <chrono>  
 10 #include "tbl.h"
 11 
 12 using namespace std;
 13 
 14 //===============================
 15 const  int MAN = 0;
 16 const  int MAN1 = 1;
 17 const  int MAN2 = 2;
 18 const  int MAN3 = 3;
 19 const  int MAN4 = 4;
 20 const  int MAN5 = 5;
 21 const  int MAN6 = 6;
 22 const  int MAN7 = 7;
 23 const  int MAN8 = 8;
 24 const  int MAN9 = 9;
 25 const  int PIN = 10;
 26 const  int PIN1 = 11;
 27 const  int PIN2 = 12;
 28 const  int PIN3 = 13;
 29 const  int PIN4 = 14;
 30 const  int PIN5 = 15;
 31 const  int PIN6 = 16;
 32 const  int PIN7 = 17;
 33 const  int PIN8 = 18;
 34 const  int PIN9 = 19;
 35 const  int SOU = 20;
 36 const  int SOU1 = 21;
 37 const  int SOU2 = 22;
 38 const  int SOU3 = 23;
 39 const  int SOU4 = 24;
 40 const  int SOU5 = 25;
 41 const  int SOU6 = 26;
 42 const  int SOU7 = 27;
 43 const  int SOU8 = 28;
 44 const  int SOU9 = 29;
 45 const  int TON = 31;
 46 const  int NANF = 33;
 47 const  int SHA = 35;
 48 const  int PEI = 37;
 49 const  int HAK = 39;
 50 const  int HAT = 41;
 51 const  int CHU = 43;
 52 //==========================
 53 
 54 std::map<int, std::vector<int>> tbl;
 55 
 56 //===========================================
 57     
 58 
 59 void Init()
 60 {
 61     init1(tbl);
 62     init2(tbl);
 63     init3(tbl);
 64     init4(tbl);
 65 }
 66 
 67 void InitFirst() {
 68     Init();
 69 }
 70 
 71 std::vector<int> analyse(std::vector<int> hai)
 72 {
 73     std::vector<int> vec(44,0);
 74     for (int i : hai) {
 75         vec[i]++;
 76     }
 77     return vec;
 78 }
 79 
 80 int calc_key(std::vector<int>& n, std::vector<int>& pos)
 81 {
 82     int p = -1;
 83     int x = 0;
 84     int pos_p = 0; // posの配列インデックス
 85     bool b = false; // ひとつ前が0以外
 86                        // 数牌
 87                        //循环两次
 88     for (int i = 0; i < 3; i++) {
 89         for (int j = 0; j < 9; j++) {
 90             if (n[i * 9 + j] == 0) {
 91                 if (b) {
 92                     b = false;
 93                     x |= 0x1 << p;
 94                     p++;
 95                 }
 96             }
 97             else {
 98                 p++;
 99                 b = true;
100                 pos[pos_p++] = i * 9 + j;
101                 switch (n[i * 9 + j]) {
102                 case 2:
103                     x |= 0x3 << p;
104                     p += 2;
105                     break;
106                 case 3:
107                     x |= 0xF << p;
108                     p += 4;
109                     break;
110                 case 4:
111                     x |= 0x3F << p;
112                     p += 6;
113                     break;
114                 }
115             }
116         }
117         if (b) {
118             b = false;
119             x |= 0x1 << p;
120             p++;
121         }
122     }
123     // 字牌
124     for (int i = TON; i <= CHU; i++) {
125         if (n[i] > 0) {
126             p++;
127             pos[pos_p++] = i;
128             switch (n[i]) {
129             case 2:
130                 x |= 0x3 << p;
131                 p += 2;
132                 break;
133             case 3:
134                 x |= 0xF << p;
135                 p += 4;
136                 break;
137             case 4:
138                 x |= 0x3F << p;
139                 p += 6;
140                 break;
141             }
142             x |= 0x1 << p;
143             p++;
144         }
145     }
146     return x;
147 }
148 
149 std::vector<int> agari(int key) {
150     return tbl[key];
151 }
152 
153 
154 int main()
155 {
156     InitFirst();
157     std::vector<int> hai1 = {
158         MAN1, MAN1, MAN1,
159         MAN2, MAN2, MAN2,
160         MAN3, MAN3, MAN3,
161         TON, TON, TON,
162         SHA, SHA };
163 
164     std::vector<int> n ;
165     std::vector<int> pos(14,0);
166     std::vector<int> ret;
167 
168     auto start = chrono::system_clock::now();
169 
170     //for (unsigned long i = 0; i < 10000000*4; i++)
171      {
172         n = analyse(hai1);
173         int key = calc_key(n, pos);
174         //std::cout << "key :" << key << std::endl;
175         //std::cout << "key :" <<hex << "0x"<< key << std::endl;
176         ret = agari(key);
177         if (ret.empty()) {
178             //std::cout << "Non hu";
179         }
180         else {
181             //std::cout << "ret :" << ret[0] << std::endl;
182             //std::cout << "ret :" << hex << "0x" << ret[0] << std::endl;
183         }
184         
185     }
186 
187      auto end = chrono::system_clock::now();
188      auto duration = chrono::duration_cast<chrono::microseconds>(end - start);
189      std::cout << "spent " << double(duration.count()) * chrono::microseconds::period::num /
190          chrono::microseconds::period::den << " second" << std::endl;
191      //return 0;
192 
193 
194      for (int r : ret) {
195          std::cout << ("quetou=");
196          std::cout << (pos[(r >> 6) & 0xF]);
197          std::cout << std::endl;
198          int num_kotsu = r & 0x7;
199          int num_shuntsu = (r >> 3) & 0x7;
200          for (int i = 0; i < num_kotsu; i++) {
201              std::cout << ("kezi=");
202              std::cout << (pos[(r >> (10 + i * 4)) & 0xF]);
203          }
204          std::cout << std::endl;
205          for (int i = 0; i < num_shuntsu; i++) {
206              std::cout << ("shunzi=");
207              std::cout << (pos[(r >> (10 + num_kotsu * 4 + i * 4)) & 0xF]);
208          }
209          std::cout << std::endl;
210      }
211      std::cout << std::endl;
212 
213 
214     return 0;
215 }
View Code
复制代码

tbl.cpp 表比较多 文件较大

  

 运行效果如图

spent 0 second
quetou=35
kezi=1kezi=2kezi=3kezi=31

quetou=35
kezi=31
shunzi=1shunzi=1shunzi=1

请按任意键继续. . .

 

posted on   itdef  阅读(1562)  评论(0编辑  收藏  举报

编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
· 25岁的心里话
历史上的今天:
2016-08-26 设计模式之模板模式 template

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

点击右上角即可分享
微信分享提示