mirror of
git://git.savannah.nongnu.org/eliot.git
synced 2025-01-17 06:11:49 +01:00
Game history class, will be used for games and players
This commit is contained in:
parent
b4e52e47cc
commit
6626d0ee8c
2 changed files with 243 additions and 0 deletions
171
game/history.cpp
Normal file
171
game/history.cpp
Normal file
|
@ -0,0 +1,171 @@
|
|||
/* Eliot */
|
||||
/* Copyright (C) 1999 Antoine Fraboulet */
|
||||
/* */
|
||||
/* This file is part of Eliot. */
|
||||
/* */
|
||||
/* Eliot is free software; you can redistribute it and/or modify */
|
||||
/* it under the terms of the GNU General Public License as published by */
|
||||
/* the Free Software Foundation; either version 2 of the License, or */
|
||||
/* (at your option) any later version. */
|
||||
/* */
|
||||
/* Eliot is distributed in the hope that it will be useful, */
|
||||
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
|
||||
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
|
||||
/* GNU General Public License for more details. */
|
||||
/* */
|
||||
/* You should have received a copy of the GNU General Public License */
|
||||
/* along with this program; if not, write to the Free Software */
|
||||
/* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
|
||||
|
||||
/* $Id: history.cpp,v 1.1 2005/08/15 22:20:20 afrab Exp $ */
|
||||
|
||||
/**
|
||||
* \file history.cpp
|
||||
* \brief Game history system
|
||||
* \author Antoine Fraboulet
|
||||
* \date 2005
|
||||
*/
|
||||
|
||||
#include <string>
|
||||
#include "rack.h"
|
||||
#include "history.h"
|
||||
#include "debug.h"
|
||||
|
||||
/* ******************************************************** */
|
||||
/* ******************************************************** */
|
||||
/* ******************************************************** */
|
||||
|
||||
|
||||
History::History()
|
||||
{
|
||||
Turn* t = new Turn ();
|
||||
history.push_back(t);
|
||||
}
|
||||
|
||||
|
||||
History::~History()
|
||||
{
|
||||
for (unsigned int i = 0; i < history.size(); i++)
|
||||
{
|
||||
if (history[i] != NULL)
|
||||
{
|
||||
delete history[i];
|
||||
history[i] = NULL;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int History::getSize() const
|
||||
{
|
||||
return history.size();
|
||||
}
|
||||
|
||||
|
||||
const PlayedRack History::getCurrentRack() const
|
||||
{
|
||||
return history.back()->getPlayedRack();
|
||||
}
|
||||
|
||||
|
||||
void History::setCurrentRack(const PlayedRack &iPld)
|
||||
{
|
||||
history.back()->setPlayedRack(iPld);
|
||||
}
|
||||
|
||||
// vector
|
||||
// size : number of elements
|
||||
// back : last element
|
||||
// pop_back : remove at the end
|
||||
// push_back : add at the end
|
||||
|
||||
const Turn History::getPreviousTurn() const
|
||||
{
|
||||
int idx = history.size() - 2;
|
||||
ASSERT(0 <= idx , "Wrong turn number");
|
||||
return *(history[ idx ]);
|
||||
}
|
||||
|
||||
|
||||
void History::playRound(int player, int turn, const Round& round)
|
||||
{
|
||||
Rack rack;
|
||||
Turn * current_turn;
|
||||
Turn * next_turn;
|
||||
|
||||
current_turn = history.back();
|
||||
|
||||
/* set the number and the round */
|
||||
current_turn->setNum(turn);
|
||||
current_turn->setPlayer(player);
|
||||
current_turn->setRound(round);
|
||||
|
||||
/* get what was the rack for the current turn */
|
||||
current_turn->getPlayedRack().getRack(rack);
|
||||
|
||||
/* remove the played tiles from the rack */
|
||||
for (int i = 0; i < round.getWordLen(); i++)
|
||||
{
|
||||
if (round.isPlayedFromRack(i))
|
||||
{
|
||||
if (round.isJoker(i))
|
||||
rack.remove(Tile::Joker());
|
||||
else
|
||||
rack.remove(round.getTile(i));
|
||||
}
|
||||
}
|
||||
|
||||
/* create a new turn */
|
||||
next_turn = new Turn();
|
||||
next_turn->getPlayedRack().setOld(rack);
|
||||
history.push_back ( next_turn );
|
||||
}
|
||||
|
||||
|
||||
void History::removeLastTurn()
|
||||
{
|
||||
int idx = history.size();
|
||||
ASSERT(0 < idx , "Wrong turn number");
|
||||
|
||||
if (idx > 1)
|
||||
{
|
||||
Turn *t = history.back();
|
||||
history.pop_back();
|
||||
delete t;
|
||||
}
|
||||
|
||||
// now we have the previous played round in back()
|
||||
Turn* t = history.back();
|
||||
t->setNum(0);
|
||||
t->setPlayer(0);
|
||||
t->setRound(Round());
|
||||
#ifdef BACK_REMOVE_RACK_NEW_PART
|
||||
t->getPlayedRound().setNew(Rack());
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
std::string
|
||||
History::toString() const
|
||||
{
|
||||
std::string rs = "";
|
||||
unsigned int i;
|
||||
for ( i = 0; i < history.size(); i++)
|
||||
{
|
||||
string pr,ro;
|
||||
pr = history[i]->getPlayedRack().toString();
|
||||
ro = history[i]->getRound().toString();
|
||||
rs += string(" ") + pr + string(" ") + ro + string("\n");
|
||||
}
|
||||
return rs;
|
||||
}
|
||||
|
||||
/* ******************************************************** */
|
||||
/* ******************************************************** */
|
||||
/* ******************************************************** */
|
||||
|
||||
|
||||
/// Local Variables:
|
||||
/// mode: hs-minor
|
||||
/// c-basic-offset: 4
|
||||
/// End:
|
72
game/history.h
Normal file
72
game/history.h
Normal file
|
@ -0,0 +1,72 @@
|
|||
/* Eliot */
|
||||
/* Copyright (C) 1999 Antoine Fraboulet */
|
||||
/* */
|
||||
/* This file is part of Eliot. */
|
||||
/* */
|
||||
/* Eliot is free software; you can redistribute it and/or modify */
|
||||
/* it under the terms of the GNU General Public License as published by */
|
||||
/* the Free Software Foundation; either version 2 of the License, or */
|
||||
/* (at your option) any later version. */
|
||||
/* */
|
||||
/* Eliot is distributed in the hope that it will be useful, */
|
||||
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
|
||||
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
|
||||
/* GNU General Public License for more details. */
|
||||
/* */
|
||||
/* You should have received a copy of the GNU General Public License */
|
||||
/* along with this program; if not, write to the Free Software */
|
||||
/* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
|
||||
|
||||
/* $Id: history.h,v 1.1 2005/08/15 22:20:20 afrab Exp $ */
|
||||
|
||||
/**
|
||||
* \file history.h
|
||||
* \brief Game history system
|
||||
* \author Antoine Fraboulet
|
||||
* \date 2005
|
||||
*/
|
||||
|
||||
#ifndef _HISTORY_H
|
||||
#define _HISTORY_H
|
||||
|
||||
#include <vector>
|
||||
#include "turn.h"
|
||||
|
||||
class History
|
||||
{
|
||||
public:
|
||||
History();
|
||||
~History();
|
||||
|
||||
/// get the size of the history
|
||||
int getSize() const;
|
||||
|
||||
/// Get the (possibly incomplete) rack
|
||||
const PlayedRack getCurrentRack() const;
|
||||
|
||||
/// Set the current rack
|
||||
void setCurrentRack(const PlayedRack &iPld);
|
||||
|
||||
/// Get the previous turn
|
||||
const Turn getPreviousTurn() const;
|
||||
|
||||
/// Update the "history" with the given round and, complete the turn.
|
||||
/// a new turn is created with the remaining letters in the rack
|
||||
void playRound(int player, int turn, const Round& round);
|
||||
|
||||
/// Remove last turn
|
||||
void removeLastTurn();
|
||||
|
||||
std::string toString() const;
|
||||
|
||||
private:
|
||||
vector < Turn* > history;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
/// Local Variables:
|
||||
/// mode: hs-minor
|
||||
/// c-basic-offset: 4
|
||||
/// End:
|
Loading…
Reference in a new issue