Award
Congratulations Richard R, for answering the question and has received an EZ-Credit award! Share what you have learned and create a tutorial to help others by clicking here.

How To Keep Score

Assistance Requested

Help bhouston with their question and receive $10 of EZ-Credit to get more robots and parts from our store. The following information was provided about their previous efforts searching tutorials for a resolution.

bhouston claims to have checked these sources:
 
#1

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

I have found a few conversations, tutorials and activities that may help with your question. Take a look at these links. I've sorted them by what I believe to be most relevant but that is not always the case as I'm still learning.


Also, consider reviewing the Learn section for informative lessons and activities. Check it out!


#2

@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"Winky
elseif $inmoov=$human
say("it's a tie, want to play again?"Winky
else
say("You won, nice game"Winky
endif

Something like that, anyway

#3

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

Code:


$P1_score = 0
$P2_score = 0

# Code for game

If($winner = "P1")
$P1_score++
ElseIf($winner = "P2")
$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.

#4

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

Code:


Error on line 90: Variable not defined: $human



Code:


# get a response value
$x = 0
RepeatWhile($x < 5)
$x++
$response = GetRandomUnique(0,3)

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

elseif($heard = "paper")
sayezb("paper beats rock, one point for you")
$human = $human+1

Sleep(2000)

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



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

#5

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.

Code:


$P1_score = 0
$P2_score = 0
$P3_score = 0
$P4_score = 0
$MaxDistance = 1000
$MinDistance = 1000
$MinLightLevel = 6
$RobotName = "Inmoov"
$RobotFavColour = "Green"
$RobotFavFood = "Nuts"
$ErrorFlag1 = 0
$ErrorFlag2 = "False"
$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.

#6

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

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

#7

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

Code:


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

#8

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

Code:


if($inmoov>$human)
sayezb("I won, nice game")
elseif ($inmoov=$human)
sayezb("it's a tie, want to play again?")
elseif($inmoov<$human)
sayezb("You won, nice game")
endif
Print("My score is "+$inmoov)
Print("Your score is "+$human)

#9

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.

#10

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

Code:


ControlCommand("Auto Position", AutoPositionAction, "Readyset")
Sleep(5000)
$inmoov = "inmoov = 0"
$human = "human = 0"
# get a response value
$x = 0
RepeatWhile($x < 5)
$x++
$response = GetRandomUnique(0,3)



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

Code:


113: if($inmoov>$human)
114: sayezb("I won, nice game")
119: endif
120: Print("My score is "+$inmoov)
> My score is inmoov = 0
121: Print("Your score is "+$human)
> Your score is human = 011111


On this one InMoov announced he won.