【问题标题】:C++ : Help understanding what this line of code is trying to doC++:帮助理解这行代码试图做什么
【发布时间】: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&gt;&gt;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


【解决方案1】:

我的问题是,while(!(std::cin>>position)) 如何确保输入的数字在 1 到 9 之间?

它没有。它仅检查用户是否输入了有效的整数。任何额外的检查都必须手动完成。

【讨论】:

  • 没有其他手动检查来确保数字在 1 到 9 之间,但是当我尝试通过输入大于 9 或小于 1 的值来测试程序时,我被要求输入不同的值 [Player 1 please enter a value between 1 and 9 -- 存在于代码中]
  • 显示的代码没有这样做。它必须在其他地方,请在您的问题中包含它。
【解决方案2】:

似乎position 只允许从 1 到 9 的数字,但这在您的代码中并不清楚。如果是这种方式,std::cin&gt;&gt;position 会读取用户的输入并尽可能将其写入position。如果不可能,则此操作评估为假值。因为它被! 否定,所以循环会重复输入,直到给出有效的输入。这种模式在 C++ 中很常见。

【讨论】:

  • 您所说的“是否可能是 position 使用枚举来强制使用 1 到 9 的间隔?”是什么意思?如果position 是自定义类型并且&gt;&gt; 有一个将自定义类型作为参数的istream 重载,则代码执行操作声明的唯一方法。
  • 是的,这将是我根据提供的 sn-p 的猜测。不过,入门课程令人惊讶。
  • 我也这么想,但是当我浏览代码时,我找不到任何确定position 将以这种方式使用的点。位置在游戏开始时被初始化为0,然后重新初始化仅在此时发生。
  • 那么程序不检查数字是在0到9之间,它只是检查它是一个数字。否则,您将不得不编辑您的问题以发布更多代码,以便其他人可以重现该问题。
  • 我发布了完整的解决方案代码@jtbandes
【解决方案3】:

它不能保证。我做了这个小程序,虽然它可以保证它在 1 到 9 之间

#include <iostream>

int main() {
    int player = 0;                                             //force integer
    while ((std::cin >> player) && (player < 1 || player > 9) || std::cin.fail()) {
        std::cout << "Player " << player << ", please enter a valid number between 1 and 9: ";
        std::cin.clear();
        std::cin.ignore();
    }
    return 0;
}

【讨论】:

  • 例如,该逻辑会将 z 视为 1-9 之间的有效数字。也没有真正回答操作员的问题(假设他们的问题不完整)。
  • 是的,这是有道理的。但是程序中没有明确的说明,它仍然按预期运行。 position 在代码的开头被初始化为 0,除此之外,直到代码中的这一点,它才被设置任何值。
  • 不必初始化,因为 std::cin 会初始化值。
猜你喜欢
  • 2011-02-21
  • 1970-01-01
  • 2016-07-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-18
  • 2016-07-08
  • 2010-11-12
相关资源
最近更新 更多