@@ -35,7 +35,9 @@ struct Hands
35
35
return sorted
36
36
end
37
37
weights = [64 , 32 , 16 , 8 , 4 , 2 , 1 ]
38
- Hands (hands, folded) = new ([i for i in 1 : size (hands, 1 ) if folded[i * 2 - 1 ] != - 1 ], size (hands, 2 ), hands, Create_Mod_Hands (hands), weights)
38
+ # & The problem is here is the only minus one counts as folded where everything except zero is a
39
+ # & fold
40
+ Hands (hands, folded) = new ([i for i in 1 : size (hands, 1 ) if folded[i * 2 - 1 ] == 0 ], size (hands, 2 ), sort (hands, dims = 2 ), Create_Mod_Hands (hands), weights)
39
41
end
40
42
41
43
# ########################################################
@@ -57,6 +59,16 @@ export High_Card,
57
59
Four_Kind,
58
60
Straight_Flush
59
61
62
+ export Check_High_Card,
63
+ Check_Two_Kind,
64
+ Check_Two_Pairs,
65
+ Check_Three_Kind,
66
+ Check_Straight,
67
+ Check_Flush,
68
+ Check_Full_House,
69
+ Check_Four_Kind,
70
+ Check_Straight_Flush
71
+
60
72
export Cards_To_Hands
61
73
export Game
62
74
export Hands
@@ -73,34 +85,32 @@ include("Checker_Functions.jl")
73
85
# ########################################################
74
86
75
87
function Simulate (game:: Game )
76
- # & Here we have to change the function so that no matter what
77
- # & we will get a player that has folded cannot win.
78
-
79
- # & We have to remove the player from the calculations before it goes to the
80
- # & checker. The question is whether the code can handle a single player.
81
- # & I do not think that should be a problem. If it is we will take that
82
- # & later. The folded cards have already been removed.
83
-
84
88
wins_by_player = zeros (Int, game. players)
85
89
split_by_player = zeros (Float64, game. players)
86
90
which_hands = zeros (Int, game. players, 9 )
87
91
for _ = 1 : game. simulations
88
92
hands = Sample (game:: Game )
89
93
player_winners, player_hand = Hands_Checker (hands)
94
+ # @debug player_winners == [1] || length(player_winners) >= 2 && println(@__LINE__, " player hand " , player_hand)
95
+
90
96
if length (player_winners) == 1
91
97
wins_by_player[player_winners] .+ = 1
92
98
which_hands[player_winners, player_hand] .+ = 1
93
- else
99
+ elseif length (player_winners) >= 2
94
100
players_in_split = 1 / length (player_winners)
95
101
split_by_player[player_winners] .+ = players_in_split
96
102
which_hands[player_winners, player_hand] .+ = 1
103
+ elseif length (player_winners) == 0
104
+ break
105
+ else
106
+ throw (ErrorException)
97
107
end
98
108
end
99
109
return wins_by_player/ game. simulations, split_by_player/ game. simulations, which_hands
100
110
end
101
111
112
+ # & This function can be removed by changing the to vector formulation.
102
113
function Sample (game:: Game )
103
- # & The change should be done in here I think
104
114
j = 0
105
115
sampled_cards = sample (game. pile, game. samples, replace = false )
106
116
cards = copy (game. cards)
@@ -114,6 +124,7 @@ function Sample(game::Game)
114
124
end
115
125
116
126
function Hands_Checker (hands:: Hands )
127
+ isempty (hands. players) && return [], 0
117
128
Check_Straight_Flush (hands:: Hands ) && return Straight_Flush (hands:: Hands ), 1
118
129
Check_Four_Kind (hands:: Hands ) && return Four_Kind (hands:: Hands ), 2
119
130
Check_Full_House (hands:: Hands ) && return Full_House (hands:: Hands ), 3
@@ -264,28 +275,20 @@ function Three_Kind(hands::Hands)
264
275
end
265
276
266
277
function Straight (hands:: Hands )
267
- player_scores = zeros (Int, maximum (hands. players))
278
+ scores = zeros (Int, maximum (hands. players))
279
+
268
280
for player in hands. players
281
+ sorted = unique (sort (hands. sorted[player, :]))
269
282
count = 1
270
- ace_updated_hand = copy (hands. sorted[player, :])
271
- if 13 in ace_updated_hand
272
- push! (ace_updated_hand, 0 )
273
- end
274
- for card = 1 : length (ace_updated_hand) - 1
275
- if ace_updated_hand[card] == ace_updated_hand[card + 1 ] + 1
276
- count += 1
277
- if count == 5
278
- player_scores[player] = copy (ace_updated_hand[card])
279
- end
280
- else
281
- count = 1
282
- end
283
+ 13 in sorted && (sorted = [0 ; sorted] )
284
+ for c in 1 : length (sorted) - 1
285
+ sorted[c] == sorted[c + 1 ] - 1 ? count += 1 : count = 1
286
+ count >= 5 && (scores[player] = sorted[c + 1 ])
283
287
end
284
288
end
285
- best_hand = findmax (player_scores)[1 ]
286
- player_winners = findall (x -> x == best_hand, player_scores)
287
- println (@__LINE__ , " " , player_winners)
288
-
289
+
290
+ best_hand = maximum (scores)
291
+ player_winners = findall (x -> x == best_hand, scores)
289
292
return player_winners
290
293
end
291
294
356
359
357
360
function Four_Kind (hands:: Hands )
358
361
card_weight = [225 , 15 , 1 ]
359
- four_of_a_kind_checker = 0
360
362
player_score = zeros (Int, maximum (hands. players), hands. cards)
361
363
for player in hands. players
362
364
for i = 1 : 13
@@ -365,7 +367,6 @@ function Four_Kind(hands::Hands)
365
367
player_score[player, find_n_kinds] = 10000 * hands. sorted[player, find_n_kinds]
366
368
remaining_cards = setdiff (1 : 7 , find_n_kinds)
367
369
player_score[player, remaining_cards] = card_weight .* hands. sorted[player, remaining_cards]
368
- four_of_a_kind_checker = 1
369
370
end
370
371
end
371
372
end
@@ -377,30 +378,33 @@ function Four_Kind(hands::Hands)
377
378
return player_winners
378
379
end
379
380
380
- function Straight_Flush (hands:: Hands )
381
- add_to_each_card = [0 , 1 , 2 , 3 , 4 , 5 , 6 ]
381
+ function Straight_Flush (hands:: Hands )
382
382
player_score = zeros (Int, maximum (hands. players))
383
383
for player in hands. players
384
- player_hand_original = hands. hands[player, :]
385
- flush, suit, indices = Check_Flush_Calculator (player_hand_original)
386
- if flush
387
- sorted_flush_hand = sort (player_hand_original[indices], rev = true )
388
- straight_adjusted_hand = add_to_each_card[1 : length (indices)] .+ sorted_flush_hand
389
- straight_and_Modulus_adjusted_hand = straight_adjusted_hand .% 13
390
- straight_and_Modulus_adjusted_hand[straight_and_Modulus_adjusted_hand .== 0 ] .= 13
391
- for i = 1 : 13
392
- values = findall (x-> x == i, straight_and_Modulus_adjusted_hand)
393
- if length (values) >= 5
394
- straight_flush_checker = 1
395
- player_score[player] = straight_and_Modulus_adjusted_hand[1 ][1 ]
384
+ for i in 0 : 3
385
+ suit = findall (x -> x in 1 + (13 * i): 13 + (13 * i), hands. hands[player, :])
386
+ len_suit = length (suit)
387
+ if len_suit in [0 , 1 , 2 ]
388
+ continue
389
+ elseif len_suit in [3 , 4 ]
390
+ break
391
+ elseif len_suit in [5 , 6 , 7 ]
392
+ sorted = sort (hands. hands[player, suit])
393
+ count = 1
394
+ 13 * (i + 1 ) in sorted && (sorted = [13 * i; sorted])
395
+ for c in 1 : length (sorted) - 1
396
+ sorted[c] == sorted[c + 1 ] - 1 ? count += 1 : count = 1
397
+ count >= 5 && (player_score[player] = (sorted[c + 1 ] - 1 ) % 13 )
396
398
end
399
+ else
400
+ throw (ErrorException)
397
401
end
398
402
end
399
-
400
403
end
401
- best_hand = findmax (player_score)[1 ]
404
+
405
+ best_hand = maximum (player_score)
402
406
player_winners = findall (x-> x == best_hand, player_score)
403
- println ( @__LINE__ , " " , player_winners)
407
+
404
408
return player_winners
405
409
end
406
410
0 commit comments