Commit 1667252e authored by Zhongjie Cai's avatar Zhongjie Cai
Browse files

Refactory of KBlocks with support of human v.s. AI game mode, and network tetris research platform.

svn path=/trunk/playground/games/kblocks-refactoring/; revision=1114449
parent a4dabca4
/***************************************************************************
* KBlocks, a falling blocks game for KDE *
* Copyright (C) 2009 University Freiburg *
* Copyright (C) 2010 University Freiburg *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
......@@ -22,8 +22,8 @@
}
#else
#define CLASSNAME(class)
#endif
#endif
class EvaluationInterface
{
public:
......@@ -64,7 +64,7 @@ public:
return "";
}
#endif
protected:
KBlocksPiece* mpPiece;
KBlocksField* mpField;
......
/***************************************************************************
* KBlocks, a falling blocks game for KDE *
* Copyright (C) 2009 University Freiburg *
* Copyright (C) 2010 University Freiburg *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
......@@ -364,22 +364,22 @@ double NBSEvaluation::evaluate(KBlocksField * field)
{
Feature features [] =
{
{FEATURE_MAX_HEIGHT,-62709}, //1
{FEATURE_HOLES_COUNT,-30271}, //2
{FEATURE_CLOSED_HOLES_COUNT,0}, //3
{FEATURE_MAX_HEIGHT_DIFFERENT,35395}, //5
{FEATURE_MAX_WELL_DEPTH,-12}, //6
{FEATURE_WELLS_COUNT,-43810}, //7
{FEATURE_BLOCKS_COUNT,0}, //9
{FEATURE_WEIGHTED_BLOCKS_COUNT,-4041}, //10
{FEATURE_ROW_TRANSITION_COUNT,-44262}, //11
{FEATURE_MAX_HEIGHT,-62709}, //1
{FEATURE_HOLES_COUNT,-30271}, //2
{FEATURE_CLOSED_HOLES_COUNT,0}, //3
{FEATURE_MAX_HEIGHT_DIFFERENT,35395}, //5
{FEATURE_MAX_WELL_DEPTH,-12}, //6
{FEATURE_WELLS_COUNT,-43810}, //7
{FEATURE_BLOCKS_COUNT,0}, //9
{FEATURE_WEIGHTED_BLOCKS_COUNT,-4041}, //10
{FEATURE_ROW_TRANSITION_COUNT,-44262}, //11
{FEATURE_COLUMN_TRANSITION_COUNT,-5832},//12
};
SFeature sfeatures [] =
{
{FEATURE_REMOVE_LINES,13344}, //4
{FEATURE_LANDING_HEIGHT,0}, //8
{FEATURE_REMOVE_LINES,13344}, //4
{FEATURE_LANDING_HEIGHT,0}, //8
};
uint n1 = sizeof(features)/sizeof(Feature);
......
/***************************************************************************
* KBlocks, a falling blocks game for KDE *
* Copyright (C) 2009 University Freiburg *
* Copyright (C) 2010 University Freiburg *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
......
/***************************************************************************
* KBlocks, a falling blocks game for KDE *
* Copyright (C) 2009 University Freiburg *
* Copyright (C) 2010 University Freiburg *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
......@@ -375,7 +375,7 @@ Evaluation_Blocks_Over_Holes_Count *Evaluation_Blocks_Over_Holes_Count::_instanc
double Evaluation_Blocks_Over_Holes_Count::evaluate(KBlocksField * field)
{
int w = field->getWidth();
int h0 = field->getHeight();
int h0 = field->getHeight();
int count = 0;
for(int col = 0; col < w; col++)
{
......
/***************************************************************************
* KBlocks, a falling blocks game for KDE *
* Copyright (C) 2009 University Freiburg *
* Copyright (C) 2010 University Freiburg *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
......@@ -27,10 +27,10 @@ enum FeatureEnumeration
FEATURE_CLOSED_HOLES_COUNT,
FEATURE_WELLS_COUNT,
FEATURE_AVERAGE_HEIGHT,
FEATURE_AVERAGE_HEIGHT_DIFFERENT,
FEATURE_MAX_HEIGHT_DIFFERENT,
FEATURE_AVERAGE_HEIGHT_DIFFERENT,
FEATURE_MAX_HEIGHT_DIFFERENT,
FEATURE_KONTUR_COUNT,
FEATURE_MAX_KONTUR_LENGTH,
FEATURE_MAX_KONTUR_LENGTH,
FEATURE_BLOCKS_COUNT,
FEATURE_BLOCKS_OVER_HOLES_COUNT,
FEATURE_WEIGHTED_BLOCKS_COUNT,
......
/***************************************************************************
* KBlocks, a falling blocks game for KDE *
* Copyright (C) 2009 University Freiburg *
* Copyright (C) 2010 University Freiburg *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
......
/***************************************************************************
* KBlocks, a falling blocks game for KDE *
* Copyright (C) 2009 University Freiburg *
* Copyright (C) 2010 University Freiburg *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
......
/***************************************************************************
* KBlocks, a falling blocks game for KDE *
* Copyright (C) 2009 University Freiburg *
* Copyright (C) 2010 University Freiburg *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
......@@ -161,6 +161,14 @@ bool KBlocksAIPlanner::getNextBoardStatus(int index, KBlocksField * field)
{
field->setCell(piece->getCellPosX(i), piece->getCellPosY(i), true);
}
int maxLines = field->getHeight();
for(int i = 0; i < maxLines; i++)
{
if (field->checkFilledLine(i))
{
field->removeFilledLine(i);
}
}
delete piece;
......
/***************************************************************************
* KBlocks, a falling blocks game for KDE *
* Copyright (C) 2009 University Freiburg *
* Copyright (C) 2010 University Freiburg *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
......@@ -30,7 +30,7 @@ public:
virtual int count();
private:
private:
AIPlanner_PieceInfo_Sequence mNextPieceValues;
};
......
/***************************************************************************
* KBlocks, a falling blocks game for KDE *
* Copyright (C) 2009 University Freiburg *
* Copyright (C) 2010 University Freiburg *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
......@@ -16,7 +16,7 @@ using namespace PlanningPath;
**** Data Structure - PathNode *************************************************
********************************************************************************/
PathNode::PathNode(KBlocksPiece * piece)
{
{
parent = 0;
next = 0;
setContent(piece);
......@@ -61,7 +61,7 @@ void PathNode::setNext(PathTree* &next)
**** Planner ******************************************************************
********************************************************************************/
KBlocksAIPlannerExtend::KBlocksAIPlannerExtend(KBlocksField* field) : KBlocksAIPlanner(field)
{
{
mPathTree = new PathTree();
mPathList = new LeafList();
}
......@@ -84,7 +84,7 @@ bool KBlocksAIPlannerExtend::getPath(int index, AIPlanner_PieceInfo_Sequence * p
PathNode *node = (*mPathList)[index];
while (node != 0)
{
KBlocksPiece piece = node->getContent();
KBlocksPiece piece = node->getContent();
pseq->push_back(piece);
node = node->getParent();
}
......@@ -117,20 +117,28 @@ bool KBlocksAIPlannerExtend::getNextBoardStatus(int index, KBlocksField * field,
field->copy(mpField);
for(int i = 0; i < (int)path.size(); i++)
for(int i = (int)path.size() - 1; i >= 0; i--)
{
KBlocksPiece * piece = &path[i];
for(int j = 0; j < KBlocksPiece_CellCount; j++)
{
field->setCell(piece->getCellPosX(j), piece->getCellPosY(j), true);
}
int maxLines = field->getHeight();
for(int i = 0; i < maxLines; i++)
{
if (field->checkFilledLine(i))
{
field->removeFilledLine(i);
}
}
}
return true;
}
bool KBlocksAIPlannerExtend::getNextPieceState(int index, KBlocksPiece * piece)
{
{
if (index >= (int)mPathList->size())
{
return false;
......@@ -210,6 +218,15 @@ void KBlocksAIPlannerExtend::process_nstep_recursive( PlanningPath::PathNode* pa
{
_tmpBS->setCell(_pstate.getCellPosX(i), _pstate.getCellPosY(i), true);
}
// remove the filled lines in current board
int maxLines = _tmpBS->getHeight();
for(int i = 0; i < maxLines; i++)
{
if (_tmpBS->checkFilledLine(i))
{
_tmpBS->removeFilledLine(i);
}
}
// get copy of piece_info_sequence
AIPlanner_PieceValue_Sequence _pseq = AIPlanner_PieceValue_Sequence(pseq);
......
/***************************************************************************
* KBlocks, a falling blocks game for KDE *
* Copyright (C) 2009 University Freiburg *
* Copyright (C) 2010 University Freiburg *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
......
/***************************************************************************
* KBlocks, a falling blocks game for KDE *
* Copyright (C) 2009 University Freiburg *
* Copyright (C) 2010 University Freiburg *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
......@@ -20,8 +20,6 @@
typedef std::pair<KBlocksPiece, double> Result;
typedef std::vector<Result> Result_Sequence;
using namespace KGrid2D;
#define MAX_UTILITY // choose the goal with the highest utility
//# PLANNER ######################################
#define TWO_PIECE_PLANNER
......@@ -318,7 +316,7 @@ void KBlocksAIPlayer::generateGoalAction()
dynamic_cast<SpecialEvaluationInterface*>(mpEvaluatorFinal);
evaluator->setCurrentPiece(piece);
value = evaluator->evaluate(field);
#endif
#endif
#endif
result.push_back(Result(piece, value));
#ifdef MAX_UTILITY
......@@ -407,13 +405,13 @@ void KBlocksAIPlayer::generateGoalAction()
}
#endif
#endif
#ifdef INITLIST
int goal_index = best_goals[0];
#else
int goal_index = best_goals[rand() % best_goals.size()];
#endif
KBlocksPiece * goal_piece = &(result[goal_index].first);
mBestRotation = goal_piece->getRotation() - mpCurPiece->getRotation();
......
/***************************************************************************
* KBlocks, a falling blocks game for KDE *
* Copyright (C) 2009 University Freiburg *
* Copyright (C) 2010 University Freiburg *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
......
/***************************************************************************
* KBlocks, a falling blocks game for KDE *
* Copyright (C) 2009 University Freiburg *
* Copyright (C) 2010 University Freiburg *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
......@@ -18,12 +18,10 @@
#define INITLIST
#include <kgrid2d.h>
#ifndef DEBUG
#define DEBUG
#endif
#endif
#ifndef LOG
#define LOG
#endif
......
/***************************************************************************
* KBlocks, a falling blocks game for KDE *
* Copyright (C) 2009 University Freiburg *
* Copyright (C) 2010 University Freiburg *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
......
......@@ -8,11 +8,13 @@ include_directories( ${CMAKE_SOURCE_DIR}/libkdegames ${CMAKE_SOURCE_DIR}/libkdeg
set(kblocks_SRCS
main.cpp
KBlocksDefine.cpp
KBlocksNetServer.cpp
KBlocksNetClient.cpp
KBlocksConfigManager.cpp
KBlocksWin.cpp
KBlocksDisplay.cpp
KBlocksRepWin.cpp
KBlocksAppThread.cpp
KBlocksScene.cpp
KBlocksView.cpp
......@@ -37,6 +39,8 @@ set(kblocks_SRCS
KBlocksPiece.cpp
KBlocksPieceGenerator.cpp
KBlocksGameMessage.cpp
KBlocksGameRecorder.cpp
KBlocksGameReplayer.cpp
AI/KBlocksAIPlayer.cpp
AI/KBlocksAIPlanner.cpp
AI/KBlocksAIPlannerExtend.cpp
......
/***************************************************************************
* KBlocks, a falling blocks game for KDE *
* Copyright (C) 2009 Zhongjie Cai <squall.leonhart.cai@gmail.com> *
* Copyright (C) 2010 Zhongjie Cai <squall.leonhart.cai@gmail.com> *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
......
/***************************************************************************
* KBlocks, a falling blocks game for KDE *
* Copyright (C) 2009 Zhongjie Cai <squall.leonhart.cai@gmail.com> *
* Copyright (C) 2010 Zhongjie Cai <squall.leonhart.cai@gmail.com> *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
......
/***************************************************************************
* KBlocks, a falling blocks game for KDE *
* Copyright (C) 2009 Zhongjie Cai <squall.leonhart.cai@gmail.com> *
* Copyright (C) 2010 Zhongjie Cai <squall.leonhart.cai@gmail.com> *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
......@@ -50,3 +50,4 @@ class GamePlayerInterface
};
#endif
/***************************************************************************
* KBlocks, a falling blocks game for KDE *
* Copyright (C) 2009 Zhongjie Cai <squall.leonhart.cai@gmail.com> *
* Copyright (C) 2010 Zhongjie Cai <squall.leonhart.cai@gmail.com> *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
......
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