Advertisement
spider68

TikTacToe

Jul 15th, 2023 (edited)
1,226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.85 KB | Source Code | 0 0
  1. /* Online C++ Compiler and Editor */
  2. #include <bits/stdc++.h>
  3.  
  4. using namespace std;
  5.  
  6. class Player{
  7.     public:
  8.         string name;
  9.         char sign;
  10.         Player(string name, char sign){
  11.             this->name=name;
  12.             this->sign=sign;
  13.         }
  14. };
  15.  
  16. class TikTacToe{
  17.     int size,count=0;
  18.     vector<vector<char>>board;
  19.     deque<Player>players;
  20.     public:
  21.         TikTacToe(int size){
  22.             this->size = size;
  23.             board = vector<vector<char>>(size,vector<char>(size, '.'));
  24.             Player p1 =  Player("player1",'X');
  25.             Player p2 =  Player("player2",'O');
  26.             players.push_back(p1);
  27.             players.push_back(p2);
  28.         }
  29.         void StartGame();
  30.         bool EnterValue(Player p);
  31.         void PrintBoard();
  32.         bool IsGameCompleted(int row, int col, Player p);
  33. };
  34.  
  35. void TikTacToe::StartGame(){
  36.     while(count<size*size){
  37.         Player current = players.front();
  38.         if(EnterValue(current)==false){
  39.             break;
  40.         }
  41.         players.pop_front();
  42.         players.push_back(current);
  43.     }
  44. }
  45.  
  46. bool TikTacToe::EnterValue(Player p){
  47.     int row, col;
  48.     cout<<p.name<<" please enter row and col\n";
  49.     cin>>row>>col;
  50.     if(row<0 || col<0 || row>=size || col>=size){
  51.         cout<<"invalid index write again\n";
  52.         return EnterValue(p);
  53.     }
  54.     if(board[row][col]!='.'){
  55.         cout<<"value is already filled in index write again\n";
  56.         return EnterValue(p);
  57.     }
  58.     board[row][col]=p.sign;
  59.     PrintBoard();
  60.     count++;
  61.    
  62.     if (IsGameCompleted(row,col,p)){
  63.         cout<<p.name<<" won game";
  64.         return false;
  65.     }
  66.     if(count==size*size){
  67.         cout<<"withddraw game";
  68.         return false;
  69.     }
  70.    
  71.     cout<<endl;
  72.     return true;
  73. }
  74.  
  75. void TikTacToe::PrintBoard(){
  76.     for(int i=0;i<size;i++){
  77.         cout<<"|";
  78.         for(int j=0;j<size;j++){
  79.             cout<<board[i][j]<<"|";
  80.         }
  81.         cout<<"\n--------\n";
  82.     }
  83. }
  84.  
  85. bool TikTacToe::IsGameCompleted(int row, int col, Player p){
  86.     bool valid=true;
  87.     for(int i=0;i<size;i++){
  88.         if(board[row][i]!=p.sign){
  89.             valid=false;
  90.             break;
  91.         }
  92.     }
  93.     if(valid)return true;
  94.    
  95.     valid=true;
  96.     for(int i=0;i<size;i++){
  97.         if(board[i][col]!=p.sign){
  98.             valid=false;
  99.             break;
  100.         }
  101.     }
  102.     if(valid)return true;
  103.    
  104.     valid=true;
  105.     for(int i=0;i<size;i++){
  106.         if(board[i][i]!=p.sign){
  107.             valid=false;
  108.             break;
  109.         }
  110.     }
  111.     if(valid)return true;
  112.    
  113.     valid=true;
  114.     for(int i=0;i<size;i++){
  115.         if(board[i][size-i-1]!=p.sign){
  116.             valid=false;
  117.             break;
  118.         }
  119.     }
  120.     if(valid)return true;
  121.     return false;
  122. }
  123.  
  124.  
  125. int main()
  126. {
  127.    TikTacToe obj = TikTacToe(3);
  128.    obj.StartGame();
  129.    return 0;
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement