简体   繁体   中英

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

I am a beginner in c++ and am taking a course at my university, I am creating a C++ [rogram for tic tac toe and im having some struggles I cannot figure out how to make the game stop when player 1 wins at the moment if player 1 wins player 2 still has to take their next turn to make the game output Player 1 wins... I will include the code below. also I need a user defined function and can't think of one that would be good to use besides displaying an empty board if anyone has any ideas that would be great. Thanks for the help in advance!!

#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;
}

please ignore the probably terrible code this is some of my best work so far

The simple explanation is that you don't check for a win condition until after player2 has gone. You have your loop to take player1 's input, then it looks like you display the board, then you have the loop to take player2 's input, and then you check if a player won.

Move the win-checking code to a function, and call that function after each player's turn. It might look something like this (pseudocode):

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;
}

I would recommend breaking up each 'phase' of the game into its own function. Player input is its own function, win checking as its own, board drawing as its own, etc. It will make your code easier to debug and tweak, and you'll be able to follow the overall game logic a lot easier.

You must merge your two loops waiting for player's input into one by using a char variable for the expected output. It would looks like:


#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;
}

Note that I did not change the general approach of your code, so you understand the changes. But this is not the best you could find for this problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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