Skip to content

Commit df3e61f

Browse files
author
brunbjerg
committed
commit before PackageCompile implementation
1 parent 8a5e4ea commit df3e61f

File tree

6 files changed

+2118
-6264
lines changed

6 files changed

+2118
-6264
lines changed

Project.toml

-4
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,9 @@ authors = ["Christian Brunbjerg Jespersen <[email protected]> and contributo
44
version = "0.1.0"
55

66
[deps]
7-
GenieDeployDocker = "a082159f-dd50-4f6d-a3c5-00c38fd36577"
87
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
98
StatsBase = "2913bbd2-ae8a-5f71-8c99-4fb6c76f3a91"
109

11-
[compat]
12-
julia = "1.7"
13-
1410
[extras]
1511
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
1612

profile.pb.gz

+1,897-6,083
Large diffs are not rendered by default.

src/Auxilary_Functions.jl

+13-1
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,16 @@ function Cards_To_Hands(player_cards, shared_cards, folded_cards)
1010
end
1111
end
1212
return Hands(hands, folded_cards)
13-
end
13+
end
14+
15+
function five_in_suit(hand)
16+
a = 0
17+
for i = 1:4
18+
a = searchsortedfirst(hand, 13 * i) - a
19+
a == 4 && return false
20+
13 * i in hand ? c = 5 : c = 6
21+
a >= c && return true
22+
end
23+
return false
24+
end
25+

src/Checker_Functions.jl

+22-39
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,24 @@
11
function Check_Straight_Flush(hands::Hands)
2-
player_scores = zeros(Int, maximum(hands.players))
32
for player in hands.players
4-
straight_counter = 1
5-
#~ Start here. Go card by card instead and also update the Straight_Flush
6-
#~ function. And change the function names to be all lowercase.
7-
8-
#& Hmm... Are we sure that this function is wrong? Yes it must be.
9-
ace_updated_hand = copy(hands.hands[player, :])
10-
if 13 in ace_updated_hand
11-
push!(ace_updated_hand, 0)
12-
end
13-
for card = 1:length(ace_updated_hand) - 1
14-
#& Here one is sorted and one is not. That means that we do not look at the same cards. I need to completely rethink this.
15-
#& I think that I should go one card at a time.
16-
17-
#& This means that there is also an error in the Straight_Flush function.
18-
if ace_updated_hand[card] == ace_updated_hand[card + 1] + 1 && any(issubset([hands.hands[card], hands.hands[card + 1]], (1 + i*13 :13 + i*13) ) for i in 0:3)
19-
straight_counter += 1
20-
if straight_counter == 5 #@ && any(count(x -> x in (1 + i*13 :13 + i*13), hands.hands[player, :]) >= 5 for i in 1:4)
21-
22-
player_scores[player] = copy(ace_updated_hand[card])
23-
println(hands.hands)
24-
println(player_scores)
25-
return true
3+
# five_in_suit(hands.hands[player, :]) ? println("yes") : println("no")
4+
five_in_suit(hands.hands[player, :]) || continue
5+
for i in 0:3
6+
suit = findall(x -> x in 1 + (13 * i):13 + (13 * i), hands.hands[player, :])
7+
len_suit = length(suit)
8+
if len_suit in [0, 1, 2]
9+
continue
10+
elseif len_suit in [3, 4]
11+
break
12+
elseif len_suit in [5, 6, 7]
13+
sorted = sort(hands.hands[player, suit])
14+
count = 1
15+
13 * (i + 1) in sorted && (sorted = [13 * i; sorted])
16+
for c in 1:length(sorted) - 1
17+
sorted[c] == sorted[c + 1] - 1 ? count += 1 : count = 1
18+
count == 5 && return true
2619
end
2720
else
28-
straight_counter = 1
21+
throw(ErrorException)
2922
end
3023
end
3124
end
@@ -58,23 +51,13 @@ function Check_Flush(hands::Hands)
5851
end
5952

6053
function Check_Straight(hands::Hands)
61-
player_scores = zeros(Int, maximum(hands.players))
6254
for player in hands.players
55+
sorted = unique(sort(hands.sorted[player, :]))
6356
count = 1
64-
ace_updated_hand = copy(hands.sorted[player, :])
65-
if 13 in ace_updated_hand
66-
push!(ace_updated_hand, 0)
67-
end
68-
for card = 1:length(ace_updated_hand) - 1
69-
if ace_updated_hand[card] == ace_updated_hand[card + 1] + 1
70-
count += 1
71-
if count == 5
72-
player_scores[player] = copy(ace_updated_hand[card])
73-
return true
74-
end
75-
else
76-
count = 1
77-
end
57+
13 in sorted && (sorted = [0; sorted] )
58+
for c in 1:length(sorted) - 1
59+
sorted[c] == sorted[c + 1] - 1 ? count += 1 : count = 1
60+
count == 5 && return true
7861
end
7962
end
8063
return false

src/Probker.jl

+52-48
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@ struct Hands
3535
return sorted
3636
end
3737
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)
3941
end
4042

4143
#########################################################
@@ -57,6 +59,16 @@ export High_Card,
5759
Four_Kind,
5860
Straight_Flush
5961

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+
6072
export Cards_To_Hands
6173
export Game
6274
export Hands
@@ -73,34 +85,32 @@ include("Checker_Functions.jl")
7385
#########################################################
7486

7587
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-
8488
wins_by_player = zeros(Int, game.players)
8589
split_by_player = zeros(Float64, game.players)
8690
which_hands = zeros(Int, game.players, 9)
8791
for _ = 1:game.simulations
8892
hands = Sample(game::Game)
8993
player_winners, player_hand = Hands_Checker(hands)
94+
# @debug player_winners == [1] || length(player_winners) >= 2 && println(@__LINE__, " player hand " , player_hand)
95+
9096
if length(player_winners) == 1
9197
wins_by_player[player_winners] .+= 1
9298
which_hands[player_winners, player_hand] .+= 1
93-
else
99+
elseif length(player_winners) >= 2
94100
players_in_split = 1/length(player_winners)
95101
split_by_player[player_winners] .+= players_in_split
96102
which_hands[player_winners, player_hand] .+= 1
103+
elseif length(player_winners) == 0
104+
break
105+
else
106+
throw(ErrorException)
97107
end
98108
end
99109
return wins_by_player/game.simulations, split_by_player/game.simulations, which_hands
100110
end
101111

112+
#& This function can be removed by changing the to vector formulation.
102113
function Sample(game::Game)
103-
#& The change should be done in here I think
104114
j = 0
105115
sampled_cards = sample(game.pile, game.samples, replace = false)
106116
cards = copy(game.cards)
@@ -114,6 +124,7 @@ function Sample(game::Game)
114124
end
115125

116126
function Hands_Checker(hands::Hands)
127+
isempty(hands.players) && return [], 0
117128
Check_Straight_Flush(hands::Hands) && return Straight_Flush(hands::Hands), 1
118129
Check_Four_Kind(hands::Hands) && return Four_Kind(hands::Hands), 2
119130
Check_Full_House(hands::Hands) && return Full_House(hands::Hands), 3
@@ -264,28 +275,20 @@ function Three_Kind(hands::Hands)
264275
end
265276

266277
function Straight(hands::Hands)
267-
player_scores = zeros(Int, maximum(hands.players))
278+
scores = zeros(Int, maximum(hands.players))
279+
268280
for player in hands.players
281+
sorted = unique(sort(hands.sorted[player, :]))
269282
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])
283287
end
284288
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)
289292
return player_winners
290293
end
291294

@@ -356,7 +359,6 @@ end
356359

357360
function Four_Kind(hands::Hands)
358361
card_weight = [225, 15, 1]
359-
four_of_a_kind_checker = 0
360362
player_score = zeros(Int, maximum(hands.players), hands.cards)
361363
for player in hands.players
362364
for i = 1:13
@@ -365,7 +367,6 @@ function Four_Kind(hands::Hands)
365367
player_score[player, find_n_kinds] = 10000 * hands.sorted[player, find_n_kinds]
366368
remaining_cards = setdiff(1:7, find_n_kinds)
367369
player_score[player, remaining_cards] = card_weight .* hands.sorted[player, remaining_cards]
368-
four_of_a_kind_checker = 1
369370
end
370371
end
371372
end
@@ -377,30 +378,33 @@ function Four_Kind(hands::Hands)
377378
return player_winners
378379
end
379380

380-
function Straight_Flush(hands::Hands)
381-
add_to_each_card = [0, 1, 2, 3, 4, 5, 6]
381+
function Straight_Flush(hands::Hands)
382382
player_score = zeros(Int, maximum(hands.players))
383383
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)
396398
end
399+
else
400+
throw(ErrorException)
397401
end
398402
end
399-
400403
end
401-
best_hand = findmax(player_score)[1]
404+
405+
best_hand = maximum(player_score)
402406
player_winners = findall(x->x == best_hand, player_score)
403-
println(@__LINE__, " " , player_winners)
407+
404408
return player_winners
405409
end
406410

0 commit comments

Comments
 (0)