【发布时间】:2020-10-02 23:10:01
【问题描述】:
我是编码新手,我从 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 之间。我完全有可能错过了它,如果能提供更清晰的信息,我将在下面添加整个代码,我最初没有这样做的原因是因为这是一个解决方案,我不确定是否可以将其粘贴到公共论坛中
#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";
}
}
【问题讨论】:
-
您是否运行过该程序以查看它是否确实如此?请发minimal reproducible example
-
是的,我确实运行了程序,这似乎是代码的功能。此外,在阅读程序时从更广泛的背景来看——我明白这就是他们使用它的原因。我只是无法弄清楚如何。
-
然后你需要展示相关的代码。例如
position的定义。请参阅第一条评论中的链接。 -
这要好得多,但如果有调用此代码的 main 会很好。此外,您可以删除大量代码,并且仍然重现该行为。
-
@SrirakshaVR 密切相关:stackoverflow.com/questions/24504582/… 正如前面提到的,您不能事先将
std::cin输入限制为特定范围的数字,您需要在从控制台提示符。
标签: c++ tic-tac-toe