C++游戏、线程和并发编程
C++ game, threads and concurrent programming
我迫切需要一些指导。我需要编写一个Connect 4的游戏程序,其中将应用线程并行编程。我需要自己学习,并在一周内写出这样的程序。我现在不知道如何实现并发部分,我应该寻找什么资源。。。希望你能对此有所了解…
这个想法似乎是计算机玩家本身就是一个线程,主线程处理游戏,即协调人类玩家和机器人玩家。
下面是一些(伪)代码,可以帮助您入门。
std::mutex m;
std::queue<int> from_opponent;
std::queue<int> from_robot;
void robotplayer(bool& go_on)
{
bool wait_for_opponent = true;
while (go_on)
{
int opponents_move;
while (wait_for_opponent)
{
// Spend x milliseconds planning next move
// ...
// ...
m.lock();
if (!from_opponent.empty())
{
opponents_move = from_opponent.front();
from_opponent.pop();
wait_for_opponent = false;
}
m.unlock();
}
// ... calculate robots move
// ...
m.lock();
from_robot.push(row_number);
m.unlock();
wait_for_opponent = true;
}
}
int main()
{
bool go_on = true;
std::thread robot(robotplayer, go_on);
while(go_on)
{
// Wait for input from user (e.g. via cin)
// Send move to robot
m.lock();
from_opponent.push(row_number);
m.unlock();
// Update display
// Check for a winner (i.e. set go_on = false)
// Wait for input from robot
// Update display
// Check for a winner (i.e. set go_on = false)
}
robot.join();
}
相关文章:
- 有一个打印语句的函数是一种糟糕的编程实践吗
- 控制允许动态运行c++的并发操作数
- 我是C++编程的新手,这些代码之间有什么区别,我应该使用哪一个
- 节俭并发:未解决的外部问题
- 模板元编程:如何将参数包组合成新的参数包
- Qt Q串行端口未编程设备未关闭
- 模板元编程 - 尝试实现维度分析
- 我是编程新手
- 并发/多线程:是否可以以这种方式生成相同的输出?
- C++编程从外部文本文件定义数组大小
- 了解算法的性能差异(如果以不同的编程语言实现)
- 使用 Gtkmm 以编程方式选择 Gtk::TextView 中的文本
- 如何将可变参数模板转换为多个单个模板?(C++竞争编程调试模板)
- 以编程方式锁定注册表项以避免并发问题
- 自C 11以来,挥发性在并发编程中的有用性
- C++和并发编程中的继承异常
- C++游戏、线程和并发编程
- QT、并发编程和向函数传递参数
- 内存管理 - C/C++ 中的并发编程、堆栈和堆
- Visual studio c++并发编程