Commit d16f0617 authored by Dmitry Suzdalev's avatar Dmitry Suzdalev

Experimenting... with:

QGraphicsView
Board representation
Engine porting to new classes (which are a stubs :) )

Nothing works, nothing compiles :).
Just saving my progress.

svn path=/branches/work/kreversi_rewrite/; revision=571814
parent 53e1f31d
......@@ -9,6 +9,8 @@ set(kreversi_SRCS
kreversiboard.cpp
kreversigame.cpp
kreversiscene.cpp
kreversiview.cpp
Engine.cpp
mainwindow.cpp
main.cpp )
......
......@@ -117,8 +117,6 @@
// or nearly equal value after the search is completed.
#include <qapplication.h>
#include "Engine.h"
......@@ -296,7 +294,7 @@ void Engine::yield()
// Calculate the best move from the current position, and return it.
Move Engine::computeMove(Game *game, bool competitive)
KReversiMove Engine::computeMove(const KReversiGame& game, bool competitive)
{
Color color;
......@@ -467,7 +465,7 @@ Move Engine::computeMove(Game *game, bool competitive)
// Get the first move. We can pick any move at random.
//
Move Engine::ComputeFirstMove(Game *game)
KReversiMove Engine::ComputeFirstMove(const KReversiGame& game)
{
int r;
Color color = game->toMove();
......
......@@ -119,15 +119,18 @@
#ifndef __ENGINE__H__
#define __ENGINE__H__
#include "SuperEngine.h"
#include "Position.h"
#include "Game.h"
#include "Move.h"
#include "Score.h"
//#include "SuperEngine.h"
//#include "Position.h"
//#include "Game.h"
//#include "Move.h"
//#include "Score.h"
#include <sys/times.h>
#include <qbitarray.h>
//#include <sys/times.h>
#include <QBitArray>
#include <QVector>
#include "kreversiboard.h"
class KReversiGame;
// Class ULONG64 is used as a bitmap for the squares.
......@@ -196,37 +199,39 @@ public:
// The real beef of this program: the engine that finds good moves for
// the computer player.
//
class Engine : public SuperEngine {
class Engine {
public:
Engine(int st, int sd);
Engine(int st);
Engine();
Move computeMove(Game *game, bool competitive);
KReversiMove computeMove(const KReversiGame& game, bool competitive);
private:
Move ComputeFirstMove(Game *game);
int ComputeMove2(int xplay, int yplay, Color color, int level,
KReversiMove ComputeFirstMove(const KReversiGame& game);
int ComputeMove2(int xplay, int yplay, ChipColor color, int level,
int cutoffval,
ULONG64 colorbits, ULONG64 opponentbits);
int TryAllMoves(Color opponent, int level, int cutoffval,
int TryAllMoves(ChipColor opponent, int level, int cutoffval,
ULONG64 opponentbits, ULONG64 colorbits);
int EvaluatePosition(Color color);
int EvaluatePosition(ChipColor color);
void SetupBcBoard();
void SetupBits();
int CalcBcScore(Color color);
ULONG64 ComputeOccupiedBits(Color color);
int CalcBcScore(ChipColor color);
ULONG64 ComputeOccupiedBits(ChipColor color);
void yield();
private:
// FIXME dimsuz: do these needed outside Engine?
// If not put them as static globals in cpp
static const int LARGEINT;
static const int ILLEGAL_VALUE;
static const int BC_WEIGHT;
Color m_board[10][10];
ChipColor m_board[10][10];
int m_bc_board[9][9];
Score m_score;
Score m_bc_score;
......
......@@ -2,26 +2,26 @@
#include "kreversiboard.h"
KReversiBoard::KReversiBoard(int boardSize)
: m_boardSize(boardSize)
KReversiBoard::KReversiBoard()
{
resetBoard();
}
void KReversiBoard::resetBoard()
{
m_cells.resize( m_boardSize * m_boardSize );
m_cells.fill( noChip );
for(int r=0; r<8; ++r)
for(int c=0; c<8; ++c)
m_cells[r][c] = NoColor;
}
void KReversiBoard::setChip(int row, int col, ChipType type)
void KReversiBoard::setChipColor(int row, int col, ChipColor color)
{
Q_ASSERT( row < m_boardSize && col < m_boardSize );
m_cells[row * m_boardSize + col] = type;
Q_ASSERT( row < 8 && col < 8 );
m_cells[row][col] = color;
}
KReversiBoard::ChipType KReversiBoard::chipAt(int row, int col) const
ChipColor KReversiBoard::chipColorAt(int row, int col) const
{
Q_ASSERT( row < m_boardSize && col < m_boardSize );
return m_cells.at( row * m_boardSize + col );
Q_ASSERT( row < 8 && col < 8 );
return m_cells[row][col];
}
......@@ -3,6 +3,9 @@
#include <QVector>
// noColor = empty
enum ChipColor { NoColor, White, Black };
/**
* This class represents a reversi board.
* It only holds a current state of the game,
......@@ -12,9 +15,7 @@
class KReversiBoard
{
public:
enum ChipType { noChip, playerChip, computerChip };
KReversiBoard(int boardSize=10);
KReversiBoard();
/**
* Resets board to initial (start-of-new-game) state
*/
......@@ -22,13 +23,22 @@ public:
/**
* Sets the type of chip at (row,col)
*/
void setChip(int row, int col, ChipType type);
void setChipColor(int row, int col, ChipColor type);
/**
* Retruns type of the chip in cell at (row, col)
*/
ChipType chipAt(int row, int col) const;
ChipColor chipColorAt(int row, int col) const;
private:
QVector<ChipType> m_cells;
int m_boardSize;
ChipColor m_cells[8][8];
};
struct KReversiMove
{
KReversiMove( ChipColor col = NoColor, int r = -1, int c = -1 )
: color(col), row(r), col(c) { }
ChipColor color;
int row;
int col;
};
#endif
......@@ -4,9 +4,9 @@
#include "kreversigame.h"
KReversiGame::KReversiGame( int boardSize )
KReversiGame::KReversiGame()
{
m_board = new KReversiBoard(boardSize);
m_board = new KReversiBoard();
}
KReversiGame::~KReversiGame()
......
......@@ -15,7 +15,7 @@ class KReversiBoard;
class KReversiGame : public QObject
{
public:
KReversiGame( int boardSize );
KReversiGame();
~KReversiGame();
/**
* @returns the board so the callers can examine its current state
......
#include "kreversiscene.h"
#include <QGraphicsRectItem>
KReversiScene::KReversiScene( KReversiGame* game )
{
setGame(game);
setBackgroundBrush( Qt::green );
}
void KReversiScene::setGame( KReversiGame* game )
{
QPen pen(Qt::red);
//pen.setWidth(1);
addRect( sceneRect(), pen )->show();
m_game = game;
}
#include "kreversiview.h"
#include <QGraphicsScene>
#include <kdebug.h>
const int BOARD_SIZE = 8;
// FIXME dimsuz: something to remove/give-more-thinking
const int CHIP_SIZE = 32;
KReversiView::KReversiView( QGraphicsScene* scene, QWidget *parent )
: QGraphicsView(scene, parent)
{
scene->setSceneRect( 0, 0, CHIP_SIZE*BOARD_SIZE, CHIP_SIZE*BOARD_SIZE );
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
// FIXME dimsuz: let it be so at the beginning
setFixedSize(sizeHint());
}
QSize KReversiView::sizeHint() const
{
kDebug() << "sceneRect:" << sceneRect() << endl;
QRect r = mapFromScene(sceneRect()).boundingRect();
kDebug() << "viewRect:" << r << endl;
return QSize(r.size());
}
#ifndef KREVERSI_VIEW_H
#define KREVERSI_VIEW_H
#include <QGraphicsView>
class KReversiView : public QGraphicsView
{
public:
KReversiView( QGraphicsScene* scene, QWidget *parent );
private:
virtual QSize sizeHint() const;
};
#endif
#include "mainwindow.h"
#include "kreversigame.h"
#include "kreversiscene.h"
#include "kreversiview.h"
#include <kaction.h>
#include <kstdaction.h>
......@@ -8,11 +9,13 @@
#include <QGraphicsView>
KReversiMainWindow::KReversiMainWindow(QWidget* parent)
: KMainWindow(parent)
: KMainWindow(parent), m_game(0)
{
setupActions();
m_scene = new KReversiScene;
QGraphicsView *view = new QGraphicsView(m_scene, this);
KReversiView *view = new KReversiView(m_scene, this);
view->show();
setCentralWidget(view);
newGame();
setupGUI();
}
......@@ -27,8 +30,9 @@ void KReversiMainWindow::setupActions()
void KReversiMainWindow::newGame()
{
KReversiGame *game = new KReversiGame(10);
m_scene->setGame( game );
delete m_game;
m_game = new KReversiGame;
m_scene->setGame( m_game );
}
#include "mainwindow.moc"
......@@ -4,6 +4,7 @@
#include <kmainwindow.h>
class KReversiScene;
class KReversiGame;
class KReversiMainWindow : public KMainWindow
{
......@@ -15,6 +16,7 @@ public slots:
private:
void setupActions();
KReversiScene* m_scene;
KReversiScene *m_scene;
KReversiGame *m_game;
};
#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