Skip to content

Commit 53be168

Browse files
author
richelbilderbeek
committed
Can do 1 en-passant, progress #21
1 parent 5d1364b commit 53be168

File tree

4 files changed

+70
-40
lines changed

4 files changed

+70
-40
lines changed

game.cpp

+44-37
Original file line numberDiff line numberDiff line change
@@ -480,57 +480,64 @@ std::vector<piece_action> collect_all_pawn_en_passant_actions(
480480
if (color == chess_color::black)
481481
{
482482
if (x != 3) return {};
483+
if (y > 0)
483484
{
484-
if (y > 0)
485+
const square to_square{square(x - 1, y - 1)};
486+
const square enemy_square{square(x, y - 1)};
487+
if (is_empty(g, to_square)
488+
&& is_piece_at(g, enemy_square)
489+
&& get_piece_at(g, enemy_square).get_color() == enemy_color
490+
&& has_just_double_moved(get_piece_at(g, enemy_square), g.get_time())
491+
)
485492
{
486-
const square to_square{square(x - 1, y - 1)};
487-
const square enemy_square{square(x, y - 1)};
488-
if (is_empty(g, to_square)
489-
&& is_piece_at(g, enemy_square)
490-
&& get_piece_at(g, enemy_square).get_color() == enemy_color
491-
&& has_just_double_moved(get_piece_at(g, enemy_square), g.get_time())
492-
)
493-
{
494-
actions.push_back(piece_action(color, type, piece_action_type::en_passant, from, to_square));
495-
}
493+
actions.push_back(piece_action(color, type, piece_action_type::en_passant, from, to_square));
496494
}
497-
if (y < 7)
495+
}
496+
if (y < 7)
497+
{
498+
const square to_square{square(x - 1, y + 1)};
499+
const square enemy_square{square(x, y + 1)};
500+
if (is_empty(g, to_square)
501+
&& is_piece_at(g, enemy_square)
502+
&& get_piece_at(g, enemy_square).get_color() == enemy_color
503+
&& has_just_double_moved(get_piece_at(g, enemy_square), g.get_time())
504+
)
498505
{
499-
const square to_square{square(x - 1, y + 1)};
500-
const square enemy_square{square(x, y + 1)};
501-
if (is_empty(g, to_square)
502-
&& is_piece_at(g, enemy_square)
503-
&& get_piece_at(g, enemy_square).get_color() == enemy_color
504-
&& has_just_double_moved(get_piece_at(g, enemy_square), g.get_time())
505-
)
506-
{
507-
actions.push_back(piece_action(color, type, piece_action_type::en_passant, from, to_square));
508-
}
506+
assert(!"YAY");
507+
actions.push_back(piece_action(color, type, piece_action_type::en_passant, from, to_square));
509508
}
510509
}
511510
}
512511
else
513512
{
514513
assert(color == chess_color::white);
515-
assert(get_rank(s) != 1);
516-
// Attack
517-
if (x != 7)
514+
if (x != 4) return {};
515+
if (y > 0)
518516
{
519-
if (y > 0)
517+
const square to_square{square(x + 1, y - 1)};
518+
const square enemy_square{square(x, y - 1)};
519+
if (is_empty(g, to_square)
520+
&& is_piece_at(g, enemy_square)
521+
&& get_piece_at(g, enemy_square).get_color() == enemy_color
522+
&& has_just_double_moved(get_piece_at(g, enemy_square), g.get_time())
523+
)
520524
{
521-
const square enemy_square{square(x + 1, y - 1)};
522-
if (is_piece_at(g, enemy_square) && get_piece_at(g, enemy_square).get_color() == enemy_color)
523-
{
524-
actions.push_back(piece_action(color, type, piece_action_type::attack, from, enemy_square));
525-
}
525+
assert(!"YAY");
526+
actions.push_back(piece_action(color, type, piece_action_type::en_passant, from, to_square));
526527
}
527-
if (y < 7)
528+
}
529+
if (y < 7)
530+
{
531+
const square to_square{square(x + 1, y + 1)};
532+
const square enemy_square{square(x, y + 1)};
533+
if (is_empty(g, to_square)
534+
&& is_piece_at(g, enemy_square)
535+
&& get_piece_at(g, enemy_square).get_color() == enemy_color
536+
&& has_just_double_moved(get_piece_at(g, enemy_square), g.get_time())
537+
)
528538
{
529-
const square enemy_square{square(x + 1, y + 1)};
530-
if (is_piece_at(g, enemy_square) && get_piece_at(g, enemy_square).get_color() == enemy_color)
531-
{
532-
actions.push_back(piece_action(color, type, piece_action_type::attack, from, enemy_square));
533-
}
539+
assert(!"YAY");
540+
actions.push_back(piece_action(color, type, piece_action_type::en_passant, from, to_square));
534541
}
535542
}
536543
}

piece_action.cpp

+13
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#include "piece.h"
44

5+
#include <algorithm>
56
#include <cassert>
67
#include <iostream>
78
#include <sstream>
@@ -49,6 +50,18 @@ piece_action get_test_piece_action() noexcept
4950
);
5051
}
5152

53+
bool has_action_of_type(
54+
const std::vector<piece_action>& actions,
55+
const piece_action_type t
56+
)
57+
{
58+
return std::find_if(
59+
std::begin(actions),
60+
std::end(actions),
61+
[t](const piece_action& action) { return action.get_action_type() == t; }
62+
) != std::end(actions);
63+
}
64+
5265
bool is_double_move(const piece_action& a) noexcept
5366
{
5467
return a.get_piece_type() == piece_type::pawn

piece_action.h

+6
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,12 @@ std::string describe_action(const piece_action& p);
5656
/// Get a piece_action to be used in testing
5757
piece_action get_test_piece_action() noexcept;
5858

59+
/// Is there at least one action of the specified type?
60+
bool has_action_of_type(
61+
const std::vector<piece_action>& actions,
62+
const piece_action_type t
63+
);
64+
5965
/// Is this a pawn moving two squares forward?
6066
bool is_double_move(const piece_action& a) noexcept;
6167

test_game.cpp

+7-3
Original file line numberDiff line numberDiff line change
@@ -496,16 +496,20 @@ void test_game_functions()
496496
);
497497
assert(!is_in(ke8d8, actions));
498498
}
499-
//#define FIX_ISSUE_21
499+
#define FIX_ISSUE_21
500500
#ifdef FIX_ISSUE_21
501-
// 21: can do en-passant
501+
// 21: can do en-passant, black h4xg3
502502
{
503503
game g{
504504
get_game_with_starting_position(starting_position_type::before_en_passant)
505505
};
506506
do_select_and_move_keyboard_player_piece(g, "g2", "g4");
507+
// It takes 1 time unit to move,
508+
// aim at halfway to window of opportunity for en-passant
509+
for (int i{0}; i!=6; ++i) g.tick(delta_t(0.25));
507510
const auto actions{collect_all_actions(g)};
508511
assert(!actions.empty());
512+
assert(has_action_of_type(actions, piece_action_type::en_passant));
509513

510514
const piece_action h4xg3ep(
511515
chess_color::black,
@@ -520,8 +524,8 @@ void test_game_functions()
520524
g.tick(delta_t(1.0));
521525
const auto actions_again{collect_all_actions(g)};
522526
assert(!is_in(h4xg3ep, actions_again));
523-
assert(!"Progress #21");
524527
}
528+
//assert(!"Progress #21");
525529
#endif // FIX_ISSUE_21
526530
}
527531
// count_control_actions

0 commit comments

Comments
 (0)