我是编程新手,从Codeacademy开始学习C++。这是一个非常基础的问题,感谢您的帮助。
这个程序的目的是构建一个井字棋游戏。我想了解可用解决方案。在浏览程序时,我看到了其中一个函数,名为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";
}
}
position
的定义。请参见第一个评论中的链接。 - cigienstd::cin
输入限制在特定范围的数字内,您需要在从控制台提示输入后进行检查。 - πάντα ῥεῖ