繁体   English   中英

没有玩家 2 采取行动,基本井字游戏不会获胜

[英]basic Tic tac toe game not winning without player 2 making a move

我是 c++ 的初学者,正在我的大学上一门课程,我正在创建一个 C++ [井字游戏,我遇到了一些困难,如果玩家 1 获胜,我无法弄清楚如何让游戏停止1 赢玩家 2 仍然需要下一轮来制作游戏 output 玩家 1 赢...我将包含下面的代码。 我还需要一个用户定义的 function 并且如果有人有任何很棒的想法,除了显示一个空板之外,我想不出一个很好用的。 我在这里先向您的帮助表示感谢!!

#include <iomanip>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
void gameBoard() {
  cout << " Reference:" << endl;
  cout << " | | "
       << " 1 2 3" << endl;
  cout << " | | " << endl;
  cout << "______|________|______ " << endl;
  cout << " | | "
       << " 4 5 6" << endl;
  cout << " | | " << endl;
  cout << "______|________|______ " << endl;
  cout << " | | "
       << " 7 8 9" << endl;
  cout << " | | " << endl;
}
int main(int argc, const char* argv[]) {
  string player1Name;
  string player2Name;
  int j;
  int k;
  int l;
  int userInput;
  const int NUM_ELEMENTS = 10;
  vector<char> boxVals(NUM_ELEMENTS);
  unsigned int i;
  for (i = 0; i < boxVals.size(); ++i) {
    boxVals.at(i) = ' ';
  }
  cout << "enter Player 1's Name: ";
  cin >> player1Name;
  cout << "enter Player 2's Name: ";
  cin >> player2Name;
  gameBoard();
  for (k = 0; k < 10; k++) {
    for (j = 0; j < 10; j++) {
      cin >> userInput;
      boxVals.at(userInput) = 'x';
      break;
    }
    cout << " Reference:" << endl;
    cout << " | | "
         << " 1 2 3" << endl;
    cout << " " << boxVals.at(1) << " | " << boxVals.at(2) << " | "
         << boxVals.at(3) << " " << endl;
    cout << "______|________|______ " << endl;
    cout << " | | "
         << " 4 5 6" << endl;
    cout << " " << boxVals.at(4) << " | " << boxVals.at(5) << " | "
         << boxVals.at(6) << " " << endl;
    cout << "______|________|______ " << endl;
    cout << " | | "
         << " 7 8 9" << endl;
    cout << " " << boxVals.at(7) << " | " << boxVals.at(8) << " | "
         << boxVals.at(9) << " " << endl;
    for (l = 0; l < 10; l++) {
      cin >> userInput;
      if (boxVals.at(userInput != 'x')) {
        boxVals.at(userInput) = 'O';
      } else if (boxVals.at(userInput) == 'x') {
        cout << "invalid move" << endl;
        continue;
      }
      break;
    }
    cout << " Reference:" << endl;
    cout << " | | "
         << " 1 2 3" << endl;
    cout << " " << boxVals.at(1) << " | " << boxVals.at(2) << " | "
         << boxVals.at(3) << " " << endl;
    cout << "______|________|______ " << endl;
    cout << " | | "
         << " 4 5 6" << endl;
    cout << " " << boxVals.at(4) << " | " << boxVals.at(5) << " | "
         << boxVals.at(6) << " " << endl;
    cout << "______|________|______ " << endl;
    cout << " | | "
         << " 7 8 9" << endl;
    cout << " " << boxVals.at(7) << " | " << boxVals.at(8) << " | "
         << boxVals.at(9) << " " << endl;
    if ((boxVals.at(1) == 'x' && boxVals.at(2) == 'x' &&
         boxVals.at(3) == 'x') ||
        (boxVals.at(4) == 'x' && boxVals.at(5) == 'x' &&
         boxVals.at(6) == 'x') ||
        (boxVals.at(7) == 'x' && boxVals.at(8) == 'x' &&
         boxVals.at(9) == 'x') ||
        (boxVals.at(1) == 'x' && boxVals.at(5) == 'x' &&
         boxVals.at(9) == 'x') ||
        (boxVals.at(3) == 'x' && boxVals.at(5) == 'x' &&
         boxVals.at(7) == 'x') ||
        (boxVals.at(1) == 'x' && boxVals.at(4) == 'x' &&
         boxVals.at(7) == 'x') ||
        (boxVals.at(2) == 'x' && boxVals.at(5) == 'x' &&
         boxVals.at(8) == 'x') ||
        (boxVals.at(3) == 'x' && boxVals.at(6) == 'x' &&
         boxVals.at(9) == 'x')) {
      cout << player1Name << " Wins!!" << endl;
      break;
    }
    if ((boxVals.at(1) == 'O' && boxVals.at(2) == 'O' &&
         boxVals.at(3) == 'O') ||
        (boxVals.at(4) == 'O' && boxVals.at(5) == 'O' &&
         boxVals.at(6) == 'O') ||
        (boxVals.at(7) == 'O' && boxVals.at(8) == 'O' &&
         boxVals.at(9) == 'O') ||
        (boxVals.at(1) == 'O' && boxVals.at(5) == 'O' &&
         boxVals.at(9) == 'O') ||
        (boxVals.at(3) == 'O' && boxVals.at(5) == 'O' &&
         boxVals.at(7) == 'O') ||
        (boxVals.at(1) == 'O' && boxVals.at(4) == 'O' &&
         boxVals.at(7) == 'O') ||
        (boxVals.at(2) == 'O' && boxVals.at(5) == 'O' &&
         boxVals.at(8) == 'O') ||
        (boxVals.at(3) == 'O' && boxVals.at(6) == 'O' &&
         boxVals.at(9) == 'O')) {
      cout << player2Name << " Wins!" << endl;
      break;
    }
  }
  return 0;
}

请忽略可能很糟糕的代码这是我迄今为止最好的一些工作

简单的解释是直到player2离开后才检查获胜条件。 你有你的循环来接受player1的输入,然后看起来你显示了棋盘,然后你有循环来接受player2的输入,然后你检查一个玩家是否赢了。

将获胜检查代码移动到 function,并在每个玩家回合后调用该 function。 它可能看起来像这样(伪代码):

bool is_game_over(char player) {
  if (player == 'x' && <your code to check for x winning>) {
    <your code that returns true or false>
  }

  if (player == 'o' && <your code to check for o winning>) {
    <your code that returns true or false>
  }

  return false;
}

我建议将游戏的每个“阶段”分解为自己的 function。 玩家输入是它自己的 function,它自己的胜利检查,它自己的棋盘绘图等。它将使您的代码更易于调试和调整,并且您将能够更轻松地遵循整体游戏逻辑。

您必须使用预期 output 的 char 变量将等待玩家输入的两个循环合并为一个。 它看起来像:


#include <iomanip>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
void gameBoard() {
  cout << " Reference:" << endl;
  cout << " | | "
       << " 1 2 3" << endl;
  cout << " | | " << endl;
  cout << "______|________|______ " << endl;
  cout << " | | "
       << " 4 5 6" << endl;
  cout << " | | " << endl;
  cout << "______|________|______ " << endl;
  cout << " | | "
       << " 7 8 9" << endl;
  cout << " | | " << endl;
}
int main(int argc, const char* argv[]) {
  string player1Name;
  string player2Name;
  int j;
  int k;
  int l;
  int userInput;
  const int NUM_ELEMENTS = 10;
  vector<char> boxVals(NUM_ELEMENTS);
  unsigned int i;
  char currentChar='x'; //tic-tac-toe begins with x
  for (i = 0; i < boxVals.size(); ++i) {
    boxVals.at(i) = ' ';
  }
  cout << "enter Player 1's Name: ";
  cin >> player1Name;
  cout << "enter Player 2's Name: ";
  cin >> player2Name;
  gameBoard();
  for (k = 0; k < 10; k++) {
    for (l = 0; l < 10; l++) {
      cin >> userInput;
      if (boxVals.at(userInput != ' ')) {
        cout << "invalid move" << endl;
        continue;
        }
      else {
          boxVals.at(userInput)=currentChar;
          break;
          }
    }
    cout << " Reference:" << endl;
    cout << " | | "
         << " 1 2 3" << endl;
    cout << " " << boxVals.at(1) << " | " << boxVals.at(2) << " | "
         << boxVals.at(3) << " " << endl;
    cout << "______|________|______ " << endl;
    cout << " | | "
         << " 4 5 6" << endl;
    cout << " " << boxVals.at(4) << " | " << boxVals.at(5) << " | "
         << boxVals.at(6) << " " << endl;
    cout << "______|________|______ " << endl;
    cout << " | | "
         << " 7 8 9" << endl;
    cout << " " << boxVals.at(7) << " | " << boxVals.at(8) << " | "
         << boxVals.at(9) << " " << endl;
    if ((boxVals.at(1) == currentChar && boxVals.at(2) == currentChar &&
         boxVals.at(3) == currentChar ) ||
        (boxVals.at(4) == currentChar && boxVals.at(5) == currentChar &&
         boxVals.at(6) == currentChar ) ||
        (boxVals.at(7) == currentChar && boxVals.at(8) == currentChar &&
         boxVals.at(9) == currentChar ) ||
        (boxVals.at(1) == currentChar && boxVals.at(5) == currentChar &&
         boxVals.at(9) == currentChar ) ||
        (boxVals.at(3) == currentChar && boxVals.at(5) == currentChar &&
         boxVals.at(7) == currentChar ) ||
        (boxVals.at(1) == currentChar && boxVals.at(4) == currentChar &&
         boxVals.at(7) == currentChar ) ||
        (boxVals.at(2) == currentChar && boxVals.at(5) == currentChar &&
         boxVals.at(8) == currentChar )||
        (boxVals.at(3) == currentChar && boxVals.at(6) == currentChar &&
         boxVals.at(9) == currentChar )) {
      cout << ((currentChar=='x')?player1Name : player2Name)<< " Wins!!" << endl;
      break;
    }
  else currentChar = (currentChar == 'x')?'O':'x';
  }
  return 0;
}

请注意,我没有更改您代码的一般方法,因此您了解更改。 但这不是你能找到的最好的解决这个问题的方法。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM