/* www.glthr.com/20monkeys
   Guillaume Lethuillier
   Oct. 2015  v.2.1      
*/

#include "Header.h"

// 2026: the two things this program asks the browser (monkeys-worker.js answers)
extern "C" int line_to_type();    // the line of the book to type now, counted from 0
extern "C" void back_to_the_browser();  // shows the attempt; waits when the monkeys go faster than the server of 2016

int main()
{
	bool next { false };
	std::vector<Primate> monkeys(POPULATION * 2);
	Population troop;
	Text book;

	initialize(monkeys, book, troop);
	
	for (;;) 
	{
		restart(book, troop);
		log(monkeys, book, troop, 0);

		do
		{
			book.current_line = line_to_type(); // 2026
			reset(monkeys, troop);
			
			do
			{ 
				back_to_the_browser(); // 2026
				evaluation(monkeys, book, troop, next);

				selection(monkeys, book, troop);

				if (next) break;

				reproduction(monkeys);

				++troop.generations;

				mutation(monkeys);

			} while (!next);

			log(monkeys, book, troop, 2);
	
			next = false;
			
			++book.current_line;
	
		} while (book.current_line < book.lines.size());

		++book.editions;
	}
}