void TuringMachine3d::step()
{
    if (crashed and verbose)
        std::cout << "Machine has crashed" << std::endl;
    
    move_no++;
    Symbol sym = read_square();
    
    Instruction instr;
    try
    {
        instr = rulebook->get_instruction(state, sym);
    }
    catch (std::out_of_range)
    {
        if (verbose)
            std::cout << "Machine has crashed" << std::endl;
        crashed = true;
        return;
    }
    
    if (verbose)
        std::cout << "Move " << move_no <<
        ", pos " << position << ": " <<
        '(' << state << "," << sym << ")->(" <<
        instr.new_state << "," << instr.new_symbol << "), move by " <<
        instr.direction << std::endl;
    
    write(instr.new_symbol);
    move(instr.direction);
    change_state(instr.new_state);
}
Exemple #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);
    }
}