Skip to content

Commit

Permalink
Solving capture problem.
Browse files Browse the repository at this point in the history
  • Loading branch information
julianalucena committed Jun 29, 2011
1 parent 8aee711 commit 26233ec
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 8 deletions.
6 changes: 4 additions & 2 deletions Board.hs
Expand Up @@ -78,8 +78,10 @@ capture (player, pos) b = holes2board (getOtherPlayer player) holesCapturedSeeds
where holesCapturedSeeds = updateHole mancalaPos newMancalaSeeds holesRemovedSeeds
newMancalaSeeds = (holesRemovedSeeds !! mancalaPos) + opositeSeeds
mancalaPos = length holesRemovedSeeds - 1
holesRemovedSeeds = updateHole pos 0 (board2holes (getOtherPlayer player) b)
opositeSeeds = (getOtherPlayerHoles player b) !! pos
holesRemovedSeeds = updateHole opositePos 0 (board2holes (getOtherPlayer player) b)
opositeSeeds = otherPlayerHoles !! opositePos
opositePos = length otherPlayerHoles - pos - 2
otherPlayerHoles = (getOtherPlayerHoles player b)

getLastHole :: Hole -> Board -> Hole
getLastHole (player, pos) b = (getPlayer, newPosition `mod` (boardSize `div` 2))
Expand Down
4 changes: 2 additions & 2 deletions Board_Test.hs
Expand Up @@ -52,9 +52,9 @@ moveTest = TestList [

captureTest = TestList [
TestCase $ assertEqual "Player A captures from hole 3"
([3, 5, 0, 1, 4], [7, 0, 1, 0, 2]) (capture (A, 3) sampleBoard),
([3, 5, 0, 1, 10], [0, 0, 1, 1, 2]) (capture (A, 3) sampleBoard),
TestCase $ assertEqual "Player B captures from hole 0"
([0, 5, 0, 1, 3], [7, 0, 1, 1, 5]) (capture (B, 0) sampleBoard)
([3, 0, 0, 1, 3], [7, 0, 1, 1, 7]) (capture (B, 2) sampleBoard)
]

getLastHoleTest = TestList [
Expand Down
4 changes: 3 additions & 1 deletion Rules.hs
Expand Up @@ -29,7 +29,9 @@ canCapture (player, pos) b
| seeds == 1 && opositeSeeds /= 0 = True
| otherwise = False
where seeds = (getPlayerHoles player b) !! pos
opositeSeeds = (getOtherPlayerHoles player b) !! pos
opositeSeeds = otherPlayerHoles !! opositePos
opositePos = length otherPlayerHoles - pos - 2
otherPlayerHoles = (getOtherPlayerHoles player b)

-- Make a move and capture if it is possible
makeMove :: Hole -> Board -> Board
Expand Down
6 changes: 3 additions & 3 deletions Rules_Test.hs
Expand Up @@ -6,7 +6,7 @@ import Board
import Rules
import Test.HUnit

sampleBoard = ([3, 5, 0, 1, 3], [7, 0, 1, 1, 2])
sampleBoard = ([3, 5, 0, 1, 3], [7, 2, 1, 1, 2])

isMancalaTest = TestList [
TestCase $ assertEqual "The last hole is mancala"
Expand Down Expand Up @@ -37,11 +37,11 @@ makeMoveTest = TestList [
TestCase $ assertEqual "Player A makes a simple move"
([0, 6, 0], [7, 9, 1]) (makeMove (A, 0) ([1, 5, 0], [7, 9, 1])),
TestCase $ assertEqual "Player A makes a move and capture"
([0, 6, 1, 3], [7, 9, 0, 3]) (makeMove (A, 0) ([2, 5, 0, 0], [7, 9, 3, 3])),
([0, 6, 1, 7], [0, 9, 3, 3]) (makeMove (A, 0) ([2, 5, 0, 0], [7, 9, 3, 3])),
TestCase $ assertEqual "Player B makes a simple move"
([2, 5, 0], [7, 0, 2]) (makeMove (B, 1) ([1, 5, 0], [7, 2, 1])),
TestCase $ assertEqual "Player B makes a move and capture"
([0, 6, 1, 0], [1, 9, 0, 7]) (makeMove (B, 2) ([2, 5, 0, 0], [0, 9, 5, 3]))
([3, 6, 0, 0], [1, 9, 0, 5]) (makeMove (B, 2) ([2, 5, 0, 0], [0, 9, 5, 3]))
]

canMoveAgainTest = TestList [
Expand Down

0 comments on commit 26233ec

Please sign in to comment.