Commit a69582db5722fe883d498f4281c33ab3006f387e

Authored by Hakeem
1 parent 7c6ea7ad

orange monkey working, and stuff

CMakeLists.txt.user
1 1 <?xml version="1.0" encoding="UTF-8"?>
2 2 <!DOCTYPE QtCreatorProject>
3   -<!-- Written by QtCreator 4.7.0, 2018-09-19T15:34:16. -->
  3 +<!-- Written by QtCreator 4.7.0, 2018-10-02T18:59:24. -->
4 4 <qtcreator>
5 5 <data>
6 6 <variable>EnvironmentId</variable>
... ...
gamemodel.cpp
... ... @@ -53,9 +53,32 @@ int GameModel::boardSize() const { return int(othello::detail::computeBoardSize(
53 53  
54 54 uint GameModel::currentPlayer() const {
55 55  
56   - return uint(m_game_engine.currentPlayerId());
  56 + return uint(m_game_engine.currentPlayerId());
57 57 }
58 58  
  59 +uint GameModel::playerOneScore() const
  60 +{
  61 + return uint(m_game_engine.board().at(0).count());
  62 +}
  63 +
  64 +
  65 +uint GameModel::playerTwoScore() const
  66 +{
  67 + return uint(m_game_engine.board().at(1).count());
  68 +}
  69 +
  70 +//bool GameModel::currentWinner()
  71 +//{
  72 +// draw;
  73 +// switch(1) {
  74 +// case draw:
  75 +// if(board_pos.value() < 56)
  76 +// return othello::BitPos(board_pos.value() + 8);
  77 +// break;
  78 +
  79 +// }
  80 +//}
  81 +
59 82 QHash<int, QByteArray> GameModel::roleNames() const
60 83 {
61 84 QHash<int, QByteArray> role_names;
... ... @@ -72,4 +95,5 @@ void GameModel::update()
72 95 endResetModel();
73 96 emit boardSizeChanged(othello::detail::computeBoardSize());
74 97 emit currentPlayerChanged(size_t(m_game_engine.currentPlayerId()));
  98 + emit playerScoreChanged(playerOneScore(),playerTwoScore());
75 99 }
... ...
gamemodel.h
... ... @@ -20,6 +20,8 @@ class GameModel : public QAbstractListModel {
20 20 public:
21 21 Q_PROPERTY(int boardSize READ boardSize NOTIFY boardSizeChanged)
22 22 Q_PROPERTY(uint currentPlayer READ currentPlayer NOTIFY currentPlayerChanged)
  23 + Q_PROPERTY(uint playerOneScore READ playerOneScore NOTIFY playerScoreChanged)
  24 + Q_PROPERTY(uint playerTwoScore READ playerTwoScore NOTIFY playerScoreChanged)
23 25  
24 26 enum ModelRoles { OccupiedRole = Qt::UserRole + 1, PlayerNrRole, PieceNrRole };
25 27  
... ... @@ -40,10 +42,14 @@ private:
40 42  
41 43 int boardSize() const;
42 44 uint currentPlayer() const;
  45 + uint playerOneScore() const;
  46 + uint playerTwoScore() const;
  47 + bool currentWinner();
43 48  
44 49 signals:
45 50 void boardSizeChanged(size_t);
46 51 void currentPlayerChanged(size_t);
  52 + void playerScoreChanged(size_t,size_t);
47 53 };
48 54  
49 55  
... ...
guiapplication.cpp
... ... @@ -41,36 +41,68 @@ GuiApplication::GuiApplication(int&amp; argc, char** argv)
41 41 connect(root_window, SIGNAL(initNewGameVsOrangeMonkeyAI()), this,
42 42 SLOT(initNewGameVsOrangeMonkeyAI()));
43 43  
  44 + connect(root_window, SIGNAL(orangeMonkeyAIVsOrangeMonkeyAI()), this,
  45 + SLOT(orangeMonkeyAIVsOrangeMonkeyAI()));
  46 +
44 47 connect(this, &GuiApplication::gameEnded, this,
45 48 &GuiApplication::endOfGameActions);
46 49  
47 50 connect(this, &GuiApplication::enqueueNextTurn, this,
48 51 &GuiApplication::startNextTurn);
49 52  
50   - connect(this, SIGNAL(displayFinalScores(int,int)),
51   - root_window, SIGNAL(displayFinalScores(int,int)));
  53 + connect(this, SIGNAL(displayFinalScores()),
  54 + root_window, SIGNAL(displayFinalScores()));
52 55 }
53 56 }
54 57  
  58 +//void GuiApplication::endOfTurn()
  59 +//{
  60 +// m_model.update();
  61 +// if (m_game_engine.noLegalMoves(m_game_engine.currentPlayerId()) and
  62 +// m_game_engine.noLegalMoves(othello::utility::opponent(m_game_engine.currentPlayerId())) )
  63 +// emit gameEnded();
  64 +// else if (m_game_engine.noLegalMoves(m_game_engine.currentPlayerId()) and not
  65 +// m_game_engine.noLegalMoves(othello::utility::opponent(m_game_engine.currentPlayerId()))){
  66 +// m_game_engine.switchCurrentPlayerId();
  67 +// emit enqueueNextTurn();
  68 +// }
  69 +//}
  70 +
55 71 void GuiApplication::endOfTurn()
56 72 {
57 73 m_model.update();
58   - if (m_game_engine.legalMovesCheck())
59   -
60 74 emit enqueueNextTurn();
61 75 }
62 76  
63 77 void GuiApplication::startNextTurn()
64 78 {
65   - if (m_game_engine.currentPlayerType() == othello::PlayerType::AI){
66   - m_game_engine.think(20s);
67   - endOfTurn();
  79 + if (!m_game_engine.noLegalMoves(m_game_engine.currentPlayerId())){
  80 + if (m_game_engine.currentPlayerType() == othello::PlayerType::AI){
  81 + m_game_engine.think(20s);
  82 + endOfTurn();
  83 + }
  84 + }
  85 + else{
  86 + if (m_game_engine.forfeitedLastTurn() == true)
  87 + {
  88 + emit gameEnded();
  89 + qDebug() << "no more moves" ;
  90 + return;
  91 + }
  92 + else
  93 + {
  94 + m_game_engine.forfeitTurn(true);
  95 + emit enqueueNextTurn();
  96 + }
  97 +
68 98 }
69 99  
70 100 }
71 101  
72 102 void GuiApplication::boardClicked(int pos)
73 103 {
  104 + if (!m_game_engine.ifGameStarted())
  105 + return;
74 106 if (m_game_engine.currentPlayerType() == othello::PlayerType::AI){
75 107 return;
76 108 }
... ... @@ -99,9 +131,25 @@ void GuiApplication::initNewGameVsOrangeMonkeyAI()
99 131 emit enqueueNextTurn();
100 132 }
101 133  
102   -void GuiApplication::endGameAndQuit() { QGuiApplication::quit(); }
  134 +void GuiApplication::orangeMonkeyAIVsOrangeMonkeyAI()
  135 +{
  136 + m_game_engine.initPlayerType<othello::monkey_ais::OrangeMonkeyAI, othello::PlayerId::One>();
  137 + m_game_engine.initPlayerType<othello::monkey_ais::OrangeMonkeyAI, othello::PlayerId::Two>();
  138 + m_game_engine.initNewGame();
  139 + m_model.update();
  140 + emit enqueueNextTurn();
  141 +}
  142 +
  143 +
  144 +void GuiApplication::endGameAndQuit() {
  145 + QGuiApplication::quit();
  146 +}
103 147  
104 148  
105 149 void GuiApplication::endOfGameActions()
106 150 {
  151 + if (othello::utility::occupiedPositions(m_game_engine.board()).all())
  152 + emit displayFinalScores();
  153 + qDebug() << "Game Over!" ;
  154 + emit displayFinalScores();
107 155 }
... ...
guiapplication.h
... ... @@ -32,6 +32,7 @@ private:
32 32 private slots:
33 33 void initNewHumanGame();
34 34 void initNewGameVsOrangeMonkeyAI();
  35 + void orangeMonkeyAIVsOrangeMonkeyAI();
35 36 void endGameAndQuit();
36 37 void endOfGameActions();
37 38 void startNextTurn();
... ... @@ -40,7 +41,7 @@ private slots:
40 41 signals:
41 42 void enqueueNextTurn();
42 43 void gameEnded();
43   - void displayFinalScores(int player_one_score, int player_two_score);
  44 + void displayFinalScores();
44 45  
45 46 }; // END class GuiApplication
46 47  
... ...
resources/qml/gui.qml
... ... @@ -7,24 +7,22 @@ ApplicationWindow {
7 7  
8 8 signal initNewHumanGame()
9 9 signal initNewGameVsOrangeMonkeyAI()
10   - signal initNewMonkeyAIGame()
  10 + signal orangeMonkeyAIVsOrangeMonkeyAI()
11 11 signal endGameAndQuit()
12 12 signal boardClicked(int board_pos)
13 13  
14   - signal displayFinalScores(int player_one_score, int player_two_score)
  14 + signal displayFinalScores()
15 15  
16 16 onDisplayFinalScores: {
17   - final_score_window.player_one_final_score = player_one_score
18   - final_score_window.player_two_final_score = player_two_score
19 17 final_score_window.visible = true
20 18  
21   - console.debug("Player scores: " + player_one_score + " vs. " + player_two_scores)
  19 + console.debug("Player scores: " + gamemodel.playerOneScore + " vs. " + gamemodel.playerTwoScore)
22 20 }
23 21  
24 22 visible: true
25 23  
26   - width: 800
27   - height: 800
  24 + width: 700
  25 + height: 600
28 26  
29 27 menuBar: MenuBar {
30 28 Menu {
... ... @@ -38,6 +36,10 @@ ApplicationWindow {
38 36 onTriggered: initNewGameVsOrangeMonkeyAI()
39 37 }
40 38 MenuItem {
  39 + text: "New Game: Orange Monkey vs Orange Monkey"
  40 + onTriggered: orangeMonkeyAIVsOrangeMonkeyAI()
  41 + }
  42 + MenuItem {
41 43 text: "Quit"
42 44 onTriggered: endGameAndQuit()
43 45 }
... ... @@ -46,7 +48,9 @@ ApplicationWindow {
46 48  
47 49  
48 50 GridLayout {
49   - anchors.fill: parent
  51 + //anchors.fill: parent
  52 + width: 600
  53 + height: 600
50 54 columns: 8
51 55 rows: 8
52 56 columnSpacing: 0.2
... ... @@ -64,22 +68,38 @@ ApplicationWindow {
64 68 property string player_two_color: "blue"
65 69 property string player_one_hover_color: "yellow"
66 70 property string player_two_hover_color: "steelblue"
67   -
68 71 Layout.fillHeight: true
69 72 Layout.fillWidth: true
70   - color: {
71   - if (occupied) {
72   - if(playernr == 0) return player_one_color
73   - else if(playernr == 1) return player_two_color
74   - else return "black"
75   - }
76   - else if(contains_mouse) {
77   - if(gamemodel.currentPlayer === 0) return player_one_hover_color
78   - else if(gamemodel.currentPlayer === 1) return player_two_hover_color
79   - else return "black"
80   - }
81   - else return "white"
82   - }
  73 + Rectangle {
  74 + anchors.centerIn: parent
  75 + width: Math.min(parent.width, parent.height) * 0.7
  76 + height: width
  77 + radius: width * 0.5
  78 +
  79 + color: {
  80 + if(occupied)
  81 + return playernr === 0 ? player_one_color : player_two_color
  82 + else if(occupied && contains_mouse)
  83 + return gamemodel.currentPlayer === 0 ? player_one_hover_color : player_two_color
  84 + else if(!occupied && contains_mouse)
  85 + return gamemodel.currentPlayer === 0 ? player_one_hover_color : player_two_color
  86 + else return piece_rec.color
  87 + }
  88 + }
  89 +
  90 +// color: {
  91 +// if (occupied) {
  92 +// if(playernr == 0) return player_one_color
  93 +// else if(playernr == 1) return player_two_color
  94 +// else return "black"
  95 +// }
  96 +// else if(contains_mouse) {
  97 +// if(gamemodel.currentPlayer === 0) return player_one_hover_color
  98 +// else if(gamemodel.currentPlayer === 1) return player_two_hover_color
  99 +// else return "black"
  100 +// }
  101 +// else return "white"
  102 +// }
83 103  
84 104 border.color: "black"
85 105 border.width: 1
... ... @@ -94,20 +114,40 @@ ApplicationWindow {
94 114 onContainsMouseChanged: piece_rec.contains_mouse = containsMouse
95 115  
96 116 }
  117 +
  118 + //...
  119 +
97 120 }
98 121 }
99 122 }
100 123 }
101 124  
102 125 Rectangle {
  126 + id: in_game_scores
  127 +
  128 + width: 100
  129 + x: 600
  130 +
  131 + ColumnLayout {
  132 + anchors.fill: parent
  133 + anchors.margins: 10
  134 +
  135 + Item{ height: 20}
  136 + Text{ text: "Current Score!" }
  137 + Item{ height: 20}
  138 + Text{ text: "Player one: " + gamemodel.playerOneScore}
  139 + Text{ text: "Player two: " + gamemodel.playerTwoScore}
  140 + //Item{ height: 20}
  141 + //Text{ text: + gamemodel.winner "Player one: " + gamemodel.playerOneScore}
  142 + }
  143 + }
  144 +
  145 + Rectangle {
103 146 id: final_score_window
104 147 visible: false
105 148  
106   - property int player_one_final_score: 0
107   - property int player_two_final_score: 0
108   -
109 149 anchors.fill: parent
110   - anchors.margins: 50
  150 + anchors.margins: 30
111 151  
112 152 color: "red"
113 153 opacity: 0.8
... ... @@ -120,9 +160,9 @@ ApplicationWindow {
120 160 text: "Game Over!" }
121 161 Item{ height: 20}
122 162 Text{ Layout.preferredWidth: parent.width; height: 20;
123   - text: "Player one: " + final_score_window.player_one_final_score}
  163 + text: "Player one: " + gamemodel.playerOneScore}
124 164 Text{ Layout.preferredWidth: parent.width; height: 20;
125   - text: "Player two: " + final_score_window.player_two_final_score}
  165 + text: "Player two: " + gamemodel.playerTwoScore}
126 166 Item{ Layout.fillHeight: true}
127 167 Button{ text: "Ok"; onClicked: final_score_window.visible = false }
128 168 }
... ...