Resolved by Dunning-Kruger!

How To Keep Score

I am writing a script for playing a game with my robot. I am stumped on what to put in the script to keep score. How would i do that? Thanks

Unleash your creativity with the power of easy robot programming using Synthiam ARC Pro

#1

@Bob... If it is just a numeric value then you can just use a variable....

EG... your inmoov just scored a point in a game \$inmoov =\$inmoov+1 or \$inmoov++

... then later you scored a point \$human=\$human + 1 or you can use \$human++

at the end of the game just compare values

if \$inmoov>\$human say(I won, nice game") elseif \$inmoov=\$human say("it's a tie, want to play again?") else say("You won, nice game") endif

Something like that, anyway

United Kingdom
#2

Yep, as Richard said. My method is as so...

``````
\$P1_score = 0
\$P2_score = 0

# Code for game

If(\$winner = &quot;P1&quot;)
\$P1_score++
ElseIf(\$winner = &quot;P2&quot;)
\$P2_score++
EndIf

``````

I've combined my method into one script there. Usually the \$P1_score = 0 (and P2, P3 etc) are defined in the init script to avoid accidental resetting.

Then the code for the game is a new script. At the end of the script has the If shown above.

#3

Thanks guys, - Richard, using your example and putting it in my script as below. I get a error;

``````
Error on line 90: Variable not defined: \$human

``````
``````
# get a response value
\$x = 0
RepeatWhile(\$x &lt; 5)
\$x++
\$response = GetRandomUnique(0,3)

If(\$response = 0)
sleep(1500)
ControlCommand(&quot;Auto Position&quot;, AutoPositionAction, &quot;1Rock&quot;)
Sleep(4000)
Servo(d0,116)
servoSpeed(d0,5)
servo(d1,112)
servospeed(D1,5)
Sayezb(&quot;what do you have&quot;)
\$heard =&quot;speech&quot;
WaitforChange(\$heard)
if (\$heard = &quot;rock&quot;)
Sayezb(&quot;that's what I had, no points&quot;)
sleep(2000)

elseif(\$heard = &quot;paper&quot;)
sayezb(&quot;paper beats rock, one point for you&quot;)
\$human = \$human+1

Sleep(2000)

elseif(\$heard = &quot;sissors&quot;)
sleep(2000)
Sayezb(&quot;rock beats sissors, one point for me&quot;)
\$inmoov = \$inmoov+1

``````

This is where I still struggle with these variables - how to define them. How do I define \$human / \$inmoov?

United Kingdom
#4

Variables are defined as soon as they are made. However, in order to make a variable it must have contents.

I use an init script which defines all of my variables. For example you could have this in an init script which runs automatically on connection or on load.

``````
\$P1_score = 0
\$P2_score = 0
\$P3_score = 0
\$P4_score = 0
\$MaxDistance = 1000
\$MinDistance = 1000
\$MinLightLevel = 6
\$RobotName = &quot;Inmoov&quot;
\$RobotFavColour = &quot;Green&quot;
\$RobotFavFood = &quot;Nuts&quot;
\$ErrorFlag1 = 0
\$ErrorFlag2 = &quot;False&quot;
\$TurnTime = 350
\$TurnSpeed = 90

``````

I have many variables on my projects due to the way I write scripts in ways that can be reused and easily modified. Usually I rewrite public scripts to include the variables in the script and define them amongst the first few lines. In reality these variables on my projects are defined in the init script, this avoids accidental resetting of counters etc. should there be a problem with a script stopping.

#5

@bhouston... see Rich's post #5

\$human="Bob" or something like that... or whatever name you wish...

#6

I got it to work but it doesn't add up the score correctly. There's what I have at the end.

``````
if(\$inmoov&gt;\$human)
sayezb(&quot;I won, nice game&quot;)
elseif (\$inmoov=\$human)
sayezb(&quot;it's a tie, want to play again?&quot;)
elseif(\$inmoov&lt;\$human)
sayezb(&quot;You won, nice game&quot;)
endif

``````
#7

Are you setting \$human and \$inmoov variables to 0 before starting each game? Try putting in print statements at the end (see below) so you can see the values of \$human and \$inmoov

``````
if(\$inmoov&gt;\$human)
sayezb(&quot;I won, nice game&quot;)
elseif (\$inmoov=\$human)
sayezb(&quot;it's a tie, want to play again?&quot;)
elseif(\$inmoov&lt;\$human)
sayezb(&quot;You won, nice game&quot;)
endif
Print(&quot;My score is &quot;+\$inmoov)

``````
United Kingdom
#8

Add the variable watcher control, it comes in handy for debugging.

Also, I noticed you are using WaitForChange for the response in your script. You should use WaitForSpeech(), it will work much better.

#9

InMoov cheats! Here's what I have at the beginning of my script to set the variables;

``````
Sleep(5000)
\$inmoov = &quot;inmoov = 0&quot;
\$human = &quot;human = 0&quot;
# get a response value
\$x = 0
RepeatWhile(\$x &lt; 5)
\$x++
\$response = GetRandomUnique(0,3)

``````

it's adding up correctly but saying the wrong thing.

``````
113: if(\$inmoov&gt;\$human)
114: sayezb(&quot;I won, nice game&quot;)
119: endif
120: Print(&quot;My score is &quot;+\$inmoov)
&gt; My score is inmoov = 0
&gt; Your score is human = 011111

``````

On this one InMoov announced he won.

United Kingdom
#10

Every time you use the script

``````\$human = \$human + 1
``````

You are effectively only adding a 1 to the end of the string. "Human = 0" + 1 is "Human = 01".

It is saying the correct thing for what you are asking to to say. What you are asking for is not the same as what you want.

Your variables for the scoring aren't decimal they are strings. The lines of code should be;

``````\$inmoov = 0
\$human = 0
``````

These are decimal and you will be able to use math with them. You cannot use math with strings.

With these lines of code \$human + 1 would be 1, then 2, then 3 as the variable is a number not a string.

#11

oops sorry Bob... Rich is right, I made a mistake...

before your game set your variables like this... then start the game...

``````
\$human=0
\$inmoov=0

``````
#12

Thank you both for all your help with this.