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 >
Text File  |  1992-02-27  |  4KB  |  169 lines

  1. // This may look like C code, but it is really -*- C++ -*-
  2.  
  3. //   Module      : Player.C   Version 1.2
  4. //   LastSCCS    : 2/26/92  16:37:56
  5. //   LastEdit    : "Tue Feb 25 10:32:48 1992"
  6. //   Description : 
  7. //   Author      : 
  8. //   Copyright   : GMD Schloss Birlinghoven
  9.  
  10. Player::Player(SleuthDocument *doc, int pos)
  11. {
  12.     player_view   = 0;
  13.     document      = doc;
  14.     position      = pos;
  15.     selected      = False;
  16.     
  17.     for(int j = 0; j < 3; j++)
  18.     for(int k = 0; k < 4; k++)
  19.         for(int l = 0; l < 3; l++)
  20.         fields[j][k][l] =
  21.             new Field(this, document->GetCard(j,k,l), j, k, l);
  22. }
  23.  
  24. FieldStatus Player::
  25. GetCard(int shape, int color, int value)
  26. {
  27.     Require(shape >= 0 && shape < 3);
  28.     Require(color >= 0 && color < 4);
  29.     Require(value >= 0 && value < 3);
  30.     
  31.     return fields[shape][color][value]->GetState();
  32. }
  33.  
  34. FieldStatus Player::
  35. SetCard(int shape, int color, int value, FieldStatus cv)
  36. {
  37.     Require(shape >= 0 && shape < 3);
  38.     Require(color >= 0 && color < 4);
  39.     Require(color >= 0 && value < 3);
  40.     
  41.     return fields[shape][color][value]->SetState(cv);
  42. }
  43.  
  44. void Player::
  45. ChangeCard(Field *field, FieldStatus cv)
  46. {
  47.     FieldStatus old_state = field->GetState();
  48.     
  49.     if(old_state == eOwned) {
  50.     CHECK(cv == eNeutral, "Transition from owned to neutral");
  51.     owned_cards.Remove(field->GetCard());
  52.     field->GetCard()->SetPlayer(0);
  53.     }
  54.     else if( cv == eOwned ) {
  55.     CHECK(old_state == eNeutral, "Transition from neutral to owned");
  56.     owned_cards.Append(field->GetCard());
  57.     field->GetCard()->SetPlayer(this);
  58.     } else {
  59.     CHECK((old_state == eNeutral && cv == eNotOwned) ||
  60.           (old_state == eNotOwned && cv == eNeutral), "");
  61.     }
  62.     field->SetState(cv);
  63.     if(player_view && player_view->DrawingIsPossible())
  64.     player_view->UpdateField(field);
  65.     document->DisplayOwner(field->GetCard());
  66. }
  67.  
  68. void Player::
  69. Add(Axiom *p_axiom)
  70. {
  71.     REQUIRE(! axioms.Member(p_axiom), "Axiom does not exist for player");
  72.     
  73.     axioms.Append(p_axiom);
  74.     axiom_list->add(p_axiom);
  75.  
  76.     ENSURE(axioms.Member(p_axiom), "Axiom exists for player");    
  77. }
  78.  
  79. void Player::
  80. Remove(Axiom *p_axiom)
  81. {
  82.     REQUIRE(axioms.Member(p_axiom), "Axiom exists for player");
  83.     
  84.     axioms.Remove(p_axiom);
  85.     axiom_list->Remove(p_axiom);
  86.     
  87.     ENSURE(! axioms.Member(p_axiom), "Axiom does not exist for player");
  88. }
  89.  
  90. void Player::
  91. SetSelected(Boolean state)
  92. {
  93.     if( player_view && selected != state)
  94.     player_view->DrawSelected(state);
  95.     selected = state;
  96. }
  97.  
  98. Boolean Player::
  99. write_to_stream(ostream &stream)
  100. {
  101.     int shape, color, value;
  102.     
  103.     for(color = 0; color < 4; color++) {
  104.     for(shape = 0; shape < 3; shape++) {
  105.         for(value = 0; value < 3; value++) {
  106.         stream << GetCard(shape, color, value) << " ";
  107.         }
  108.     }
  109.     stream << "\n";
  110.     }
  111.     stream << axioms.Length() << "\n";
  112.     for( AxiomListIterator it(axioms); ! it.Off(); it.Forth() ) {
  113.     stream << it.Item()->GetPlayer()->GetPosition() << ' ';
  114.     stream << it.Item()->GetShape() << ' ';
  115.     stream << it.Item()->GetColor() << ' ';
  116.     stream << it.Item()->GetValue() << ' ';
  117.     stream << it.Item()->GetN() << '\n';
  118.     }
  119.     stream << "\n";
  120.     return stream.fail() ? False : True;
  121. }
  122.  
  123. Boolean Player::
  124. read_from_stream(istream &stream)
  125. {
  126.     int shape, color, value, card_value;
  127.     
  128.     for(color = 0; color < 4; color++)
  129.     for(shape = 0; shape < 3; shape++)
  130.         for(value = 0; value < 3; value++) {
  131.         stream >> card_value;
  132.         if( stream.fail() )
  133.             return False;
  134.         if( (FieldStatus)card_value != eNeutral )
  135.             ChangeCard(GetField(shape, color, value),
  136.                    (FieldStatus)card_value);
  137.         }
  138.     int n_axioms, a_player, a_shape, a_color, a_value, a_n;
  139.     stream >> n_axioms;
  140.     for( int i = 0; i < n_axioms; i++) {
  141.     stream >> a_player >> a_shape >> a_color >> a_value >> a_n;
  142.         CHECK( a_player == position, "Axiom is for this player");
  143.     Add( new Axiom(this, a_shape, a_color, a_value, a_n) );
  144.     }
  145.     return True;
  146. }
  147.  
  148. Axiom *Player::
  149. FindAxiom(int p_shape, int p_color, int p_value, int p_how_many)
  150. {
  151.     for( AxiomListIterator it(axioms); ! it.Off(); it.Forth() ) {
  152.     if(it.Item()->GetShape() == p_shape &&
  153.        it.Item()->GetColor() == p_color &&
  154.        it.Item()->GetValue() == p_value &&
  155.        it.Item()->GetN() == p_how_many)
  156.         return it.Item();
  157.     }
  158.     return 0;
  159. }
  160.  
  161. void Player::
  162. PrintAxioms(ostream &p_stream)
  163. {
  164.     for( AxiomListIterator it(axioms); ! it.Off(); it.Forth() ) {
  165.     it.Item()->DumpOn(p_stream);
  166.     p_stream << "\n";
  167.     }
  168. }
  169.