SVN_SILENT made messages (.desktop file)
[kdegames.git] / ksame / board.h
blob3da200310d34d9e8dac7f4533c429c839c063dfb
1 /*******************************************************************
3 * Copyright (C) 1997,1998 Marcus Kreutzberger <kreutzbe@informatik.mu-luebeck.de>
4 * Copyright (C) 2006 Henrique Pinto <henrique.pinto@kdemail.net>
5 *
6 * This file is part of the KDE project
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2, or (at your option)
11 * any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; see the file COPYING. If not, write to
20 * the Free Software Foundation, 51 Franklin Street, Fifth Floor,
21 * Boston, MA 02110-1301, USA.
23 ********************************************************************/
24 #ifndef BOARD_H
25 #define BOARD_H
27 #include "renderer.h"
29 #include <QGraphicsView>
30 #include <QGraphicsScene>
31 #include <QSize>
32 #include <QVector>
33 #include <QGraphicsPixmapItem>
34 #include <QStack>
35 #include <QPair>
38 namespace KSame
40 class Board;
42 class Stone: public QGraphicsPixmapItem
44 public:
45 Stone( KSame::Board *board, int x, int y, QGraphicsItem *parent = 0 );
47 protected:
48 virtual void hoverEnterEvent( QGraphicsSceneHoverEvent * event );
49 virtual void hoverLeaveEvent( QGraphicsSceneHoverEvent * event );
50 virtual void mousePressEvent( QGraphicsSceneMouseEvent * event );
52 private:
53 int m_x, m_y;
54 KSame::Board *m_board;
57 typedef QPair<int, int> Coordinate;
59 class GameState
61 public:
62 GameState(): m_valid( false ) {}
63 GameState( KSame::Board *board );
65 QVector<quint8> m_boardData;
66 quint8 m_width;
67 quint8 m_height;
68 quint8 m_colorCount;
69 quint32 m_boardNumber;
70 quint32 m_score;
71 bool m_valid;
72 bool m_changed;
75 class Board: public QGraphicsScene
77 friend class KSame::Stone;
78 friend class KSame::GameState;
80 Q_OBJECT
82 public:
83 explicit Board( QObject *parent = 0 );
85 void newGame( quint32 boardNumber = 0, quint8 width = 15, quint8 height = 10, quint8 colorCount = 3 );
86 void resize( const QSize& size );
88 quint32 width() const { return m_width; }
89 quint32 height() const { return m_height; }
90 quint32 score() const { return m_score; }
91 quint32 boardNumber() const { return m_boardNumber; }
92 quint8 colors() const { return m_colorCount; }
93 quint8 count( quint8 color ) const { return m_boardData.count( color ); }
95 int markedStones() const { return m_markedStones.count(); }
96 static int calculateScore( int markedStones );
98 bool changed() const { return m_changed; }
99 bool canUndo() const { return !m_undoList.empty(); }
100 bool undo();
102 bool isGameOver() const;
103 bool won() const;
105 signals:
106 void newGameStarted( quint32 boardNumber, quint8 colors );
107 void newCountOfMarkedStones( int markedStones );
108 void stonesRemoved( int numberOfRemovedStones );
109 void gameOver();
110 void scoreChanged( quint32 newScore );
112 protected:
113 virtual void drawBackground( QPainter *painter, const QRectF& rect );
115 void mark( int x, int y );
116 void unmark();
117 void removeMarked();
119 private:
120 void initializeBoardData();
121 void createItems();
122 void generateGameOverPixmap( bool won );
123 void markHelper( int x, int y, quint8 color );
124 int map( int x, int y ) const;
125 bool validPosition( int x, int y ) const;
127 KSame::Renderer m_renderer;
129 quint8 m_width;
130 quint8 m_height;
131 quint8 m_colorCount;
132 quint32 m_boardNumber;
133 quint32 m_score;
134 bool m_changed;
136 QVector<quint8> m_boardData;
137 QVector<KSame::Stone*> m_stones;
138 QSize m_elementsSize;
139 QList<KSame::Coordinate> m_markedStones;
140 QStack<KSame::GameState> m_undoList;
142 QGraphicsPixmapItem *m_gameOverOverlay;
145 } // namespace KSame
147 #endif // BOARD_H