如何循环跟踪游戏中的人物? 我试图创建一个简单的C++循环游戏,其中2个人玩家轮流,在一系列字母中键入,并且必须与序列中的最后一个字母匹配到它们的第一个或它们丢失。p>
例如,如果玩家1输入:嘿 玩家2输入:你,玩家1输入:使用,如果玩家2输入:嘿,玩家2将失败,因为他没有用玩家1输入的最后一个字母(使用中的“e”)开始他的字母序列 我的代码中的问题是,我不知道如何让程序跟踪每个玩家输入的最后一个字母和第一个字母,以确定获胜者。如何循环跟踪游戏中的人物? 我试图创建一个简单的C++循环游戏,其中2个人玩家轮流,在一系列字母中键入,并且必须与序列中的最后一个字母匹配到它们的第一个或它们丢失。p>,c++,loops,for-loop,while-loop,C++,Loops,For Loop,While Loop,例如,如果玩家1输入:嘿 玩家2输入:你,玩家1输入:使用,如果玩家2输入:嘿,玩家2将失败,因为他没有用玩家1输入的最后一个字母(使用中的“e”)开始他的字母序列 我的代码中的问题是,我不知道如何让程序跟踪每个玩家输入的最后一个字母和第一个字母,以确定获胜者。 #include <iostream> #include <cstdlib> #include <string> using namespace std; i
#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;
}
我的代码:
#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;
}
#包括
#包括
#包括
使用名称空间std;
int main(){
字符串字;
字符串字2;
bool userTurn=true;
cout-response)芯片(“输入故障”);
响应=静态_cast(toupper(响应));
如果(响应='Y')
userTurn=true;
else if(响应='N')
userTurn=false;
其他的
死(“你应该回答y或n”);
while(true){
如果(userTurn){
单词;
cout-word2;
}if(userTurn&&word!=word2){
cout要获取std::string
的第一个/最后一个字符,请使用std::string::front()
/std::string::back()
例如word.back()!=word2.front()
此外,今后尽量与谷歌交朋友。你会找到一个解决方案,尽管不是通过你提出的问题的标题,这与你的问题无关。我建议你在这个程序中使用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;
}
#包括
#包括
int main()
{
std::字符串word1,word2;
最后,首先;
std::cout>word1;
last=word1[word1.length()-1];//播放器1的最后一个字符
while(true)
{
std::cout>word2;
first=word2[0];//播放器2的第一个字符
如果(第一个!=最后一个)
{
例如,plyer2知道player1输入的单词吗?如果任何一个玩家调用一个已经使用过的单词,该怎么办?我有一个更好的代码!:)谢谢!!我不知道那种类型的函数!@Beezy我知道你还没有接受任何问题的答案。你为什么不接受?是时候改变了。