C ++井字游戏仅以一种组合获胜

时间:2019-06-26 00:47:28

标签: c++

当要给定Champion()函数中的任何实例时,我希望程序打印“ You win”。仅在输入“ 123”时显示获胜者。每当其他任何地方显示三个X时,程序就会继续。例如,如果对角线给出三个X,程序将继续执行。新手程序员,因此,任何批评都将不胜感激。

class TicTacToe {
 private:
  char map[GRID_SIZE][GRID_SIZE];

 public:
  void computers_turn() {
    while (true) {
      int choice = (rand() % 9) + 1;

      int row = choice / 3;
      int col = choice % 3;
      char grid_position = map[row][col];

      if (grid_position == 'X' || grid_position == 'O') {
        std::cout << "Space taken. Try again" << std::endl;
      } else {
        map[row][col] = (char)'O';
        break;
      }
    }
  }

  void champion() {
    const char* possiblities[8] = {"123", "456", "789", "147",
                                   "258", "369", "159", "753"};

    for (int i = 0; i < 8; i++) {
      char previous_pos = '0';
      bool winner = true;
      const char* possible_moves = possiblities[i];

      for (int index = 0; index < GRID_SIZE; index++) {
        char character = possible_moves[i];
        int entered_num = character - '0';
        int grid_space = entered_num - '1';

        int row = index / GRID_SIZE;
        int col = index % GRID_SIZE;

        char grid_coordinate = map[row][col];

        if (previous_pos == '0') {
          previous_pos = grid_coordinate;
        } else if (previous_pos == grid_coordinate) {
          continue;
        } else {
          winner = false;
          break;
        }
      }
      if (winner) {
        puts("You win");
        exit(0);
        break;
      }
    }
  }

  void playgame() {
    std::string input;

    while (true) {
      std::cout << "Go player one" << std::endl;
      getline(std::cin, input);
      if (input != " ") {
        char entered = input.c_str()[0];

        if (entered >= '1' && entered <= '9') {
          int entered_num = entered - '0';
          int index = entered_num - 1;
          int row = index / 3;
          int col = index % 3;
          char grid_position = map[row][col];

          if (grid_position == 'X' || grid_position == 'O') {
            std::cout << "Space taken. Try again" << std::endl;
          } else {
            map[row][col] = (char)'X';
            break;
          }
        } else {
          std::cout << "Only numbers 1 - 9" << std::endl;
        }
      } else {
        std::cout << "Have to enter something, try again" << std::endl;
      }
    }
  }

  void generateGrid() {
    int number = 1;

    for (int x = 0; x < GRID_SIZE; x++) {
      for (int y = 0; y < GRID_SIZE; y++) {
        map[x][y] = std::to_string(number).c_str()[0];
        number += 1;
      }
    }
  }

  void tictacToeMap() {
    std::cout << std::endl;

    for (int x = 0; x < GRID_SIZE; x++) {
      for (int y = 0; y < GRID_SIZE; y++) {
        std::printf(" %c ", map[x][y]);
      }
      std::cout << std::endl;
    }
  }

  TicTacToe() {
    generateGrid();

    while (true) {
      champion();
      tictacToeMap();
      playgame();
      computers_turn();
    }
  }
};

int main() {
  TicTacToe ticTacToe;

  return 0;
}

0 个答案:

没有答案