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

Change piece type #4

Merged
merged 2 commits into from
Jan 5, 2019
Merged
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
3 changes: 2 additions & 1 deletion elm.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
"direct": {
"elm/browser": "1.0.1",
"elm/core": "1.0.2",
"elm/html": "1.0.0"
"elm/html": "1.0.0",
"elm-community/list-extra": "8.1.0"
},
"indirect": {
"elm/json": "1.1.2",
Expand Down
2 changes: 1 addition & 1 deletion public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title>Rob Chess</title>
<title>Rob Chess Zero (lol)</title>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😉

<link rel="stylesheet" href="/css/main.css">
<link rel="stylesheet" href="https://unpkg.com/tachyons@4.10.0/css/tachyons.min.css"/>
<script src="js/elm.js" charset="utf-8"></script>
Expand Down
211 changes: 50 additions & 161 deletions src/Board.elm
Original file line number Diff line number Diff line change
@@ -1,195 +1,81 @@
module Board exposing (renderBoard, startingBoard)
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 Piece exposing (getPieceByIndex, pieceImgTag)
import Types exposing (..)



-- Update board functions
-- Render


updateBoard : Int -> Piece -> Board -> Board
updateBoard index piece board =
let
updatedTile =
getTile index board |> updateTilePiece piece

beforeTile =
List.take (index - 1) board

afterTile =
List.drop index board
in
beforeTile ++ updatedTile :: afterTile


updateTilePiece : Piece -> Tile -> Tile
updateTilePiece piece tile =
Tile tile.index tile.status <| Just piece


getTile : Int -> Board -> Tile
getTile int board =
board
|> List.drop (int - 1)
|> List.head
|> Maybe.withDefault (Tile 0 Legal Nothing)

renderBoard : List Piece -> Board -> Html Msg
renderBoard pieceList board =
div [] <| List.map (renderRow pieceList) <| chunk 10 board []


-- Render board functions
renderRow : List Piece -> List Tile -> Html Msg
renderRow pieceList row =
div [ class "flex" ] <| List.map (renderTile pieceList) row


renderBoard : Board -> Html Msg
renderBoard board =
div [ class "" ] <| List.map (renderRow board) <| splitBoardIntoRows board


renderRow : Board -> List Tile -> Html Msg
renderRow board tileList =
div [ class "flex justify-center" ] <| List.map (renderTile board) tileList


renderTile : Board -> Tile -> Html Msg
renderTile board tile =
div [ class <| tileClasses tile ]
[ displayInTile board tile
]


displayInTile : Board -> Tile -> Html Msg
displayInTile board tile =
renderTile : List Piece -> Tile -> Html Msg
renderTile pieceList tile =
let
piece =
Maybe.withDefault (Piece King Light) tile.piece

inTileHtml =
if tile.piece == Nothing then
p [ class "tc" ] [ text <| String.fromInt <| tile.index ]

else
div [ class "h3 w3 flex items-center justify-center", onClick <| CheckAvailableMoves tile board ]
[ img [ src <| makePieceImgStr piece, class "w2-5" ] []
]
pieceIndexes =
List.map (\p -> p.index) pieceList
in
inTileHtml

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 ]

makePieceImgStr : Piece -> String
makePieceImgStr 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"
div [ class <| tileClasses tile ] [ text <| String.fromInt tile.index ]

Knight ->
"n"

Pawn ->
"p"


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

Dark ->
"d"



-- Create board functions


addDarkRooks : Board -> Board
addDarkRooks board =
addPiecesToBoard [ 82, 89 ] (Piece Rook Dark) board


addDarkPawns : Board -> Board
addDarkPawns board =
addPiecesToBoard (List.range 72 79) (Piece Pawn Dark) board


startingBoard : Board
startingBoard =
createBoard
-- Light pieces
|> addPiecesToBoard (List.range 22 29) (Piece Pawn Light)
|> addPiecesToBoard [ 12, 19 ] (Piece Rook Light)
|> addPiecesToBoard [ 13, 18 ] (Piece Knight Light)
|> addPiecesToBoard [ 14, 17 ] (Piece Bishop Light)
|> addPiecesToBoard [ 15 ] (Piece Queen Light)
|> addPiecesToBoard [ 16 ] (Piece King Light)
-- Dark pieces
|> addPiecesToBoard (List.range 72 79) (Piece Pawn Dark)
|> addPiecesToBoard [ 82, 89 ] (Piece Rook Dark)
|> addPiecesToBoard [ 83, 88 ] (Piece Knight Dark)
|> addPiecesToBoard [ 84, 87 ] (Piece Bishop Dark)
|> addPiecesToBoard [ 85 ] (Piece Queen Dark)
|> addPiecesToBoard [ 86 ] (Piece King Dark)


addPiecesToBoard : List Int -> Piece -> Board -> Board
addPiecesToBoard intList piece board =
List.foldl (\i acc -> updateBoard i piece acc) board intList
-- Create


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


createEmptyTile : Int -> Tile
createEmptyTile index =
Tile index (getTileStatus index) Nothing
Tile index (emptyTileStatus index)


getTileStatus : Int -> Status
getTileStatus int =
emptyTileStatus : Int -> TileStatus
emptyTileStatus int =
if isIndexOutOfBounds int then
OutOfBounds

else
Legal



-- Helpers


isEven : Int -> Bool
isEven int =
0 == modBy 2 int


isIndexOutOfBounds : Int -> Bool
isIndexOutOfBounds int =
List.member int outOfBoundsList
Expand All @@ -210,7 +96,6 @@ tileClasses tile =
lightOrDarkTile tile ++ "h3 w3 flex items-center justify-center"

OutOfBounds ->
-- will be dn in future
"h3 w3 flex items-center justify-center bg-gray"


Expand All @@ -223,15 +108,19 @@ lightOrDarkTile tile =
"c-bg-dark "


isEven : Int -> Bool
isEven int =
0 == modBy 2 int


sumOfIndex : Tile -> Int
sumOfIndex tile =
tile.index
|> String.fromInt
|> 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 ]
21 changes: 17 additions & 4 deletions src/Main.elm
Original file line number Diff line number Diff line change
@@ -1,10 +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 @@ -14,18 +18,27 @@ main =

init : Model
init =
{ board = startingBoard }
{ board = createBoard
, pieces = createAllPieces
}


update : Msg -> Model -> Model
update msg model =
case msg of
CheckAvailableMoves tile board ->
CheckAvailableMoves piece ->
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.board ]
[ div [ class "mt4 flex justify-center" ]
[ renderBoard model.pieces model.board
]
]
Loading