Guest User

Untitled

a guest
Sep 23rd, 2023
17
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.13 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <cstdlib>
  4. #include <stdexcept>
  5. using std::cout;
  6. using std::cin;
  7. using std::endl;
  8. using std::string;
  9. // using std::;
  10. string player_marker() {
  11.   char player_asgmt = ' ';
  12.   string xo_or_ox = "";
  13.   while (player_asgmt != 'X' && player_asgmt != 'O') {
  14.       cout << "Player 1, do you want to be X or O?\n";
  15.       cin >> player_asgmt;
  16.       if (player_asgmt == 'X' || player_asgmt == 'x') {
  17.         xo_or_ox = "XO";break;
  18.       } else if (player_asgmt == 'O' || player_asgmt == 'o') {
  19.         xo_or_ox = "OX";break;
  20.       } else {
  21.         cout << "Invalid input\n";
  22.       }
  23.   }
  24.   return xo_or_ox;
  25. }
  26. string display(string board) {
  27.     return std::string(1,board[1])+"|"+std::string(1,board[2])+"|"+std::string(1,board[3])+"\n-----\n"+std::string(1,board[4])+"|"+std::string(1,board[5])+"|"+std::string(1,board[6])+"\n-----\n"+std::string(1,board[7])+"|"+std::string(1,board[8])+"|"+std::string(1,board[9]);
  28. }
  29. void place_marker(string &board,char mark,int space) {
  30.     board[space] = mark;
  31.     cout << display(board) << endl;
  32. }
  33. bool win_check(string board,char mark) {
  34.   bool wincheck = ((board[1] == mark && board[2] == mark && board[3] == mark) ||
  35.                    (board[4] == mark && board[5] == mark && board[6] == mark) ||
  36.                    (board[7] == mark && board[8] == mark && board[9] == mark) ||
  37.                    (board[1] == mark && board[4] == mark && board[7] == mark) ||
  38.                    (board[2] == mark && board[5] == mark && board[8] == mark) ||
  39.                    (board[3] == mark && board[6] == mark && board[9] == mark) ||
  40.                    (board[1] == mark && board[5] == mark && board[9] == mark) ||
  41.                    (board[3] == mark && board[5] == mark && board[7] == mark));
  42.   return wincheck;
  43. }
  44. int who_goes_first() {
  45.   srand(100);
  46.   int first_player = rand() % 2 + 1;
  47.   return first_player;
  48. }
  49. bool space_check(int position, string board) {
  50.   return (board[position] == ' ');
  51. }
  52. bool full_board(string board) {
  53.   int clear_spaces = 0;
  54.   for (int i{}; i < 9; ++i) {
  55.     if (space_check(i+1, board))
  56.       clear_spaces++;
  57.   }
  58.   return !(clear_spaces >= 1);
  59. }
  60. int player_choice(string board1, string board2) {
  61.   int pos = 0;
  62.   while (pos <= 0 || pos >= 10 || !space_check(pos,board2)) {
  63.     cout << "Where would you like to place your marker? Enter a number from the grid below\n" << display(board1) << endl;
  64.     cin >> pos;
  65.     if (pos <= 0 || pos >= 10 || !space_check(pos,board2))
  66.       cout << "Invalid choice" << endl;
  67.   }
  68.   cout << display(board2);
  69.   return pos;
  70. }
  71. bool replay() {
  72.   char yn = ' ';
  73.   while (yn != 'Y' && yn != 'y' && yn != 'N' && yn != 'n') {
  74.     cout << "Do you want to play again? Y/N";
  75.     cin >> yn;
  76.     if (yn == 'Y' || yn == 'y')
  77.       return true;
  78.     else if (yn == 'N' || yn == 'n')
  79.       return false;
  80.   }
  81. }
  82. void player1s_turn(string board1, string &board2, char mark) {
  83.     int place = player_choice(board1,board2);
  84.     place_marker(board2,mark,place);
  85. }
  86. void player2s_turn(string board1,string &board2, char mark) {
  87.     int place = player_choice(board1,board2);
  88.     place_marker(board2,mark,place);
  89. }
  90. int main() {
  91.   string board_indexes = "0123456789";
  92.   string xo = player_marker();
  93.   char player1 = xo[0], player2 = xo[1];
  94.   string current_board = "          ";
  95.   cout << "Welcome to Tic Tac Toe!" << endl;
  96.   cout << "Player 1 is " << player1 << ", Player 2 is " << player2 << endl;
  97.   int turn = who_goes_first();
  98.   cout << "Player " << turn << " goes first. (picked randomly)" << endl;
  99.   cout << "Ready to play? Y/N" << endl;
  100.   bool gameon;
  101.   char ready = ' ';
  102.   cin >> ready;
  103.   // cout << display(board_indexes) << endl;
  104.   gameon = (ready == 'Y' || ready == 'y');
  105.   while (gameon) {
  106.     cout << display(current_board) << endl;
  107.     if (full_board(current_board)){
  108.       if ((!win_check(current_board,player1) || win_check(current_board,player2))){
  109.         cout << "Tie game!" << display(current_board) << endl;break;
  110.       } else {
  111.         cout << "Player " << turn % 2 + 1 << " wins!" << endl;break;
  112.       }
  113.     }
  114.     if (turn == 1) {
  115.       if (win_check(current_board,player2)) {
  116.         cout << "Player 2 wins!" << endl;
  117.         gameon = false;
  118.         break;
  119.       } else {
  120.         if (!full_board(current_board)) {
  121.           player1s_turn(board_indexes,current_board,player1);
  122.           system("clear");
  123.           cout << display(current_board) << endl;
  124.           turn = 2;
  125.         } else
  126.           cout << "Tie game!" << endl;break;
  127.       }
  128.     }
  129.     if (turn == 2) {
  130.       if (win_check(current_board,player1)) {
  131.         cout << "Player 1 wins!" << endl;
  132.         gameon = false;
  133.         break;
  134.       } else {
  135.         if (!full_board(current_board)) {
  136.           player2s_turn(board_indexes,current_board,player2);
  137.           system("clear");
  138.           cout << display(current_board) << endl;
  139.           turn = 1;
  140.         }
  141.         else
  142.           cout << "Tie game!" << endl;break;
  143.       }
  144.     }
  145.     if (full_board(current_board) || win_check(current_board,player1) || win_check(current_board,player2)) {
  146.       if (!replay())
  147.         cout << "OK" << endl;break;
  148.     }
  149.   }
  150.   return 0;
  151. }
Advertisement
Add Comment
Please, Sign In to add comment