📄 pacman.cpp
字号:
/*************************************************************************** pacman.cpp - description ------------------- begin : Tue Nov 6 2001 copyright : (C) 2001 by Rudiger Koch email : rkoch@rkoch.org ***************************************************************************//*************************************************************************** * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * ***************************************************************************/#include <iostream.h>#include "pacman.h"#include "pacmanspikeinput.h"#include "amygdala/genome.h"#include "amygdala/cherrymoya.h"#include "amygdala/neuron.h"#include "amygdala/network.h"Pacman::Pacman(){}Pacman::~Pacman(){ cout << "Deleting Pacman" << endl; delete spikeOut; delete spikeIn;}void Pacman::Setup(string genefile){ // create a handler. The cherrymoya interprets a genome // The radius is set to 3 Cherrymoya *theCherrymoya = new Cherrymoya(3); genome->setHandler(theCherrymoya);// Neuron::CaptureOutput(ALL); Neuron::CaptureOutput(OUTPUT_LAYERS); try{ // get the genome. This can either be a file or a http URL. genome->get(genefile); genome->parse(); NN = theCherrymoya->getNetwork(); NN->StreamingInput(true); // we need to replace the default SpikeInput with our own // because we need to feed the Pacman data into Amygdala spikeIn = new PacmanSpikeInput(NN, this); // tell our SpikeInput class which it's input neurons are spikeIn->SetInputNeurons(theCherrymoya->GetInputNeurons()); // We also need to replace the default SpikeOutput // because we need to translate the output of Amygdala into // Pacman movements spikeOut = new PacmanSpikeOutput(); spikeOut->SetOutputNeurons(theCherrymoya->GetOutputNeurons()); NN->SetSpikeInput(spikeIn); Neuron::SetSpikeOutput(spikeOut); HardwireNeurons(theCherrymoya); } catch (string e) { cout << "Caught Exception at " << __FILE__ << ":" << __LINE__ << " from: " << e << endl; exit(-1); } // free some memory - the genome hander is not needed any more delete theCherrymoya; // some Pacman initialization stuff which is probably not interesting for you. // Manix was written in plain C and I didn't convert it into C++ so we have some // global functions here. initialize(); initLevel();}Senses* Pacman::step(){ // check if we're done if(getLives()<= 0) finalize(); // only some Manix specific stuff. Just ignore it. if(levelFinished()){ ActualizaNivel(); initLevel(); } // we move Pacman a single step into the direction determined by Amygdala. This // is the connection point between the 1989 Pacman game and the 2002 mega library // Amygdala singleStep(spikeOut->getDirection()); // return the response. This function determines what pacman touches and how close // ghosts are and translates it into a Senses structure. return getFeeling();}unsigned int Pacman::GetScore(){ // Simply return the Pacman score return getFinalScore();}void Pacman::HardwireNeurons(Cherrymoya* c){ vector <AmIdInt> in = c->GetInputNeurons(); vector <Neuron*> out = c->GetOutputNeurons(); // 0 (negative), 1 (positive): Up Neurons NN->ConnectNeurons(in[10], out[1]->GetID(), -1.0, 0); NN->ConnectNeurons(in[10], out[0]->GetID(), 1.0, 0); // 2 (negative), 3 (positive): Down Neurons NN->ConnectNeurons(in[11], out[3]->GetID(), -1.0, 0); NN->ConnectNeurons(in[11], out[2]->GetID(), 1.0, 0); // 4 (negative), 5 (positive): Left Neurons NN->ConnectNeurons(in[12], out[5]->GetID(), -1.0, 0); NN->ConnectNeurons(in[12], out[4]->GetID(), 1.0, 0); // 6 (negative), 7 (positive): Left Neurons NN->ConnectNeurons(in[13], out[7]->GetID(), -1.0, 0); NN->ConnectNeurons(in[13], out[6]->GetID(), 1.0, 0);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -