# Board index » delphi » Help for a game

## Help for a game

Hi all

I started to program (TP7) a game, i'm sure you know it : you have 3
choices
between a stone, a paper and a cutter. Each win against one of the other
and
lose against the other.
It's quite simple but we're actually searching for a complex
algorithm that
can win most oftently ....

Thanks

## Re:Help for a game

##### Quote
Jean-Philippe wrote:
> Hi all

>     I started to program (TP7) a game, i'm sure you know it : you have 3
> choices
> between a stone, a paper and a cutter. Each win against one of the other
> and
> lose against the other.
>     It's quite simple but we're actually searching for a complex
> algorithm that
> can win most oftently ....

>     Thanks

An algorithm that can let the computer win more often than not?
I thought the whole idea of the game was that it was entirely random and
neither player knew what the opponent's choice would be until they both
revealed their choices. The only way the computer can beat the user is if
the computer effectively looks at what the user has selected and then
chooses the one item that can beat the user's selection. Let's see how that
would work.

{Paper beats stone; Stone beats Scissor; Scissor beats Paper}

Validate USERselection
if USERselection = paper   then CPUselection = scissor
if USERselection = scissor then CPUselection = stone
if USERselection = stone   then CPUselection = paper

Of course this is just the beginning of an idea for how an algorithm like
this would work. You could have the computer cheat 1 turn out of 3, or
generate a number from 1 to 5 every turn and if the number is greater than
3, the computer cheats.

I do believe that this takes a lot of the fun out of this game, though.

--
___________________________________________________
|||
|  _________________  |  Sam Jones(Flash Gordon)  |
|    ((||  |                           |
|    |                           |
|        a.k.a.       |     fly.to/mongo.city     |
|  _________________  |===========================|
|  |||||  |                           |
|  ||=     || _       |                           |
|  ||LASH  ||_|ORDON  |   WWW = World Wide Wait   |
|    |                           |
|_____________________|___________________________|

## Re:Help for a game

##### Quote
Jean-Philippe wrote:
> Hi all

>     I started to program (TP7) a game, i'm sure you know it : you have 3
> choices
> between a stone, a paper and a cutter. Each win against one of the other
> and
> lose against the other.

Called "rock, paper, scissors" around here.  :-)

##### Quote
>     It's quite simple but we're actually searching for a complex
> algorithm that
> can win most oftently ....

It's not possible without cheating.  You have three choices, each of which can
either lose, win, or tie against the opponent's draw.  By probability, each
has a 1/3 chance.  The *only* way you could get around it is if you make the
computer's choice based upon the user's choice.  For instance:

type
eDraw = (rock, paper, scissors);

var
userdraw, compdraw : eDraw;

..
..
..

procedure computerdraw;

{assume player's draw has been read into userdraw}

begin
if random < 0.1 then
case userdraw of
rock : compdraw := paper;
paper : compdraw := scissors;
scissors : compdraw := rock;
end
else
compdraw := eDraw(random(3));
end;

This will make it guarantee a win 10% of the time, but as I said, this is
cheating and goes against the rules of the game.

##### Quote
>     Thanks

--
Scott Earnest            | SPAM protection in effect. Remove  |
setech@_ix.netcom.com    | "_" as needed for true addresses.  |
earnests@_homenet.lm.com |    UIN:1136443  EFnet:pale_blue    |
sinykal@_{*word*104}space.org  | URL: http://www.netcom.com/~setech |

## Re:Help for a game

##### Quote
Jean-Philippe wrote:

> Hi all

>     I started to program (TP7) a game, i'm sure you know it : you have
> 3
> choices
> between a stone, a paper and a cutter. Each win against one of the
> other
> and
> lose against the other.
>     It's quite simple but we're actually searching for a complex
> algorithm that
> can win most oftently ....

>     Thanks

You can't master a totally-random-game like this.
Unless you assume the players actions aren't random in which case you
probably would need a 'psychological algorithm'. You could use a
tit-for-tat-approach to this. In this case I think you'd need some
statistical information on human behaviour in this game. You could
gather that yourself: play the game endlessly and analyse the sequences
of moves by several players. It won't make your program a sure winner,
it might increase its chances, slightly.

Good luck,

Remco de Korte
Soft Machine
http://www.xs4all.nl/~remcodek/

## Re:Help for a game

##### Quote
> You can't master a totally-random-game like this.
> Unless you assume the players actions aren't random in which case you
> probably would need a 'psychological algorithm'. You could use a
> tit-for-tat-approach to this. In this case I think you'd need some
> statistical information on human behaviour in this game. You could
> gather that yourself: play the game endlessly and analyse the sequences
> of moves by several players. It won't make your program a sure winner,
> it might increase its chances, slightly.

> Good luck,

> Remco de Korte
> Soft Machine
> http://www.xs4all.nl/~remcodek/

Actually it's easy to prouve that the random strategy is not the best one
and
we can build algorithm to win against an all random strategy method.

But my problem is that i have to buid some algorithm witch must win in all
cases
even if there is an human playing or random.
So the program must read and analys sequences, following ..........

I don't know if working on a array like that is the best thing ??

1    2    3    4    5    6    .......

Paper        0    1    1    0    0    0    ......
Scissors     1    0    0    0    0    1    ......
Rock         0    0    0    1    1    0    ........

Thanks if someone couls help me with these algorithm

## Re:Help for a game

##### Quote
Jean-Philippe wrote:
> Hi all

>     I started to program (TP7) a game, i'm sure you know it : you have 3
> choices
> between a stone, a paper and a cutter. Each win against one of the other
> and
> lose against the other.
>     It's quite simple but we're actually searching for a complex
> algorithm that
> can win most oftently ....

>     Thanks

Here's a similar program I made for my Programming Class.

<-------begin code----------->
program dumbgame;
uses crt;
var  ties,round,roundend,pwin,plose,compwin,complose,computerint:integer;
compstring,playerstring:string;
playerchar,p,s,r:char;

begin
{init functions}
clrscr;
randomize;
pwin:=0;
plose:=0;
compwin:=0;
complose:=0;
ties:=0;
{end init functions}

{begin of instructions}
textcolor(lightgreen);
writeln('Hello User':40);
textcolor(lightblue);
writeln('                  Welcome to Paper, Scissors, Rocks');
textcolor(white+16);
writeln('                  Paper = p, Scissor = s, Rock = r');
textcolor(white);
writeln;
writeln;
writeln('How many rounds do you want to play?');
clrscr;
{end of instructions}

{variables Paper=1, Scissor=2, Rock=3}
{begin of user input and computer input}
for round:=1 to roundend do
begin
computerint:=(random(3)+1);
case computerint of
1:compstring:='paper';
2:compstring:='scissors';
3:compstring:='rock';
end;
clrscr;
textcolor(white);
writeln;
textcolor(5);
writeln('Paper = p, Scissor = s, Rock = r');
textcolor(white);
repeat
until ((playerchar='r') or (playerchar='s') or
(playerchar='p'));
case playerchar of
'p':playerstring:='paper';
's':playerstring:='scissors';
'r':playerstring:='rock';
end;
textcolor(white);
clrscr;
writeln;
writeln('The computer choose: ',compstring);
writeln('You choose:          ',playerstring);
writeln;

{star of math+score functions}
if ((playerstring=compstring) or (compstring=playerstring))
then begin
inc(ties);
textcolor(12+16);
writeln('A Tie!');
clrscr;
end;
{player wins section}
if (((playerchar='p') and (computerint=3))
or ((playerchar='s') and (computerint=1))
or ((playerchar='r') and (computerint=2)))
then begin
textcolor(lightgreen);
writeln('Doh! You Won This Round!');
pwin:=pwin+1;
complose:=complose+1;
clrscr;
end;

{paper=1 scissors=2 rocks=3}
if (((computerint=1) and (playerchar='r'))
or ((computerint=2) and (playerchar='p'))
or ((computerint=3) and (playerchar='s')))
then begin
textcolor(lightblue+16);
writeln('The computer beat you!');
plose:=plose+1;
compwin:=compwin+1;
clrscr;
end;
end;

{End of Math+Score functions {Total Score functions}

{Start of Stastic Functions}     textcolor(3);
clrscr;
writeln('Total Rounds Played:      ',round);
writeln('----------------------------------------');
writeln('Computer Winnings:        ',compwin);
writeln('Computer Loses:           ',complose);
writeln;
writeln;
writeln('Number of Ties:           ',ties);
writeln;
writeln;
{End of Stastic Functions}

{Begin of Taunt Functions}
textcolor(lightgreen);
if (compwin>pwin)
then
begin
writeln('HAHAH YOU LOST IDIOT!');
end
else
begin
if (compwin<pwin)
then
begin
textcolor(lightred);
writeln('You won, but you won''t be so
lucky nextime!');
end
else
begin
textcolor(12);
writeln('Damn... tied, but the
computer win next time for sure');
end;
end;
{End of Taunt Functions}
end.

<------end code------->

Yeah, its kinda stupid, but what do you expect when the students often know
more than the teachers?...

am

## Re:Help for a game

##### Quote
Jean-Philippe wrote:

> > You can't master a totally-random-game like this.
> > Unless you assume the players actions aren't random in which case
> you
> > probably would need a 'psychological algorithm'. You could use a
> > tit-for-tat-approach to this. In this case I think you'd need some
> > statistical information on human behaviour in this game. You could
> > gather that yourself: play the game endlessly and analyse the
> sequences
> > of moves by several players. It won't make your program a sure
> winner,
> > it might increase its chances, slightly.

> > Good luck,

> > Remco de Korte
> > Soft Machine
> > http://www.xs4all.nl/~remcodek/

> Actually it's easy to prouve that the random strategy is not the best
> one
> and
> we can build algorithm to win against an all random strategy method.

I'd like to see that!
Of course the random strategy is seldom the most succesful one, but I
don't see how another strategy in this game will work; not without
cheating.

- Show quoted text -

##### Quote

> But my problem is that i have to buid some algorithm witch must win in
> all
> cases
> even if there is an human playing or random.
> So the program must read and analys sequences, following ..........

> I don't know if working on a array like that is the best thing ??

>                     1    2    3    4    5    6    .......

>     Paper        0    1    1    0    0    0    ......
>     Scissors     1    0    0    0    0    1    ......
>     Rock         0    0    0    1    1    0    ........

>     Thanks if someone couls help me with these algorithm

Aha! That IS cheating (if I understand what you're getting at).
Or are you trying to find a predefined sequence of moves? If you're
playing against a human you can NEVER win all the time, if you don't
know what his or her move is before you decide on yours.
I could be wrong of course and I'd like it very much if you proved it to
me.

...

It just occured to me: are you per chance saying that you want to win a
game consisting of several moves? (Man, am I thick!)

Good luck,

Remco de Korte
Thick Machine
http://www.xs4all.nl/~remcodek/

## Re:Help for a game

##### Quote
> Aha! That IS cheating (if I understand what you're getting at).
> Or are you trying to find a predefined sequence of moves? If you're
> playing against a human you can NEVER win all the time, if you don't
> know what his or her move is before you decide on yours.
> I could be wrong of course and I'd like it very much if you proved it to
> me.

> ...

> It just occured to me: are you per chance saying that you want to win a
> game consisting of several moves? (Man, am I thick!)

> Good luck,

> Remco de Korte
> Thick Machine
> http://www.xs4all.nl/~remcodek/

I understand what he means now (I think). I believe he wants the computer to
remember patterns made by the user in a sequence of turns, and use that
information to decide what move the computer should make next.
--
___________________________________________________
|||
|  _________________  |  Sam Jones(Flash Gordon)  |
|    ((||  |                           |
|    |                           |
|        a.k.a.       |     fly.to/mongo.city     |
|  _________________  |===========================|
|  |||||  |                           |
|  ||=     || _       |                           |
|  ||LASH  ||_|ORDON  |   WWW = World Wide Wait   |
|    |                           |
|_____________________|___________________________|

## Re:Help for a game

##### Quote
Jean-Philippe wrote in message <3518447B.171E0...@mail.dotcom.fr>...
>...
>But my problem is that i have to buid some algorithm witch must win
in all
>cases
>even if there is an human playing or random.
>So the program must read and analys sequences, following ..........

This is getting puzzling. I begin to think it's not the
scissors/paper/stone game my kids know about, where there are _no_
predefined sequences. If you detect that your opponent (the computer
in this case) has a tendency to follow a pattern, you can predict what
the next choice will be and choose the option that will beat it. Each
of the 3 options can be beaten by one of the others: stone blunts
scissors, paper covers stone, scissors cuts paper. A human opponent
can abandon the sequence and win. Maybe you'd describe the game,

As far as I can see, the only way to be sure of winning is cheat, e.g.

- insist on the opponent choosing first (this is a non-runner, the
human opponent will just hit the power key and win again)
- choose the plasma cannon. This will defeat scissors, paper _or_
stone.

FP

## Re:Help for a game

##### Quote
Flash Gordon wrote:

> > Aha! That IS cheating (if I understand what you're getting at).
> > Or are you trying to find a predefined sequence of moves? If you're
> > playing against a human you can NEVER win all the time, if you don't
> > know what his or her move is before you decide on yours.
> > I could be wrong of course and I'd like it very much if you proved
> it to
> > me.

> > ...

> > It just occured to me: are you per chance saying that you want to
> win a
> > game consisting of several moves? (Man, am I thick!)

> > Good luck,

> > Remco de Korte
> > Thick Machine
> > http://www.xs4all.nl/~remcodek/

> I understand what he means now (I think). I believe he wants the
> computer to
> remember patterns made by the user in a sequence of turns, and use
> that
> information to decide what move the computer should make next.
> --

That's what I assumed in my first reaction, but there are no (known)
algorithms in human behaviour in such a game. Not in the mathematic
sense. You could look at patterns and try to analyse those but then
you're talking about statistics and/or psychology. Very interesting but
not a basis for a foolproof program as far as I can see.

Still, I'm very curious about any results.

Remco de Korte
Soft Machine
http://www.xs4all.nl/~remcodek

## Re:Help for a game

##### Quote
Flash Gordon wrote:
> I understand what he means now (I think). I believe he wants the computer to
> remember patterns made by the user in a sequence of turns, and use that
> information to decide what move the computer should make next.
> --

Yes, but this way the computer would not win everytime, because the computer
would have to "learn" how the player plays, and that information could only be
used on THAT player.. so it isn't much like an algorithm (wich the original post
wanted).
I don't  think you can create an algorithm that would win most times, because it
would be some sort of "anti-random" algorithm that should be able to know what
the most possible random option of the human player is before the players thinks
of it..

Making the computer cheat, is the best way to make it win..

Greets,
--
Pascal Bestebroer
Just4Fun Productions
internet: http://people.zeelandnet.nl/rpb
e-mail  : just4...@zeelandnet.nl
ICQ : 2309110