[Orxonox-commit 7529] r12135 - code/branches/wagnis_HS18/src/modules/wagnis
samuelbl at orxonox.net
samuelbl at orxonox.net
Tue Dec 4 13:56:56 CET 2018
Author: samuelbl
Date: 2018-12-04 13:56:55 +0100 (Tue, 04 Dec 2018)
New Revision: 12135
Modified:
code/branches/wagnis_HS18/src/modules/wagnis/WagnisPlayer.cc
code/branches/wagnis_HS18/src/modules/wagnis/WagnisPlayer.h
Log:
Player functionalities added
Modified: code/branches/wagnis_HS18/src/modules/wagnis/WagnisPlayer.cc
===================================================================
--- code/branches/wagnis_HS18/src/modules/wagnis/WagnisPlayer.cc 2018-12-02 21:47:00 UTC (rev 12134)
+++ code/branches/wagnis_HS18/src/modules/wagnis/WagnisPlayer.cc 2018-12-04 12:56:55 UTC (rev 12135)
@@ -4,6 +4,8 @@
#include "WagnisPlayer.h"
#include <vector>
#include <string>
+#include <cstdlib>
+#include <ctime>
namespace orxonox
{
@@ -59,13 +61,14 @@
case CHOOSE_PROVINCE_STAGE:
{
if (checkMove(SET_TROOPS_INITIAL)){
- this->target_province->owner_ID = this->Player_ID;
+ this->target_province->setOwner_ID(this->Player_ID);
this->target_province->setTroops(this->target_province->getTroops()+1);
- orxout()<<"Province "<<this->target_province->ID<<" owned by Player "<<this->target_province->owner_ID<<" troops: "<<this->target_province->getTroops()<<endl;
+ orxout()<<"Province "<<this->target_province->getID()<<" owned by Player "<<this->target_province->getOwner_ID()<<" troops: "<<this->target_province->getTroops()<<endl;
master->playerFinishedStageCallback(this);
}else{
orxout()<<"Sorry, someone already owns this provice"<<endl;
}
+ //TODO: Set more troops to provinces before game begins
break;
}
@@ -72,11 +75,19 @@
case REINFORCEMENT_STAGE:
{
- if (checkMove(SET_TROOPS)){
- this->target_province->setTroops(this->target_province->getTroops()+1);
- orxout()<<"Province "<<this->target_province->ID<<" owned by Player "<<this->target_province->owner_ID<<" troops: "<<this->target_province->getTroops()<<endl;
+ int i = reinforcementCounter(); //i tells how many troops player gets
+ while (i > 0)
+ {
+ if (checkMove(SET_TROOPS))
+ {
+ this->target_province->setTroops(this->target_province->getTroops()+1);
+ orxout()<<"Province "<<this->target_province->getID()<<" owned by Player "<<this->target_province->getOwner_ID()<<" troops: "<<this->target_province->getTroops()<<endl;
+ --i;
+ }
}
-
+
+ master->playerFinishedStageCallback(this);
+
break;
}
case ATTACK_STAGE:{
@@ -88,24 +99,107 @@
while ((this->origin_province->getTroops() >= 4) && (this->target_province->getTroops() >= 2))
{
//normal fight, 3 attackers, 2 defenders
+ int att1 = dice();
+ int att2 = dice();
+ int att3 = dice();
+ int def1 = dice();
+ int def2 = dice();
+ int attBest = best3(att1, att2, att3);
+ int attSecond = second3(att1, att2, att3);
+ int defBest = best2(def1, def2);
+ int defSecond = second2(def1, def2);
+
+ if(defBest >= attBest)
+ this->origin_province->setTroops(this->origin_province->getTroops()-1);
+ if (attBest > defBest)
+ this->target_province->setTroops(this->target_province->getTroops()-1);
+ if(defSecond >= attSecond)
+ this->origin_province->setTroops(this->origin_province->getTroops()-1);
+ if (attSecond > defSecond)
+ this->target_province->setTroops(this->target_province->getTroops()-1);
}
if ((this->origin_province->getTroops() == 3) && (this->target_province->getTroops() >= 2))
{
//2 attackers, 2 defenders
+ int att1 = dice();
+ int att2 = dice();
+ int def1 = dice();
+ int def2 = dice();
+ int attBest = best2(att1, att2);
+ int attSecond = second2(att1, att2);
+ int defBest = best2(def1, def2);
+ int defSecond = second2(def1, def2);
+
+ if(defBest >= attBest)
+ this->origin_province->setTroops(this->origin_province->getTroops()-1);
+ if (attBest > defBest)
+ this->target_province->setTroops(this->target_province->getTroops()-1);
+ if(defSecond >= attSecond)
+ this->origin_province->setTroops(this->origin_province->getTroops()-1);
+ if (attSecond > defSecond)
+ this->target_province->setTroops(this->target_province->getTroops()-1);
}
if((this->origin_province->getTroops() == 2) && (this->target_province->getTroops() >= 2))
{
//1 attacker, 2 defenders
+ int attBest = dice();
+ int def1 = dice();
+ int def2 = dice();
+ int defBest = best2(def1, def2);
+
+ if(defBest >= attBest)
+ this->origin_province->setTroops(this->origin_province->getTroops()-1);
+ if (attBest > defBest)
+ this->target_province->setTroops(this->target_province->getTroops()-1);
}
- //TODO: implement other cases
+ if((this->origin_province->getTroops() >= 4) && (this->target_province->getTroops() == 1))
+ {
+ //3 attackers, 1 defender
+ int att1 = dice();
+ int att2 = dice();
+ int att3 = dice();
+ int defBest = dice();
+ int attBest = best3(att1, att2, att3);
+
+ if(defBest >= attBest)
+ this->origin_province->setTroops(this->origin_province->getTroops()-1);
+ if (attBest > defBest)
+ this->target_province->setTroops(this->target_province->getTroops()-1);
+ }
+
+ if((this->origin_province->getTroops() == 3) && (this->target_province->getTroops() == 1))
+ {
+ //2 attackers, 1 defender
+ int att1 = dice();
+ int att2 = dice();
+ int defBest = dice();
+ int attBest = best2(att1, att2);
+
+ if(defBest >= attBest)
+ this->origin_province->setTroops(this->origin_province->getTroops()-1);
+ if (attBest > defBest)
+ this->target_province->setTroops(this->target_province->getTroops()-1);
+ }
+
+ if((this->origin_province->getTroops() == 2) && (this->target_province->getTroops() == 1))
+ {
+ //1 attacker, 1 defender
+ int attBest = dice();
+ int defBest = dice();
+
+ if(defBest >= attBest)
+ this->origin_province->setTroops(this->origin_province->getTroops()-1);
+ if (attBest > defBest)
+ this->target_province->setTroops(this->target_province->getTroops()-1);
+ }
}
if (this->target_province->getTroops() == 0) //attacker won
{
- this->target_province->owner_ID = this->Player_ID;
+ this->target_province->setOwner_ID(this->Player_ID);
this->target_province->setTroops(this->origin_province->getTroops() - 1);
this->origin_province->setTroops(1);
}
@@ -119,6 +213,7 @@
{
this->target_province->setTroops(this->origin_province->getTroops()-1);
this->origin_province->setTroops(1);
+ master->playerFinishedStageCallback(this);
}
break;
}
@@ -220,4 +315,61 @@
return false;
}
+
+ int WagnisPlayer::dice() //returns random integer in range [1, 2, 3, 4, 5, 6]
+ {
+ srand(time(NULL));
+ return (rand()%6+1);
+ }
+
+ int WagnisPlayer::best3(int a, int b, int c)
+ {
+ if(a >= b && a>= c)
+ return a;
+ if(b >= a && b>= c)
+ return b;
+ else
+ return c;
+ }
+
+ int WagnisPlayer::best2(int a, int b)
+ {
+ if(a >= b)
+ return a;
+ else
+ return b;
+ }
+
+ int WagnisPlayer::second3(int a, int b, int c)
+ {
+ if((a >= b && a <= c)||(a <= b && a >= c))
+ return a;
+ if((b >= a && b <= c)||(b <= a && b >= c))
+ return b;
+ else
+ return c;
+ }
+
+ int WagnisPlayer::second2(int a, int b)
+ {
+ if(a <= b)
+ return a;
+ else
+ return b;
+ }
+
+ int WagnisPlayer::reinforcementCounter() //calculates how many troops a player gets at beginning of his turn
+ {
+
+ return 10;
+ /*
+ int counter = 0;
+
+ for (int i = 0; i <= 100; ++i)
+ {
+ if
+ }
+ */
+
+ }
}
Modified: code/branches/wagnis_HS18/src/modules/wagnis/WagnisPlayer.h
===================================================================
--- code/branches/wagnis_HS18/src/modules/wagnis/WagnisPlayer.h 2018-12-02 21:47:00 UTC (rev 12134)
+++ code/branches/wagnis_HS18/src/modules/wagnis/WagnisPlayer.h 2018-12-04 12:56:55 UTC (rev 12135)
@@ -48,6 +48,12 @@
WagnisProvince* target_province;
bool isNeighbour(WagnisProvince*, WagnisProvince*);
bool existPath(WagnisProvince*, WagnisProvince*);
+ int dice();
+ int best3(int, int, int);
+ int best2(int, int);
+ int second3(int, int, int);
+ int second2(int, int);
+ int reinforcementCounter();
};
}
More information about the Orxonox-commit
mailing list