Пример #1
0
/**
 * Unit test of function: SquareState player_color(Player p).
 */
void test_player_color(void)
{
  CU_ASSERT(BLACK_SQUARE == player_color(BLACK_PLAYER));
  CU_ASSERT(WHITE_SQUARE == player_color(WHITE_PLAYER));
}
Пример #2
0
void Game::message_handler(Server& server, int player, std::string msg)
{
    std::vector<std::string> words;
    boost::trim(msg);
    boost::split(words, msg, boost::is_any_of("\t "), boost::token_compress_on);

    if (words.empty()) {
        error(server, player);
        return;
    }

    if (words[0] == "ready") {
        if (ready(player)) {
            error(server, player);
            return;
        }

        ready(player) = true;
        player_color(player) = ready(other(player)) ? BLACK : WHITE;
        server.send(player, "color " + show(player_color(player)));

        if (ready1 && ready2) {
            reset_playing();
            server.broadcast("start");
        }
    } else if (words[0] == "say") {
        std::stringstream ss;
        ss << "say" << player;
        for (size_t i = 1; i < words.size(); ++i) {
            ss << " " << words[i];
        }
        server.broadcast(ss.str());
    } else if (words[0] == "move") {
        if (!playing || current_color != player_color(player) ||
            words.size() != 3) {
            server.send(player, "error move");
            return;
        }

        boost::optional<Square> maybe_from = read_square(words[1]);
        boost::optional<Square> maybe_to = read_square(words[2]);
        if (!maybe_from || !maybe_to) {
            error(server, player);
            return;
        }

        boost::optional<MoveResult> maybe_move_result =
            try_move(board, player_color(player), *maybe_from, *maybe_to);
        if (!maybe_move_result) {
            server.send(player, "error move");
            return;
        }

        current_color = current_color == WHITE ? BLACK : WHITE;
        if (maybe_move_result->opponent_cannot_move) {
            reset_waiting();
        }

        server.broadcast(show(*maybe_move_result));
    } else if (words[0] == "resign") {
        if (!playing || current_color != player_color(player)) {
            error(server, player);
            return;
        }

        reset_waiting();
        server.broadcast("resign");
    } else {
        error(server, player);
    }
}