Да, я учился в колледже. Нет, мы еще не изучали массивы или программы более высокого уровня. Мы находимся в главе 4 книги. Хорошо, в соответствии с отзывами моей последней попытки, я думаю, что на этот раз я правильно разместил сообщение. Сожалею!C++ switch statement
Здесь код теперь замерзает.
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;
}
}
Он надлежащим образом исполняет UNLESS, когда пользователю требуется, чтобы компьютер выполнил оператор 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;
}
Добро пожаловать в переполнение стека! Похоже, вам, возможно, потребуется научиться использовать отладчик для выполнения вашего кода. С хорошим отладчиком вы можете выполнить свою программу по очереди и посмотреть, где она отклоняется от ожидаемого. Это важный инструмент, если вы собираетесь заниматься программированием. Дальнейшее чтение: ** [Как отлаживать небольшие программы] (http://ericlippert.com/2014/03/05/how-to-debug-small-programs/) ** – NathanOliver
'num_toothpicks' is' int', ' «4» - это не то же самое, что «4». – LogicStuff
Да, нам просто сказали не использовать отладчик нашим профессором. – p3aceful