我是编码新手,我是在 Codeacademy 开始学习 C++ 的。这是一个非常基本的疑问,感谢 TIA 帮助我。
该程序的重点是构建 TIC TAC TOC 游戏。我一直在寻找了解可用的解决方案。在那里我遇到了程序中的一个函数,称为 void set_position()
,它用于确定玩家想要玩的棋盘中的哪个位置(从 1 到 9)。我将在下面粘贴整个代码块以获取上下文,然后再进行查询。
void set_position() {
std::cout << "Player " << player << "'s Turn (Enter 1-9): ";
while (!(std::cin>>position)) {
std::cout << "Player " << player << ", please enter a valid number between 1 and 9: ";
std::cin.clear();
std::cin.ignore();
}
我的问题是,while(!(std::cin>>position))
如何确保输入的数字在 1 到 9 之间?
编辑添加:
position 是用户定义的变量,在程序开始时初始化为 0。我没有发现 position 在程序中的任何时候都被迫取 1 到 9 之间的值。我完全有可能错过了它,如果这样更清楚的话,我会在下面添加整个代码,我最初没有这样做的原因是因为这是一个解决方案,我不确定我是否可以将它粘贴到公共(public)论坛
#include <iostream>
#include "play.hpp"
std::string board[9] = {" ", " ", " ", " ", " ", " ", " ", " ", " "};
int player = 1;
int position = 0;
void introduction() {
std::cout << "Press [Enter] to begin: ";
std::cin.ignore();
std::cout << "\n";
std::cout << "===========\n";
std::cout << "Tic-Tac-Toe\n";
std::cout << "===========\n\n";
std::cout << "Player 1) ✖\n";
std::cout << "Player 2) ⊙\n\n";
std::cout << "Here's the 3 x 3 grid:\n\n";
std::cout << " | | \n";
std::cout << " 1 | 2 | 3 \n";
std::cout << "_____|_____|_____ \n";
std::cout << " | | \n";
std::cout << " 4 | 5 | 6 \n";
std::cout << "_____|_____|_____ \n";
std::cout << " | | \n";
std::cout << " 7 | 8 | 9 \n";
std::cout << " | | \n\n";
}
bool is_winner() {
bool winner = false;
// rows
if ((board[0] == board[1]) && (board[1] == board[2]) && board[0] != " ") {
winner = true;
} else if ((board[3] == board[4]) && (board[3] == board[5]) && board[3] != " ") {
winner = true;
} else if ((board[6] == board[7]) && (board[6] == board[8]) && board[6] != " ") {
winner = true;
}
// columns
else if ((board[0] == board[3]) && (board[0] == board[6]) && board[0] != " ") {
winner = true;
} else if ((board[1] == board[4]) && (board[1] == board[7]) && board[1] != " ") {
winner = true;
} else if ((board[2] == board[5]) && (board[2] == board[8]) && board[2] != " ") {
winner = true;
} // diagonals
else if ((board[0] == board[4]) && (board[0] == board[8]) && board[0] != " ") {
winner = true;
}
else if ((board[2] == board[4]) && (board[2] == board[6]) && board[2] != " ") {
winner = true;
}
return winner;
}
bool filled_up() {
bool filled = true;
for (int i = 0; i < 9; i++) {
if (board[i] == " ") {
filled = false;
}
}
return filled;
}
void draw() {
std::cout << " | | \n";
std::cout << " " << board[0] << " | " << board[1] << " | " << board[2] << "\n";
std::cout << "_____|_____|_____ \n";
std::cout << " | | \n";
std::cout << " " << board[3] << " | " << board[4] << " | " << board[5] << "\n";
std::cout << "_____|_____|_____ \n";
std::cout << " | | \n";
std::cout << " " << board[6] << " | " << board[7] << " | " << board[8] << "\n";
std::cout << " | | \n";
std::cout << "\n";
}
void set_position() {
std::cout << "Player " << player << "'s Turn (Enter 1-9): ";
while(!(std::cin>>position)) {
std::cout << "Player " << player << ", please enter a valid number between 1 and 9: ";
std::cin.clear();
std::cin.ignore();
}
std::cout << "\n";
while (board[position-1] != " ") {
std::cout << "Oops, there's already something in that position!\n\n";
std::cout << "Player " << player << "'s Turn (Enter 1-9): ";
std::cin >> position;
std::cout << "\n";
}
}
void update_board() {
if (player % 2 == 1) {
board[position-1] = "✖";
} else {
board[position-1] = "⊙";
}
}
void change_player() {
if (player == 1) {
player++;
} else {
player--;
}
}
void take_turn() {
while ( !is_winner() && !filled_up() ) {
set_position();
update_board();
change_player();
draw();
}
}
void end_game() {
if (is_winner()) {
std::cout << "There's a winner!\n";
}
else if (filled_up()) {
std::cout << "There's a tie!\n";
}
}
最佳答案
My question is, how is the while(!(std::cin>>position)) ensuring that the number being entered is between 1 to 9?
事实并非如此。它仅检查用户是否输入了有效整数。任何额外的检查都必须手动完成。
关于C++ : Help understanding what this line of code is trying to do,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64179195/