Commit a69582db5722fe883d498f4281c33ab3006f387e

Authored by Hakeem
1 parent 7c6ea7ad

orange monkey working, and stuff

CMakeLists.txt.user
1 <?xml version="1.0" encoding="UTF-8"?> 1 <?xml version="1.0" encoding="UTF-8"?>
2 <!DOCTYPE QtCreatorProject> 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 <qtcreator> 4 <qtcreator>
5 <data> 5 <data>
6 <variable>EnvironmentId</variable> 6 <variable>EnvironmentId</variable>
@@ -53,9 +53,32 @@ int GameModel::boardSize() const { return int(othello::detail::computeBoardSize( @@ -53,9 +53,32 @@ int GameModel::boardSize() const { return int(othello::detail::computeBoardSize(
53 53
54 uint GameModel::currentPlayer() const { 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 QHash<int, QByteArray> GameModel::roleNames() const 82 QHash<int, QByteArray> GameModel::roleNames() const
60 { 83 {
61 QHash<int, QByteArray> role_names; 84 QHash<int, QByteArray> role_names;
@@ -72,4 +95,5 @@ void GameModel::update() @@ -72,4 +95,5 @@ void GameModel::update()
72 endResetModel(); 95 endResetModel();
73 emit boardSizeChanged(othello::detail::computeBoardSize()); 96 emit boardSizeChanged(othello::detail::computeBoardSize());
74 emit currentPlayerChanged(size_t(m_game_engine.currentPlayerId())); 97 emit currentPlayerChanged(size_t(m_game_engine.currentPlayerId()));
  98 + emit playerScoreChanged(playerOneScore(),playerTwoScore());
75 } 99 }
@@ -20,6 +20,8 @@ class GameModel : public QAbstractListModel { @@ -20,6 +20,8 @@ class GameModel : public QAbstractListModel {
20 public: 20 public:
21 Q_PROPERTY(int boardSize READ boardSize NOTIFY boardSizeChanged) 21 Q_PROPERTY(int boardSize READ boardSize NOTIFY boardSizeChanged)
22 Q_PROPERTY(uint currentPlayer READ currentPlayer NOTIFY currentPlayerChanged) 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 enum ModelRoles { OccupiedRole = Qt::UserRole + 1, PlayerNrRole, PieceNrRole }; 26 enum ModelRoles { OccupiedRole = Qt::UserRole + 1, PlayerNrRole, PieceNrRole };
25 27
@@ -40,10 +42,14 @@ private: @@ -40,10 +42,14 @@ private:
40 42
41 int boardSize() const; 43 int boardSize() const;
42 uint currentPlayer() const; 44 uint currentPlayer() const;
  45 + uint playerOneScore() const;
  46 + uint playerTwoScore() const;
  47 + bool currentWinner();
43 48
44 signals: 49 signals:
45 void boardSizeChanged(size_t); 50 void boardSizeChanged(size_t);
46 void currentPlayerChanged(size_t); 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,36 +41,68 @@ GuiApplication::GuiApplication(int&amp; argc, char** argv)
41 connect(root_window, SIGNAL(initNewGameVsOrangeMonkeyAI()), this, 41 connect(root_window, SIGNAL(initNewGameVsOrangeMonkeyAI()), this,
42 SLOT(initNewGameVsOrangeMonkeyAI())); 42 SLOT(initNewGameVsOrangeMonkeyAI()));
43 43
  44 + connect(root_window, SIGNAL(orangeMonkeyAIVsOrangeMonkeyAI()), this,
  45 + SLOT(orangeMonkeyAIVsOrangeMonkeyAI()));
  46 +
44 connect(this, &GuiApplication::gameEnded, this, 47 connect(this, &GuiApplication::gameEnded, this,
45 &GuiApplication::endOfGameActions); 48 &GuiApplication::endOfGameActions);
46 49
47 connect(this, &GuiApplication::enqueueNextTurn, this, 50 connect(this, &GuiApplication::enqueueNextTurn, this,
48 &GuiApplication::startNextTurn); 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 void GuiApplication::endOfTurn() 71 void GuiApplication::endOfTurn()
56 { 72 {
57 m_model.update(); 73 m_model.update();
58 - if (m_game_engine.legalMovesCheck())  
59 -  
60 emit enqueueNextTurn(); 74 emit enqueueNextTurn();
61 } 75 }
62 76
63 void GuiApplication::startNextTurn() 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 void GuiApplication::boardClicked(int pos) 102 void GuiApplication::boardClicked(int pos)
73 { 103 {
  104 + if (!m_game_engine.ifGameStarted())
  105 + return;
74 if (m_game_engine.currentPlayerType() == othello::PlayerType::AI){ 106 if (m_game_engine.currentPlayerType() == othello::PlayerType::AI){
75 return; 107 return;
76 } 108 }
@@ -99,9 +131,25 @@ void GuiApplication::initNewGameVsOrangeMonkeyAI() @@ -99,9 +131,25 @@ void GuiApplication::initNewGameVsOrangeMonkeyAI()
99 emit enqueueNextTurn(); 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 void GuiApplication::endOfGameActions() 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 }
@@ -32,6 +32,7 @@ private: @@ -32,6 +32,7 @@ private:
32 private slots: 32 private slots:
33 void initNewHumanGame(); 33 void initNewHumanGame();
34 void initNewGameVsOrangeMonkeyAI(); 34 void initNewGameVsOrangeMonkeyAI();
  35 + void orangeMonkeyAIVsOrangeMonkeyAI();
35 void endGameAndQuit(); 36 void endGameAndQuit();
36 void endOfGameActions(); 37 void endOfGameActions();
37 void startNextTurn(); 38 void startNextTurn();
@@ -40,7 +41,7 @@ private slots: @@ -40,7 +41,7 @@ private slots:
40 signals: 41 signals:
41 void enqueueNextTurn(); 42 void enqueueNextTurn();
42 void gameEnded(); 43 void gameEnded();
43 - void displayFinalScores(int player_one_score, int player_two_score); 44 + void displayFinalScores();
44 45
45 }; // END class GuiApplication 46 }; // END class GuiApplication
46 47
resources/qml/gui.qml
@@ -7,24 +7,22 @@ ApplicationWindow { @@ -7,24 +7,22 @@ ApplicationWindow {
7 7
8 signal initNewHumanGame() 8 signal initNewHumanGame()
9 signal initNewGameVsOrangeMonkeyAI() 9 signal initNewGameVsOrangeMonkeyAI()
10 - signal initNewMonkeyAIGame() 10 + signal orangeMonkeyAIVsOrangeMonkeyAI()
11 signal endGameAndQuit() 11 signal endGameAndQuit()
12 signal boardClicked(int board_pos) 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 onDisplayFinalScores: { 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 final_score_window.visible = true 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 visible: true 22 visible: true
25 23
26 - width: 800  
27 - height: 800 24 + width: 700
  25 + height: 600
28 26
29 menuBar: MenuBar { 27 menuBar: MenuBar {
30 Menu { 28 Menu {
@@ -38,6 +36,10 @@ ApplicationWindow { @@ -38,6 +36,10 @@ ApplicationWindow {
38 onTriggered: initNewGameVsOrangeMonkeyAI() 36 onTriggered: initNewGameVsOrangeMonkeyAI()
39 } 37 }
40 MenuItem { 38 MenuItem {
  39 + text: "New Game: Orange Monkey vs Orange Monkey"
  40 + onTriggered: orangeMonkeyAIVsOrangeMonkeyAI()
  41 + }
  42 + MenuItem {
41 text: "Quit" 43 text: "Quit"
42 onTriggered: endGameAndQuit() 44 onTriggered: endGameAndQuit()
43 } 45 }
@@ -46,7 +48,9 @@ ApplicationWindow { @@ -46,7 +48,9 @@ ApplicationWindow {
46 48
47 49
48 GridLayout { 50 GridLayout {
49 - anchors.fill: parent 51 + //anchors.fill: parent
  52 + width: 600
  53 + height: 600
50 columns: 8 54 columns: 8
51 rows: 8 55 rows: 8
52 columnSpacing: 0.2 56 columnSpacing: 0.2
@@ -64,22 +68,38 @@ ApplicationWindow { @@ -64,22 +68,38 @@ ApplicationWindow {
64 property string player_two_color: "blue" 68 property string player_two_color: "blue"
65 property string player_one_hover_color: "yellow" 69 property string player_one_hover_color: "yellow"
66 property string player_two_hover_color: "steelblue" 70 property string player_two_hover_color: "steelblue"
67 -  
68 Layout.fillHeight: true 71 Layout.fillHeight: true
69 Layout.fillWidth: true 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 border.color: "black" 104 border.color: "black"
85 border.width: 1 105 border.width: 1
@@ -94,20 +114,40 @@ ApplicationWindow { @@ -94,20 +114,40 @@ ApplicationWindow {
94 onContainsMouseChanged: piece_rec.contains_mouse = containsMouse 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 Rectangle { 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 id: final_score_window 146 id: final_score_window
104 visible: false 147 visible: false
105 148
106 - property int player_one_final_score: 0  
107 - property int player_two_final_score: 0  
108 -  
109 anchors.fill: parent 149 anchors.fill: parent
110 - anchors.margins: 50 150 + anchors.margins: 30
111 151
112 color: "red" 152 color: "red"
113 opacity: 0.8 153 opacity: 0.8
@@ -120,9 +160,9 @@ ApplicationWindow { @@ -120,9 +160,9 @@ ApplicationWindow {
120 text: "Game Over!" } 160 text: "Game Over!" }
121 Item{ height: 20} 161 Item{ height: 20}
122 Text{ Layout.preferredWidth: parent.width; height: 20; 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 Text{ Layout.preferredWidth: parent.width; height: 20; 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 Item{ Layout.fillHeight: true} 166 Item{ Layout.fillHeight: true}
127 Button{ text: "Ok"; onClicked: final_score_window.visible = false } 167 Button{ text: "Ok"; onClicked: final_score_window.visible = false }
128 } 168 }