Skip to content

Commit

Permalink
calculate all possible moves all pieces and pawns can make. does not …
Browse files Browse the repository at this point in the history
…yet filter out blocked tiles #1
  • Loading branch information
RobStallion committed Jan 5, 2019
1 parent 88e91c1 commit 9ba8268
Show file tree
Hide file tree
Showing 5 changed files with 275 additions and 134 deletions.
167 changes: 49 additions & 118 deletions src/Board.elm
Original file line number Diff line number Diff line change
@@ -1,134 +1,56 @@
module Board exposing (createBoard, createLightPawns, movePiece, renderBoard)
module Board exposing (createBoard, outOfBoundsList, renderBoard)

import Browser
import Html exposing (Html, div, img, p, text)
import Html.Attributes exposing (class, src)
import Array exposing (Array)
import Html exposing (Html, div, text)
import Html.Attributes exposing (class)
import Html.Events exposing (onClick)
import List.Extra
import Piece exposing (getPieceByIndex, pieceImgTag)
import Types exposing (..)


createLightPawn : Int -> Piece
createLightPawn index =
Piece Pawn Light index Alive


createLightPawns : List Int -> List Piece
createLightPawns indexList =
List.map createLightPawn indexList
-- Render


renderBoard : List Piece -> Board -> Html Msg
renderBoard pieces board =
let
pieceIndexes =
List.map (\p -> p.index) pieces
in
div [] <|
List.map
(\tile ->
if List.member tile.index pieceIndexes then
let
piece =
getPieceByIndex tile.index pieces
in
if piece.status == Alive then
div [ class <| tileClasses tile, onClick <| CheckAvailableMoves piece ] [ pieceImgTag piece ]

else
div [ class <| tileClasses tile ] [ text <| String.fromInt tile.index ]

else
div [ class <| tileClasses tile ] [ text <| String.fromInt tile.index ]
)
board


movePiece : Piece -> Piece
movePiece piece =
let
_ =
Debug.log "--------" piece
in
{ piece | index = piece.index + 10 }


getPieceByIndex : Int -> List Piece -> Piece
getPieceByIndex int pieceList =
List.filter (\piece -> piece.index == int) pieceList
|> List.head
|> Maybe.withDefault (Piece King Light 0 Alive)
renderBoard pieceList board =
div [] <| List.map (renderRow pieceList) <| chunk 10 board []


pieceImgTag : Piece -> Html Msg
pieceImgTag piece =
div [ class "h3 w3 flex items-center justify-center" ]
[ img [ src <| pieceImgStr piece, class "w2-5" ] []
]
renderRow : List Piece -> List Tile -> Html Msg
renderRow pieceList row =
div [ class "flex" ] <| List.map (renderTile pieceList) row


renderTile : List Piece -> Tile -> Html Msg
renderTile pieceList tile =
let
pieceIndexes =
List.map (\p -> p.index) pieceList
in
if List.member tile.index pieceIndexes then
let
piece =
getPieceByIndex tile.index pieceList
in
if piece.status == Alive then
div [ class <| tileClasses tile, onClick <| CheckAvailableMoves piece ] [ pieceImgTag piece ]

-- Render board functions


pieceImgStr : Piece -> String
pieceImgStr piece =
"images/" ++ colorToText piece.colour ++ pieceToText piece.piece ++ ".svg"


splitBoardIntoRows : Board -> List (List Tile)
splitBoardIntoRows tileList =
chunk 10 tileList []


chunk : Int -> List a -> List (List a) -> List (List a)
chunk int list acc =
if List.length list <= int then
acc ++ [ list ]
else
div [ class <| tileClasses tile ] [ text <| String.fromInt tile.index ]

else
chunk int (List.drop int list) acc ++ [ List.take int list ]



-- pieceToText and colorToText will be replaced with images in later commit


pieceToText : PieceType -> String
pieceToText piece =
case piece of
King ->
"k"

Queen ->
"q"

Rook ->
"r"

Bishop ->
"b"

Knight ->
"n"
div [ class <| tileClasses tile ] [ text <| String.fromInt tile.index ]

Pawn ->
"p"


colorToText : Colour -> String
colorToText colour =
case colour of
Light ->
"l"

Dark ->
"d"
-- Create


createBoard : Board
createBoard =
List.map createEmptyTile <| List.range 1 100
List.range 1 100
|> List.map createEmptyTile


createEmptyTile : Int -> Tile
Expand All @@ -145,16 +67,6 @@ emptyTileStatus int =
Legal


tileClasses : Tile -> String
tileClasses tile =
case tile.status of
Legal ->
lightOrDarkTile tile ++ "h3 w3 flex items-center justify-center"

OutOfBounds ->
"h3 w3 flex items-center justify-center bg-gray"



-- Helpers

Expand All @@ -177,6 +89,16 @@ outOfBoundsList =
++ (List.map (\n -> n * 10) <| List.range 2 9)


tileClasses : Tile -> String
tileClasses tile =
case tile.status of
Legal ->
lightOrDarkTile tile ++ "h3 w3 flex items-center justify-center"

OutOfBounds ->
"h3 w3 flex items-center justify-center bg-gray"


lightOrDarkTile : Tile -> String
lightOrDarkTile tile =
if isEven <| sumOfIndex tile then
Expand All @@ -193,3 +115,12 @@ sumOfIndex tile =
|> String.split ""
|> List.map String.toInt
|> List.foldl (\maybeN acc -> Maybe.withDefault 0 maybeN + acc) 0


chunk : Int -> List a -> List (List a) -> List (List a)
chunk int list acc =
if List.length list <= int then
acc ++ [ list ]

else
chunk int (List.drop int list) acc ++ [ List.take int list ]
18 changes: 14 additions & 4 deletions src/Main.elm
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
module Main exposing (init, main, update, view)

import Board exposing (..)
import Array
import Board exposing (createBoard, renderBoard)
import Browser
import Html exposing (Html, button, div, text)
import Html.Attributes exposing (class)
import Html.Events exposing (onClick)
import List.Extra
import Move exposing (getPossibleMoves)
import Piece exposing (createAllPieces)
import Types exposing (..)


Expand All @@ -16,19 +19,26 @@ main =
init : Model
init =
{ board = createBoard
, pieces = createLightPawns <| List.range 22 29
, pieces = createAllPieces
}


update : Msg -> Model -> Model
update msg model =
case msg of
CheckAvailableMoves piece ->
{ model | pieces = List.Extra.updateAt 0 movePiece model.pieces }
let
_ =
Debug.log "" <| getPossibleMoves piece
in
-- { model | pieces = List.Extra.updateAt 0 movePiece model.pieces }
model


view : Model -> Html Msg
view model =
div []
[ div [ class "mt4" ] [ renderBoard model.pieces model.board ]
[ div [ class "mt4 flex justify-center" ]
[ renderBoard model.pieces model.board
]
]
117 changes: 117 additions & 0 deletions src/Move.elm
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
module Move exposing (getPossibleMoves)

import Board exposing (outOfBoundsList)
import List.Extra
import Types exposing (..)


getPossibleMoves : Piece -> List Int
getPossibleMoves piece =
case piece.piece of
King ->
kingMoves piece

Queen ->
queenMoves piece

Rook ->
rookMoves piece

Bishop ->
bishopMoves piece

Knight ->
knightMoves piece

Pawn ->
pawnMoves piece


kingMoves : Piece -> List Int
kingMoves piece =
[ 1, 9, 10, 11, -1, -9, -10, -11 ]
|> List.map (\i -> piece.index + i)
|> List.filter (\i -> not <| List.member i outOfBoundsList)


queenMoves : Piece -> List Int
queenMoves piece =
rookMoves piece ++ bishopMoves piece


knightMoves : Piece -> List Int
knightMoves piece =
[ 21, 19, 12, 8, -21, -19, -12, -8 ]
|> List.map (\i -> piece.index + i)
|> List.filter (\i -> not <| List.member i outOfBoundsList)
|> List.filter (\i -> i > 1 && i < 100)


rookMoves : Piece -> List Int
rookMoves piece =
addMovesToList piece.index 1 []
++ addMovesToList piece.index -1 []
++ addMovesToList piece.index 10 []
++ addMovesToList piece.index -10 []


bishopMoves : Piece -> List Int
bishopMoves piece =
addMovesToList piece.index 9 []
++ addMovesToList piece.index -9 []
++ addMovesToList piece.index 11 []
++ addMovesToList piece.index -11 []


pawnMoves : Piece -> List Int
pawnMoves piece =
let
moves =
case piece.colour of
Light ->
[ 9, 10, 11, 20 ]

Dark ->
[ -9, -10, -11, -20 ]
in
moves
|> List.map (\i -> piece.index + i)
|> List.filter (\i -> not <| List.member i outOfBoundsList)
|> checkTwoTileMove piece



-- if pawn is on home square, (for now) remove the plus 20 move


checkTwoTileMove : Piece -> List Int -> List Int
checkTwoTileMove piece moves =
if isPawnOnStartingPosition piece then
moves

else
List.filter (\i -> not <| i == piece.index + 20) moves


isPawnOnStartingPosition : Piece -> Bool
isPawnOnStartingPosition piece =
List.member piece.index <| pawnHomeIndexes piece


pawnHomeIndexes : Piece -> List Int
pawnHomeIndexes piece =
case piece.colour of
Light ->
List.range 22 29

Dark ->
List.range 71 79


addMovesToList : Int -> Int -> List Int -> List Int
addMovesToList index count acc =
if List.member (index + count) outOfBoundsList then
acc

else
addMovesToList (index + count) count (index + count :: acc)
Loading

0 comments on commit 9ba8268

Please sign in to comment.