-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcpp_python.py
71 lines (55 loc) · 1.69 KB
/
cpp_python.py
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
from time import time, sleep
import random
from ctypes import cdll
from ctypes import c_uint
import numpy as np
from constants import CARDS, CARDS_REVERSE
# C++ library for heavy computations
# 65,000,000 H/s C++ -O3 -m64
# 20,000,000 H/s C++ -O3 (no errors found with O3)
# 15,000,000 H/s C++
# 125,000 H/s Python calling C++
lib = cdll.LoadLibrary("main.dll")
lib.HandEval_evaluate.restype = c_uint
lib.main_main()
hands = []
deck_ = list(range(0, 52))
for i in range(100000):
hands.append(random.sample(deck_, 7))
hero_h = [CARDS['As'], CARDS['Ts']]
villain_h = [CARDS['3s'], CARDS['3c']]
results = []
def reverse_cards(board):
s = ""
for card in board:
s += CARDS_REVERSE[card] + " "
return s
def simulate():
deck = list(range(0, 52))
dead_cards = hero_h + villain_h
np.random.shuffle(deck)
board = []
for j in range(5):
card = deck[j]
x = j + 5
while card in dead_cards:
card = deck[x]
x += 5
board.append(card)
hero_score = lib.HandEval_evaluate(hero_h[0], hero_h[1], board[0], board[1], board[2], board[3], board[4])
villain_score = lib.HandEval_evaluate(villain_h[0], villain_h[1], board[0], board[1], board[2], board[3], board[4])
if hero_score > villain_score:
results.append(1)
elif hero_score < villain_score:
results.append(2)
else:
results.append(0)
z = 0
runtime = time()
while z < 100000:
simulate()
z += 1
print(time() - runtime)
print(z / (time() - runtime), "hands per second")
print(100 * (results.count(1) + (results.count(0) / 2)) / len(results), "%")
print(100 * (results.count(2) + (results.count(0) / 2)) / len(results), "%")