Skip to content

Commit b035b02

Browse files
committed
display all legal moves from first position
1 parent 60ee4e2 commit b035b02

6 files changed

Lines changed: 94 additions & 24 deletions

File tree

ecg/Cargo.lock

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ecg/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ version = "0.1.0"
44
edition = "2024"
55

66
[dependencies]
7+
schach = { path = "../schach" }
78
yew = { version = "0.21", features = ["csr"] }

ecg/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<html>
33
<head>
44
<meta charset="utf-8" />
5-
<title>Trunk Template</title>
5+
<title>Every Chess Game</title>
66
<link data-trunk rel="sass" href="index.scss" />
77
</head>
88
<body></body>

ecg/index.scss

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,32 +4,42 @@ body {
44
margin: 0;
55
}
66

7-
body {
8-
align-items: center;
7+
.boards {
98
display: flex;
10-
justify-content: center;
11-
12-
background: linear-gradient(to bottom right, #444444, #009a5b);
13-
font-size: 1.5rem;
9+
flex-wrap: wrap;
10+
/* allows wrapping onto new lines */
11+
gap: 20px;
12+
/* optional space between boards */
1413
}
1514

16-
main {
17-
color: #fff6d5;
18-
font-family: sans-serif;
19-
text-align: center;
15+
.chessboard {
16+
display: grid;
17+
grid-template-columns: repeat(8, 50px);
18+
width: 400px;
19+
border: 2px solid #333;
20+
font-family: "FreeSerif", sans-serif;
21+
2022
}
2123

22-
.logo {
23-
height: 20em;
24+
.cell {
25+
width: 50px;
26+
height: 50px;
27+
display: flex;
28+
justify-content: center;
29+
align-items: center;
30+
font-size: 50px;
2431
}
2532

26-
.heart:after {
27-
content: "❤️";
33+
.light {
34+
background-color: #eee;
35+
}
2836

29-
font-size: 1.75em;
37+
.dark {
38+
background-color: #769656;
3039
}
3140

32-
h1 + .subtitle {
33-
display: block;
34-
margin-top: -1em;
41+
.highlight {
42+
background-color: #9af5ff;
43+
box-shadow: inset 0 0 0 2px #29495c;
3544
}
45+

ecg/src/app.rs

Lines changed: 57 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,65 @@
1+
use schach::board::Board;
2+
use schach::coord::Square;
3+
use schach::piece::Piece;
14
use yew::prelude::*;
25

36
#[function_component(App)]
47
pub fn app() -> Html {
8+
let board = Board::new();
9+
10+
let game = schach::game::GameState::new();
11+
let legals = game.core.legal_moves().collect::<Vec<_>>();
12+
513
html! {
6-
<main>
7-
<img class="logo" src="https://yew.rs/img/logo.svg" alt="Yew logo" />
8-
<h1>{ "Hello World!" }</h1>
9-
<span class="subtitle">{ "from Yew with " }<i class="heart" /></span>
14+
<main class="board-root">
15+
<h1>{ "Every Chess Game" }</h1>
16+
<div class="boards">
17+
{for legals.iter().map(|mv| html!{<BoardDisplay board={game.core.board} from_to={FromTo { from: mv.origin, to: mv.destination }}/>})}
18+
</div>
1019
</main>
1120
}
1221
}
22+
23+
#[derive(PartialEq)]
24+
pub struct FromTo {
25+
from: Square,
26+
to: Square,
27+
}
28+
#[derive(PartialEq, Properties)]
29+
pub struct BoardProps {
30+
pub board: Board,
31+
pub from_to: FromTo,
32+
}
33+
34+
#[function_component]
35+
pub fn BoardDisplay(props: &BoardProps) -> Html {
36+
let board = &props.board;
37+
let from_to = &props.from_to;
38+
39+
html! {
40+
<div class="chessboard">
41+
{
42+
for Square::ALL.into_iter().map(|sq| {
43+
let piece_opt = board[sq];
44+
let piece_char = piece_opt.map(|p|p.to_string()).unwrap_or_default();
45+
46+
let mut class = "cell".to_string();
47+
if sq == from_to.from || sq == from_to.to{
48+
class.push_str(" highlight");
49+
} else if sq.is_black(){
50+
class.push_str(" dark");
51+
} else {
52+
class.push_str(" light")
53+
}
54+
55+
56+
html! {
57+
<div class={class}>
58+
{ piece_char }
59+
</div>
60+
}
61+
})
62+
}
63+
</div>
64+
}
65+
}

schach/src/game.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ pub enum StepResult {
5353
Continued(GameState),
5454
}
5555
impl StepResult {
56-
pub(crate) fn game_state(self) -> GameState {
56+
#[must_use]
57+
pub fn game_state(self) -> GameState {
5758
match self {
5859
Self::Terminated(GameResult {
5960
final_game_state: game_state,

0 commit comments

Comments
 (0)