全部评论 6

  • #include <iostream>

    char board[3][3] = {{'1', '2', '3'}, {'4', '5', '6'}, {'7', '8', '9'}};
    char current_marker;
    int current_player;

    void drawBoard() {
    stdcout << "Current Board:\n";
    for (int i = 0; i < 3; i++) {
    for (int j = 0; j < 3; j++) {
    std
    cout << board[i][j];
    if (j < 2) stdcout << " | ";
    }
    std
    cout << stdendl;
    if (i < 2) std
    cout << "--|---|--\n";
    }
    }

    bool placeMarker(int slot) {
    int row = (slot - 1) / 3;
    int col = (slot - 1) % 3;

    if (board[row][col] != 'X' && board[row][col] != 'O') {
        board[row][col] = current_marker;
        return true;
    } else {
        return false;
    }
    

    }

    int winner() {
    // Check rows and columns
    for (int i = 0; i < 3; i++) {
    if (board[i][0] == board[i][1] && board[i][1] == board[i][2]) return current_player;
    if (board[0][i] == board[1][i] && board[1][i] == board[2][i]) return current_player;
    }
    // Check diagonals
    if (board[0][0] == board[1][1] && board[1][1] == board[2][2]) return current_player;
    if (board[0][2] == board[1][1] && board[1][1] == board[2][0]) return current_player;

    return 0; // No winner yet
    

    }

    void switchPlayer() {
    current_player = (current_player == 1) ? 2 : 1;
    current_marker = (current_marker == 'X') ? 'O' : 'X';
    }

    void game() {
    stdcout << "Player 1, choose your marker (X or O): ";
    char marker_p1;
    std
    cin >> marker_p1;

    current_player = 1;
    current_marker = marker_p1;
    
    drawBoard();
    
    int player_won;
    for (int i = 0; i < 9; i++) {
        std::cout << "Player " << current_player << "'s turn. Enter your slot: ";
        int slot;
        std::cin >> slot;
    
        if (slot < 1 || slot > 9) {
            std::cout << "Invalid slot! Please choose a slot between 1 and 9.\n";
            i--;
            continue;
        }
    
        if (!placeMarker(slot)) {
            std::cout << "Slot already occupied! Try again.\n";
            i--
    

    2024-08-20 来自 北京

    0
    •         continue;
          }
      
          if (!placeMarker(slot)) {
              std::cout << "Slot already occupied! Try again.\n";
              i--;
              continue;
          }
      
          drawBoard();
      
          player_won = winner();
      
          if (player_won == 1) {
              std::cout << "Player 1 wins! Congratulations!\n";
              break;
          } else if (player_won == 2) {
              std::cout << "Player 2 wins! Congratulations!\n";
              break;
          }
      
          switchPlayer();
      }
      
      if (player_won == 0) {
          std::cout << "It's a draw!\n";
      }
      

      }

      int main() {
      game();
      return 0;
      }

      2024-08-20 来自 北京

      0
  • 2024-08-20 来自 北京

    0
  • https://www.acgo.cn/problemset/info/28551

    2024-08-20 来自 北京

    0
  • 2024-08-19 来自 北京

    0
  • 2024-08-19 来自 北京

    0
  • 2024-08-19 来自 北京

    0

热门讨论