1 2 #ifndef CHROMOSOME_HPP 3 #define CHROMOSOME_HPP 4 5 /* "Species" - a CoreWars evolver. Copyright (C) 2003 'Varfar' 6 * 7 * This program is free software; you can redistribute it and/or modify it 8 * under the terms of the GNU General Public License as published by the Free 9 * Software Foundation; either version 1, or (at your option) any later 10 * version. 11 * 12 * This program is distributed in the hope that it will be useful, but WITHOUT 13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 15 * more details. 16 * 17 * You should have received a copy of the GNU General Public License along 18 * with this program; if not, write to the Free Software Foundation, Inc., 19 * 675 Mass Ave, Cambridge, MA 02139, USA. 20 */ 21 22 /* a Chromosome is a part of a warrior; each of these parts can have different 23 settings etc */ 24 25 #include "species.hpp" 26 #include <string> 27 28 class CChromosome { 29 friend class CWarrior; 30 public: 31 // construction 32 CChromosome(CWarrior *warrior,const unsigned int idx,const unsigned int len=0); 33 // getters warrior() const34 CWarrior *warrior() const { return _warrior; } type() const35 CChromosomeType *type() const { return _type; } idx() const36 unsigned int idx() const { return _idx; } len() const37 unsigned int len() const { return _len; } 38 //insn_t &operator[](unsigned i) const { return code(i); } 39 insn_t &code(unsigned i) const; info() const40 const std::string &info() const { return _info; } 41 bool is_start() const; 42 unsigned start() const; // returns MAXLENGTH if warrior start is not in this chromosome 43 // setters 44 void set_len(const unsigned int len); 45 void rnd_len(); 46 void clear(); // invalidates body 47 void set_start(const unsigned i); // actually sets the appropriate stuff in the warrior info()48 std::string &info() { return _info; } 49 // safety 50 void check(const field_t coresize) const; // panics if not ok 51 private: 52 CWarrior *_warrior; 53 CChromosomeType *_type; 54 unsigned int _idx, _len, _ofs; 55 void calc_ofs(); 56 std::string _info; 57 }; 58 59 class CChromosomeType { 60 public: 61 // construction 62 CChromosomeType(CSpecies *species); // create an auto-chromosome shim 63 CChromosomeType(CSpecies *species,const char *name); // create a user-ready shim 64 // serialisation 65 void read_ini(INIFile &ini); 66 void write_ini(std::ostream &os); 67 // getters species() const68 CSpecies *species() const { return _species; } name() const69 const char *name() const { return _name.c_str(); } is_auto() const70 bool is_auto() const { return _auto; } length() const71 CLength *length() const { return _length; } freq() const72 CInstGenerator *freq() const { return _freq; } operands() const73 COperand *operands() const { return _operands; } reproduction() const74 CReproduction *reproduction() const { return _reproduction; } 75 private: 76 bool _auto; // is this just a shim, nothing local? 77 std::string _name; 78 CSpecies *_species; 79 void init(); 80 CLength *_length; // might be CGenus::_length if not overriden 81 CInstGenerator *_freq; // might be CGenus::_freq if not overriden 82 COperand *_operands; // might be CGenus::_operands if not overriden 83 CReproduction *_reproduction; // might be CGenus::_reproduction if not overriden 84 }; 85 86 #endif // CHROMOSOME_HPP 87 88