Blame view

gamemodel.cpp 2.32 KB
505e594e   Hakeem   h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
  #include "gamemodel.h"
  
  GameModel::GameModel(const othello::OthelloGameEngine& game_engine,
                           QObject*                         parent)
    : QAbstractListModel(parent), m_game_engine{game_engine}
  {
  }
  
  int GameModel::rowCount(const QModelIndex& parent) const
  {
    if (parent.isValid()) return 0;
    return int(boardSize());
  }
  
  QVariant GameModel::data(const QModelIndex& index, int role) const
  {
    if (!index.isValid()) return QVariant();
  
    const size_t st_row = size_t(boardSize() - index.row() - 1);
    const auto   piece_id = othello::BitPos(st_row);
  
    const auto occupied = othello::utility::occupied(m_game_engine.board(), piece_id);
  
  
    if (role == OccupiedRole)
      return occupied;
    else if (role == PlayerNrRole) {
  
      if (!occupied)  {
  
        return QVariant(-1);
      }
  
      if (othello::utility::occupied(m_game_engine.pieces(othello::PlayerId::One),
                                     piece_id)) {
        return QVariant(uint(othello::PlayerId::One));
      }
      else if (othello::utility::occupied(
                 m_game_engine.pieces(othello::PlayerId::Two), piece_id)) {
        return QVariant(uint(othello::PlayerId::Two));
      }
    }
    else if (role == PieceNrRole) {
  
      return QVariant(int(piece_id.value()));
    }
  
  
    return QVariant();
  }
  
  int GameModel::boardSize() const { return int(othello::detail::computeBoardSize()); }
  
  uint GameModel::currentPlayer() const {
  
a69582db   Hakeem   orange monkey wor...
56
      return uint(m_game_engine.currentPlayerId());
505e594e   Hakeem   h
57
58
  }
  
a69582db   Hakeem   orange monkey wor...
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
  uint GameModel::playerOneScore() const
  {
      return uint(m_game_engine.board().at(0).count());
  }
  
  
  uint GameModel::playerTwoScore() const
  {
      return uint(m_game_engine.board().at(1).count());
  }
  
  //bool GameModel::currentWinner()
  //{
  //     draw;
  //    switch(1) {
  //    case draw:
  //        if(board_pos.value() < 56)
  //            return othello::BitPos(board_pos.value() + 8);
  //        break;
  
  //    }
  //}
  
505e594e   Hakeem   h
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
  QHash<int, QByteArray> GameModel::roleNames() const
  {
    QHash<int, QByteArray> role_names;
    role_names[OccupiedRole] = "occupied";
    role_names[PlayerNrRole] = "playernr";
    role_names[PieceNrRole] =  "piecenr";
  
    return role_names;
  }
  
  void GameModel::update()
  {
    beginResetModel();
    endResetModel();
    emit boardSizeChanged(othello::detail::computeBoardSize());
    emit currentPlayerChanged(size_t(m_game_engine.currentPlayerId()));
a69582db   Hakeem   orange monkey wor...
98
    emit playerScoreChanged(playerOneScore(),playerTwoScore());
505e594e   Hakeem   h
99
  }