Nim游戏程序问题求助:非do-while循环实现重复及输出异常
Hey there, let's fix this Nim game issue fast—since your deadline is tonight at 9:30, we'll cut straight to the solution.
First, the root cause of those repeated messages ("the mode is set to dumb" and "it is your turn.") is almost certainly that you're putting those print statements inside your do-while loop, so they run every time the loop repeats. To avoid do-while and fix the duplicate output, we can restructure your code with nested while loops: one to handle the game rounds, and an outer one to let players restart the game.
Here's a revised version of your code that follows the "take last marble loses" rule, avoids do-while, and stops those duplicate prints:
#include "stdafx.h" #include <ctime> #include <string> #include <iostream> using namespace std; int main() { bool playAgain = true; // Outer loop to control entire program restarts while (playAgain) { // Game initialization - runs once per game session cout << "the mode is set to dumb" << endl; srand(time(0)); // Seed random number generator for AI int marbles = 15; // Adjust to your assignment's starting marble count // Inner loop for individual game turns while (marbles > 0) { // Player's turn cout << "it is your turn." << endl; cout << "Marbles remaining: " << marbles << endl; int playerTake; cout << "How many marbles will you take? (1-3): "; cin >> playerTake; // Basic input validation (expand as needed) while (playerTake < 1 || playerTake > 3 || playerTake > marbles) { cout << "Invalid input! Take 1-3 marbles (no more than are left): "; cin >> playerTake; } marbles -= playerTake; // Check if player took the last marble (they lose) if (marbles == 0) { cout << "Oh no! You took the last marble—you lose!" << endl; break; } // Dumb AI's turn (random 1-3 marbles, avoids taking last if possible) int aiTake = rand() % 3 + 1; if (marbles == 1) { aiTake = 1; // AI has to take last marble and lose } else if (aiTake >= marbles) { aiTake = marbles - 1; // Don't let AI take last marble early } marbles -= aiTake; cout << "AI takes " << aiTake << " marbles. Marbles remaining: " << marbles << endl; // Check if AI took the last marble (player wins) if (marbles == 0) { cout << "AI took the last marble—you win!" << endl; break; } } // Ask if player wants to play again char restartChoice; cout << "\nPlay another round? (y/n): "; cin >> restartChoice; playAgain = (restartChoice == 'y' || restartChoice == 'Y'); } return 0; }
Let's break down the key fixes:
- The outer
while(playAgain)loop replaces your do-while for repeating the entire game. It only restarts when the player chooses to play again. - Game setup (like the AI mode message) is inside the outer loop but runs once per game—so "the mode is set to dumb" won't spam every turn.
- "it is your turn." is printed only right before the player's input, so it shows up exactly when it's their turn, not on every loop iteration.
- We added checks after each turn to enforce the "take last marble loses" rule clearly.
A couple quick tweaks to match your assignment:
- Adjust the starting
marblesvalue to whatever your specs require. - Expand the input validation if your assignment has stricter rules for player moves.
- Modify the AI logic if "dumb mode" has specific requirements beyond random picks.
That should get your program working correctly without those annoying repeated messages. Good luck hitting that deadline!
内容的提问来源于stack exchange,提问作者noshkren

