1 /*******************************************************************
3 * Copyright (C) 1997,1998 Marcus Kreutzberger <kreutzbe@informatik.mu-luebeck.de>
4 * Copyright (C) 2006 Henrique Pinto <henrique.pinto@kdemail.net>
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)
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 ********************************************************************/
29 #include <QGraphicsView>
30 #include <QGraphicsScene>
33 #include <QGraphicsPixmapItem>
42 class Stone
: public QGraphicsPixmapItem
45 Stone( KSame::Board
*board
, int x
, int y
, QGraphicsItem
*parent
= 0 );
48 virtual void hoverEnterEvent( QGraphicsSceneHoverEvent
* event
);
49 virtual void hoverLeaveEvent( QGraphicsSceneHoverEvent
* event
);
50 virtual void mousePressEvent( QGraphicsSceneMouseEvent
* event
);
54 KSame::Board
*m_board
;
57 typedef QPair
<int, int> Coordinate
;
62 GameState(): m_valid( false ) {}
63 GameState( KSame::Board
*board
);
65 QVector
<quint8
> m_boardData
;
69 quint32 m_boardNumber
;
75 class Board
: public QGraphicsScene
77 friend class KSame::Stone
;
78 friend class KSame::GameState
;
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(); }
102 bool isGameOver() const;
106 void newGameStarted( quint32 boardNumber
, quint8 colors
);
107 void newCountOfMarkedStones( int markedStones
);
108 void stonesRemoved( int numberOfRemovedStones
);
110 void scoreChanged( quint32 newScore
);
113 virtual void drawBackground( QPainter
*painter
, const QRectF
& rect
);
115 void mark( int x
, int y
);
120 void initializeBoardData();
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
;
132 quint32 m_boardNumber
;
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
;