Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds logic that allows white pawns to move to what would be the corre… #31

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
109 changes: 90 additions & 19 deletions src/Move.elm
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,90 @@ import Dict
import Types exposing (..)


getPossibleMoves : Int -> Board -> List Int
getPossibleMoves tileIndex board =
board
getPossibleMoves : Int -> Model -> List Int
getPossibleMoves tileIndex model =
model
|> .boardWithoutPossibleMoves
|> getTilePiece tileIndex
|> Maybe.withDefault (Piece King White)
|> (\piece ->
case piece.pieceType of
King ->
kingMoves tileIndex piece board
kingMoves tileIndex piece model.boardWithoutPossibleMoves

Queen ->
queenMoves tileIndex piece board
queenMoves tileIndex piece model.boardWithoutPossibleMoves

Rook ->
rookMoves tileIndex piece board
rookMoves tileIndex piece model.boardWithoutPossibleMoves

Bishop ->
bishopMoves tileIndex piece board
bishopMoves tileIndex piece model.boardWithoutPossibleMoves

Knight ->
knightMoves tileIndex piece board
knightMoves tileIndex piece model.boardWithoutPossibleMoves

Pawn ->
pawnMoves tileIndex piece board
let
enPassentCap =
canPawnCaptureEP tileIndex piece model
in
enPassentCap ++ pawnMoves tileIndex piece model.boardWithoutPossibleMoves
)


didPawnMoveTwoSpaces : Int -> Piece -> Model -> Bool
didPawnMoveTwoSpaces currentIndex piece model =
case model.previousMove of
Just ( p, i1, i2 ) ->
i2 == currentIndex && i1 == currentIndex + 20

Nothing ->
False


canPawnCaptureEP : Int -> Piece -> Model -> List Int
canPawnCaptureEP tileIndex piece model =
case piece.team of
Black ->
[]

White ->
if List.member tileIndex (List.range 52 59) then
let
tile_int =
[ 1, -1 ]
|> List.map (\i -> ( tileIndex + i, getTilePiece (tileIndex + i) model.board ))
|> List.foldl
(\mp acc ->
case mp of
( _, Nothing ) ->
acc

( i, Just p ) ->
if p.pieceType == Pawn && True then
( i, p ) :: acc

else
acc
)
[]
|> List.foldl
(\( i, p ) acc ->
if didPawnMoveTwoSpaces i p model then
i + 10 :: acc

else
acc
)
[]
in
tile_int

else
[]


kingMoves : Int -> Piece -> Board -> List Int
kingMoves tileIndex piece board =
[ 1, 9, 10, 11, -1, -9, -10, -11 ]
Expand Down Expand Up @@ -67,17 +124,21 @@ pawnMoves : Int -> Piece -> Board -> List Int
pawnMoves tileIndex piece board =
case piece.team of
White ->
getPawnMoves Light 10 20 9 11 tileIndex piece board
getPawnMoves 10 20 9 11 tileIndex piece board

Black ->
getPawnMoves Dark -10 -20 -9 -11 tileIndex piece board
getPawnMoves -10 -20 -9 -11 tileIndex piece board


getPawnMoves : Colour -> Int -> Int -> Int -> Int -> Int -> Piece -> Board -> List Int
getPawnMoves colour oneSq twoSq capL capR tileIndex clickedPiece board =
getPawnMoves : Int -> Int -> Int -> Int -> Int -> Piece -> Board -> List Int
getPawnMoves oneSq twoSq capL capR tileIndex clickedPiece board =
let
possMoves =
if allowTwoSpaceMove tileIndex clickedPiece && isTileFree (tileIndex + oneSq) board && isTileFree (tileIndex + twoSq) board then
if
isPawnOnStartingTile tileIndex clickedPiece
&& isTileFree (tileIndex + oneSq) board
&& isTileFree (tileIndex + twoSq) board
then
[ tileIndex + oneSq, tileIndex + twoSq ]

else if isTileFree (tileIndex + oneSq) board then
Expand All @@ -91,8 +152,8 @@ getPawnMoves colour oneSq twoSq capL capR tileIndex clickedPiece board =
|> checkTile tileIndex clickedPiece board capR


allowTwoSpaceMove : Int -> Piece -> Bool
allowTwoSpaceMove tileIndex piece =
isPawnOnStartingTile : Int -> Piece -> Bool
isPawnOnStartingTile tileIndex piece =
List.member tileIndex (pawnHomeIndexes piece)


Expand All @@ -106,9 +167,19 @@ pawnHomeIndexes piece =
List.range 71 79


pawnPossCapEPIndexes : Piece -> List Int
pawnPossCapEPIndexes piece =
case piece.team of
Black ->
List.range 42 49

White ->
List.range 52 59


checkTile : Int -> Piece -> Board -> Int -> List Int -> List Int
checkTile tileIndex clickedPiece board count intList =
case getTilePiece (tileIndex + count) board of
checkTile tileIndex clickedPiece board move intList =
case getTilePiece (tileIndex + move) board of
Nothing ->
intList

Expand All @@ -117,7 +188,7 @@ checkTile tileIndex clickedPiece board count intList =
intList

else
(tileIndex + count) :: intList
(tileIndex + move) :: intList


addMovesToList : Int -> Piece -> Board -> Int -> List Int -> List Int
Expand Down
32 changes: 15 additions & 17 deletions src/State.elm
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ update msg model =
CheckPossibleMoves tileIndex ->
let
possMoves =
getPossibleMoves tileIndex model.boardWithoutPossibleMoves
getPossibleMoves tileIndex model
in
{ model | board = updatePossibleMoves possMoves model.boardWithoutPossibleMoves }

Expand All @@ -39,34 +39,32 @@ update msg model =
model

Drop targetIndex ->
let
boardWithoutPossibleMoves =
model.boardWithoutPossibleMoves

updatedBoard =
case model.beingDragged of
Just ( piece, currentIndex ) ->
boardWithoutPossibleMoves
|> moveToNewTile targetIndex piece
|> removeFromPreviousTile currentIndex piece

Nothing ->
model.board
in
case model.beingDragged of
Nothing ->
model

Just ( piece, currentIndex ) ->
{ model
| beingDragged = Nothing
, board = updatedBoard
, boardWithoutPossibleMoves = updatedBoard
, board = updateBoard model targetIndex
, boardWithoutPossibleMoves = updateBoard model targetIndex
, turn = oppositeTeam piece.team
, previousMove = Just ( piece, currentIndex, targetIndex )
}


updateBoard : Model -> Int -> Board
updateBoard model targetIndex =
case model.beingDragged of
Just ( piece, currentIndex ) ->
model.boardWithoutPossibleMoves
|> moveToNewTile targetIndex piece
|> removeFromPreviousTile currentIndex piece

Nothing ->
model.board


oppositeTeam : Team -> Team
oppositeTeam team =
case team of
Expand Down