-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
89 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
module Base where | ||
|
||
data Player = CliPlayer String | Idiot | CpuPlayer deriving Show | ||
|
||
type Heap = Int | ||
|
||
type Heaps = [Heap] | ||
|
||
data Game = Game { heaps :: Heaps, players :: [Player] } | ||
|
||
player :: Game -> Player | ||
player = head . players | ||
|
||
nextPlayer :: Game -> Game | ||
nextPlayer g = g { players = rotate $ players g } where | ||
rotate (x:xs) = xs ++ [x] | ||
|
||
type Move = ( Int -- ^ heap from which to take the pieces | ||
, Int -- ^ amount of pieces to take away | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
module CpuPlayer (runCpuPlayer) where | ||
|
||
import Data.Bits (xor) | ||
import Data.List (findIndices) | ||
import Base | ||
|
||
-- | A list of pissbilbe winning moves in a game of Nim | ||
-- | ||
-- >>> runCpuPlayer [3,4,5] | ||
-- [(1,2)] | ||
runCpuPlayer :: Heaps -> [Move] | ||
runCpuPlayer hs = filter ((>0) . snd) $ zip [1..] $ map aux hs | ||
where | ||
aux h = h - (h `xor` s) | ||
s = foldr1 xor hs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters