~keith-penguin/kdegames/trunk

« back to all changes in this revision

Viewing changes to bovo/game/game.cc

  • Committer: Keith Worrell
  • Date: 2009-03-18 05:35:28 UTC
  • Revision ID: keith.worrell@gmail.com-20090318053528-mx6x9c0ngmg0kg6p
imported project

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*******************************************************************
 
2
*
 
3
* This file is part of the KDE project "Bovo"
 
4
*
 
5
* Bovo is free software; you can redistribute it and/or modify
 
6
* it under the terms of the GNU General Public License as published by
 
7
* the Free Software Foundation; either version 2, or (at your option)
 
8
* any later version.
 
9
*
 
10
* Bovo is distributed in the hope that it will be useful,
 
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
* GNU General Public License for more details.
 
14
*
 
15
* You should have received a copy of the GNU General Public License
 
16
* along with Bovo; see the file COPYING.  If not, write to
 
17
* the Free Software Foundation, 51 Franklin Street, Fifth Floor,
 
18
* Boston, MA 02110-1301, USA.
 
19
*
 
20
********************************************************************/
 
21
 
 
22
/** @file game.cc implements class Game in namespace bovo */
 
23
 
 
24
#include "game.h"
 
25
 
 
26
#include <QtCore/QTimer>
 
27
#include <QtCore/QString>
 
28
#include <QtCore/QStringList>
 
29
 
 
30
#include "ai.h"
 
31
#include "board.h"
 
32
#include "coord.h"
 
33
#include "dimension.h"
 
34
#include "move.h"
 
35
 
 
36
using namespace ai;
 
37
 
 
38
/** namespace for gui stuff */
 
39
namespace bovo
 
40
{
 
41
 
 
42
Game::Game(const Dimension& dimension, Player startingPlayer, 
 
43
           KGameDifficulty::standardLevel skill, DemoMode demoMode, 
 
44
           unsigned int playTime)
 
45
  : m_curPlayer(startingPlayer),m_computerMark(O), m_demoMode(demoMode),
 
46
  m_inUndoState(false), m_playerMark(X), m_playTime(playTime),
 
47
  m_replaying(false) {
 
48
    m_board = new Board(dimension);
 
49
    m_ai = new Ai(dimension, skill, m_computerMark);
 
50
    m_winDir = -1;
 
51
    m_gameOver = false;
 
52
    connect(this, SIGNAL(boardChanged(const Move&)),
 
53
            m_ai, SLOT(changeBoard(const Move&)));
 
54
    connect(this, SIGNAL(oposerTurn()), m_ai, SLOT(slotMove()),
 
55
            Qt::QueuedConnection);
 
56
    connect(m_ai, SIGNAL(move(const Move&)),
 
57
            this,  SLOT(move(const Move&)));
 
58
}
 
59
 
 
60
Game::Game(const Dimension& dimension, const QStringList &restoreGame, 
 
61
           KGameDifficulty::standardLevel skill, unsigned int playTime)
 
62
  : m_computerMark(O), m_demoMode(NotDemo), m_inUndoState(false),
 
63
  m_playerMark(X), m_playTime(playTime), m_replaying(false) {
 
64
    m_board = new Board(dimension);
 
65
    m_ai = new Ai(dimension, skill, m_computerMark);
 
66
    m_winDir = -1;
 
67
    m_gameOver = false;
 
68
    m_curPlayer = No;
 
69
    foreach (const QString &turn, restoreGame) {
 
70
        QStringList tmp = turn.split(':');
 
71
        if (tmp.count() != 2) {
 
72
            qFatal("Wrong save file format!");
 
73
        }
 
74
        Player tmpPlayer = (tmp[0] == "1") ? X : O;
 
75
        if (m_curPlayer == No) {
 
76
            m_curPlayer = tmpPlayer;
 
77
        }
 
78
        tmp = tmp[1].split(',');
 
79
        if (tmp.count() != 2) {
 
80
            qFatal("Wrong save file format!");
 
81
        }
 
82
        bool ok;
 
83
        uint x = tmp[0].toUInt(&ok);
 
84
        if (!ok) {
 
85
            qFatal("Wrong save file format!");
 
86
        }
 
87
        uint y = tmp[1].toUInt(&ok);
 
88
        if (!ok) {
 
89
            qFatal("Wrong save file format!");
 
90
        }
 
91
        Move tmpMove(tmpPlayer, Coord(x, y));
 
92
        m_board->setPlayer(tmpMove);
 
93
        m_history << tmpMove;
 
94
    }
 
95
}
 
96
 
 
97
Game::~Game() {
 
98
    delete m_board;
 
99
    delete m_ai;
 
100
}
 
101
 
 
102
bool Game::computerTurn() const {
 
103
    return m_curPlayer == m_computerMark;
 
104
}
 
105
 
 
106
DemoMode Game::demoMode() const {
 
107
    return m_demoMode;
 
108
}
 
109
 
 
110
bool Game::isGameOver() const {
 
111
    return m_gameOver || m_demoMode;
 
112
}
 
113
 
 
114
QList<Move> Game::history() const {
 
115
    return m_history;
 
116
}
 
117
 
 
118
Move Game::latestMove() const {
 
119
    if (m_history.empty()) {
 
120
        return Move();
 
121
    } else {
 
122
        return m_history.back();
 
123
    }
 
124
}
 
125
 
 
126
bool Game::ok(const Coord& coord) const {
 
127
    return m_board->ok(coord);
 
128
}
 
129
 
 
130
Player Game::player() const {
 
131
    return m_playerMark;
 
132
}
 
133
 
 
134
Player Game::player(const Coord& coord) const {
 
135
    return m_board->player(coord);
 
136
}
 
137
 
 
138
bool Game::save(const QString& filename) const {
 
139
    Q_UNUSED( filename );
 
140
 
 
141
    QString fileContent;
 
142
    fileContent.append(QString("<bovo width=\"%1\" height=\"%2\">")
 
143
            .arg("").arg(""));
 
144
    foreach (const Move &move, m_history) {
 
145
        fileContent.append(QString("<move player=\"%1\" x=\"%2\" y=\"%3\" />").
 
146
                arg(move.player()).arg(move.x()).arg(move.y()));
 
147
    }
 
148
    fileContent.append("</bovo>");
 
149
    return false;
 
150
}
 
151
 
 
152
QStringList Game::saveLast() const {
 
153
    QStringList save;
 
154
    foreach (const Move &move, m_history) {
 
155
        save << QString("%1:%2,%3").arg(move.player())
 
156
                .arg(move.x()).arg(move.y());
 
157
    }
 
158
    return save;
 
159
}
 
160
 
 
161
void Game::setSkill(KGameDifficulty::standardLevel skill) {
 
162
    if (m_ai!=0)
 
163
        m_ai->setSkill(skill);
 
164
}
 
165
 
 
166
void Game::start() {
 
167
    if (computerTurn()) {
 
168
        emit oposerTurn();
 
169
    } else {
 
170
        emit playerTurn();
 
171
    }
 
172
}
 
173
 
 
174
void Game::startRestored() {
 
175
    connect(this, SIGNAL(boardChanged(const Move&)),
 
176
            m_ai, SLOT(changeBoard(const Move&)));
 
177
    foreach (const Move &move, m_history) {
 
178
        emit boardChanged(move);
 
179
    }
 
180
    connect(this, SIGNAL(oposerTurn()), m_ai, SLOT(slotMove()),
 
181
            Qt::QueuedConnection);
 
182
    connect(m_ai, SIGNAL(move(const Move&)),
 
183
            this,  SLOT(move(const Move&)));
 
184
    if (!m_history.isEmpty() && m_history.last().player() == X) {
 
185
        m_curPlayer = O;
 
186
        emit oposerTurn();
 
187
    } else {
 
188
        m_curPlayer = X;
 
189
        emit playerTurn();
 
190
    }
 
191
    if (!m_history.isEmpty()) {
 
192
        emit undoAble();
 
193
    }
 
194
}
 
195
 
 
196
short Game::winDir() const {
 
197
    return m_winDir;
 
198
}
 
199
 
 
200
/* public slots */
 
201
 
 
202
void Game::move(const Move& move) {
 
203
    bool tmp_emptyHistory = m_history.empty();
 
204
    if (!m_board->empty(move.coord()) || move.player() != m_curPlayer
 
205
         || m_inUndoState) {
 
206
        return;
 
207
    }
 
208
    makeMove(move);
 
209
    if (tmp_emptyHistory && !m_history.empty() && !m_demoMode) {
 
210
        emit undoAble();
 
211
    }
 
212
}
 
213
 
 
214
void Game::replay() {
 
215
    m_replayIterator = m_history.constBegin();
 
216
    m_replayIteratorEnd = m_history.constEnd();
 
217
    if (m_gameOver && !m_replaying) {
 
218
        disconnect(this, SIGNAL(replayBegin()), this, SLOT(replayNext()));
 
219
        connect(this, SIGNAL(replayBegin()), this, SLOT(replayNext()));
 
220
        emit replayBegin();
 
221
    }
 
222
}
 
223
 
 
224
void Game::undoLatest() {
 
225
    m_inUndoState = true;
 
226
    if (m_history.empty() ||  m_demoMode || m_gameOver) {
 
227
        m_inUndoState = false;
 
228
        return;
 
229
    } else if (m_curPlayer == m_computerMark) {
 
230
        Move move(No, m_history.last().coord());
 
231
        m_history.removeLast();
 
232
        m_board->setPlayer(move);
 
233
        emit boardChanged(move);
 
234
        m_curPlayer = m_playerMark;
 
235
        emit playerTurn();
 
236
    } else if (m_curPlayer == m_playerMark && m_history.count() == 1) {
 
237
        Move move(No, m_history.last().coord());
 
238
        m_history.removeLast();
 
239
        m_board->setPlayer(move);
 
240
        emit boardChanged(move);
 
241
        m_curPlayer = m_computerMark;
 
242
        emit oposerTurn();
 
243
    } else if (m_curPlayer == m_playerMark && m_history.count() > 1 ) {
 
244
        Move move(No, m_history.last().coord());
 
245
        m_history.removeLast();
 
246
        m_board->setPlayer(move);
 
247
        emit boardChanged(move);
 
248
        Move move2(No, m_history.last().coord());
 
249
        m_history.removeLast();
 
250
        m_board->setPlayer(move2);
 
251
        emit boardChanged(move2);
 
252
        emit playerTurn();
 
253
    }
 
254
    if (m_history.empty() && !m_demoMode) {
 
255
        emit undoNotAble();
 
256
    }
 
257
    m_inUndoState = false;
 
258
}
 
259
 
 
260
/* private slots */
 
261
 
 
262
void Game::replayNext() {
 
263
    if (m_replayIterator != m_replayIteratorEnd) {
 
264
        QTimer::singleShot(m_playTime, this, SLOT(replayNext()));
 
265
        emit boardChanged(*m_replayIterator);
 
266
        ++m_replayIterator;
 
267
    } else {
 
268
        m_replaying = false;
 
269
        emit replayEnd(winningMoves()); // FIX:!!!!!!!
 
270
    }
 
271
}
 
272
 
 
273
/* private methods */
 
274
 
 
275
void Game::makeMove(const Move& move) {
 
276
    if (move.player() != m_curPlayer) {
 
277
        return;
 
278
    }
 
279
    m_board->setPlayer(move);
 
280
    m_winDir = win(move.coord());
 
281
    if (m_winDir != -1) {
 
282
        m_gameOver = true;
 
283
    }
 
284
    m_history << move;
 
285
    m_curPlayer = (m_curPlayer == X ? O : X );
 
286
    emit boardChanged(move);
 
287
    if (m_gameOver) {
 
288
        QList<Move> moves = winningMoves();
 
289
        emit undoNotAble();
 
290
        emit gameOver(moves);
 
291
        this->disconnect(m_ai);
 
292
    } else {
 
293
        if (computerTurn()) {
 
294
            if (m_demoMode) {
 
295
                QTimer::singleShot(m_playTime, this, SIGNAL(oposerTurn()));
 
296
            } else {
 
297
                emit oposerTurn();
 
298
            }
 
299
        } else {
 
300
            if (m_demoMode) {
 
301
                QTimer::singleShot(m_playTime, this, SIGNAL(playerTurn()));
 
302
            } else {
 
303
                emit playerTurn();
 
304
            }
 
305
        }
 
306
    }
 
307
}
 
308
 
 
309
Coord Game::next(const Coord& coord, usi dir) const {
 
310
    usi LEFT = 1;
 
311
    usi UP = 2;
 
312
    usi RIGHT = 4;
 
313
    usi DOWN = 8;
 
314
    Coord tmp = coord;
 
315
    if (dir & LEFT) {
 
316
        tmp = tmp.left();
 
317
    } else if (dir & RIGHT) {
 
318
        tmp = tmp.right();
 
319
    }
 
320
    if (dir & UP) {
 
321
        tmp = tmp.up();
 
322
    } else if (dir & DOWN) {
 
323
        tmp = tmp.down();
 
324
    }
 
325
    return tmp;
 
326
}
 
327
 
 
328
short Game::win(const Coord& c) const {
 
329
    usi LEFT = 1;
 
330
    usi UP = 2;
 
331
    usi RIGHT = 4;
 
332
    usi DOWN = 8;
 
333
    usi DIR[8] = {LEFT, RIGHT, UP, DOWN, LEFT | UP, RIGHT | DOWN,
 
334
                  LEFT | DOWN, RIGHT | UP};
 
335
    Player p = player(c);
 
336
    for (int i = 0; i < 4; ++i) {
 
337
        usi count = 1;
 
338
        Coord tmp = next(c, DIR[2*i]);
 
339
        while (m_board->ok(tmp) && player(tmp) == p) {
 
340
            ++count;
 
341
            tmp = next(tmp, DIR[2*i]);
 
342
        }
 
343
        tmp = next(c, DIR[2*i+1]);
 
344
        while (m_board->ok(tmp) && player(tmp) == p) {
 
345
            ++count;
 
346
            tmp = next(tmp, DIR[2*i+1]);
 
347
        }
 
348
        if (count >= 5) {
 
349
            return i;
 
350
        }
 
351
    }
 
352
    return -1;
 
353
}
 
354
 
 
355
QList<Move> Game::winningMoves() const {
 
356
    if (m_winDir == -1) {
 
357
        return QList<Move>();
 
358
    }
 
359
    QList<Move> moves;
 
360
    short dy, dx;
 
361
    switch (m_winDir) {
 
362
        case 0: dx = 1; dy =  0; break;
 
363
        case 1: dx = 0; dy =  1; break;
 
364
        case 2: dx = 1; dy =  1; break;
 
365
        default: dx = 1; dy = -1; break;
 
366
    }
 
367
    usi x = latestMove().x();
 
368
    usi y = latestMove().y();
 
369
    Player winner = player(Coord(x, y));
 
370
    Player tmp;
 
371
    while ((tmp = player(Coord(x, y))) == winner) {
 
372
        moves << Move(player(Coord(x, y)), Coord(x, y));
 
373
        x += dx;
 
374
        y += dy;
 
375
    }
 
376
    x = latestMove().x() - dx;
 
377
    y = latestMove().y() - dy;
 
378
    while ((tmp = player(Coord(x, y))) == winner) {
 
379
        moves << Move(player(Coord(x, y)), Coord(x, y));
 
380
        x -= dx;
 
381
        y -= dy;
 
382
    }
 
383
    return moves;
 
384
}
 
385
 
 
386
}
 
387
 
 
388
#include "game.moc"