Commit 53e1f31d authored by Dmitry Suzdalev's avatar Dmitry Suzdalev

Added basic templates of classes KReversiGame, KReversiScene

svn path=/branches/work/kreversi_rewrite/; revision=571722
parent c7055d65
......@@ -7,6 +7,8 @@ add_subdirectory( icons )
set(kreversi_SRCS
kreversiboard.cpp
kreversigame.cpp
kreversiscene.cpp
mainwindow.cpp
main.cpp )
......@@ -22,5 +24,5 @@ install(TARGETS kreversi DESTINATION ${BIN_INSTALL_DIR} )
install( FILES kreversi.desktop DESTINATION ${XDG_APPS_DIR} )
#install( FILES kreversi.kcfg DESTINATION ${KCFG_INSTALL_DIR} )
#install( FILES kreversiui.rc DESTINATION ${DATA_INSTALL_DIR}/kreversi )
install( FILES kreversiui.rc DESTINATION ${DATA_INSTALL_DIR}/kreversi )
#include <kdebug.h>
#include "kreversiboard.h"
KReversiBoard::KReversiBoard(int boardSize)
......@@ -9,5 +11,17 @@ KReversiBoard::KReversiBoard(int boardSize)
void KReversiBoard::resetBoard()
{
m_cells.resize( m_boardSize * m_boardSize );
m_cells.fill( emptyCell );
m_cells.fill( noChip );
}
void KReversiBoard::setChip(int row, int col, ChipType type)
{
Q_ASSERT( row < m_boardSize && col < m_boardSize );
m_cells[row * m_boardSize + col] = type;
}
KReversiBoard::ChipType KReversiBoard::chipAt(int row, int col) const
{
Q_ASSERT( row < m_boardSize && col < m_boardSize );
return m_cells.at( row * m_boardSize + col );
}
......@@ -12,15 +12,23 @@
class KReversiBoard
{
public:
enum CellType { emptyCell, playerCell, computerCell };
enum ChipType { noChip, playerChip, computerChip };
KReversiBoard(int boardSize=10);
/**
* Resets board to initial (start-of-new-game) state
*/
void resetBoard();
/**
* Sets the type of chip at (row,col)
*/
void setChip(int row, int col, ChipType type);
/**
* Retruns type of the chip in cell at (row, col)
*/
ChipType chipAt(int row, int col) const;
private:
QVector<CellType> m_cells;
QVector<ChipType> m_cells;
int m_boardSize;
};
#endif
#include <kdebug.h>
#include "kreversiboard.h"
#include "kreversigame.h"
KReversiGame::KReversiGame( int boardSize )
{
m_board = new KReversiBoard(boardSize);
}
KReversiGame::~KReversiGame()
{
delete m_board;
}
const KReversiBoard& KReversiGame::board() const
{
return *m_board;
}
#ifndef KREVERSI_GAME_H
#define KREVERSI_GAME_H
#include <QObject>
class KReversiBoard;
/**
* KReversiGame incapsulates all of the game logic.
* It creates KReversiBoard and manages a chips on it.
* Whenever the board state changes it emits corresponding signals.
* FIXME dimsuz: enumerate them briefly
* The idea is also to abstract from any graphic representation of the game process
*/
class KReversiGame : public QObject
{
public:
KReversiGame( int boardSize );
~KReversiGame();
/**
* @returns the board so the callers can examine its current state
*/
const KReversiBoard& board() const;
private:
KReversiBoard *m_board;
};
#endif
#include "kreversiscene.h"
KReversiScene::KReversiScene( KReversiGame* game )
{
setGame(game);
}
void KReversiScene::setGame( KReversiGame* game )
{
m_game = game;
}
#ifndef KREVERSI_SCENE_H
#define KREVERSI_SCENE_H
#include <QGraphicsScene>
class KReversiGame;
/**
* This class provides graphical representation of KReversiGame
* using QGraphicsScene for graphics display.
* It displays the reversi board in its current state,
* receives a mouse events, translates them so KReversiGame can understand,
* sends them to it,
* receives board-changed notifications, nicely animates them.
* FIXME dimsuz: make this description more descriptive :)
*/
class KReversiScene : public QGraphicsScene
{
public:
KReversiScene( KReversiGame* game=0 );
void setGame( KReversiGame* game );
private:
KReversiGame *m_game;
};
#endif
<!DOCTYPE kpartgui>
<kpartgui name="kreversi" version="2">
<ActionProperties>
<Action name="game_stop" icon="stop"/>
</ActionProperties>
<MenuBar>
<Menu name="game"><text>&amp;Game</text>
<Action name="game_sound"/>
</Menu>
<Menu name="move"><text>&amp;Move</text>
<Action name="game_undo"/>
<Action name="game_hint"/>
<Action name="game_switch_sides"/>
<Separator/>
<Action name="game_stop"/>
<Action name="game_continue"/>
<Action name="new_game"/>
<Action name="quit"/>
</Menu>
</MenuBar>
<ToolBar name="mainToolBar"><text>Main Toolbar</text>
<Action name="game_new"/>
<Action name="game_stop"/>
<Action name="game_continue"/>
<Action name="game_undo"/>
<Action name="options_show_menubar"/>
<Action name="new_game"/>
</ToolBar>
<ToolBar name="viewToolBar"><text>View Toolbar</text>
<Action name="game_hint"/>
<Action name="show_last_move"/>
<Action name="show_legal_moves"/>
</ToolBar>
<Menu name="popup">
<Action name="options_show_menubar"/>
<Separator/>
<Action name="game_new"/>
<Action name="game_highscores"/>
<Separator/>
<Action name="game_quit"/>
</Menu>
</kpartgui>
#include "mainwindow.h"
#include "kreversigame.h"
#include "kreversiscene.h"
#include <kaction.h>
#include <kstdaction.h>
#include <QGraphicsView>
KReversiMainWindow::KReversiMainWindow(QWidget* parent)
: KMainWindow(parent)
{
setupActions();
m_scene = new KReversiScene;
QGraphicsView *view = new QGraphicsView(m_scene, this);
newGame();
setupGUI();
}
void KReversiMainWindow::setupActions()
{
KStdAction::quit(this, SLOT(close()), actionCollection());
KAction *quitAct = KStdAction::quit(this, SLOT(close()), actionCollection(), "quit");
KAction *newGameAct = KStdAction::openNew(this, SLOT(newGame()), actionCollection(), "new_game");
addAction(newGameAct);
addAction(quitAct);
}
void KReversiMainWindow::newGame()
{
KReversiGame *game = new KReversiGame(10);
m_scene->setGame( game );
}
#include "mainwindow.moc"
......@@ -3,11 +3,18 @@
#include <kmainwindow.h>
class KReversiScene;
class KReversiMainWindow : public KMainWindow
{
Q_OBJECT
public:
KReversiMainWindow(QWidget* parent=0);
public slots:
void newGame();
private:
void setupActions();
KReversiScene* m_scene;
};
#endif
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment