Board Logo
« Rock Paper Scissors and AI »

Welcome Guest. Please Login or Register.
Jan 16th, 2018, 3:33pm


Conforums Terms of Service | Membership Rules | Home | Search | Recent Posts | Notification | Format Your Message | Installation FAQ

Please use the forums Search feature before asking.
Please post code using the code box described in Format Your Messages.
This will keep indentation, separate it better form the message and prevent gibberish.
If the code is too long for one post or additional files are needed, upload a ZIP archive to the Just BASIC Files Archive Site.

« Previous Topic | Next Topic »
Pages: 1 2  Notify Send Topic Print
 hotthread  Author  Topic: Rock Paper Scissors and AI  (Read 1922 times)
tomc
Senior Member
ImageImageImageImageImage


member is offline

Avatar




PM


Posts: 590
xx Rock Paper Scissors and AI
« Thread started on: Jan 26th, 2016, 7:29pm »

Not so much a programming challenge as this can be ongoing. Tell me your comments !! Just write AI that
would fit in [player_one] or two.

Code:
' RPS Wars
'                                           1       2           3
' Rules - develope a routine which chooses (r)ock, (p)aper, or (s)cissors.

' 1) you can not select based on rnd or random.
' 2) you can not change your routine, one routine for all 200 chances.
' 3) the first 100 chances do not count in the scoring (learn from them).
' 4) you can review prior selections by yourself and opponent, and prior results
' 5) scoring is one point for winning RPS, chances 101 to 200.
' 6) one logic, no fine tuning your routine for each opponent
' or changing your code after viewing opponents code.
' 7) invalid choice (not 1, 2, or 3) is automatic loss

mt=200                                           ' max turns
dim a(mt)                                        ' player one turn
dim b(mt)                                        ' player two turn
dim c(mt)                                        ' results

a(0)=1:b(0)=2:c(0)=2                             ' 0 chance default, just in case you look at last result

cls:print:print,"RSP WARS - by TomC":print

for x=1 to mt
    gosub [player_one]                           ' return a(x)=1,2, or 3
    gosub [player_two]                           ' return b(x)

    f1=0:f2=0
    if a(x)<>1 and a(x)<>2 and a(x)<>3 then f1=1
    if b(x)<>1 and b(x)<>2 and b(x)<>3 then f2=1

    if f1 then print:print "   player one loses - invalid choice":end
    if f2 then print:print "   player two loses - invalid choice":end

    if a(x)=1 and b(x)=2 then c(x)=2
    if a(x)=1 and b(x)=3 then c(x)=1
    if a(x)=2 and b(x)=1 then c(x)=1
    if a(x)=2 and b(x)=3 then c(x)=2
    if a(x)=3 and b(x)=1 then c(x)=2
    if a(x)=3 and b(x)=2 then c(x)=1
next

for x=101 to mt
    if c(x)=1 then p1=p1+1
    if c(x)=2 then p2=p2+1
next

print "    Player One ";p1
print "    Player Two ";p2

r$="tie"
if p1>p2 then r$="One Wins !!!"
if p2>p1 then r$="Two Wins !!!"
if p1=p2 then r$="Tied ......."
print:print "    ";r$

end

[player_one]   ' rotate logic
    if a(x-1)=1 then a(x)=2
    if a(x-1)=2 then a(x)=3
    if a(x-1)=3 then a(x)=1
return


[player_two]
    b(x)=1     ' if I won or tied last same number, else shift down 1
    if c(x-1)=2 then b(x)=b(x-1)
    if c(x-1)=1 then b(x)=b(x-1)-1
    if b(x)=0 then b(x)=1
    if a(x-1)=b(x-1) then b(x)=b(x-1)
return


 
User IP Logged

tsh73
JB-Supporter


member is offline

Avatar




PM

Gender: Male
Posts: 3635
xx Re: Rock Paper Scissors and AI
« Reply #1 on: Jan 27th, 2016, 01:29am »

Code:
b(x)= (x+1) mod 3 +1 

Code:
              RSP WARS - by TomC

    Player One 0
    Player Two 100

    Two Wins !!!

 

LOL

Actually, serious question:
Quote:
1) you can not select based on rnd or random.

could I use some bizarre function of my own that will behave in similar way (what is, I will not know what it'll return)?
User IP Logged

Q: "And if I took your codes and compile them, and sell them for a profit"?
A: Go ahead. I had my share of good then I coded it for fun, if you can make better use of it - please do.
(enjoying JB 1.01 on WinXP, netbook and desktop)
bplus
Senior Member
ImageImageImageImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 1255
xx Re: Rock Paper Scissors and AI
« Reply #2 on: Jan 27th, 2016, 07:07am »

Cheater!
Code:
[player_two]
a(x)=1
b(x)=2
return 

« Last Edit: Jan 27th, 2016, 07:28am by bplus » User IP Logged

B+
tomc
Senior Member
ImageImageImageImageImage


member is offline

Avatar




PM


Posts: 590
xx Re: Rock Paper Scissors and AI
« Reply #3 on: Jan 27th, 2016, 07:12am »

all simples answers like in my example and yours, usually lose 100 to 0. The reason for not wanting random type answers is: so that the opponent, can try to learn your AI and counter against it. Ofcourse both sides are doing this simultaneously.

I also thought to prohibit additional arrays as one could build some massive tables.
User IP Logged

bplus
Senior Member
ImageImageImageImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 1255
xx Re: Rock Paper Scissors and AI
« Reply #4 on: Jan 27th, 2016, 10:16am »

Hi Tom,

So to make cheaters = loosers:

Players should depend on knowing that the other player's array and the scoring array are READ ONLY and not accessible for changing from a player's GOSUB routine.

That is easily done.

But the design of one's own AI may be heavily influenced by AI routines submitted so in theory the latest submission will beat the earlier and so on. (Who would submit a looser?)

Ah, so the winner is probably the last to submit... until someone comes along and finds a way around all before.

So to beat player_one, player_two and player_tsh...

wait.... tsh did you beat player_two?

Append: NO, not beat but not bad either
Quote:
RSP WARS - by TomC

Player tsh1 33
Player Player Two 33

Tied .......


test Code:
' RPS Wars
'                                           1       2           3
' Rules - develope a routine which chooses (r)ock, (p)aper, or (s)cissors.

' 1) you can not select based on rnd or random.
' 2) you can not change your routine, one routine for all 200 chances.
' 3) the first 100 chances do not count in the scoring (learn from them).
' 4) you can review prior selections by yourself and opponent, and prior results
' 5) scoring is one point for winning RPS, chances 101 to 200.
' 6) one logic, no fine tuning your routine for each opponent
' or changing your code after viewing opponents code.
' 7) invalid choice (not 1, 2, or 3) is automatic loss

mt=200                                           ' max turns
dim a(mt)                                        ' player one turn
dim b(mt)                                        ' player two turn
dim c(mt)                                        ' results

a(0)=1:b(0)=2:c(0)=2                             ' 0 chance default, just in case you look at last result
player1$="tsh1"
player2$="Player Two"
cls:print:print,"RSP WARS - by TomC":print

for x=1 to mt
    gosub [player_one]                           ' return a(x)=1,2, or 3
    gosub [player_two]                           ' return b(x)

    f1=0:f2=0
    if a(x)<>1 and a(x)<>2 and a(x)<>3 then f1=1
    if b(x)<>1 and b(x)<>2 and b(x)<>3 then f2=1

    if f1 then print:print "   player ";player1$;" loses - invalid choice":end
    if f2 then print:print "   player ";player2$;" loses - invalid choice":end

    if a(x)=1 and b(x)=2 then c(x)=2
    if a(x)=1 and b(x)=3 then c(x)=1
    if a(x)=2 and b(x)=1 then c(x)=1
    if a(x)=2 and b(x)=3 then c(x)=2
    if a(x)=3 and b(x)=1 then c(x)=2
    if a(x)=3 and b(x)=2 then c(x)=1
next

for x=101 to mt
    if c(x)=1 then p1=p1+1
    if c(x)=2 then p2=p2+1
next

print "    Player ";player1$;" ";p1
print "    Player ";player2$;" ";p2

r$="tie"
if p1>p2 then r$=player1$;" Wins !!!"
if p2>p1 then r$=player2$;" Wins !!!"
if p1=p2 then r$="Tied ......."
print:print "    ";r$

end

'player is tsh1
[player_one]  'tsh 1 because tsh 2, tsh 3,... are sure to follow
    a(x)= (x+1) mod 3 +1
return

'[player_one]   ' rotate logic original
'    if a(x-1)=1 then a(x)=2
'    if a(x-1)=2 then a(x)=3
'    if a(x-1)=3 then a(x)=1
'return

[player_two] 'tom c original
    b(x)=1     ' if I won or tied last same number, else shift down 1
    if c(x-1)=2 then b(x)=b(x-1)
    if c(x-1)=1 then b(x)=b(x-1)-1
    if b(x)=0 then b(x)=1
    if a(x-1)=b(x-1) then b(x)=b(x-1)
return

' -------------------------- cheaters are loosers (but still, it is kind of fun!)

[player_cheat] 'cheater! 2016-01-27
a(x)=1
b(x)=2
return

 


Need an easier way to plug in someone's AI and test against every other someone's AI code. Is there some way to load gosub routines into an array? Would writing routines as functions be easier?

BTW shouldn't the title be RPS WARS?
Rock, Paper Scissors

Is someone confused with RS(V)P for your seat at RPS Wars? ;)
« Last Edit: Jan 27th, 2016, 11:49am by bplus » User IP Logged

B+
tomc
Senior Member
ImageImageImageImageImage


member is offline

Avatar




PM


Posts: 590
xx Re: Rock Paper Scissors and AI
« Reply #5 on: Jan 27th, 2016, 5:20pm »

obviously the thought is that one would not cheat and change prior arrays. Acting and re=acting is common in AI, like chess programs always monitor and improve on other chess programs. Hopefully one would just accept an opponents code, place it in the program, and hit RUN.
.
There would be so much Ai in here, my guess is routines would be huge.
.
Still thinking about the best approach.
User IP Logged

AltBas
Full Member
ImageImageImageImage


member is offline

Avatar




PM


Posts: 461
xx Re: Rock Paper Scissors and AI
« Reply #6 on: Jan 27th, 2016, 9:27pm »

Some code I found illuminating (insert after printing Winner)
Code:
T$(1) = "Rock"
T$(2) = "Paper"
T$(3) = "Scissors"
print
for a = 0 to mt
  if c(a) = 1 then print T$(a(a));" *", T$(b(a))  '* Mark winner *
  if c(a) = 2 then print T$(a(a)), T$(b(a));" *"
  if c(a) = 0 then print T$(a(a)), T$(b(a))
next a 

It showed me the error of my ways in an early naive version.

I re-post NJames AI code for RPS, as he doesn't seem to post on the forum any more.

Original post: http://justbasic.conforums.com/index.cgi?board=games&action=display&num=1331825757
Code:
[player_one]
  redim t(3)  '* Count opponent play choices *
  t$ = ""
  for a = 0 to x - 1 : t$ = t$ + str$(b(a)) : next
  gosub [Check]
  if best = 0 then
    if c(x-1) = 2 then
      a(x) = ((b(x-1) + 2) MOD 3) + 1
    else
      a(x) = (best MOD 3) + 1
    end if
  else
    a(x) = (best MOD 3) + 1
  end if
RETURN

[player_two]
  redim t(3)
  t$ = ""
  for a = 0 to x - 1 : t$ = t$ + str$(a(a)) : next
  gosub [Check]
  if best = 0 then
    if c(x-1) = 1 then
      b(x) = ((a(x-1) + 2) MOD 3) + 1
    else
      b(x) = (best MOD 3) + 1
    end if
  else
    b(x) = (best MOD 3) + 1
  end if
RETURN

[Check]  '*** NJames code ***
  if len(t$) > 3 then
    Last$ = right$(t$, 3)
    idx = instr(t$, Last$)
    DO while idx > 0
      prv = val(mid$(t$, idx + 3, 1))
      t(prv) = t(prv) + 3.2
      idx = instr(t$, Last$, idx + 1)
    LOOP
  end if
  if len(t$) > 2 then
    Last$ = right$(t$, 2)
    idx = instr(t$, Last$)
    DO while idx > 0
      prv = val(mid$(t$, idx + 3, 1))
      t(prv) = t(prv) + 2.1
      idx = instr(t$, Last$, idx + 1)
    LOOP
  end if
  Last$ = right$(t$, 2)
  idx = instr(t$, Last$)
  DO while idx > 0
    prv = val(mid$(t$, idx + 3, 1))
    t(prv) = t(prv) + 1
    idx = instr(t$, Last$, idx + 1)
  LOOP

  best = 0 : hi = 0
  for a = 1 to 3
    if t(a) > hi then hi = t(a) : best = a
  next a
RETURN
 

Some explanation of the code in [Check]:
Code:
prv = val(mid$(t$, idx + 3, 1)) 

This picks the play AFTER the match on (1,2,3) run of plays; since the longer run has lower frequency, it gets a higher value; and the longer runs get a slightly higher weighting, ie 2.1 instead of 2 and 3.2 instead of 3. I have notes that a RPS choice following a run of 2 happens 3 times more frequently than runs of 3, and a RPS choice following a "run" of 1 happens 3 times more frequently than a run of 2. Apparently I generated a 999 string of random 1s, 2s, and 3s and counted occurences.

The algorithm is sensitive to initial conditions; if the created a(0), b(0) is a tie, no one ever wins; otherwise whoever won play (0) wins over all.
Code:
'* Player 1 wins play (0): *
    Player One 38
    Player Two 36

    One Wins !!!

'* Player 2 wins play(0): *
    Player One 36
    Player Two 38

    Two Wins !!!

'* Play (0) a tie: *
    Player One 0
    Player Two 0

    Tied ....... 

What would the output of the "ultimate" RPS algorithm look like when played against itself? Would it always tie? Would it alternate wins?

From the title of the code in this thread, I imagine tomc is going to run the algorithms against each other...

- AltBas, the Re-Poster
« Last Edit: Jan 27th, 2016, 9:33pm by AltBas » User IP Logged

bplus
Senior Member
ImageImageImageImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 1255
xx Re: Rock Paper Scissors and AI
« Reply #7 on: Jan 28th, 2016, 12:57am »

Interesting, thanks for link!
User IP Logged

B+
bplus
Senior Member
ImageImageImageImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 1255
xx Re: Rock Paper Scissors and AI
« Reply #8 on: Jan 29th, 2016, 8:55pm »

Upon further reflection and checking into the way JB works, here is a case when using a GO SUB makes sense: when you want to preserve variable values between "CALL"s to a subroutine. JB does not have a STATIC mechanism for variables in procedures. It does have a GLOBAL one but that might get unwieldy trying to track manually when comparing a new AI routine with all the others submitted in past.

To me, if I were playing another human a sequence of RPS games, I would look for patterns and likely be successful in picking up something to increase odds in winning. But! if I were to play a bot, I don't see how RANDOM can be beat! (This could be a sort of a reverse Turing Test. If you can't come up with a decent sequence of random responses then you must be human. A bot could fake being a human but not vice versa unless you run into a savant.)

Wouldn't the limit of an infinitely improving sequence of AI prediction eventually lead to some form of RANDOM choice?

BTW, I think I found another way to cheat, getting RANDOM responses but without using RANDOM but I doubt this was the direction tomc wanted to go when he started this thread. lipsrsealed

I also wonder if a sequence of 100 is really enough when coding a bot against a bot... I would think much longer sequences might be needed, some inhuman amount that no human could tolerate (another part of the reverse Turing test. There is also an aspect of how quick you are with next choice.) Perhaps I have read too much sci fi.
« Last Edit: Jan 29th, 2016, 9:19pm by bplus » User IP Logged

B+
ezprogramming
Guest
xx Re: Rock Paper Scissors and AI
« Reply #9 on: Jan 30th, 2016, 08:02am »

Post deleted
« Last Edit: Sep 15th, 2016, 2:16pm by ezprogramming » User IP Logged

bplus
Senior Member
ImageImageImageImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 1255
xx Re: Rock Paper Scissors and AI
« Reply #10 on: Jan 30th, 2016, 08:43am »

on Jan 30th, 2016, 08:02am, ezprogramming wrote:
Personally I don't think using GOSUB is ever desirable. The absence of STATIC or PRIVATE variables in JB is certainly inconvenient, but the way I would prefer to work around that limitation is to use BYREF parameters instead:

Code:
    for i = 1 to 10
      call mysub dummy
    next
    end

sub mysub byref mystatic
    mystatic = mystatic + 1
    print mystatic
end sub 

Richard.


Here is what I meant:
Code:
' byref check
    for i = 1 to 10
      call mysub dummy
      gosub [myOtherSub]
      print
    next
    end

sub mysub byref mystatic
    mystatic = mystatic + 1
    print "by ref ";mystatic;"  ";
    innerVariable=innerVariable+10
    print innerVariable
end sub

[myOtherSub]
   mystatic = mystatic + 1
    print "gosub ";mystatic;"  ";
    innerVariable=innerVariable+10
    print innerVariable
return

 


Output Code:
by ref 1  10
gosub 1  10

by ref 2  10
gosub 2  20

by ref 3  10
gosub 3  30

by ref 4  10
gosub 4  40

by ref 5  10
gosub 5  50

by ref 6  10
gosub 6  60

by ref 7  10
gosub 7  70

by ref 8  10
gosub 8  80

by ref 9  10
gosub 9  90

by ref 10  10
gosub 10  100

 
« Last Edit: Jan 30th, 2016, 08:47am by bplus » User IP Logged

B+
ezprogramming
Guest
xx Re: Rock Paper Scissors and AI
« Reply #11 on: Jan 30th, 2016, 11:34am »

Post deleted
« Last Edit: Sep 15th, 2016, 2:16pm by ezprogramming » User IP Logged

bplus
Senior Member
ImageImageImageImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 1255
xx Re: Rock Paper Scissors and AI
« Reply #12 on: Jan 30th, 2016, 7:44pm »

Code:
[MyGOSUB]
'using very unique varaible name in GOSUB not likely to used in main code:
if beenhereMyGOSUB=0 then 
'do massive initialization calc that only needs to be done once
end if
'continue, using massive calculation with beenhere variable

beenhereMyGOSUB=beenhereMyGOSUB + 1
return 


I am picturing an add on sub that does not interfere in anyway with main program code that will be written by someone else. Or adding someone else sub code that does not interfere with my main code.

It is true that we must still worry about variable double use between main and a GOSUB which would not be a concern with modern subs and functions, perhaps using both is solution =>
Code:
[MyGOSUB]
if beenhereMyGOSUB=0 then 
'do massive initialization calc that only needs to be done once
end if
'continue, using massive calculation with beenhere variable
call mysub(beenhereMyGOSUB, massiveCalcResult)
beenhereMyGOSUB=beenhereMyGOSUB + 1
return 


Like I said I am looking for ways to test my AI subroutine(s) against other peoples to see how it/they hold up.

How are you doing with yours?
User IP Logged

B+
tomc
Senior Member
ImageImageImageImageImage


member is offline

Avatar




PM


Posts: 590
xx Re: Rock Paper Scissors and AI
« Reply #13 on: Jan 30th, 2016, 11:00pm »

I got too much work lately, little time sad
embarassed
User IP Logged

ezprogramming
Guest
xx Re: Rock Paper Scissors and AI
« Reply #14 on: Jan 31st, 2016, 05:22am »

Post deleted
« Last Edit: Sep 15th, 2016, 2:17pm by ezprogramming » User IP Logged

Pages: 1 2  Notify Send Topic Print
« Previous Topic | Next Topic »

Conforums Terms of Service | Membership Rules | Home | Search | Recent Posts | Notification | Format Your Message | Installation FAQ

Donate $6.99 for 50,000 Ad-Free Pageviews!

| |

This forum powered for FREE by Conforums ©
Sign up for your own Free Message Board today!
Terms of Service | Privacy Policy | Conforums Support | Parental Controls