【问题标题】:I'm having trouble with tic tac toe?我在井字游戏中遇到问题?
【发布时间】:2015-11-12 12:03:12
【问题描述】:

如何不允许用户两次输入相同的招式?例如,玩家 X 输入 1,然后玩家 O 输入 1,或者 X 在下一回合再次输入 1。如何让他们重新输入有效的输入? My code

【问题讨论】:

  • 家庭作业?将你的代码归结为封装你的问题的东西。您发布了整个程序。
  • 使用do {} while(..); 循环,直到他们输入有效的移动。
  • 您可以使用if(){} else{} 循环来检查该框中是否已经下过棋。

标签: c++


【解决方案1】:

使用数据结构(例如数组或向量)来存储已经做出的动作。如果用户尝试输入已经做出的动作,请重新提示他们:

void getMove()
{
//Get move input from user through something like std::cin
    if (hasBeenPlayed()) //If the move (ex: 1) is already in the array or vector
    {
    getMove();
    }
    else
    {
    playedMoves.push_back(move); // Add the move to the vector (or an array)
    //Compute the move that was entered here
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多