使用线程停止操作(Using a thread to stop an action)
我正在为Scattegories游戏开发一个程序。 在程序中有一个表,当定时器线程在后台工作时,玩家输入单词。 我希望当时间结束时,玩家将无法再输入并且轮到他了。
我怎么能做到这一点?
表和计时器:
void timer() { cout &t;< "You got 2 minutes to finish\n"; //Changing the duration of the timer is done by changing the value of 'i' in the "for" loop for (int i = 120; i > 0; i--) { sleep_for(1s); } cout << "DING DONG!!! DING DONG!!! Time's up!!!\n"; } void table(int plr) { string ctr[12] = { "A cuntry", "A city", "An animal", "A plant", "An object", "A name", "Food", "Drink", "A game", "A movie", "A book", "A famous person" }; string lst[6][12]; //first dimantion: how many players. second dimantion: how many catagories, third dimantion(if added) will be the round cin.ignore(); //To avoid the "getline" reading the last input for (int x = 0; x<plr; x++) //the player changes only after the previus player finishes { std::thread t1(timer); //gives time to write the words. Optimaly it would finish the round for player when time is up t1.detach(); cout << "When the timer ends please enter '0' in the remaining catagories\n"; for (int i = 0; i<12; i++) //changing catagory { cout << ctr[i] << ": "; getline(cin, lst[x][i]); } system("cls"); cout << "Next player\n"; } }
完整代码:
// A program to keep track of points and time and to give a random letter for the game scattergories #include<iostream> #include<ctime> #include<string> #include <chrono> #include <thread> using std::cout; using std::cin; using std::string; using std::getline; using namespace std::chrono_literals; using std::this_thread::sleep_for; void ltr() //gives a random letter { char letter; letter = rand() % 26 + 65; //assigns a random letter in ascii code to a char (resulting in a random letter) cout << "The letter is " << letter << "\n"; } void timer() { cout << "You got 2 minutes to finish\n"; //Changing the duration of the timer is done by changing the value of 'i' in the "for" loop for (int i = 120; i > 0; i--) { sleep_for(1s); } cout << "DING DONG!!! DING DONG!!! Time's up!!!\n"; } void table(int plr) { string ctr[12] = { "A cuntry", "A city", "An animal", "A plant", "An object", "A name", "Food", "Drink", "A game", "A movie", "A book", "A famous person" }; string lst[6][12]; //first dimantion: how many players. second dimantion: how many catagories, third dimantion(if added) will be the round cin.ignore(); //To avoid the "getline" reading the last input for (int x = 0; x<plr; x++) //the player changes only after the previus player finishes { std::thread t1(timer); //gives time to write the words. Optimaly it would finish the round for player when time is up t1.detach(); cout << "When the timer ends please enter '0' in the remaining catagories\n"; for (int i = 0; i<12; i++) //changing catagory { cout << ctr[i] << ": "; getline(cin, lst[x][i]); } system("cls"); cout << "Next player\n"; } for (int x = 0; x<plr; x++) //this part (the whole "for" loop) is for confirming evreything is writen down { cout << "Player number " << x + 1 << ": "; for (int i = 0; i<12; i++) { cout << lst[x][i] << " "; } cout << "\n"; } sleep_for(5s); } int points() //points gained per round { int a, b, c, sum; cout << "How many sections only you got?\n"; //worth 15 points cin >> a; cout << "How many words only you got?\n"; //worth 10 points cin >> b; cout << "How many words you and another person got?\n"; //worth 5 points cin >> c; sum = a * 15 + b * 10 + c * 5; return sum; //Note: It doesn't matter how many sections there are. } int act() //running the program { int Players, Points[6]; cout << "How many people are playing? (Up to six players)"; cin >> Players; ltr(); table(Players); //Points = points(); cout << "You have earned " << Points << " this round\n\n"; return 1; } int main() { auto start = std::chrono::high_resolution_clock::now(); srand(time(NULL)); //gives a differant pattern of letters every time int Points; Points = act(); for (;;) //inf loop { int ph; cout << "Press 1 to continue or anything else to stop\n"; cin >> ph; if (ph == 1) { Points += act(); //keeping score of the rounds } else { auto end = std::chrono::high_resolution_clock::now(); break; } } cout << "You have earned a total of " << Points << " great job!"; sleep_for(5s); //time to read the last text return 0; } /* To do list: -Make timer stop the table when time is up -Check if words in the table (for differant players) are the same and give points accordingly -Check if words are actual words (connect an online dictonary?) -Make interface? (if possible and I have time to learn how) -Comment rest of the code */
PS
我正在努力保持这段代码的便携性,所以我喜欢这种方式的sugestions。 但我会感激任何建议。 我正在使用Windows 10(但我正在编写控制台应用程序)我可以访问c ++ 11和c ++ 14
I am working on a program for the game Scattegories. In the program there is a table that the player inputs the words while a timer thread is working in the background. I would that when the time is over, the player won't be able to input anymore and for his turn to end.
How can I make that happen?
The table and the timer:
void timer() { cout << "You got 2 minutes to finish\n"; //Changing the duration of the timer is done by changing the value of 'i' in the "for" loop for (int i = 120; i > 0; i--) { sleep_for(1s); } cout << "DING DONG!!! DING DONG!!! Time's up!!!\n"; } void table(int plr) { string ctr[12] = { "A cuntry", "A city", "An animal", "A plant", "An object", "A name", "Food", "Drink", "A game", "A movie", "A book", "A famous person" }; string lst[6][12]; //first dimantion: how many players. second dimantion: how many catagories, third dimantion(if added) will be the round cin.ignore(); //To avoid the "getline" reading the last input for (int x = 0; x<plr; x++) //the player changes only after the previus player finishes { std::thread t1(timer); //gives time to write the words. Optimaly it would finish the round for player when time is up t1.detach(); cout << "When the timer ends please enter '0' in the remaining catagories\n"; for (int i = 0; i<12; i++) //changing catagory { cout << ctr[i] << ": "; getline(cin, lst[x][i]); } system("cls"); cout << "Next player\n"; } }
Full code:
// A program to keep track of points and time and to give a random letter for the game scattergories #include<iostream> #include<ctime> #include<string> #include <chrono> #include <thread> using std::cout; using std::cin; using std::string; using std::getline; using namespace std::chrono_literals; using std::this_thread::sleep_for; void ltr() //gives a random letter { char letter; letter = rand() % 26 + 65; //assigns a random letter in ascii code to a char (resulting in a random letter) cout << "The letter is " << letter << "\n"; } void timer() { cout << "You got 2 minutes to finish\n"; //Changing the duration of the timer is done by changing the value of 'i' in the "for" loop for (int i = 120; i > 0; i--) { sleep_for(1s); } cout << "DING DONG!!! DING DONG!!! Time's up!!!\n"; } void table(int plr) { string ctr[12] = { "A cuntry", "A city", "An animal", "A plant", "An object", "A name", "Food", "Drink", "A game", "A movie", "A book", "A famous person" }; string lst[6][12]; //first dimantion: how many players. second dimantion: how many catagories, third dimantion(if added) will be the round cin.ignore(); //To avoid the "getline" reading the last input for (int x = 0; x<plr; x++) //the player changes only after the previus player finishes { std::thread t1(timer); //gives time to write the words. Optimaly it would finish the round for player when time is up t1.detach(); cout << "When the timer ends please enter '0' in the remaining catagories\n"; for (int i = 0; i<12; i++) //changing catagory { cout << ctr[i] << ": "; getline(cin, lst[x][i]); } system("cls"); cout << "Next player\n"; } for (int x = 0; x<plr; x++) //this part (the whole "for" loop) is for confirming evreything is writen down { cout << "Player number " << x + 1 << ": "; for (int i = 0; i<12; i++) { cout << lst[x][i] << " "; } cout << "\n"; } sleep_for(5s); } int points() //points gained per round { int a, b, c, sum; cout << "How many sections only you got?\n"; //worth 15 points cin >> a; cout << "How many words only you got?\n"; //worth 10 points cin >> b; cout << "How many words you and another person got?\n"; //worth 5 points cin >> c; sum = a * 15 + b * 10 + c * 5; return sum; //Note: It doesn't matter how many sections there are. } int act() //running the program { int Players, Points[6]; cout << "How many people are playing? (Up to six players)"; cin >> Players; ltr(); table(Players); //Points = points(); cout << "You have earned " << Points << " this round\n\n"; return 1; } int main() { auto start = std::chrono::high_resolution_clock::now(); srand(time(NULL)); //gives a differant pattern of letters every time int Points; Points = act(); for (;;) //inf loop { int ph; cout << "Press 1 to continue or anything else to stop\n"; cin >> ph; if (ph == 1) { Points += act(); //keeping score of the rounds } else { auto end = std::chrono::high_resolution_clock::now(); break; } } cout << "You have earned a total of " << Points << " great job!"; sleep_for(5s); //time to read the last text return 0; } /* To do list: -Make timer stop the table when time is up -Check if words in the table (for differant players) are the same and give points accordingly -Check if words are actual words (connect an online dictonary?) -Make interface? (if possible and I have time to learn how) -Comment rest of the code */
P.S
I'm tying to keep this code portable so I preffer sugestions that will keep it this way. But I would appreciate any suggestion. I'm using windows 10 (but I'm writing console aplications) I have access to c++11 and c++14
原文:https://stackoverflow.com/questions/42031258
最满意答案
Puppet由您的VM上的VM运行。 确保仍为用户vagrant和root安装gem。 它也可能是你的默认ruby版本的转换(系统vs通过rvm或rbenv安装?)。
希望这可以帮助。
Puppet is run by you VM on your VM. Make sure the gem is still installed for users vagrant and root. It could be a switch of your default ruby version too (system vs installed via rvm or rbenv ?).
Hope this helps.
相关问答
更多-
Puppet Postgresql错误(Puppet Postgresql Error)[2023-05-16]
@ ChrisPitman的评论指出了我正确的方向。 我需要设置正确的模块路径,以包括我们的自定义模块和预先构建的模块。 以下对我有用: sudo puppet apply --modulepath=/vagrant/puppet/modules:/etc/puppet/modules -e "include iwd-postgresql" @ChrisPitman's comments pointed me in the right direction. I needed to set up the c ... -
只有在Puppetfile中使用“metadata”命令时,或者如果工作目录中没有Puppetfile但是有一个metadata.json,并且metadata.json没有'dependencies'部分,那么该错误才会发生。 您发布的Puppetfile在这里工作正常,检查是否正在正确的目录中调用librarian-puppet并且metadata.json有好处 That error would only happen if you are using the "metadata" command i ...
-
在Vagrant多机器环境中使用Puppet的模块的范围(Scope of modules using Puppet in a Vagrant multi machine environment)[2023-07-31]
定义图书管理员应该找到木偶文件的位置 Vagrant.configure("2") do |config| config.librarian_puppet.puppetfile_dir = "puppet/box1" config.vm.provision :puppet do |puppet| puppet.manifests_path = "puppet/box_1/manifests" puppet.manifest_file = "site.pp" end defi ... -
通过puppet设置nginx(Setting up nginx via puppet)[2022-02-04]
检查您是否正确配置了Vagrant: 必须启用Puppet配置 您可以尝试显式设置module_path 尝试将目录puppet-nginx重命名为nginx (或创建符号链接) Check that you configured Vagrant properly: provisioning with Puppet must be enabled you can try to set module_path explicitly try to rename directory puppet-nginx to ... -
您需要使用puppet模块路径配置vagrant 。 另外,您通常还会将清单和模块文件夹保存在同一文件夹中,而不是清单中的模块。 You need to configure vagrant with the puppet module path. On a side note, you would also usually keep the manifest and module folder in the same folder, instead of modules inside manifests.
-
PuPHPet使用Example42 Apache Puppet模块,请参阅README文件。 该模块在vhost资源中没有目录参数: https://github.com/puphpet/puphpet-apache/blob/master/manifests/vhost.pp#L100 只有Puppet Labs Apache模块有一个目录参数,但它是一个不同的模块: https://github.com/puppetlabs/puppetlabs-apache/blob/master/manifest ...
-
流浪汉用于木偶供应的确切傀儡命令是什么?(What is the exact puppet command that vagrant uses for the puppet provisioning?)[2022-08-24]
它是开源的,你可以随时查看源代码: plugins / provisioners / puppet / provisioner / puppet.rb 。 相关方法是run_puppet_apply 。 和/或您可以在测试配置上启用详细日志记录并检查日志以查看命令行。 我有另一个不受vagrant管理的VM,并希望在其上应用puppet配置。 我想使用vagrant正在使用的'puppet apply'命令。 那不行。 确切的vagrant puppet provisioning命令包含对vagrant文件 ... -
根据数据库 , precise没有任何Ruby 1.9包装。 在此平台上,您需要为某些ruby1.9.3或类似软件包添加存储库。 切换到更新操作系统的基本映像可能要容易得多。 According to the database, precise had no Ruby 1.9 packaging whatsoever. On this platform, you will need to add a repository for some ruby1.9.3 or similar packages. It ...
-
Puppet由您的VM上的VM运行。 确保仍为用户vagrant和root安装gem。 它也可能是你的默认ruby版本的转换(系统vs通过rvm或rbenv安装?)。 希望这可以帮助。 Puppet is run by you VM on your VM. Make sure the gem is still installed for users vagrant and root. It could be a switch of your default ruby version too (system ...
-
2选项: 1)你可以让vagrant读取你从文件中显示的哈希值(放在它的目录,数据库中,等等),然后使用该内容生成Vagrantfile。 Vagrant的配置只运行一些ruby代码(我认为)如果没有 2)让shell脚本生成Vagrantfile和/或puppet config / static文件。 实施细节是微不足道的。 2 Options: 1) You can have vagrant read a hash that you shown from a file ( placed in it's ...