blob: a168b203aa70314d6702b161a812cc6268a3bf30 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
LuckyWinner←{
⍝ The elements of ⍵ (coins) are arranged in a line (vector/list) in a random order
⍝ The player and computer take turns choosing from either the right- or left-most coins without replacement
⍝ Who has the highest total value of coins after they have all been chosen?
⎕IO←0 ⍝ ⎕IO delenda est
⎕←'A tie is ','possible',⍨'im' ''⊃⍨∨/=/+/TieTest ⍵
⍺←1 ⍝ Computer goes first by default (⍺←0 for player first)
n←≢⍵ ⍝ Number of coins
end←?2⍴⍨n ⍝ Random end choices (0 left, 1 right)
choose←{⌽@(0=⊢)⍋⍋⍵⍳⍵}end ⍝ Coin index choice sequence
turns←n⍴(~,⊢)⍺ ⍝ Turn sequence (0 player, 1 computer)
⎕←coins←↑⍵∘{⍵⌷⍤0 1⊢⍺}¨(turns(⊂⊢)⌸choose) ⍝ Coins chosen (1st row player, 2nd row computer)
=/+/coins:'It''s a tie!'
' wins!',⍨(>⌿+/coins)⊃'Player' 'Computer' ⍝ Return winner in text message
}
|