home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
The World of Computer Software
/
World_Of_Computer_Software-02-385-Vol-1of3.iso
/
g
/
gina15.zip
/
demos
/
sleuth
/
Player.C
< prev
next >
Wrap
Text File
|
1992-02-27
|
4KB
|
169 lines
// This may look like C code, but it is really -*- C++ -*-
// Module : Player.C Version 1.2
// LastSCCS : 2/26/92 16:37:56
// LastEdit : "Tue Feb 25 10:32:48 1992"
// Description :
// Author :
// Copyright : GMD Schloss Birlinghoven
Player::Player(SleuthDocument *doc, int pos)
{
player_view = 0;
document = doc;
position = pos;
selected = False;
for(int j = 0; j < 3; j++)
for(int k = 0; k < 4; k++)
for(int l = 0; l < 3; l++)
fields[j][k][l] =
new Field(this, document->GetCard(j,k,l), j, k, l);
}
FieldStatus Player::
GetCard(int shape, int color, int value)
{
Require(shape >= 0 && shape < 3);
Require(color >= 0 && color < 4);
Require(value >= 0 && value < 3);
return fields[shape][color][value]->GetState();
}
FieldStatus Player::
SetCard(int shape, int color, int value, FieldStatus cv)
{
Require(shape >= 0 && shape < 3);
Require(color >= 0 && color < 4);
Require(color >= 0 && value < 3);
return fields[shape][color][value]->SetState(cv);
}
void Player::
ChangeCard(Field *field, FieldStatus cv)
{
FieldStatus old_state = field->GetState();
if(old_state == eOwned) {
CHECK(cv == eNeutral, "Transition from owned to neutral");
owned_cards.Remove(field->GetCard());
field->GetCard()->SetPlayer(0);
}
else if( cv == eOwned ) {
CHECK(old_state == eNeutral, "Transition from neutral to owned");
owned_cards.Append(field->GetCard());
field->GetCard()->SetPlayer(this);
} else {
CHECK((old_state == eNeutral && cv == eNotOwned) ||
(old_state == eNotOwned && cv == eNeutral), "");
}
field->SetState(cv);
if(player_view && player_view->DrawingIsPossible())
player_view->UpdateField(field);
document->DisplayOwner(field->GetCard());
}
void Player::
Add(Axiom *p_axiom)
{
REQUIRE(! axioms.Member(p_axiom), "Axiom does not exist for player");
axioms.Append(p_axiom);
axiom_list->add(p_axiom);
ENSURE(axioms.Member(p_axiom), "Axiom exists for player");
}
void Player::
Remove(Axiom *p_axiom)
{
REQUIRE(axioms.Member(p_axiom), "Axiom exists for player");
axioms.Remove(p_axiom);
axiom_list->Remove(p_axiom);
ENSURE(! axioms.Member(p_axiom), "Axiom does not exist for player");
}
void Player::
SetSelected(Boolean state)
{
if( player_view && selected != state)
player_view->DrawSelected(state);
selected = state;
}
Boolean Player::
write_to_stream(ostream &stream)
{
int shape, color, value;
for(color = 0; color < 4; color++) {
for(shape = 0; shape < 3; shape++) {
for(value = 0; value < 3; value++) {
stream << GetCard(shape, color, value) << " ";
}
}
stream << "\n";
}
stream << axioms.Length() << "\n";
for( AxiomListIterator it(axioms); ! it.Off(); it.Forth() ) {
stream << it.Item()->GetPlayer()->GetPosition() << ' ';
stream << it.Item()->GetShape() << ' ';
stream << it.Item()->GetColor() << ' ';
stream << it.Item()->GetValue() << ' ';
stream << it.Item()->GetN() << '\n';
}
stream << "\n";
return stream.fail() ? False : True;
}
Boolean Player::
read_from_stream(istream &stream)
{
int shape, color, value, card_value;
for(color = 0; color < 4; color++)
for(shape = 0; shape < 3; shape++)
for(value = 0; value < 3; value++) {
stream >> card_value;
if( stream.fail() )
return False;
if( (FieldStatus)card_value != eNeutral )
ChangeCard(GetField(shape, color, value),
(FieldStatus)card_value);
}
int n_axioms, a_player, a_shape, a_color, a_value, a_n;
stream >> n_axioms;
for( int i = 0; i < n_axioms; i++) {
stream >> a_player >> a_shape >> a_color >> a_value >> a_n;
CHECK( a_player == position, "Axiom is for this player");
Add( new Axiom(this, a_shape, a_color, a_value, a_n) );
}
return True;
}
Axiom *Player::
FindAxiom(int p_shape, int p_color, int p_value, int p_how_many)
{
for( AxiomListIterator it(axioms); ! it.Off(); it.Forth() ) {
if(it.Item()->GetShape() == p_shape &&
it.Item()->GetColor() == p_color &&
it.Item()->GetValue() == p_value &&
it.Item()->GetN() == p_how_many)
return it.Item();
}
return 0;
}
void Player::
PrintAxioms(ostream &p_stream)
{
for( AxiomListIterator it(axioms); ! it.Off(); it.Forth() ) {
it.Item()->DumpOn(p_stream);
p_stream << "\n";
}
}