Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: Rock Paper Scissors Lizard Spock Help Needed



Permlink Replies: 8 - Last Post: Mar 16, 2016 11:13 AM Last Post By: Jeff Overcash (...
Donovan Abrey

Posts: 4
Registered: 6/22/10
Rock Paper Scissors Lizard Spock Help Needed
Click to report abuse...   Click to reply to this thread Reply
  Posted: Mar 15, 2016 9:14 AM
Hi guys I need help getting this game to work, it is resulting in incorrect answers, i believe everything is correct but i`m just nesting my ifs incorrectly, all help appreciated!


//THE BRAINS//
        if bothand=hand then begin
            showmessage('You Tie!');
            ties:= ties+1;
        end
 
 
        else if (hand=('rock')) and (bothand=('scissors')) then begin
            showmessage('You Win!');
            Wins:= wins+1;
        end
 
        else if (hand=('rock')) and (bothand=('lizard')) then begin
            showmessage('You Win!');
            Wins:= wins+1;
        end
 
 
        else if (hand=('paper')) and (bothand=('rock')) then begin
            showmessage('You Win!');
            Wins:= wins+1;
        end
 
        else if (hand=('paper')) and (bothand=('spock')) then begin
            showmessage('You Win!');
            Wins:= wins+1;
        end
 
 
        else if (hand=('scissors')) and (bothand=('paper')) then begin
            showmessage('You Win!');
            Wins:= wins+1;
        end
 
        else if (hand=('scissors')) and (bothand=('lizard')) then begin
            showmessage('You Win!');
            Wins:= wins+1;
        end
 
 
        else if (hand=('lizard')) and (bothand=('spock')) then begin
            showmessage('You Win!');
            Wins:= wins+1;
        end
 
        else if (hand=('lizard')) and (bothand=('paper')) then begin
            showmessage('You Win!');
            Wins:= wins+1;
        end
 
 
        else if (hand=('spock')) and (bothand=('scissors')) then begin
            showmessage('You Win!');
            Wins:= wins+1;
        end
 
        else if (hand=('spock')) and (bothand=('rock')) then begin
            showmessage('You Win!');
            Wins:= wins+1;
        end
 
        else
        begin
            showmessage('You lose!');
            losses:= losses +1;
        end;
 
        // THE BRAINS END//
 
Robert Triest

Posts: 687
Registered: 3/24/05
Re: Rock Paper Scissors Lizard Spock Help Needed
Click to report abuse...   Click to reply to this thread Reply
  Posted: Mar 15, 2016 9:28 AM   in response to: Donovan Abrey in response to: Donovan Abrey
Try to use a structure like Enumerated Types.
type RPS= (Rock, Paper, Scissors, Lizard, Spock);


Then you can check things like Rock<Paper

http://www.delphibasics.co.uk/Article.asp?Name=Sets
Donovan Abrey

Posts: 4
Registered: 6/22/10
Re: Rock Paper Scissors Lizard Spock Help Needed
Click to report abuse...   Click to reply to this thread Reply
  Posted: Mar 15, 2016 9:34 AM   in response to: Robert Triest in response to: Robert Triest
Robert Triest wrote:
Try to use a structure like Enumerated Types.
type RPS= (Rock, Paper, Scissors, Lizard, Spock);


Then you can check things like Rock<Paper

http://www.delphibasics.co.uk/Article.asp?Name=Sets


Thanks for the quick reply, I will check that out, but for the time being, do you see anything wrong with the way I formatted my if, else and if else statements?

Donovan Abrey

Posts: 4
Registered: 6/22/10
Re: Rock Paper Scissors Lizard Spock Help Needed
Click to report abuse...   Click to reply to this thread Reply
  Posted: Mar 15, 2016 9:47 AM   in response to: Donovan Abrey in response to: Donovan Abrey
Donovan Abrey wrote:
Robert Triest wrote:
Try to use a structure like Enumerated Types.
type RPS= (Rock, Paper, Scissors, Lizard, Spock);


Then you can check things like Rock<Paper

http://www.delphibasics.co.uk/Article.asp?Name=Sets


Thanks for the quick reply, I will check that out, but for the time being, do you see anything wrong with the way I formatted my if, else and if else statements?

It seems to be that it only results in either a tie or a loss but never a win, even when it is meant to be a win!?

Edited by: Donovan Abrey on Mar 15, 2016 9:47 AM

Edited by: Donovan Abrey on Mar 15, 2016 9:48 AM

John Kouraklis

Posts: 209
Registered: 3/10/01
Re: Rock Paper Scissors Lizard Spock Help Needed
Click to report abuse...   Click to reply to this thread Reply
  Posted: Mar 15, 2016 9:51 AM   in response to: Donovan Abrey in response to: Donovan Abrey
Donovan Abrey wrote:
Hi guys I need help getting this game to work, it is resulting in incorrect answers, i believe everything is correct but i`m just nesting my ifs incorrectly, all help appreciated!

 
//THE BRAINS//
        if bothand=hand then begin
            showmessage('You Tie!');
            ties:= ties+1;
        end
 
 
        else if (hand=('rock')) and (bothand=('scissors')) then begin
            showmessage('You Win!');
            Wins:= wins+1;
        end
 
        else if (hand=('rock')) and (bothand=('lizard')) then begin
            showmessage('You Win!');
            Wins:= wins+1;
        end
 
 
        else if (hand=('paper')) and (bothand=('rock')) then begin
            showmessage('You Win!');
            Wins:= wins+1;
        end
 
        else if (hand=('paper')) and (bothand=('spock')) then begin
            showmessage('You Win!');
            Wins:= wins+1;
        end
 
 
        else if (hand=('scissors')) and (bothand=('paper')) then begin
            showmessage('You Win!');
            Wins:= wins+1;
        end
 
        else if (hand=('scissors')) and (bothand=('lizard')) then begin
            showmessage('You Win!');
            Wins:= wins+1;
        end
 
 
        else if (hand=('lizard')) and (bothand=('spock')) then begin
            showmessage('You Win!');
            Wins:= wins+1;
        end
 
        else if (hand=('lizard')) and (bothand=('paper')) then begin
            showmessage('You Win!');
            Wins:= wins+1;
        end
 
 
        else if (hand=('spock')) and (bothand=('scissors')) then begin
            showmessage('You Win!');
            Wins:= wins+1;
        end
 
        else if (hand=('spock')) and (bothand=('rock')) then begin
            showmessage('You Win!');
            Wins:= wins+1;
        end
 
        else
        begin
            showmessage('You lose!');
            losses:= losses +1;
        end;
 
        // THE BRAINS END//
 

Hi Donovan,

I suspect one of the if-else branches is let orphan (without else) in the flow of the code.

A cleaner way to do this would be:

if bothand=... then
begin
///
end
else
begin
if hand='rock' and 'scissors' then //<-- need to write it properly
begin
showmessage (...)
wins:=wins+1
Exit; <---- this will exit the method and no other ifs will be evaluated
end
end

BTW, when you say you have incorrect results, which combination creates them?

As others pointed out, enums is good way to go.

Hope this helps.

John

Edited by: John Kour on Mar 15, 2016 9:52 AM
Donovan Abrey

Posts: 4
Registered: 6/22/10
Re: Rock Paper Scissors Lizard Spock Help Needed
Click to report abuse...   Click to reply to this thread Reply
  Posted: Mar 15, 2016 10:07 AM   in response to: John Kouraklis in response to: John Kouraklis
John Kour wrote:
Donovan Abrey wrote:
Hi guys I need help getting this game to work, it is resulting in incorrect answers, i believe everything is correct but i`m just nesting my ifs incorrectly, all help appreciated!

 
//THE BRAINS//
        if bothand=hand then begin
            showmessage('You Tie!');
            ties:= ties+1;
        end
 
 
        else if (hand=('rock')) and (bothand=('scissors')) then begin
            showmessage('You Win!');
            Wins:= wins+1;
        end
 
        else if (hand=('rock')) and (bothand=('lizard')) then begin
            showmessage('You Win!');
            Wins:= wins+1;
        end
 
 
        else if (hand=('paper')) and (bothand=('rock')) then begin
            showmessage('You Win!');
            Wins:= wins+1;
        end
 
        else if (hand=('paper')) and (bothand=('spock')) then begin
            showmessage('You Win!');
            Wins:= wins+1;
        end
 
 
        else if (hand=('scissors')) and (bothand=('paper')) then begin
            showmessage('You Win!');
            Wins:= wins+1;
        end
 
        else if (hand=('scissors')) and (bothand=('lizard')) then begin
            showmessage('You Win!');
            Wins:= wins+1;
        end
 
 
        else if (hand=('lizard')) and (bothand=('spock')) then begin
            showmessage('You Win!');
            Wins:= wins+1;
        end
 
        else if (hand=('lizard')) and (bothand=('paper')) then begin
            showmessage('You Win!');
            Wins:= wins+1;
        end
 
 
        else if (hand=('spock')) and (bothand=('scissors')) then begin
            showmessage('You Win!');
            Wins:= wins+1;
        end
 
        else if (hand=('spock')) and (bothand=('rock')) then begin
            showmessage('You Win!');
            Wins:= wins+1;
        end
 
        else
        begin
            showmessage('You lose!');
            losses:= losses +1;
        end;
 
        // THE BRAINS END//
 

Hi Donovan,

I suspect one of the if-else branches is let orphan (without else) in the flow of the code.

A cleaner way to do this would be:

if bothand=... then
begin
///
end
else
begin
if hand='rock' and 'scissors' then //<-- need to write it properly
begin
showmessage (...)
wins:=wins+1
Exit; <---- this will exit the method and no other ifs will be evaluated
end
end

BTW, when you say you have incorrect results, which combination creates them?

As others pointed out, enums is good way to go.

Hope this helps.

John

Edited by: John Kour on Mar 15, 2016 9:52 AM

Thanks for the reply, i will try and implement that.

BTW Donovan is my teachers name, he signed us all up(class) under his email address and name, we all have our own serial tho. :D

Robert Triest

Posts: 687
Registered: 3/24/05
Re: Rock Paper Scissors Lizard Spock Help Needed
Click to report abuse...   Click to reply to this thread Reply
  Posted: Mar 16, 2016 4:13 AM   in response to: Donovan Abrey in response to: Donovan Abrey
I could not resist to check if the problem could be solved by using Enumerations and Sets ;-)

mmm. doesn't work well ;-))

Edited by: Robert Triest on Mar 16, 2016 3:24 PM
Robert Triest

Posts: 687
Registered: 3/24/05
Re: Rock Paper Scissors Lizard Spock Help Needed
Click to report abuse...   Click to reply to this thread Reply
  Posted: Mar 16, 2016 4:19 AM   in response to: Robert Triest in response to: Robert Triest
mmm doesn't work well..

Edited by: Robert Triest on Mar 16, 2016 3:24 PM
Jeff Overcash (...

Posts: 1,529
Registered: 9/23/99
Re: Rock Paper Scissors Lizard Spock Help Needed
Click to report abuse...   Click to reply to this thread Reply
  Posted: Mar 16, 2016 11:13 AM   in response to: Donovan Abrey in response to: Donovan Abrey
Donovan Abrey wrote:
Hi guys I need help getting this game to work, it is resulting in incorrect answers, i believe everything is correct but i`m just nesting my ifs incorrectly, all help appreciated!


Something like this should work

type
   TRPS= (Rock, Paper, Scissors, Lizard, Spock);
 
procedure WinningHand(hand, bothand : TRPS);
var
   beats : Boolean;
begin
   if hand = bothand then
     showmessage('You Tie!')
   else
   begin
     case hand of
       Rock: beats := bothand in [Lizard, Scissors];
       Paper: beats := bothand in [Rock, Spock];
       Scissors: beats := bothand in [Paper, Lizard];
       Lizard: beats := bothand in [Spock, Paper];
       Spock: beats := bothand in [Scissors, Rock];
     end;
     if beats then
       ShowMessage('Wins')
     else
       ShowMessage('Losses');
   end;
end;


Pretty easy to map the strings to enum or use RTTI do translate back and forth
if you want.

--
Jeff Overcash (TeamB)
(Please do not email me directly unless asked. Thank You)
Learning is finding out what you already know. Doing is demonstrating that you
know it. Teaching is reminding others that they know it as well as you. We are
all leaners, doers, teachers. (R Bach)

---
This email has been checked for viruses by Avast antivirus software.
https://www.avast.com/antivirus
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02