用 C++ 实现游戏机排队系统

本篇文章将使用 C++ 语言实现一个简单的游戏机排队系统。游戏机只能容纳两人同时游玩,而玩家数量可能超过两人,因此需要维护一个排队队列,并根据不同事件进行处理。

系统事件:

  • start: 一局游戏开始。若不是第一局游戏,则上一局的参与者按照原本的顺序回到队尾。此时你应该按在队列中的顺序输出这一局上场的人的名字(正常来讲是队列前两位或者唯一一个人),若有两个则以空格分割。若这一局无人上场,则输出 'Error' 并忽略这条事件。
  • arrive x: x 到达机厅并且将自己加入队尾,此时 x 不应该在排队,否则输出 'Error' 并忽略这条事件。若该事件成功执行则输出 'OK'。
  • leave x: x 离开机厅并离开队列。此时 x 应该在排队但不应该在游玩,否则输出 'Error' 并忽略这条事件。若该事件成功执行则输出 'OK'。

C++ 代码:

#include <iostream>
#include <queue>
#include <sstream>

using namespace std;

// 定义一个队列,用于存储排队的人
queue<string> waitingQueue;

// 输出队列中前两个人的名字
void printPlayers() {
    int count = 0;
    while (!waitingQueue.empty() && count < 2) {
        cout << waitingQueue.front() << " ";
        waitingQueue.pop();
        count++;
    }
    cout << endl;
}

int main() {
    string line;
    while (getline(cin, line)) {
        stringstream ss(line);
        string event;
        ss >> event;
        if (event == "start") {
            if (waitingQueue.size() < 2) {
                cout << "Error" << endl;
            } else {
                printPlayers();
            }
        } else if (event == "arrive") {
            string name;
            ss >> name;
            bool found = false;
            // 检查当前人是否已经在队列中
            queue<string> tempQueue;
            while (!waitingQueue.empty()) {
                if (waitingQueue.front() == name) {
                    found = true;
                }
                tempQueue.push(waitingQueue.front());
                waitingQueue.pop();
            }
            // 若当前人已经在队列中,则输出Error;否则将其加入队列
            if (found) {
                cout << "Error" << endl;
            } else {
                waitingQueue.push(name);
                cout << "OK" << endl;
            }
            // 恢复队列
            while (!tempQueue.empty()) {
                waitingQueue.push(tempQueue.front());
                tempQueue.pop();
            }
        } else if (event == "leave") {
            string name;
            ss >> name;
            bool found = false;
            // 检查当前人是否在游玩
            queue<string> tempQueue;
            while (!waitingQueue.empty()) {
                if (waitingQueue.front() == name) {
                    found = true;
                }
                tempQueue.push(waitingQueue.front());
                waitingQueue.pop();
            }
            // 若当前人在游玩,则输出Error;否则将其从队列中移除
            if (found && tempQueue.size() > 2) {
                cout << "Error" << endl;
            } else {
                while (!tempQueue.empty()) {
                    if (tempQueue.front() != name) {
                        waitingQueue.push(tempQueue.front());
                    }
                    tempQueue.pop();
                }
                cout << "OK" << endl;
            }
        }
    }
    return 0;
}

代码解析:

  1. 数据结构: 使用 queue<string> waitingQueue 来存储所有等待游戏的人员,使用队列的特点:先进先出,模拟排队逻辑。
  2. 事件处理:
    • start: 检查队列中是否有至少两人,如果有则输出队列前两位的名字,否则输出 'Error'。
    • arrive x: 检查 x 是否已经在队列中,如果在则输出 'Error',否则将 x 加入队列尾部,输出 'OK'。
    • leave x: 检查 x 是否在队列中且不在前两位(即不在游玩),如果在则将其从队列中移除,输出 'OK',否则输出 'Error'。
  3. 输出: 针对不同事件,根据代码逻辑进行相应的输出。

总结:

本文通过 C++ 代码实现了一个简单的游戏机排队系统,使用队列数据结构来存储玩家信息,并根据不同事件进行处理。代码逻辑清晰易懂,可供读者参考和学习。

后续改进:

  • 可以添加更多事件类型,例如:
    • reserve x: 玩家 x 预约游戏,加入预约队列。
    • cancel x: 玩家 x 取消预约。
  • 可以使用更复杂的算法来优化排队逻辑,例如优先级队列,以实现更灵活的排队规则。
  • 可以使用图形界面来展示游戏机排队情况,方便玩家查看。
C++ 游戏机排队系统实现:用队列管理玩家

原文地址: https://www.cveoy.top/t/topic/qinw 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录