Skip to content
Snippets Groups Projects
Commit b41f5254 authored by Jaime Torres Amate's avatar Jaime Torres Amate
Browse files

always return a valid move to avoid the game freeze

When Controller::finalizeShot receives an Illegal move, it just does
not know what to do next, and the game freeze.
This patch implements the last option to return a valid move, searching
for a free cell one by one, therefore it becomes impossible to return
an Illegal move because or the computer wins or the opponen wins before
the board is empty of free cells.

Another solution for dummyAI could be to implement a list of
free cells and choose on them randomly.

BUG: 246382
FIXED-IN: KDE/4.12
REVIEW: 113854
parent 6c0346de
No related branches found
No related tags found
No related merge requests found
......@@ -15,3 +15,15 @@ AI::AI(Sea::Player player, Sea* sea)
{
}
Coord AI::desperateMove() const
{
Sea::Player opp = Sea::opponent(m_player);
for (int i = 0; i < m_sea->size().x; i++)
for (int j = 0; j < m_sea->size().y; j++) {
if (m_sea->at(opp, Coord(i,j)).free()) {
return Coord(i,j);
}
}
return Coord::invalid();
}
......@@ -23,6 +23,7 @@ public:
virtual Coord getMove() = 0;
virtual void setShips() = 0;
virtual void notify(Sea::Player player, const Coord& c, const HitInfo& hit) = 0;
Coord desperateMove() const;
};
#endif // AI__AI_H
......
......@@ -31,9 +31,8 @@ Coord DummyAI::getMove()
return c;
}
}
kDebug() << "Could not find a valid move for DummyAI";
}
return Coord::invalid();
}
return desperateMove();
}
void DummyAI::setShips()
......
......@@ -300,10 +300,16 @@ Coord SmartAI::getMove()
if (m_strategy.get() &&
m_sea->turn() == m_player &&
m_sea->status() == Sea::PLAYING) {
return m_strategy->getMove();
Coord move = m_strategy->getMove();
if ( move == Coord::invalid() ) {
return desperateMove();
}
else {
return move;
}
}
else {
return Coord::invalid();
return desperateMove();
}
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment