Skip to content

Commit

Permalink
Added turns.
Browse files Browse the repository at this point in the history
  • Loading branch information
koenenalexander committed Apr 29, 2012
1 parent 941eb14 commit 1ab6bab
Show file tree
Hide file tree
Showing 11 changed files with 125 additions and 30 deletions.
2 changes: 1 addition & 1 deletion Node.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class Node {
int compare(Node<T>&, Node<T>&);
T* getValue() { return value; }
Node<T>* getThis() { return this; }
int getClassValue() { return value->value; }
int getClassValue() { return value->getValue(); }
bool hasChildren() { return children != NULL; }
Node* parent;

Expand Down
1 change: 1 addition & 0 deletions Pokeman.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class Pokeman {
TinyAttack *getAttack(int);
Pokeman* clone();
bool usedAction(int);
bool isAlive();
int getLastAction();

void print();
Expand Down
26 changes: 7 additions & 19 deletions State.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@
#define MY_TURN 0
#define THAT_BASTARDS_TURN 1

int stateEval(State*);


State::State(Pokeman* pokeman1, Pokeman* pokeman2, int turn){
this->myPokemon = pokeman1;
this->thatBastardsPokemon = pokeman2;
Expand All @@ -35,7 +32,7 @@ State::State(Pokeman* pokeman1, Pokeman* pokeman2, int turn){
this->whoseTurn = turn;
this->isOver = false;
}
this->value = stateEval(this);
this->value = this->stateEval();
}

State::State(Pokeman* winner){
Expand Down Expand Up @@ -70,17 +67,8 @@ State* State::nextState(int selectedAction){
if(this->whoseTurn == MY_TURN && selectedAction != SWITCH){
active = myNewPokeman;
passive = hisNewPokeman;
/* myNewPokeman->usedAction(used);
action = myNewPokeman->getAttack(selectedAction)->getType();
if(action.compare("damage") == 0) {
continueMatch = useAttack(hisNewPokeman, myNewPokeman->getAttack(selectedAction));
}
*/
}
else if(selectedAction != SWITCH){
/* hisNewPokeman->usedAction(used);
continueMatch = useAttack(myNewPokeman, hisNewPokeman->getAttack(selectedAction));
*/
active = hisNewPokeman;
passive = myNewPokeman;
}
Expand Down Expand Up @@ -121,15 +109,15 @@ State* State::nextState(int selectedAction){
return newState;
}

int stateEval(State* state){
if(state == NULL){
int State::stateEval(){
if(this == NULL){
cout << "Null state provided to State Evaluation function" << endl;
return -1000000000;
}
int myHealth = state->myPokemon->getHealth();
int hisHealth = state->thatBastardsPokemon->getHealth();
int myMax = state->myPokemon->getMaxHealth();
int hisMax = state->thatBastardsPokemon->getMaxHealth();
int myHealth = this->myPokemon->getHealth();
int hisHealth = this->thatBastardsPokemon->getHealth();
int myMax = this->myPokemon->getMaxHealth();
int hisMax = this->thatBastardsPokemon->getMaxHealth();
int myHealthPercent = myHealth/(myMax * 1.0) * 100;
int hisHealthPercent = hisHealth/(hisMax * 1.0) * 100;
return myHealthPercent - hisHealthPercent;
Expand Down
4 changes: 3 additions & 1 deletion State.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@ class State {
public:
State(Pokeman *pokemon1, Pokeman *pokemon2, int turn);
State(Pokeman *winner);
// State* nextState();
State* nextState(int);
int stateEval();
void print();
Pokeman* getActivePokemon();
Pokeman* getPassivePokemon();
bool isOver;
Pokeman* myPokemon;
Pokeman* thatBastardsPokemon;
Expand Down
8 changes: 6 additions & 2 deletions TinyAttack.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,24 @@ using namespace std;

class TinyAttack {
public:
TinyAttack(string attackType, int attackDamage, int pp);
TinyAttack(string attackType, int attackDamage, int pp, int spd);
TinyAttack();
// Copy constructor
TinyAttack(const TinyAttack& ta) : power(ta.power) , type(ta.type) , pp(ta.pp) {}
~TinyAttack();
TinyAttack(const TinyAttack& ta) : power(ta.power) , type(ta.type) , pp(ta.pp) , speed(ta.speed) {}
string getType();
int getPower();
int getPP();
void print();
void use();
int getSpeed();
bool equals(TinyAttack*);

protected:
string type;
int power;
int pp;
int speed;
};

#endif /* TINYATTACK_H_ */
2 changes: 1 addition & 1 deletion debug_print.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include <iostream>
using namespace std;

#define DEBUG 0
#define DEBUG 1

void dprint(char* str, int level = 1) {
if (DEBUG >= level) {
Expand Down
9 changes: 9 additions & 0 deletions makefile
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ minimax_test: minimax.cpp minimax_test.cpp Tree.h Node.h

tinyAttack.o: tinyAttack.cpp TinyAttack.h
$(compiler) $(flags) -c tinyAttack.cpp

tiny_attack_test: tiny_attack_test.cpp tinyAttack.o debug_print.o
$(compiler) $(flags) -o"tiny_attack_test" tiny_attack_test.cpp tinyAttack.o debug_print.o

tiny_test: pokeman tinyattack state
$(compiler) $(flags) -o"tiny_test" tiny_test.cpp pokeman.o tinyAttack.o State.o
Expand All @@ -50,6 +53,12 @@ state: State.cpp State.h pokeman
state_tree_test: state tree_test state_tree_test.cpp pokeman tinyattack
$(compiler) $(flags) -o"state_tree_test" state_tree_test.cpp debug_print.cpp State.o pokeman.o tinyAttack.o

turn: Turn.cpp Turn.h state
$(compiler) $(flags) -c Turn.cpp -o Turn.o

turn_test: turn_test.cpp turn turn_minimax.cpp
$(compiler) $(flags) -o"turn_test" turn_test.cpp debug_print.cpp turn_minimax.cpp State.o pokeman.o tinyAttack.o Turn.o

# Other Targets
clean:
-rm -fr *.o *_test
Expand Down
5 changes: 5 additions & 0 deletions pokeman.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ Pokeman::Pokeman(int hP, int totalHp, TinyAttack* attack1){
this->lastAction = -1;
}

bool Pokeman::isAlive() {
if(this->getHealth() > 0) { return true; }
else return false;
}

Pokeman::~Pokeman() {
for(int i = 0; i < NUM_OF_ATTACKS; i++){
delete this->attack[i];
Expand Down
50 changes: 50 additions & 0 deletions pokeman_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,56 @@

using namespace std;

bool testHP(Pokeman *p, int expected) {
if (p->getHealth() == expected) {
return true;
}
cout << "Failed hp test. Expected " << expected << " but received " << p->getHealth();
return false;
}

bool testLastAction(Pokeman *p, int expected) {
int test = p->getLastAction();
if (test == expected) {
return true;
}
cout << "Failed last action test. Expected " << expected << " but received " << test;
return false;
}

bool testTotalHP(Pokeman *p, int expected) {
int test = p->getMaxHealth();
if (test == expected) {
return true;
}
cout << "Failed max health test. Expected " << expected << " but received " << test;
return false;
}

bool testTotalHP(Pokeman *p, int expected) {
int test = p->getMaxHealth();
if (test == expected) {
return true;
}
cout << "Failed max health test. Expected " << expected << " but received " << test;
return false;
}

bool testGetAttack(Pokeman *p, TinyAttack *expected[]) {
int i;
TinyAttack *test;
for (i=0; expected[i] != "NULL") {
test = p->getMaxHealth();
}
if (test == expected) {
return true;
}
cout << "Failed max health test. Expected " << expected << " but received " << test;
return false;
}



int main(int args, char** argv) {

TinyAttack *t1 = new TinyAttack("Cool Attack", 12);
Expand Down
11 changes: 7 additions & 4 deletions state_tree_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "Tree.h"
#include "Node.h"
#include "State.h"
#include "minimax.h"

#define USE_ATTACK_1 1
#define USE_ATTACK_2 2
Expand Down Expand Up @@ -98,8 +99,8 @@ int main(int args, char** argv) {
int selectedAction = USE_ATTACK_1;
int value;
bool done = false;
TinyAttack* ta1 = new TinyAttack("damage", -3, 5);
TinyAttack* ta2 = new TinyAttack("heal", 4, 2);
TinyAttack* ta1 = new TinyAttack("damage", -3, 5, 3);
TinyAttack* ta2 = new TinyAttack("heal", 4, 2, 3);
Pokeman* p1 = new Pokeman(5, 5, ta1, ta2, ta1, ta1);
Pokeman* p2 = new Pokeman(*p1);
p1->print();
Expand All @@ -112,10 +113,12 @@ int main(int args, char** argv) {
cout << "Starting State" << endl;
state->print();
GBFS(tree.getThis());
cout << "---------------END GBWFS ------------------------" << endl;
// cout << "Minimax: " << minimax(tree.getThis()) << endl;
return i;
}

int StateEval(State* state){
/*int StateEval(State* state){
if(state == NULL){
cout << "Null state provided to State Evaluation function" << endl;
return -1;
Expand All @@ -124,4 +127,4 @@ int StateEval(State* state){
int hisHealth = state->thatBastardsPokemon->getHealth();
return myHealth - hisHealth;
}
}*/
37 changes: 35 additions & 2 deletions tinyAttack.cpp
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
#include <iostream>

#include "TinyAttack.h"

TinyAttack::TinyAttack(string attackType, int attackDamage, int pp){
#include "debug_print.h"
TinyAttack::TinyAttack(string attackType, int attackDamage, int pp, int spd){
this->type = attackType;
this->power = attackDamage;
this->pp = pp;
this->speed = spd;
}

TinyAttack::TinyAttack(){
this->power = 0;
this->type = "ERROR: Default constructor used for TinyAttack";
}

TinyAttack::~TinyAttack(){
}

string TinyAttack::getType(){
return this->type;
}
Expand All @@ -25,10 +29,39 @@ int TinyAttack::getPP(){
return this->pp;
}

int TinyAttack::getSpeed(){
return this->speed;
}

void TinyAttack::use(){
this->pp = this->getPP() - 1;
}

bool TinyAttack::equals(TinyAttack *ta)
{
if(this->getType() != ta->getType())
{
dprint("Type mismatch", 2);
return false;
}
if(this->getPower() != ta->getPower())
{
dprint("Power mismatch", 2);
return false;
}
/* if(this->getPP() != ta->getPP())
{
dprint("PP mismatch", 1);
return false;
} //debatable*/
if(this->getSpeed() != ta->getSpeed())
{
dprint("Speed mismatch", 2);
return false;
}
return true;
}

void TinyAttack::print(){
cout << "Type: " << this->getType() << endl;
cout << "Power: " << this->getPower() << endl;
Expand Down

0 comments on commit 1ab6bab

Please sign in to comment.