# Controller Calculator Code for TT

Howdy y’all,

So, last summer, before we played any Tower Takeover matches, I coded a calculator on the controller screen in VexCode V5 Text. The plan was to have one of the coaches using a partner controller to input the cubes stacked and in towers and whatnot and then the controller would tell you the score (excepting auton) and what cubes in towers would best help your team and worst harm your team. However as we got into matches, we realized that:

• It really isn’t that hard to see that you have more green than your opponent and hence should put green in towers (just for example).
• If we are going to win a match, we will win it with or without a calculator.
• I was the only one with the patience to use it, as it wasn’t that smooth.

I am not really planning on using this again this season (haha) so I thought I would post it as a learning material or something to get people thinking. Mostly I just wanted to say I made a calculator for the controller.

Pros of the code: Makes okay use of classes, multi-tasking, and math
Cons of the code: Probably has extra classes it didn’t need, unnecessarily used a template, and wasn’t that streamlined. Also, really confusing if you didn’t write it. I didn’t really know that much C++ when I wrote it.

How to use:

• Bottom bumpers control color
• Top bumpers control screen
• Up and down arrows increase/decrease quantities

I tended to abbreviate stuff, so s might be scored, p might be possible or purple, g might be green, o might be orange, opp is probably opponent. Sigh.

``````#include "vex.h"
using namespace vex;

class scores
{
private:
double scorep1;
double scorep2;
double scorep3;

public:
double score;
std::string scorestring;

void setscore(double gs, double ps, double os,
double gt, double pt, double ot)
{
scorep1 = gs * gt;
scorep2 = ps * pt;
scorep3 = os * ot;
score = scorep1 + scorep2 + scorep3;
}
};

class cubesscored
{
public:
int numberscored;

void reset ()
{
numberscored = 0;
}
{
numberscored++;
}
void subtractone ()
{
numberscored--;
}

};

class cubesintower
{
public:
int numberintower;
int totalmultiplier;

void reset ()
{
numberintower = 0;
totalmultiplier = 1;
}
{
numberintower++;
totalmultiplier=numberintower+1;
}
void subtractone ()
{
numberintower--;
totalmultiplier=numberintower+1;
}

};

class p_cubesintower
{
public:
double p_numberintower;
double p_totalmultiplier;

void reset ()
{
p_numberintower = 0;
p_totalmultiplier = 0;
}
{
p_numberintower++;
p_totalmultiplier=p_numberintower+1;
}
void subtractone ()
{
p_numberintower--;
p_totalmultiplier=p_numberintower+1;
}

};

class p_scores
{
private:
double p_purplemultiplier;
double p_greenmultiplier;
double p_orangemultiplier;

double p_totalpurplescore;
double p_totalgreenscore;
double p_totalorangescore;

public:
double p_score;

void setpossiblescore (int URpurple, int URgreen, int URorange,
int purpletowers, int greentowers, int orangetowers)
{
p_purplemultiplier = 1+purpletowers;
p_greenmultiplier = 1+greentowers;
p_orangemultiplier = 1+orangetowers;

p_totalpurplescore = URpurple * p_purplemultiplier;
p_totalgreenscore = URgreen * p_greenmultiplier;
p_totalorangescore = URorange * p_orangemultiplier;

p_score = p_totalorangescore + p_totalgreenscore + p_totalpurplescore;
}

};

class p_storage
{
public:
double p_URscore;
double p_THEIRscore;
double difference;
};

template <class T>
T GetMax (T a, T b, T c, T d, T e, T f)
{
T result1;
T result2;
T result3;
T result4;
T result5;
int number;

result1 = (a>b)? a : b;
result2 = (c>d)? c : d;
result3 = (e>f)? e : f;
result4 = (result1>result2)? result1 : result2;
result5 = (result4>result3)? result4 : result3;

if(result5 == a)
{
number = 1;
}
if(result5 == b)
{
number = 2;
}
if(result5 == c)
{
number = 3;
}
if(result5 == d)
{
number = 4;
}
if(result5 == e)
{
number = 5;
}
if(result5 == f)
{
number = 6;
}
return (number);
}

/*
1 = greenlesspurplemore
2 = orangelesspurplemore
3 = purplelessgreenmore
4 = orangelessgreenmore
5 = purplelessorangemore
6 = greenlessorangemore
*/

int bestoption;

std::string instructions1;
std::string instructions2;

//The following is a couple of integers that are used to keep
// track of the interface
/*
Screen index
2 = opponent stacks
3 = towers
4 = calculate
5 = Score
*/
int screen;
/*
Color index
1 = purple
2 = green
3 = orange
4 = no color
*/
int colorfocus;
//Now we have some functions used to look for controller 2
//presses and responding to them
//Function for going forward through screens
void screengoforward ()
{

switch(screen)
{
case 1:
screen = 2;
break;

case 2:
screen = 3;
break;

case 3:
screen = 4;
colorfocus =4;
break;

case 4:
screen = 5;
colorfocus = 4;
break;

case 5:
screen = 1;
break;
}
}
//Function for going backward through screens
void screengoback ()
{
switch(screen)
{
case 1:
screen = 5;
break;

case 2:
screen = 1;
break;

case 3:
screen = 2;
break;

case 4:
screen = 3;
break;

case 5:
screen = 4;
break;
}
}

//Function for going forward through colorfocus
void colorfocusgoforward ()
{
switch(colorfocus)
{
//if colorfocus is 1 then set it to two
case 1:
colorfocus = 2;
break;
//if colorfocus is 2 then set it to three
case 2:
colorfocus = 3;
break;
//if colorfocus is 3 then set it to one
case 3:
colorfocus = 1;
break;
//if colorfocus is 4 then set it to one
case 4:
colorfocus = 1;
break;
}
}

//Function for going backward through colorfocuss
void colorfocusgoback ()
{
switch(colorfocus)
{
//if colorfocus is 1 then set it to two
case 1:
colorfocus = 3;
break;
//if colorfocus is 2 then set it to three
case 2:
colorfocus = 1;
break;
//if colorfocus is 3 then set it to one
case 3:
colorfocus = 2;
break;
//if colorfocus is 4 then set it to one
case 4:
colorfocus = 2;
break;
}
}

//class for printing the menu out.
class printingclass
{
private:
std::string screenpart;
std::string colorpart;

//First void for finding the screen part
void findscreenpart ()
{
switch(screen)
{
//if screen is 1 then make the part your stacks
case 1:
screenpart = "UR Stacks";
break;
//if screen is 2 then make the part opponent stacks
case 2:
screenpart = "Opp. Stacks";
break;
//if screen is 3 then make the part towers
case 3:
screenpart = "Towers";
break;
//if screen is 4 then make the part calculate
case 4:
screenpart = "Calculate";
break;
//if screen is 5 then make the part Scores
case 5:
screenpart = "Scores";
break;

}
}

//Second void for finding the color part
void findcolorpart ()
{

switch(colorfocus)
{
//if colorfocus is 1 then make the part your stacks
case 1:
colorpart = "PURPLE";
break;
//if colorfocus is 2 then make the part opponent stacks
case 2:
colorpart = "GREEN";
break;
//if colorfocus is 3 then make the part towers
case 3:
colorpart = "ORANGE";
break;
//if colorfocus is 4 then make the part calculate
case 4:
colorpart = "None";
break;
}

}

//This is a string for the completed sequence of the main menu.
std::string completed;

public:

{
findcolorpart();
findscreenpart();
controller2.Screen.clearScreen();
//As a failsafe, the code prints a bunch of spaces to the screen in case
//the clear screen command doesn't work
controller2.Screen.setCursor(1,1);
controller2.Screen.print("                                                ");
controller2.Screen.setCursor(2,1);
controller2.Screen.print("                                                ");
controller2.Screen.setCursor(3,1);
controller2.Screen.print("                                                ");
controller2.Screen.setCursor(1,1);
if((screen - 4) < 0)
{
completed = screenpart + ": " + colorpart;
}
else
{
completed = screenpart;
}
controller2.Screen.print(completed.c_str());
}

};

int calculate()
{
//put some stuff here

cubesscored URscoredpurple;
URscoredpurple.reset();
cubesscored URscoredgreen;
URscoredgreen.reset();
cubesscored URscoredorange;
URscoredorange.reset();

//THEIR PURPLE
cubesscored THEIRscoredpurple;
THEIRscoredpurple.reset();
//THEIR GREEN
cubesscored THEIRscoredgreen;
THEIRscoredgreen.reset();
//THEIR ORANGE
cubesscored THEIRscoredorange;
THEIRscoredorange.reset();

//Purple towers
cubesintower purpletower;
purpletower.reset();
//Green towers
cubesintower greentower;
greentower.reset();
//Orange towers
cubesintower orangetower;
orangetower.reset();

//p_cubesintower p_purpletower;
//p_cubesintower p_greentower;
//p_cubesintower p_orangetower;

scores URscoring;
scores THEIRscoring;

p_scores p_URscore;
p_scores p_THEIRscore;

p_storage p_greenlesspurplemore;
p_storage p_greenlessorangemore;
p_storage p_purplelessgreenmore;
p_storage p_purplelessorangemore;
p_storage p_orangelesspurplemore;
p_storage p_orangelessgreenmore;

printingclass printer;

screen = 1;
colorfocus = 1;

while(569 == 569)
{
URscoring.setscore(URscoredgreen.numberscored, URscoredpurple.numberscored, URscoredorange.numberscored,
greentower.totalmultiplier,purpletower.totalmultiplier,orangetower.totalmultiplier);

THEIRscoring.setscore(THEIRscoredgreen.numberscored, THEIRscoredpurple.numberscored, THEIRscoredorange.numberscored,
greentower.totalmultiplier,purpletower.totalmultiplier,orangetower.totalmultiplier);

if((colorfocus == 4) && !((screen - 4) < 0))
{
colorfocus = 1;
}
//Code for the directional pad controlling of the values
if((controller2.ButtonUp.pressing()) || (controller2.ButtonDown.pressing()))
{
//Code for changing the values in your scored cubes
if(screen == 1)
{
if(colorfocus == 1)
{
if(controller2.ButtonUp.pressing())
{
}
if(controller2.ButtonDown.pressing())
{
URscoredpurple.subtractone();
}
}
if(colorfocus == 2)
{
if(controller2.ButtonUp.pressing())
{
}
if(controller2.ButtonDown.pressing())
{
URscoredgreen.subtractone();
}
}
if(colorfocus == 3)
{
if(controller2.ButtonUp.pressing())
{
}
if(controller2.ButtonDown.pressing())
{
URscoredorange.subtractone();
}
}
}
//Code for changing values in opponenet scored cubes
if(screen == 2)
{
if(colorfocus == 1)
{
if(controller2.ButtonUp.pressing())
{
}
if(controller2.ButtonDown.pressing())
{
THEIRscoredpurple.subtractone();
}
}
if(colorfocus == 2)
{
if(controller2.ButtonUp.pressing())
{
}
if(controller2.ButtonDown.pressing())
{
THEIRscoredgreen.subtractone();
}
}
if(colorfocus == 3)
{
if(controller2.ButtonUp.pressing())
{
}
if(controller2.ButtonDown.pressing())
{
THEIRscoredorange.subtractone();
}
}
}

//Code for changing values in tower cubes
if(screen == 3)
{
if(colorfocus == 1)
{
if(controller2.ButtonUp.pressing())
{
}
if(controller2.ButtonDown.pressing())
{
purpletower.subtractone();
}
}
if(colorfocus == 2)
{
if(controller2.ButtonUp.pressing())
{
}
if(controller2.ButtonDown.pressing())
{
greentower.subtractone();
}
}
if(colorfocus == 3)
{
if(controller2.ButtonUp.pressing())
{
}
if(controller2.ButtonDown.pressing())
{
orangetower.subtractone();
}
}
}

//End of the left joystick control is the next curly brace
}

//Code for going through screens.
if(controller2.ButtonL1.pressing())
{
screengoforward();
}
if(controller2.ButtonR1.pressing())
{
screengoback();
}
//Code for going through colors.
if(controller2.ButtonL2.pressing())
{
colorfocusgoforward();
}
if(controller2.ButtonR2.pressing())
{
colorfocusgoback();
}

//This will be fun.
if(screen == 4)
{
//------------------------------------------------------------------------------------------------------
//Purple gains 1, Green loses one.
//Find your score if there is one more purple and one less green
p_URscore.setpossiblescore( URscoredpurple.numberscored, URscoredgreen.numberscored, URscoredorange.numberscored,
/*This is continued*/ purpletower.numberintower + 1, greentower.numberintower- 1, orangetower.numberintower);
//Set the variable to the value
p_greenlesspurplemore.p_URscore = p_URscore.p_score;

//Find the opponent score if there is one more purple and one less green
p_THEIRscore.setpossiblescore( THEIRscoredpurple.numberscored, THEIRscoredgreen.numberscored, THEIRscoredorange.numberscored,
/*This is continued*/ purpletower.numberintower + 1, greentower.numberintower- 1, orangetower.numberintower);
//Set the variable to the value
p_greenlesspurplemore.p_THEIRscore = p_THEIRscore.p_score;

//------------------------------------------------------------------------------------------------------
//Purple gains 1, Orange loses one.
//Find your score if there is one more purple and one less orange
p_URscore.setpossiblescore( URscoredpurple.numberscored, URscoredgreen.numberscored, URscoredorange.numberscored,
/*This is continued*/ purpletower.numberintower + 1, greentower.numberintower, orangetower.numberintower - 1);
//Set the variable to the value
p_orangelesspurplemore.p_URscore = p_URscore.p_score;

//Find the opponent score if there is one more purple and one less orange
p_THEIRscore.setpossiblescore( THEIRscoredpurple.numberscored, THEIRscoredgreen.numberscored, THEIRscoredorange.numberscored,
/*This is continued*/ purpletower.numberintower + 1, greentower.numberintower, orangetower.numberintower - 1);
//Set the variable to the value
p_orangelesspurplemore.p_THEIRscore = p_THEIRscore.p_score;

//------------------------------------------------------------------------------------------------------
//Green gains 1, Purple loses one.
//Find your score if there is one more purple and one less orange
p_URscore.setpossiblescore( URscoredpurple.numberscored, URscoredgreen.numberscored, URscoredorange.numberscored,
/*This is continued*/ purpletower.numberintower - 1, greentower.numberintower + 1, orangetower.numberintower);
//Set the variable to the value
p_purplelessgreenmore.p_URscore = p_URscore.p_score;

//Find the opponent score if there is one more purple and one less orange
p_THEIRscore.setpossiblescore( THEIRscoredpurple.numberscored, THEIRscoredgreen.numberscored, THEIRscoredorange.numberscored,
/*This is continued*/ purpletower.numberintower + 1, greentower.numberintower, orangetower.numberintower - 1);
//Set the variable to the value
p_purplelessgreenmore.p_THEIRscore = p_THEIRscore.p_score;

//-----------------------------------------------------------------------------------------------------
//Green gains 1, Orange loses one.
//Find your score if there is one more purple and one less orange
p_URscore.setpossiblescore( URscoredpurple.numberscored, URscoredgreen.numberscored, URscoredorange.numberscored,
/*This is continued*/ purpletower.numberintower, greentower.numberintower + 1, orangetower.numberintower - 1);
//Set the variable to the value
p_orangelessgreenmore.p_URscore = p_URscore.p_score;

//Find the opponent score if there is one more purple and one less orange
p_THEIRscore.setpossiblescore( THEIRscoredpurple.numberscored, THEIRscoredgreen.numberscored, THEIRscoredorange.numberscored,
/*This is continued*/ purpletower.numberintower, greentower.numberintower + 1, orangetower.numberintower - 1);
//Set the variable to the value
p_orangelessgreenmore.p_THEIRscore = p_THEIRscore.p_score;

//-----------------------------------------------------------------------------------------------------
//Orange gains 1, Purple loses one.
//Find your score if there is one more purple and one less orange
p_URscore.setpossiblescore( URscoredpurple.numberscored, URscoredgreen.numberscored, URscoredorange.numberscored,
/*This is continued*/ purpletower.numberintower - 1, greentower.numberintower, orangetower.numberintower + 1);
//Set the variable to the value
p_purplelessorangemore.p_URscore = p_URscore.p_score;

//Find the opponent score if there is one more purple and one less orange
p_THEIRscore.setpossiblescore( THEIRscoredpurple.numberscored, THEIRscoredgreen.numberscored, THEIRscoredorange.numberscored,
/*This is continued*/ purpletower.numberintower - 1, greentower.numberintower, orangetower.numberintower + 1);
//Set the variable to the value
p_purplelessorangemore.p_THEIRscore = p_THEIRscore.p_score;

//------------------------------------------------------------------------------------------------------
//Orange gains 1, Green loses one.
//Find your score if there is one more purple and one less orange
p_URscore.setpossiblescore( URscoredpurple.numberscored, URscoredgreen.numberscored, URscoredorange.numberscored,
/*This is continued*/ purpletower.numberintower, greentower.numberintower - 1, orangetower.numberintower + 1);
//Set the variable to the value
p_greenlessorangemore.p_URscore = p_URscore.p_score;

//Find the opponent score if there is one more purple and one less orange
p_THEIRscore.setpossiblescore( THEIRscoredpurple.numberscored, THEIRscoredgreen.numberscored, THEIRscoredorange.numberscored,
/*This is continued*/ purpletower.numberintower, greentower.numberintower - 1, orangetower.numberintower + 1);
//Set the variable to the value
p_greenlessorangemore.p_THEIRscore = p_THEIRscore.p_score;

/*
The following code is the discernment of what would help your score the most. It uses a
lot of painful
template stuff.
*/
//Find the differences between all the scores for you and opponent
p_greenlesspurplemore.difference = p_greenlesspurplemore.p_URscore - p_greenlesspurplemore.p_THEIRscore;
p_orangelesspurplemore.difference = p_orangelesspurplemore.p_URscore - p_orangelesspurplemore.p_THEIRscore;
p_purplelessgreenmore.difference = p_purplelessgreenmore.p_URscore - p_purplelessgreenmore.p_THEIRscore;
p_orangelessgreenmore.difference = p_orangelessgreenmore.p_URscore - p_orangelessgreenmore.p_THEIRscore;
p_purplelessorangemore.difference = p_purplelessorangemore.p_URscore - p_purplelessorangemore.p_THEIRscore;
p_greenlessorangemore.difference = p_greenlessorangemore.p_URscore - p_greenlessorangemore.p_THEIRscore;

bestoption = GetMax<double>(p_greenlesspurplemore.difference,p_orangelesspurplemore.difference,p_purplelessgreenmore.difference,
p_orangelessgreenmore.difference,p_purplelessorangemore.difference,p_greenlessorangemore.difference);

if(bestoption == 1)
{
instructions1 = "- GREEN";
instructions2 = "+ PURPLE";
}
if(bestoption == 2)
{
instructions1 = "- ORANGE";
instructions2 = "+ PURPLE";
}
if(bestoption == 3)
{
instructions1 = "- PURPLE";
instructions2 = "+ GREEN";
}
if(bestoption == 4)
{
instructions1 = "- ORANGE";
instructions2 = "+ GREEN";
}
if(bestoption == 5)
{
instructions1 = "- PURPLE";
instructions2 = "+ ORANGE";
}
if(bestoption == 6)
{
instructions1 = "- GREEN";
instructions2 = "+ ORANGE";
}

}

if(screen == 1)
{
if(colorfocus == 1)
{
controller2.Screen.setCursor(3, 1);
controller2.Screen.print(URscoredpurple.numberscored);
}
if(colorfocus == 2)
{
controller2.Screen.setCursor(3, 1);
controller2.Screen.print(URscoredgreen.numberscored);
}
if(colorfocus == 3)
{
controller2.Screen.setCursor(3, 1);
controller2.Screen.print(URscoredorange.numberscored);
}
}
//Code for printing values of opponent
if(screen == 2)
{
if(colorfocus == 1)
{
controller2.Screen.setCursor(3, 1);
controller2.Screen.print(THEIRscoredpurple.numberscored);
}
if(colorfocus == 2)
{
controller2.Screen.setCursor(3, 1);
controller2.Screen.print(THEIRscoredgreen.numberscored);
}
if(colorfocus == 3)
{
controller2.Screen.setCursor(3, 1);
controller2.Screen.print(THEIRscoredorange.numberscored);
}
}
if(screen == 3)
{
if(colorfocus == 1)

{
controller2.Screen.setCursor(3, 1);
controller2.Screen.print(purpletower.numberintower);
}
if(colorfocus == 2)
{
controller2.Screen.setCursor(3, 1);
controller2.Screen.print(greentower.numberintower);
}
if(colorfocus == 3)
{
controller2.Screen.setCursor(3, 1);
controller2.Screen.print(orangetower.numberintower);
}
}
if(screen == 4)
{
controller2.Screen.setCursor(2, 1);
controller2.Screen.print(instructions1.c_str());
controller2.Screen.setCursor(3, 1);
controller2.Screen.print(instructions2.c_str());
}
if(screen == 5)
{

URscoring.scorestring = "UR Score = ";
controller2.Screen.setCursor(2, 1);
controller2.Screen.print(URscoring.scorestring.c_str());
controller2.Screen.setCursor(2, 15);
controller2.Screen.print("%8.2f",URscoring.score);

THEIRscoring.scorestring = "THEIR Score = ";
controller2.Screen.setCursor(3, 1);
controller2.Screen.print(THEIRscoring.scorestring.c_str());
controller2.Screen.setCursor(3, 15);
controller2.Screen.print("%8.2f",THEIRscoring.score);
}
}
return (0);
}

void usercontrol ( void )
{