Skip to content

Commit

Permalink
Progress #64
Browse files Browse the repository at this point in the history
  • Loading branch information
richelbilderbeek committed Dec 30, 2022
1 parent af7a978 commit 44c6978
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 22 deletions.
34 changes: 16 additions & 18 deletions game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -982,7 +982,7 @@ std::vector<user_input> convert_move_to_user_inputs(
const square from{get_from(g, m)};

std::vector<user_input> inputs;
// Move the cursor to target square
// Move the cursor to piece's square
{
const auto v{get_user_inputs_to_move_cursor_to(g, from, player_side)};
std::copy(std::begin(v), std::end(v), std::back_inserter(inputs));
Expand All @@ -992,26 +992,24 @@ std::vector<user_input> convert_move_to_user_inputs(
const auto i{get_user_input_to_select(g, player_side)};
inputs.push_back(i);
}
#ifdef FIX_ISSUE_64
// Do something with the piece
if (is_castling(m)) {
if (m.get_castling_type()[0] == castling_type::king_side)
{
const auto v{get_user_inputs_to_castle_kingside(g, from)};
std::copy(std::begin(v), std::end(v), std::back_inserter(inputs));
}
else
{
assert(m.get_castling_type()[0] == castling_type::queen_side);
const auto v{get_user_inputs_to_castle_queenside(g, from)};
std::copy(std::begin(v), std::end(v), std::back_inserter(inputs));
}
// Move the cursor to target's square
{
assert(!m.get_to().empty());
const auto v{
get_user_inputs_to_move_cursor_from_to(
g,
from,
m.get_to()[0],
player_side
)
};
std::copy(std::begin(v), std::end(v), std::back_inserter(inputs));
}
else
// Do the action
{
// TODO
const auto i{get_user_input_to_do_action_1(g, player_side)};
inputs.push_back(i);
}
#endif
return inputs;
}

Expand Down
42 changes: 38 additions & 4 deletions user_inputs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,21 @@ void do_select_and_move_piece(
}
}

user_input get_user_input_to_do_action_1(
const game& g,
const side player_side
)
{
if (get_controller_type(g, player_side) == controller_type::keyboard)
{
return create_press_action_1(player_side);
}
else
{
return create_press_lmb_action(player_side);
}
}

user_input get_user_input_to_select(
const game& g,
const side player_side
Expand All @@ -223,17 +238,36 @@ user_input get_user_input_to_select(

std::vector<user_input> get_user_inputs_to_move_cursor_to(
const game& g,
const square& s,
const square& to,
const side player_side
)
{
assert(
g.get_options().get_controller(player_side).get_type()
== controller_type::keyboard
);
const square from{get_player_pos(g, player_side)};
return get_user_inputs_to_move_cursor_from_to(
g,
from,
to,
player_side
);
}

std::vector<user_input> get_user_inputs_to_move_cursor_from_to(
const game& g,
const square& from,
const square& to,
const side player_side
)
{
assert(
g.get_options().get_controller(player_side).get_type()
== controller_type::keyboard
);
const auto current_pos{get_player_pos(g, player_side)};
const int n_right{(s.get_x() - square(current_pos).get_x() + 8) % 8};
const int n_down{(s.get_y() - square(current_pos).get_y() + 8) % 8};
const int n_right{(to.get_x() - from.get_x() + 8) % 8};
const int n_down{(to.get_y() - from.get_y() + 8) % 8};
std::vector<user_input> inputs;
for (int i{0}; i!=n_right; ++i)
{
Expand Down
18 changes: 18 additions & 0 deletions user_inputs.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,12 @@ void do_select_and_move_piece(
const side player_side
);

/// Create the user inputs to do action_1 at the square at the cursor
user_input get_user_input_to_do_action_1(
const game& g,
const side player_side
);

/// Create the user inputs to select the square at the cursor
user_input get_user_input_to_select(
const game& g,
Expand All @@ -113,6 +119,18 @@ std::vector<user_input> get_user_inputs_to_move_cursor_to(
const side player_side
);

/// Create the user inputs to move the cursor to a target square
/// knowing it will be at the 'from' square.
/// This is useful for creating future 'user_input's,
/// e.g. for white doing e4, the cursor must be moved to e2
/// to select a pawn, then to e4 to select the target.
std::vector<user_input> get_user_inputs_to_move_cursor_from_to(
const game& g,
const square& from,
const square& to,
const side player_side
);

bool is_empty(const user_inputs& inputs) noexcept;

/// Respond to action 1
Expand Down

0 comments on commit 44c6978

Please sign in to comment.