Advertisement
Korotkodul

board.cpp

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