aboutsummaryrefslogtreecommitdiff
path: root/challenge-052/markus-holzer/raku/ch-2.p6
blob: 3d2ef06e142297b58d2982e24cf6a4735c723f03 (plain)
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
72
73
74
75
76
enum Player <Computer Human>;
subset Difficulty of Int where * ~~ 1|2|3;

sub USAGE {
	say q:to/USAGE/;
    Usage:
        ch-2.p6 <difficulty>

    Possible Difficulties: 1, 2, 3
    USAGE
}

sub MAIN( Difficulty $difficulty  )
{
	my @moneyz = (0.01, 0.02, 0.05, 0.1, 0.2, 0.5, 1, 2).pick(8);
	my @player = (Computer, Human).pick(2);
	my %score;

	while @moneyz.elems
	{
		say "@player[0] Pick: {@moneyz}";
		%score{ @player[0] } += pick-best( @player[0], $difficulty, @moneyz );
		@player = @player.rotate(1);
	}

	say %score{Computer} > %score{Human}
		?? "The machine wins with { %score{Computer} } by { %score{Computer} - %score{Human} }"
		!! "Good job, you beat the machine with { %score{Human} } by { %score{Human} - %score{Computer} }";
}


multi sub pick-best( Human, $, @moneyz )
{
	my $side = prompt "Your turn (l/r): ";
	
	$side ~~ 'l'|'r' 
		?? pick( @moneyz, $side ~~ 'l' )
		!! pick-best( Human, $, @moneyz );
}

multi sub pick-best( Computer, 1, @moneyz )
{
	pick( @moneyz, (True, False).pick );
}

multi sub pick-best( Computer, 2, @moneyz )
{
	pick( @moneyz, @moneyz[0] > @moneyz[*-1] );
}

multi sub pick-best( Computer, 3, @moneyz )
{
	my $left  = @moneyz.first( * == 2, :k);

	# 2 not in the list anymore
	return pick-best( Computer, 2, @moneyz )
		unless $left.defined;

	my $right = @moneyz.elems - $left - 1;

	return pick(@moneyz, True)  if $left  == 0;
	return pick(@moneyz, False) if $right == 0;

	my $l = $left %% 2;
	my $r = $right %% 2;

	return pick(@moneyz, True)  if $l && !$r;
	return pick(@moneyz, False) if $r && !$l;

	return pick(@moneyz, $left > $right);
}

multi sub pick( @moneyz, Bool $left )
{
	$left ?? @moneyz.shift !! @moneyz.pop;
}