Advertisement
Korotkodul

board.cpp

Nov 26th, 2024
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.25 KB | None | 0 0
  1. #include "iostream"
  2. using namespace std;
  3. int board[3][3];
  4. int moved=1;
  5. int moves=0;
  6. int checkwin()
  7. {  
  8.     int winner=0;
  9.     for (int i=0;i<3;i++)
  10.     {
  11.         if (board[0][i]==board[1][i] and board[1][i]==board[2][i] and board[0][i]!=0)
  12.             winner = board[0][i];
  13.         if (board[i][0]==board[i][1] and board[i][1]==board[i][2] and board[i][0]!=0)
  14.             winner = board[i][0];
  15.     }
  16.     if (board[0][0]==board[1][1] and board[1][1]==board[2][2] and board[0][0]!=0)
  17.         winner = board[0][0];
  18.     if (board[2][0]==board[1][1] and board[1][1]==board[0][2] and board[2][0]!=0)
  19.         winner = board[2][0];
  20.     return winner;
  21. }
  22. void checkboard()
  23. {
  24.     for (int i=0;i<3;i++)
  25.     {
  26.         for (int i1:board[i])
  27.         {
  28.             cout<<i1<<" ";
  29.         }
  30.         cout<<endl;
  31.     }
  32.     cout<<endl;
  33. }
  34. int checkpos(int x, int y)
  35. {  
  36.     if (board[y][x]==0)
  37.     {
  38.         moves++;
  39.         board[y][x]=moved;
  40.         moved=1+moved%2;
  41.         int win = checkwin();
  42.         //checkboard();
  43.         if (win!=0)
  44.             return win*10;
  45.         if (win==0 and moves==9)
  46.             return -1;
  47.         return board[y][x];
  48.     }
  49.     else
  50.         return 0;
  51. }
  52. int main()
  53. {
  54.     checkpos(1,2);
  55.     checkboard();
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement