Skip to content
Open
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
73 changes: 73 additions & 0 deletions lib/chess/board.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
defmodule Chess.Board do
defstruct a1: %{ square_color: "white", figure: {"black", "rook"}},
b1: %{ square_color: "black", figure: {"black", "knight"}},
c1: %{ square_color: "white", figure: {"black", "bishop"}},
d1: %{ square_color: "black", figure: {"black", "queen"}},
e1: %{ square_color: "white", figure: {"black", "king"}},
f1: %{ square_color: "black", figure: {"black", "bishop"}},
g1: %{ square_color: "white", figure: {"black", "knight"}},
h1: %{ square_color: "black", figure: {"black", "rook"}},

a2: %{ square_color: "black", figure: {"black", "pawn"}},
b2: %{ square_color: "white", figure: {"black", "pawn"}},
c2: %{ square_color: "black", figure: {"black", "pawn"}},
d2: %{ square_color: "white", figure: {"black", "pawn"}},
e2: %{ square_color: "black", figure: {"black", "pawn"}},
f2: %{ square_color: "white", figure: {"black", "pawn"}},
g2: %{ square_color: "black", figure: {"black", "pawn"}},
h2: %{ square_color: "white", figure: {"black", "pawn"}},

a3: %{ square_color: "white", figure: nil},
b3: %{ square_color: "black", figure: nil},
c3: %{ square_color: "white", figure: nil},
d3: %{ square_color: "black", figure: nil},
e3: %{ square_color: "white", figure: nil},
f3: %{ square_color: "black", figure: nil},
g3: %{ square_color: "white", figure: nil},
h3: %{ square_color: "black", figure: nil},

a4: %{ square_color: "black", figure: nil},
b4: %{ square_color: "white", figure: nil},
c4: %{ square_color: "black", figure: nil},
d4: %{ square_color: "white", figure: nil},
e4: %{ square_color: "black", figure: nil},
f4: %{ square_color: "white", figure: nil},
g4: %{ square_color: "black", figure: nil},
h4: %{ square_color: "white", figure: nil},

a5: %{ square_color: "white", figure: nil},
b5: %{ square_color: "black", figure: nil},
c5: %{ square_color: "white", figure: nil},
d5: %{ square_color: "black", figure: nil},
e5: %{ square_color: "white", figure: nil},
f5: %{ square_color: "black", figure: nil},
g5: %{ square_color: "white", figure: nil},
h5: %{ square_color: "black", figure: nil},

a6: %{ square_color: "black", figure: nil},
b6: %{ square_color: "white", figure: nil},
c6: %{ square_color: "black", figure: nil},
d6: %{ square_color: "white", figure: nil},
e6: %{ square_color: "black", figure: nil},
f6: %{ square_color: "white", figure: nil},
g6: %{ square_color: "black", figure: nil},
h6: %{ square_color: "white", figure: nil},

a7: %{ square_color: "white", figure: {"white", "pawn"}},
b7: %{ square_color: "black", figure: {"white", "pawn"}},
c7: %{ square_color: "white", figure: {"white", "pawn"}},
d7: %{ square_color: "black", figure: {"white", "pawn"}},
e7: %{ square_color: "white", figure: {"white", "pawn"}},
f7: %{ square_color: "black", figure: {"white", "pawn"}},
g7: %{ square_color: "white", figure: {"white", "pawn"}},
h7: %{ square_color: "black", figure: {"white", "pawn"}},

a8: %{ square_color: "black", figure: {"white", "rook"}},
b8: %{ square_color: "white", figure: {"white", "knight"}},
c8: %{ square_color: "black", figure: {"white", "bishop"}},
d8: %{ square_color: "white", figure: {"white", "queen"}},
e8: %{ square_color: "black", figure: {"white", "king"}},
f8: %{ square_color: "white", figure: {"white", "bishop"}},
g8: %{ square_color: "black", figure: {"white", "knight"}},
h8: %{ square_color: "white", figure: {"white", "rook"}}
end
83 changes: 83 additions & 0 deletions lib/chess/dashboard.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
defmodule Chess.Dashboard do

alias Chess.Board
alias Chess.Coordinate

def build_dashboard() do
new_board = %Board{}
coordinate = [:a1,
:b1,
:c1,
:d1,
:e1,
:f1,
:g1,
:h1,

:a2,
:b2,
:c2,
:d2,
:e2,
:f2,
:g2,
:h2,

:a3,
:b3,
:c3,
:d3,
:e3,
:f3,
:g3,
:h3,

:a4,
:b4,
:c4,
:d4,
:e4,
:f4,
:g4,
:h4,

:a5,
:b5,
:c5,
:d5,
:e5,
:f5,
:g5,
:h5,

:a6,
:b6,
:c6,
:d6,
:e6,
:f6,
:g6,
:h6,

:a7,
:b7,
:c7,
:d7,
:e7,
:f7,
:g7,
:h7,

:a8,
:b8,
:c8,
:d8,
:e8,
:f8,
:g8,
:h8
]
{%Board{}, coordinate}
end

end
96 changes: 96 additions & 0 deletions lib/chess/figure_move.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
defmodule Chess.FigureMove do

@rows [1, 2, 3, 4 ,5 ,6 ,7, 8]

@columns [1, 2, 3, 4 ,5 ,6 ,7, 8]
@columns_value %{ "a" => 1, "b" => 2, "c" => 3, "d" => 4, "e" => 5, "f" => 6, "g" => 7, "h" => 8 }
@columns_key %{ 1 => "a", 2 => "b", 3 => "c", 4 => "d", 5 => "e", 6 => "f", 7 => "g", 8 => "h" }

def move(%{"cor" => cor, "player" => "white", "figure" => "pawn"}) do
[column, row] = String.codepoints(cor)
row = String.to_integer(row)
moves = Enum.filter(@columns, fn y -> row > y end)

total_move = case length(moves) == 6 do
true -> [ row - 1 , row - 2]
false -> [ row - 1 ]
end

Enum.map(total_move, fn move -> column <> "#{move}" end)
end

def move(%{"cor" => cor, "player" => "black", "figure" => "pawn"}) do
[column, row] = String.codepoints(cor)
row = String.to_integer(row)
moves = Enum.filter(@columns, fn y -> row < y end)

total_move = case length(moves) == 6 do
true -> [ row + 1 , row + 2]
false -> [ row + 1 ]
end

Enum.map(total_move, fn move -> column <> "#{move}" end)
end

def move(%{"cor" => cor, "player" => player, "figure" => "rook"}) do
[x, y] = String.codepoints(cor)
move_x_y(x, y)
end

def move(%{"cor" => cor, "player" => player, "figure" => "bishop"}) do
[x, y] = String.codepoints(cor)
diagonal_move(x, y)
end

def kill?(%{"cor" => cor, "player" => player} = params, coordinate, active_player, _move, {_color, "pawn"}) do
[column_kill, row_kill] = String.codepoints(cor)
row_kill = String.to_integer(row_kill)

figure = Atom.to_string(coordinate)

[column, row] = String.codepoints(figure)

row_left = String.to_integer(row) + 1
row_right = String.to_integer(row) - 1

column_value = Map.get(@columns_value, column)
column_kill_value = Map.get(@columns_value, column_kill)

column_left = column_value - 1
column_right = column_value + 1

(row_left == row_kill or row_right == row_kill) and (column_left == column_kill_value or column_right == column_kill_value) and player != active_player
end

def kill?(%{"cor" => cor, "player" => player} = params, _coordinate, active_player, move, {_color, "rook"}) do
Enum.member?(move, cor) and player != active_player
end

defp move_x_y(x,y) do
x_value = Map.get(@columns_value, x)
y = String.to_integer(y)

x_left = Enum.filter(@columns, fn column -> x_value < column end)
x_right = Enum.filter(@columns, fn column -> x_value > column end)

x_left = Enum.map(x_left, fn x_number -> Map.get(@columns_key, x_number) end)
x_right = Enum.map(x_right, fn x_number -> Map.get(@columns_key, x_number) end)

move_in_x = x_left ++ x_right

y_up = Enum.filter(@rows, fn row -> row < y end)
y_down = Enum.filter(@rows, fn row -> row > y end)

move_in_y = y_up ++ y_down

move_in_x = Enum.map(move_in_x, fn move -> move <> "#{y}" end)
move_in_y = Enum.map(move_in_y, fn move -> x <> "#{move}" end)

move_in_x ++ move_in_y
end

defp diagonal_move(x, y) do
# aqui va la logica en diagonal del alfil

end
end
22 changes: 22 additions & 0 deletions lib/chess_web/live/components/square/index.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
defmodule ChessWeb.SquareLiveComponent do

use ChessWeb, :live_component
use Phoenix.HTML

def square( %{id: id, square: %{square_color: square_color, figure: {player, figure_type}} } = assigns) do
square_color = "square" <> " " <> square_color
figure = "figure" <> " " <> player <> " " <> figure_type
~H"""
<div class={square_color}>
<div class={figure} phx-value-cor={id} phx-value-player={player} phx-value-figure={figure_type} phx-click="select"></div>
</div>
"""
end

def square( %{id: id, square: %{square_color: square_color, figure: nil} } = assigns) do
square_color = "square" <> " " <> square_color
~H"""
<div class={square_color} phx-value-cor={id} phx-click="select"></div>
"""
end
end
88 changes: 88 additions & 0 deletions lib/chess_web/live/page/game/index.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
defmodule ChessWeb.GameLive.Index do

use ChessWeb, :live_view

alias Chess.Dashboard
alias ChessWeb.SquareLiveComponent
alias Chess.FigureMove
alias Chess.Board

def mount(_params, _session, socket) do
{:ok, assign_initial_game(socket)}
end

def handle_event("select", %{"player" => figure_color, "cor" => cor } = params,
%{assigns: %{move: nil, selected_figure: nil,
board: board, player: player}} = socket) do
case figure_color == player do
true -> move = FigureMove.move(params)
coordinate = String.to_atom(cor)
selected_coordinate = Map.get(board, coordinate)
{:noreply, socket
|> assign(move: move)
|> assign(selected_figure: Map.get(selected_coordinate, :figure))
|> assign(prev_coordinate: coordinate )
}
false -> {:noreply, socket}
end
end

def handle_event("select", %{"player" => figure_color, "cor" => cor } = params,
%{assigns: %{move: move, selected_figure: selected_figure,
board: board, prev_coordinate: prev_coordinate, player: player}} = socket) do
IO.puts "KILL"
IO.inspect selected_figure
case FigureMove.kill?(params, prev_coordinate, player, move, selected_figure) do
true -> update_board(cor, socket)
false -> {:noreply, socket}
end
end

def handle_event("select", %{"cor" => cor}, socket) do
move = socket.assigns.move || []
case Enum.member?(move, cor) do
true -> update_board(cor, socket)
false -> {:noreply, socket}
end
end

defp assign_initial_game(socket) do
{new_board, coordinates} = Dashboard.build_dashboard()
socket
|> assign(board: new_board)
|> assign(coordinates: coordinates)
|> assign(player: "white")
|> assign(move: nil)
|> assign(selected_figure: nil)
end

defp update_board(cor, socket) do
coordinate = String.to_atom(cor)

first_change = socket.assigns.board
|> Map.get(socket.assigns.prev_coordinate)
|> Map.replace(:figure, nil)

second_change = socket.assigns.board
|> Map.get(coordinate)
|> Map.replace(:figure, socket.assigns.selected_figure)

board = socket.assigns.board
|> Map.replace(coordinate, second_change)
|> Map.replace(socket.assigns.prev_coordinate, first_change)

player = case socket.assigns.player == "white" do
true -> "black"
false -> "white"
end

{:noreply,
socket
|> assign(board: board)
|> assign(player: player)
|> assign(move: nil)
|> assign(selected_figure: nil)
}
end

end
11 changes: 11 additions & 0 deletions lib/chess_web/live/page/game/index.html.heex
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<section>
<div> Player <%= @player %></div>
<div class="board">
<%= for coordinate <- @coordinates do %>
<SquareLiveComponent.square
id={coordinate}
square={Map.get(@board, coordinate)}
/>
<% end %>
</div>
</section>
13 changes: 13 additions & 0 deletions lib/chess_web/live/page/home/index.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
defmodule ChessWeb.HomeLive.Index do

use ChessWeb, :live_view

def mount(_params, _session, socket) do
{:ok, socket}
end

def handle_event("create_game", _value, socket) do
{ :noreply, push_redirect(socket, to: "/game")}
end

end
3 changes: 3 additions & 0 deletions lib/chess_web/live/page/home/index.html.heex
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<section>
<button phx-click="create_game"> Create a new Game </button>
</section>
Loading