1 
2 #ifndef WARRIOR_HPP
3 #define WARRIOR_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 /* nasty workaround, code needs refactoring */
23 #ifndef SPECIES_HPP
24 
25 	class CSpecies;
26 	class CReproduction;
27 	class COperand;
28 	class CGeneration;
29 	class CGenus;
30 	class CKingdom;
31 	class CChromosome;
32 	class CSpeciesFile;
33 	#include "exhaust.hpp"
34 	#include <iostream>
35 
36 #endif // ifndef SPECIES_HPP
37 
38 #include "fitness.hpp"
39 #include "generation.hpp"
40 
41 class CExecTrail; // this is the way of the future!
42 
43 class CWarrior: private warrior_t {
44 	friend class CChromosome;
45 	public:
46 		typedef unsigned int TUid;
47 		static const TUid NONE = 0;
48 		typedef struct {
49 			CWarrior::TUid src, dest;
50 			int win, lose, tie;
51 		} FIGHT_DATA;
52 		CWarrior(CSpecies *species); // create a new evolving warrior stub; assigns self a TUid */
53 		CWarrior(CSpecies *species,TUid mother,TUid father); // create a new evolving warrior stub; assigns parents, assigns self a TUid */
54 		CWarrior(CSpecies *species,const TUid uid); // create a new evolving warrior stub; assigns specified TUid */
55 		CWarrior(CSpecies *species,const std::string &rc_filename); /* create and load a benchmark warrior */
56 		~CWarrior();
57 		void clear();
len() const58 		unsigned len() const { return warrior_t::len; }
start_idx() const59 		unsigned start_idx() const { return start; }
60 		void set_start(const unsigned i);
61 		const insn_t &code(const unsigned i) const;
62 		//const insn_t &operator[](unsigned i) const;
filename() const63 		const std::string &filename() const { return _filename; }
info() const64 		const std::string &info() const { return _info; }
set_info(std::string & info)65 		void set_info(std::string &info) { _info = info; }
set_info(const char * info)66 		void set_info(const char *info) { _info = info; }
clear_info()67 		void clear_info() { _info = ""; }
is_evolving() const68 		bool is_evolving() const { return _evolving; }
is_bench() const69 		bool is_bench() const { return !_evolving; }
uid() const70 		TUid uid() const { return _uid; }
mother() const71 		TUid mother() const { return _mother; }
father() const72 		TUid father() const { return _father; }
set_parents(const int mother,const int father)73 		void set_parents(const int mother,const int father) { _mother = mother; _father = father; }
74 		void set_uid(const TUid uid);
75 		bool is_well_formed() const; /* returns if this is *potentially* an ok warrior (basic init checks really) */
76 		bool equals(const CWarrior &other) const; /* truely equals */
generation() const77 		CGeneration::NUM generation() const { return _generation; }
set_generation(const CGeneration::NUM generation)78 		void set_generation(const CGeneration::NUM generation) { _generation = generation; }
79 		void ready(); /* this will blank all fitness metrics preceeding the fighting of a generation */
fought() const80 		bool fought() const { return _fought; } /* has this already fought? (resume support) */
fought(bool has)81 		void fought(bool has) { _fought = has; } /* a setter (resume support) */
82 		void load(const int pos) const;
83 		int fight(CWarrior &enemy,FIGHT_DATA &data,const bool fight=true); /* populates data and returns number of rounds fought */
84 		int fight(const CWarrior &bench,const int rounds,const int win,const int tie,const int lose); /* returns score */
85 		void score(const CFitness::CAT cat,const CFitness::RESULT res,const CWarrior &enemy,int rounds=1); /* this will adjust the fitness metrics to account for this result */
score() const86 		CFitness::NUM score() const { return _fitness_score; }
species() const87 		CSpecies *species() const { return _species; }
88 		CGenus *genus() const;
89 		CKingdom *kingdom() const;
90 		COperand *operands() const;
91 		CExecTrail *exec_trail() const;
92 		void code_check() const;
93 		CFitness::NUM benchmark() const;
is_benchmarked() const94 		bool is_benchmarked() const { return (0 < _benchmark_count); }
95 		void incorporate_benchmark(CFitness::NUM score); // incorporate this benchmark
benchmark_count() const96 		int benchmark_count() const { return _benchmark_count; }
set_benchmark(CFitness::NUM sum,int count)97 		void set_benchmark(CFitness::NUM sum,int count) { _benchmark_count = count; _benchmark_sum = sum; }
98 		void rc(std::ostream &os); // prints an .rc to this stream
99 		void red(std::ostream &os,const char *note = 0); // prints a headed .red file to this stream
100 		CChromosome *chromosome(unsigned int i) const;
101 	protected:
102 		CSpecies *_species;
103 		CGeneration::NUM _generation;
104 		bool _evolving, _fought;
105 		TUid _uid,
106 			_mother, _father,
107 			next_uid(); /* assigns self a new uid, and returns it */
108 		std::string _filename, _info;
109 		CFitness::NUM _fitness_score, /* these is a score of the fitness of this warrior; attacks and defences are weighed differently */
110 			_benchmark_sum;
111 		CExecTrail *_exec_trail;
112 		unsigned int _benchmark_count;
113 		CChromosome **_chromosome; // _chromosome[Species()->num_chromosomes()] details length of each chromosome in the warrior
114 		void create_chromosomes();
115 		void calc_len();
116 };
117 
118 #endif // ifndef WARRIOR_HPP
119