是的,我是一名大学生。不,我们还没有研究过数组或更高级的编程。我们在本书的第4章。 好吧,根据我上一次尝试的反馈,我认为这次发布得很好。遗憾!
这是代码现在冻结的地方。
else if (human_turn == false)
{
switch (num_toothpicks)
{
case '4':
std::cout << "The computer takes 3 toothpicks from the pile.\n";
computer_pick = 3;
num_toothpicks = num_toothpicks - computer_pick;
std::cout << "There is only one toothpick left in the pile. You lose this game.\n";
human_turn = true;
break;
case '3':
std::cout << "The computer takes 2 toothpicks from the pile.\n";
computer_pick = 2;
num_toothpicks = num_toothpicks - computer_pick;
std::cout << "There is only one toothpick left in the pile. You lose this game.\n";
human_turn = true;
break;
case '2':
std::cout << "The computer takes 1 toothpick from the pile.\n";
computer_pick = 1;
num_toothpicks = num_toothpicks - computer_pick;
std::cout << "There is only one toothpick left in the pile.\n";
human_turn = true;
break;
}
}
它正确执行,除非用户要求计算机执行上面的switch语句。
完整代码:
#include <iostream>
#include <cstdlib>
#include <ctime>
/*~~~~~~~~~~~~~~~~~~~~~~~~~
*K
*created 2/20/16
*23 Toothpicks Game
---Allows the user to play a game of 23 against the computer
~~~~~~~~~~~~~~~~~~~~~~~~~*/
int main()
{
srand(time(NULL)); //seed for random number
auto Random_computer_pick = (rand() % 3) + 1; //computer chooses a random number between 1 and 3
int user_pick, computer_pick;
int game_number = 1;
char keep_going;
int num_toothpicks = 23;
bool human_turn = true;
do {
std::cout << "23 is a two-player game that begins with a pile of 23 tooth picks.\n"
<< "Players take turns, withdrawing either 1, 2, or 3 toothpicks at a time.\n"
<< "The player to withdraw the last toothpick loses the game.\n"
<< "You will be playing against the computer.\n\n\n\n";
if (game_number % 2 != 0)
{
do
{
if (num_toothpicks > 4)
{
//userpick
std::cout << "There are " << num_toothpicks << ". Please choose to pull 1, 2, or 3 toothpicks from the pile.\n";
std::cin >> user_pick;
/*Make sure the user picks 1, 2, or 3*/
if (user_pick != 1 && user_pick != 2 && user_pick != 3)
{
std::cout << "Invalid entry. Please enter 1, 2, or 3.\n";
std::cin >> user_pick;
}
/*End Make Sure the user picks 1 2 3*/
num_toothpicks = num_toothpicks - user_pick;
human_turn = false;
std::cout << "There are " << num_toothpicks << " left in the pile.\n\n\n";
//computer pick
computer_pick = 4 - user_pick;
num_toothpicks = num_toothpicks - computer_pick;
human_turn = true;
std::cout << "The computer chose " << computer_pick << ". That leaves " << num_toothpicks << ".\n";
}
else if ((num_toothpicks <= 4) && (num_toothpicks != 1))
{
if (human_turn == true)
{
std::cout << "There are " << num_toothpicks << ". Please choose to pull 1, 2, or 3 toothpicks from the pile.\n";
std::cin >> user_pick;
human_turn = false;
/*Make sure the user picks 1, 2, or 3*/
if (user_pick != 1 && user_pick != 2 && user_pick != 3)
{
std::cout << "Invalid entry. Please enter 1, 2, or 3.\n";
std::cin >> user_pick;
}
/*End Make Sure the user picks 1 2 3*/
num_toothpicks = num_toothpicks - user_pick;
std::cout << "There are " << num_toothpicks << " left in the pile.\n\n\n";
}
else if (human_turn == false)
{
switch (num_toothpicks)
{
case '4':
std::cout << "The computer takes 3 toothpicks from the pile.\n";
computer_pick = 3;
num_toothpicks = num_toothpicks - computer_pick;
std::cout << "There is only one toothpick left in the pile. You lose this game.\n";
human_turn = true;
break;
case '3':
std::cout << "The computer takes 2 toothpicks from the pile.\n";
computer_pick = 2;
num_toothpicks = num_toothpicks - computer_pick;
std::cout << "There is only one toothpick left in the pile. You lose this game.\n";
human_turn = true;
break;
case '2':
std::cout << "The computer takes 1 toothpick from the pile.\n";
computer_pick = 1;
num_toothpicks = num_toothpicks - computer_pick;
std::cout << "There is only one toothpick left in the pile.\n";
human_turn = true;
break;
}
}
}
else if (num_toothpicks == 1)
{
if (human_turn == true)
{
std::cout << "You are stuck with the last toothpick. You lost. Sorry!\n\n";
num_toothpicks--;
game_number++;
}
if (human_turn == false)
{
std::cout << "The computer was forced to take the last toothpick. The user won the game!\n\n";
num_toothpicks--;
game_number++;
}
}
} while (num_toothpicks > 0);
std::cout << "Do you wish to play again? Enter Y to continue, or N to close the program.\n";
num_toothpicks = 23;
std::cin >> keep_going;
std::cout << std::endl;
if (keep_going != 'Y' && keep_going != 'y' && keep_going != 'N' && keep_going != 'n')
{
std::cout << "Invalid entry. Do you wish to play again ? Enter Y to continue, or N to close the program.\n";
std::cin >> keep_going;
}
if (keep_going == 'N' || keep_going == 'n')
{
std::cout << "Thanks for playing!\n";
break;
}
}
else if (game_number % 2 == 0)
{
do
{
if (num_toothpicks == 23)
{
std::cout << "23 is a two-player game that begins with a pile of 23 tooth picks.\n"
<< "Players take turns, withdrawing either 1, 2, or 3 toothpicks at a time.\n"
<< "The player to withdraw the last toothpick loses the game.\n"
<< "You will be playing against the computer.\n\n\n\n";
num_toothpicks = num_toothpicks - Random_computer_pick;
std::cout << "The computer has chosen to pull " << Random_computer_pick << " from the pile.\n";
std::cout << "That leaves " << num_toothpicks << ".\n\n\n";
human_turn = true;
}
if (num_toothpicks > 4)
{
//userpick
std::cout << "There are " << num_toothpicks << ". Please choose to pull 1, 2, or 3 toothpicks from the pile.\n";
std::cin >> user_pick;
/*Make sure the user picks 1, 2, or 3*/
if (user_pick != 1 && user_pick != 2 && user_pick != 3)
{
std::cout << "Invalid entry. Please enter 1, 2, or 3.\n";
std::cin >> user_pick;
}
/*End Make Sure the user picks 1 2 3*/
num_toothpicks = num_toothpicks - user_pick;
human_turn = false;
std::cout << "There are " << num_toothpicks << " left in the pile.\n\n\n";
//computer pick
computer_pick = 4 - user_pick;
num_toothpicks = num_toothpicks - computer_pick;
human_turn = true;
std::cout << "The computer chose " << computer_pick << ". That leaves " << num_toothpicks << ".\n";
}
else if ((num_toothpicks <= 4) && (num_toothpicks != 1))
{
if (human_turn == true)
{
std::cout << "There are " << num_toothpicks << ". Please choose to pull 1, 2, or 3 toothpicks from the pile.\n";
std::cin >> user_pick;
human_turn = false;
/*Make sure the user picks 1, 2, or 3*/
if (user_pick != 1 && user_pick != 2 && user_pick != 3)
{
std::cout << "Invalid entry. Please enter 1, 2, or 3.\n";
std::cin >> user_pick;
}
/*End Make Sure the user picks 1 2 3*/
num_toothpicks = num_toothpicks - user_pick;
std::cout << "There are " << num_toothpicks << " left in the pile.\n\n\n";
}
else if (human_turn == false)
{
switch (num_toothpicks)
{
case '4':
std::cout << "The computer takes 3 toothpicks from the pile.\n";
computer_pick = 3;
num_toothpicks = num_toothpicks - computer_pick;
std::cout << "There is only one toothpick left in the pile. You lose this game.\n";
human_turn = true;
break;
case '3':
std::cout << "The computer takes 2 toothpicks from the pile.\n";
computer_pick = 2;
num_toothpicks = num_toothpicks - computer_pick;
std::cout << "There is only one toothpick left in the pile. You lose this game.\n";
human_turn = true;
break;
case '2':
std::cout << "The computer takes 1 toothpick from the pile.\n";
computer_pick = 1;
num_toothpicks = num_toothpicks - computer_pick;
std::cout << "There is only one toothpick left in the pile.\n";
human_turn = true;
break;
}
}
}
else if (num_toothpicks == 1)
{
if (human_turn == true)
{
std::cout << "You are stuck with the last toothpick. You lost. Sorry!\n\n";
num_toothpicks--;
game_number++;
}
if (human_turn == false)
{
std::cout << "The computer was forced to take the last toothpick. The user won the game!\n\n";
num_toothpicks--;
game_number++;
}
}
} while (num_toothpicks > 0);
std::cout << "Do you wish to play again? Enter Y to continue, or N to close the program.\n";
num_toothpicks = 23;
std::cin >> keep_going;
std::cout << std::endl;
if (keep_going != 'Y' && keep_going != 'y' && keep_going != 'N' && keep_going != 'n')
{
std::cout << "Invalid entry. Do you wish to play again ? Enter Y to continue, or N to close the program.\n";
std::cin >> keep_going;
}
if (keep_going == 'N' || keep_going == 'n')
{
std::cout << "Thanks for playing!\n";
break;
}
}
}
while (keep_going != 'N' || keep_going != 'n');
system("pause");
return 0;
}
答案 0 :(得分:1)
您需要从int值中删除引号,以便将它们计算为int而不是chars。此外,您还需要包含错误处理。在switch语句中的最后一个案例之后,您需要包含default
个案,以防其他案例的条件都不满足。它可能看起来像这样:
default:
std::cout "Error: unexpected case reached: " + num_toothpicks + "\n";
break;
答案 1 :(得分:0)
原始代码开关正在寻找char而不是每种情况的int值。 添加了默认值以记住以后使用它进行故障排除并避免代码挂起
谢谢大家。
else if (human_turn == false)
{
switch (num_toothpicks)
{
case 4:
std::cout << "The computer takes 3 toothpicks from the pile.\n";
computer_pick = 3;
num_toothpicks = num_toothpicks - computer_pick;
std::cout << "There is only one toothpick left in the pile. You lose this game.\n";
human_turn = true;
break;
case 3:
std::cout << "The computer takes 2 toothpicks from the pile.\n";
computer_pick = 2;
num_toothpicks = num_toothpicks - computer_pick;
std::cout << "There is only one toothpick left in the pile. You lose this game.\n";
human_turn = true;
break;
case 2:
std::cout << "The computer takes 1 toothpick from the pile.\n";
computer_pick = 1;
num_toothpicks = num_toothpicks - computer_pick;
std::cout << "There is only one toothpick left in the pile.\n";
human_turn = true;
break;
default:
std::cout << "Something's Broken! Fix me!";
break;
}
}