如何循环游戏追踪角色
How to looping a game tracking characters?
我试图创建一个简单的c++循环游戏,其中2名人类玩家,轮流,键入字母序列,必须匹配序列中的最后一个字母到他们的第一个或他们输了。
例如,如果玩家1输入:hey玩家2输入:you,玩家1输入:use,如果玩家2输入:hey,玩家2将会输,因为他没有以玩家1输入的最后一个字母(即使用的"e")开始他的字母序列。
我的代码中的问题是我不知道如何使程序跟踪每个玩家输入的最后一个字母和第一个字母来确定获胜者。
My code:
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;
int main(){
string word;
string word2;
bool userTurn = true;
cout << "Welcome to the last letter/first letter game! ";
cout << " Do you want to play first (y/n)? ";
char response;
if (!(cin >> response)) die("input failure");
response = static_cast<char>(toupper(response));
if (response == 'Y')
userTurn = true;
else if (response == 'N')
userTurn = false;
else
die(" youre suppose to answer y or n");
while (true){
if (userTurn){
cout << "Player #1: " << endl;
cin >> word;
cout << "Player #2: " << endl;
cin >> word2;
} if (userTurn && word != word2){
cout << " Player 2 Wins! ";
}
} userTurn != userTurn;
}
我建议在这个程序中使用OOP过程。
测试程序演示跟踪每次运行的第一个和最后一个字符。
#include <iostream>
#include <string>
int main()
{
std::string word1, word2;
char last, first;
std::cout << "player1: ";
std::cin >> word1;
last = word1[word1.length() - 1]; //last character of player 1
while (true)
{
std::cout << "player2: ";
std::cin >> word2;
first = word2[0]; //first character of player 2
if (first != last)
{
std::cout << "player2 loss" << std::endl;
break;
}
last = word2[word2.length() - 1]; //last character of player 2
std::cout << "player1: ";
std::cin >> word1;
first = word1[0]; //first character of player 1
if (first != last)
{
std::cout << "player1 loss" << std::endl;
break;
}
}
return 0;
}
要获得std::string
的第一个/最后一个字符,使用std::string::front()
/std::string::back()
,例如word.back() != word2.front()
。
还有,将来试着和google交朋友。你会找到一个解决方案,虽然不是你给你的问题的标题,但与你的问题没有任何关系。
相关文章:
- 用callgrind追踪不必要的副本
- 在C++游戏中与库存系统作斗争
- 换位表导致测试失败(但在游戏中运行良好)
- Craps游戏问题,忽略if语句
- 矢量下标超出SFML游戏中的范围
- 我是 c++ 的新手.学习基本知识后,我想做井字游戏.对于印刷板,我在下面写代码,但它显示错误
- 我在贪吃蛇游戏中收到了错误代码 -1073741571
- 我应该在简单的策略游戏中为各个派系使用类吗 - C++
- 游戏体验和升级
- 光线追踪:椭球命中功能
- 我的代码运行良好,但在游戏循环中中断
- 如何使用 SFML 在贪吃蛇游戏中定义游戏结束?
- 我可以使用任何好的逻辑来阻止计算机将 O 放在井字游戏中的现有 X 上
- 将鼠标和键盘输入发送到 unity3d 游戏 (Rust)
- 在猜谜游戏 c++ 中不计算尝试次数
- 在C++不适用于猜数字游戏的情况下再次播放选项
- 在为我的基于文本的 RPG 游戏制作库存时遇到困难
- 在定义字符数组(井字游戏)的 for 循环中应用输入限制
- 如何循环游戏追踪角色
- 追踪用户登陆桌面游戏的位置