Author Topic: Two Tables Game  (Read 2627 times)

B+

  • Guest
Two Tables Game
« on: January 16, 2016, 04:25:54 AM »
Simplest game you will ever find! Easy to code in any dialect!

Code: [Select]
'Two Tables.txt Game for JB 2016-01-15 by [B+=MGA]

dim a(100),b(100)
play$=""
while play$=""
    'init tables a and b and display
    a(0)=10000:s$=""
    for i=1 to 100 'save time and presort
        a(i)=a(i-1)+int(rnd(0)*1000)
        print "  ";a(i);
        if i mod 10=0 then print
    next
    win=a((int( rnd(0)*100)+1))
    print
    b(0)=1000:adj=0
    for i=1 to 99
        if a(i)=win then adj=1
        b(i)=a((i+adj))
        print "  ";b(i);
        if i mod 10=0 then print
    next
    b(100)=a(100)+int(rnd(0)*1000)
    print "  ";b(100)
    print
    'pose the question then start timing
    t=time$("seconds")
    input "What number in top table is missing from the lower ? ";guess
    if guess=win then print "Right! in "; else print win;", sorry, in ";
    print (time$("seconds")-t);" seconds."
    print
    input "Just press enter to play again, any other+enter quits ";play$
    cls
wend
print "Good-bye"

B+

  • Guest
Re: Two Tables Game
« Reply #1 on: January 16, 2016, 10:36:01 PM »
Here is GUI (no keyboard used for program input, all mouse) SmallBASIC version:

Code: [Select]
'Two Tables.txt Game for SmallBasic 012.2 by [B+=MGA] 2016-01-15
'2016-01-16 fix logic bug to assure all numbers will be unique
'2016-01-16 fix with cols and rows for landscape or portrait screens
'2016-01-16 remove need for keyboard, use "buttons" to quit or play again

'setup for alternate screens based on text height and width, about 100 items in tables
th=txth("By")
tw=txtw("8")
if tw*7*10>xmax then cols=int(xmax/(tw*7)):rows=int(100/cols) else cols=10:rows=10
items=rows*cols
prow=(2*rows+2)*th
qrow=prow+2*th
dim a(items),b(items)
while 1
    'init tables a and b and display
    cls
    pen on
    a(0)=10000
    for i=1 to items 'save time and presort
        a(i)=a(i-1)+int(rnd*1000)+1
        print "  ";a(i);
        if i mod cols=0 then print
    next
    rwin=int(rnd*items) +1
    if rwin mod cols=0 then xwin=(cols-1)*tw*7 else xwin=((rwin mod cols)-1)*tw*7
    ywin=int((rwin-1)/cols)*th
    win=a(rwin)
    print
    adj=0
    for i=1 to (items-1)
        if a(i)=win then adj=1
        b(i)=a((i+adj))
        print "  ";b(i);
        if i mod cols=0 then print
    next
    b(items)=a(items)+int(rnd*1000)+1
    print "  ";b(items)
    print
    'pose the question then start timing
    t=ticks
    at 0,prow:print "  Tap top table # missing in lower"
    while 1
      if pen(3) then xguess=pen(4):yguess=pen(5):delay 10:exit
    wend
    xright=0:yright=0
    if xguess>xwin and xguess<xwin+7*tw then xright=1
    if yguess>ywin and yguess<ywin+th then yright=1
    at 0,prow:Print space(40):at 0,prow
    if xright and yright then print "  Right! in "; else print "  ";win;", sorry, in ";
    print int((ticks-t)/1000);" sec."
    print "   A short pause then prompt coming..."
    delay 8000
    cls
    rect .125*xmax,.375*ymax,.375*xmax,.625*ymax,10
    rect .625*xmax,.375*ymax,.875*xmax,.625*ymax,12
    at .25*xmax-5*tw,ymax/2:? "PLAY AGAIN"
    at .75*xmax-2*tw,ymax/2:? "QUIT"
    mx=0
    while 1
      if pen(3) then
        mx=pen(4):pen off
        if mx>xmax/2 then
          end
        else
          at (xmax-20*tw)/2,.9*ymax:? "One moment please...":delay 2000: exit
        end if
      end if
    wend
wend

jj2007

  • Guest
Re: Two Tables Game
« Reply #2 on: January 16, 2016, 10:59:06 PM »
I see a table with 5-digit numbers and the line "Tap top table # missing in lower". CPU is at 100%. What is it supposed to do?

B+

  • Guest
Re: Two Tables Game
« Reply #3 on: January 16, 2016, 11:55:18 PM »
I see a table with 5-digit numbers and the line "Tap top table # missing in lower". CPU is at 100%. What is it supposed to do?

Hi JJ,

It is a little mental exercise to get your brain working in morning. Find the number in all those rows and columns in the top table that is not in the bottom table. There is a trick to it after you practice awhile you get very fast.

The CPU usage is the problem of waiting for a mouse click. Wait... I have an idea, throw in some delays in the while loops.

I am seeing 15% CPU with delays in While loops (this is same as no program running at all in SmallBASIC), yes it was much worse without delays!
Code: [Select]
'Two Tables.txt Game for SmallBasic 012.2 by [B+=MGA] 2016-01-15
'2016-01-16 fix logic bug to assure all numbers will be unique
'2016-01-16 fix with cols and rows for landscape or portrait screens
'2016-01-16 remove need for keyboard, use "buttons" to quit or play again

'setup for alternate screens based on text height and width, about 100 items in tables
th=txth("By")
tw=txtw("8")
if tw*7*10>xmax then cols=int(xmax/(tw*7)):rows=int(100/cols) else cols=10:rows=10
items=rows*cols
prow=(2*rows+2)*th
qrow=prow+2*th
dim a(items),b(items)
while 1
    'init tables a and b and display
    cls
    pen on
    a(0)=10000
    for i=1 to items 'save time and presort
        a(i)=a(i-1)+int(rnd*1000)+1
        print "  ";a(i);
        if i mod cols=0 then print
    next
    rwin=int(rnd*items) +1
    if rwin mod cols=0 then xwin=(cols-1)*tw*7 else xwin=((rwin mod cols)-1)*tw*7
    ywin=int((rwin-1)/cols)*th
    win=a(rwin)
    print
    adj=0
    for i=1 to (items-1)
        if a(i)=win then adj=1
        b(i)=a((i+adj))
        print "  ";b(i);
        if i mod cols=0 then print
    next
    b(items)=a(items)+int(rnd*1000)+1
    print "  ";b(items)
    print
    'pose the question then start timing
    t=ticks
    at 0,prow:print "  Tap top table # missing in lower"
    while 1
      if pen(3) then xguess=pen(4):yguess=pen(5):delay 10:exit
      delay 100
    wend
    xright=0:yright=0
    if xguess>xwin and xguess<xwin+7*tw then xright=1
    if yguess>ywin and yguess<ywin+th then yright=1
    at 0,prow:Print space(40):at 0,prow
    if xright and yright then print "  Right! in "; else print "  ";win;", sorry, in ";
    print int((ticks-t)/1000);" sec."
    print "   A short pause then prompt coming..."
    delay 8000
    cls
    rect .125*xmax,.375*ymax,.375*xmax,.625*ymax,10
    rect .625*xmax,.375*ymax,.875*xmax,.625*ymax,12
    at .25*xmax-5*tw,ymax/2:? "PLAY AGAIN"
    at .75*xmax-2*tw,ymax/2:? "QUIT"
    mx=0
    while 1
      if pen(3) then
        mx=pen(4):pen off
        if mx>xmax/2 then
          end
        else
          at (xmax-20*tw)/2,.9*ymax:? "One moment please...":delay 2000: exit
        end if
      end if
      delay 100
    wend
wend
« Last Edit: January 17, 2016, 12:22:20 AM by B+ »

B+

  • Guest
Re: Two Tables Game
« Reply #4 on: January 17, 2016, 03:58:13 PM »
Hello,

I know when you first look at this so called game, your mind will balk at the task. It will say "Pointless!" or "That is too much work." or "Look at that damn CPU usage! My God!" :D Yeah, with that attitude, you won't find the game/mental exercise aspect. But notice the resistance before you actually try the thing out! It is probably enough that you won't try the thing out. BTW, this is so simple you can code it in your dialect in what? 20 minutes tops? (Another mental exercise, which BTW is main reason I like coding. And also, I will guarantee: Your code will be better than mine because you will add your own refinements to it.)

Balking is what my mind did when I looked at my Lychrel numbers table for 50 round checks and then looked at very same table except one number missing after running Tomaaz suggestion of 500 round checks. I actually spent maybe half a day writing a program to find the difference because I balked at just finding the one number missing from 100 in top table compared to lower table. (I encountered some unexpected snags, should have taken .5 hour I think, but practice is good and snags become teachers if you can figure your way through them.) While posting results from Tomaaz discovery on JB, I had idea that this might make a game, so I set it up. (That did take about half hour, in between testing it.)

After playing game for awhile while testing code, I discovered that there is a trick and you can find the missing number on average in less than a minute +/- maybe 30 secs consistently. The trick is stay with it until you learn the trick, then I think you might be pleased with yourself for playing the game. Maybe not, but I do believe it is a nice mental exercise involving the difference between looking and seeing.

When you pickup on that trick, you will be ready for Level 2 (already posted in JB forum).