Author Topic: Yabasic code collection  (Read 7621 times)

Galileo

  • Guest
Yabasic code collection
« on: February 13, 2016, 04:06:18 PM »
Greetings to all. I'm glad I found this forum after losing the former (whose loss I regretted very much, because there were lots of code). So, and that will not get lost, I uploaded to my website some of the programs written in Yabasic have liked. I leave here the link if anyone could find interesting.

http://galileano.tripod.com/basic/Programas.htm

Enjoy it.

(Google translation Spanish to English).
« Last Edit: January 02, 2017, 08:58:32 AM by Galileo »

B+

  • Guest
Re: Yabasic code collection
« Reply #1 on: February 13, 2016, 07:32:13 PM »
Nice List! Thanks

dragomrak

  • Guest
Re: Yabasic code collection
« Reply #2 on: February 15, 2016, 03:11:09 PM »
And so large!

B+

  • Guest
Re: Yabasic code collection
« Reply #3 on: February 18, 2016, 04:15:24 AM »
I tried translating Galileo's Mastermind into SmallBASIC (without translating to English) ran out of fun.

Then Johnno posted Bulls and Cows at SdlBasic and it was almost same thing as Galileo's (I think) but Galileo didn't want duplicate numbers in the guess number nor did he want the number to start with 0 so I modified the SdlBasic version to Galileo's specs that dragomrak was asking for also and this is result in SmallBASIC after translating from SdlBasic the working game:

Code: [Select]
'bulls and cows.bas for SmallBASIC 0.12.0 [B+=MGA] 2016-02-17 translated from:
'bulls and cows from johnno.sdlbas copied 2016-02-15 [B+=MGA] when it was
'requested no duplicate numbers. OK, try that by treating digits like deck of cards shuffle and draw 4
'
'    Converted from BBC Basic (Johnno to SdlBasic)
'
' http://www.rosettacode.org/wiki/Bulls_and_cows#BASIC
'
'===================================================== setup
randomize(timer)
'while 1                         'test output of shuffle
dim deck(9)
for i=0 to 9   'treat the 10 digits like a deck of cards, can only deal one of each kind from deck
    deck(i)=str(i)
next
for i=9 to 1 step -1  'this is Knuth shuffle AKA Fisher-Yates method
    r=int(rnd*(i+1))
    swap deck(i),deck(r)
next
if deck(0)="0" then   'Galileo did not want a number that started with 0
    secret=deck(1)+deck(2)+deck(3)+deck(4)
else
    secret=deck(0)+deck(1)+deck(2)+deck(3) '<==just use this if you dont care if first is zero or not
end if
? secret                       'test and debug
'input "press enter... ";t    'test
'wend                           'test
'====================================================== game
? "Guess a four-digit number (no repeating digits)."
guesses=0
while 1
  repeat
    OK=0
    input "(0 quits) Enter your guess: ";guess
    if guess=0 then ? "Goodbye":end
    if len(guess) <> 4 then
      ? "Must be a four-digit number":?
    else
      OK=1 'galileo had a check to see if a duplicate number was in the guess
      for i=1 to 3
        for j=i+1 to 4
          if mid(guess,i,1)=mid(guess,j,1) then
            ? "Digits must not repeat in guess.":?
            OK=0:exit for
          end if 
        next
        if OK=0 then exit for
      next
    end if
  until OK
  guesses +=1
  if guess=secret then
    ? "You won after "+str(guesses)+" guesses!"
    delay 3000:end
  end if
  bulls=0
  cows=0
  for i=1 to 4
    c=mid(secret,i,1)
    if mid(guess,i,1)=c then
      bulls +=1
    elif instr(guess,c)
      cows +=1
    end if
  next
  ?"You got "+str(bulls)+" bull(s) and "+str(cows)+ " cow(s).":?
wend

Apparently this thing is ancient! But a nice little logic puzzle made easier with no duplicate numbers in guess number.

Coming from Rosetta it is already translated into other PL's, too bad, the fun part is getting it to work in your dialect while learning the game.

Hey Tomaaz, no dots, lines, rectangles or circles in this one, maybe you will like it! (Maybe you've seen it already years ago.)
« Last Edit: February 18, 2016, 04:17:01 AM by B+ »

ScriptBasic

  • Guest
Re: Yabasic code collection
« Reply #4 on: February 18, 2016, 09:10:42 AM »
Quote
the fun part is getting it to work in your dialect while learning the game.

I think I got it converted correctly to Script BASIC.

Code: [Select]
' bulls and cows.bas for Script BASIC - JRS 2016-02-18
' bulls and cows.bas for SmallBASIC 0.12.0 [B+=MGA] 2016-02-17 translated from:
' bulls and cows from johnno.sdlbas copied 2016-02-15 [B+=MGA] when it was
' requested no duplicate numbers. OK, try that by treating digits like deck of cards shuffle and draw 4
'
' Converted from BBC Basic (Johnno to SdlBasic)
'
' http://www.rosettacode.org/wiki/Bulls_and_cows#BASIC
'
' ===================================================== setup

RANDOMIZE(NOW)

FOR I=0 TO 9
  deck[i] = i
NEXT
FOR i = 9 TO 1 STEP -1
  r = RND % 9
  SWAP deck[i], deck[r]
NEXT
IF deck[0] = "0" THEN
  secret = deck[1] & deck[2] & deck[3] & deck[4]
ELSE
  secret = deck[0] & deck[1] & deck[2] & deck[3]
END IF
PRINT "Secret is: ",secret,"\n"

PRINT "Guess a four-digit number (no repeating digits).\n"
guesses = 0
WHILE 1
  REPEAT
    OK = 0
    PRINT "(0 quits) Enter your guess: "
    LINE INPUT guess
    guess = CHOMP(guess)
    IF guess = 0 THEN
      PRINT "Goodbye\n"
      END
    END IF
    IF LEN(guess) <> 4 THEN
      PRINT "Must be a four-digit number\n"
    ELSE
      OK = 1
      FOR i = 1 TO 3
        FOR j = i + 1 TO 4
          IF MID(guess, i, 1) = MID(guess, j, 1) THEN
            PRINT "Digits must not repeat in guess.\n"
            OK = 0
            GOTO Exit_For_j
          END IF
        NEXT
        Exit_For_j:
        IF OK = 0 THEN
         GOTO Exit_For_i
        END IF
      NEXT
      Exit_For_i:
    END IF
  UNTIL OK
  guesses += 1
  IF guess = secret THEN
    PRINT "You won after "& guesses & " guesses!\n"
    END
  END IF
  bulls = 0
  cows = 0
  FOR i = 1 to 4
    c = MID(secret, i, 1)
    IF MID(guess, i, 1) = c THEN
      bulls += 1
    ELSE IF INSTR(guess, c) = undef THEN
      cows += 1
    END IF
  NEXT
  PRINT "You got " & bulls & " bull(s) and " & cows & " cow(s).\n"
WEND


jrs@laptop:~/sb/sb22/test$ scriba bc.sb
Secret is: 9254
Guess a four-digit number (no repeating digits).
(0 quits) Enter your guess: 9154
You got 3 bull(s) and 1 cow(s).
(0 quits) Enter your guess: 9014
You got 2 bull(s) and 2 cow(s).
(0 quits) Enter your guess: 9999
Digits must not repeat in guess.
(0 quits) Enter your guess: 9254
You won after 3 guesses!
jrs@laptop:~/sb/sb22/test$

« Last Edit: February 18, 2016, 11:43:05 AM by John »

ZXDunny

  • Guest
Re: Yabasic code collection
« Reply #5 on: February 18, 2016, 11:53:06 AM »
Here's my effort in SpecBAS:

Code: [Select]
10 REM Bulls + Cows
20 gs=1: n$="0123456789",s$="":
   FOR f=1 TO 4:
      e=INT(RND*LEN n$)+1,s$=s$+n$(e),n$=n$( TO e-1)+n$(e+1 TO):
   NEXT f:
   IF s$(1)="0" THEN
      s$=s$(2 TO)+s$(1)
30 PRINT "Guess a four-digit number (no repeating digits)."
40 INPUT "(0 quits) Enter your guess: "; FORMAT "####";g:
   g$=STR$(g),g$="0000"( TO 4-LEN g$)+g$:
   IF g=0 THEN
      PRINT "Goodbye!": STOP
   ELSE
      IF LEN STR$ g<>4 THEN
         PRINT "Must be a four-digit number.": GO TO 40
      ELSE
         IF g$=s$ THEN
            PRINT "You won after ";gs;IIF$(gs=1," guess!"," guesses!"): STOP
50 b=0,c=0,n$="0123456789":
   FOR f=1 TO 4:
      e=POS(g$(f),n$):
      IF e<1 THEN
         PRINT "Digits must not repeat in guess.": GO TO 40
      ELSE
         n$=n$( TO e-1)+n$(e+1 TO)
60    b+=(s$(f)=g$(f)),c+=(s$(f)<>g$(f)):
   NEXT f:
   PRINT "You got ";b;" bulls and ";c;" cows.":
   GO TO 40

D.

ScriptBasic

  • Guest
Re: Yabasic code collection
« Reply #6 on: February 18, 2016, 12:53:36 PM »
Cheating mode version.  8)

Code: [Select]
jrs@laptop:~/sb/sb22/test$ scriba dbgcon.sb bc.sb
Application: ScriptBasic Remote Debugger - Linux
Version: 1.0
Source-File-Count: 1
Source-File: bc.sb
Line: 12
-> l1-
 [0001] ' bulls and cows.bas for Script BASIC - JRS 2016-02-18
 [0002] ' bulls and cows.bas for SmallBASIC 0.12.0 [B+=MGA] 2016-02-17 translated from:
 [0003] ' bulls and cows from johnno.sdlbas copied 2016-02-15 [B+=MGA] when it was
 [0004] ' requested no duplicate numbers. OK, try that by treating digits like deck of cards shuffle and draw 4
 [0005] '
 [0006] ' Converted from BBC Basic (Johnno to SdlBasic)
 [0007] '
 [0008] ' http://www.rosettacode.org/wiki/Bulls_and_cows#BASIC
 [0009] '
 [0010] ' ===================================================== setup
 [0011]
 [0012] RANDOMIZE(NOW)
 [0013]
 [0014] ' SPLITA STRING(10,"0") BY "" TO deck
 [0015]
 [0016] FOR I=0 TO 9
 [0017]   deck[i] = i
 [0018] NEXT
 [0019] FOR i = 9 TO 1 STEP -1
 [0020]   r = RND % 9
 [0021]   SWAP deck[i], deck[r]
 [0022] NEXT
 [0023] IF deck[0] = "0" THEN
 [0024]   secret = deck[1] & deck[2] & deck[3] & deck[4]
 [0025] ELSE
 [0026]   secret = deck[0] & deck[1] & deck[2] & deck[3]
 [0027] END IF
 [0028]
 [0029] PRINT "Guess a four-digit number (no repeating digits).\n"
 [0030] guesses = 0
 [0031] WHILE 1
 [0032]   REPEAT
 [0033]     OK = 0
 [0034]     PRINT "(0 quits) Enter your guess: "
 [0035]     LINE INPUT guess
 [0036]     guess = CHOMP(guess)
 [0037]     IF guess = 0 THEN
 [0038]       PRINT "Goodbye\n"
 [0039]       END
 [0040]     END IF
 [0041]     IF LEN(guess) <> 4 THEN
 [0042]       PRINT "Must be a four-digit number\n"
 [0043]     ELSE
 [0044]       OK = 1
 [0045]       FOR i = 1 TO 3
 [0046]         FOR j = i + 1 TO 4
 [0047]           IF MID(guess, i, 1) = MID(guess, j, 1) THEN
 [0048]             PRINT "Digits must not repeat in guess.\n"
 [0049]             OK = 0
 [0050]             GOTO Exit_For_j
 [0051]           END IF
 [0052]         NEXT
 [0053]         Exit_For_j:
 [0054]         IF OK = 0 THEN
 [0055]          GOTO Exit_For_i
 [0056]         END IF
 [0057]       NEXT
 [0058]       Exit_For_i:
 [0059]     END IF
 [0060]   UNTIL OK
 [0061]   guesses += 1
 [0062]   IF guess = secret THEN
 [0063]     PRINT "You won after "& guesses & " guesses!\n"
 [0064]     END
 [0065]   END IF
 [0066]   bulls = 0
 [0067]   cows = 0
 [0068]   FOR i = 1 to 4
 [0069]     c = MID(secret, i, 1)
 [0070]     IF MID(guess, i, 1) = c THEN
 [0071]       bulls += 1
 [0072]     ELSE IF INSTR(guess, c) = undef THEN
 [0073]       cows += 1
 [0074]     END IF
 [0075]   NEXT
 [0076]   PRINT "You got " & bulls & " bull(s) and " & cows & " cow(s).\n"
 [0077] WEND
Line: 12
-> r27
Line: 27
-> ?secret
Value: "1634"
Line: 27
-> r
Guess a four-digit number (no repeating digits).
(0 quits) Enter your guess: 1634
You won after 1 guesses!
Debug session closed.
jrs@laptop:~/sb/sb22/test$
« Last Edit: February 18, 2016, 07:20:50 PM by John »

B+

  • Guest
Re: Yabasic code collection
« Reply #7 on: February 18, 2016, 05:39:17 PM »
This is interesting:

Quote
[0016] FOR I=0 TO 9
 [0017]   deck = i
 [0018] NEXT
 [0019] FOR i = 9 TO 1 STEP -1
 [0020]   r = RND % 9
 [0021]   SWAP deck, deck[r]
 [0022] NEXT

ScriptBasic

  • Guest
Re: Yabasic code collection
« Reply #8 on: February 18, 2016, 06:54:35 PM »
Looks like the TT display was causing the missing BASIC code. Using the code block seems to work.


B+

  • Guest
Re: Yabasic code collection
« Reply #9 on: February 18, 2016, 08:05:50 PM »
Hi John,

Line 20 still interesting,

SmallBASIC is always returning 0 for RND % 9, so your RND has extra powers,

also I don't see r ever being 9, which makes for a different kind of shuffle.

ScriptBasic

  • Guest
Re: Yabasic code collection
« Reply #10 on: February 18, 2016, 08:38:15 PM »
Hi John,

Line 20 still interesting,

SmallBASIC is always returning 0 for RND % 9, so your RND has extra powers,

also I don't see r ever being 9, which makes for a different kind of shuffle.

I'm still discovering Script BASIC's magic powers managing the project for over 10 years. For example the SB docs said SWAP was a TODO item but it seems to work.

If you look at the first example (non-debug) a 9 is returned in the secret string. NOW only has one second resolution so not the best seeder.
« Last Edit: February 18, 2016, 09:08:49 PM by John »

B+

  • Guest
Re: Yabasic code collection
« Reply #11 on: February 18, 2016, 09:31:36 PM »
On reflection, I see my secret number choosing method is defective with 10% chance that 0 will not be in secret number.

Back to drawing board.

B+

  • Guest
Re: Yabasic code collection
« Reply #12 on: February 18, 2016, 10:55:36 PM »
OK?

Code: [Select]
'bulls and cows.bas for SmallBASIC 0.12.0 [B+=MGA] 2016-02-18 translated from:
'2016-02-18 repair secret number maker
'bulls and cows from johnno.sdlbas copied 2016-02-15 [B+=MGA] when it was
'requested no duplicate numbers. OK, try that by treating digits like deck of cards shuffle and draw 4
'
'    Converted from BBC Basic (Johnno to SdlBasic)
'
' http://www.rosettacode.org/wiki/Bulls_and_cows#BASIC
'
'===================================================== setup
randomize(timer)
'while 1     'test output of shuffle
deck="123456789"
r=int(rnd*9)+1
secret=mid(deck,r,1)
if r<9 then
  deck=mid(deck,1,r-1)+mid(deck,r+1)+"0"
else
  deck=mid(deck,1,r-1)+"0"
end if
for i=1 to 3
  r=int(rnd*len(deck))+1
  secret=secret+mid(deck,r,1)
  if r<len(deck) then
    deck=mid(deck,1,r-1)+mid(deck,r+1)
  else
    deck=mid(deck,1,r-1)
  end if
next
'? secret                       'test and debug
'input "press enter... ";t      'test
'wend                           'test
'====================================================== game
? "Guess a four-digit number (no repeating digits)."
guesses=0
while 1
  repeat
    OK=0
    input "(0 quits) Enter your guess: ";guess
    if guess=0 then ? "Goodbye":end
    if len(guess) <> 4 then
      ? "Must be a four-digit number":?
    else
      OK=1 'galileo had a check to see if a duplicate number was in the guess
      for i=1 to 3
        for j=i+1 to 4
          if mid(guess,i,1)=mid(guess,j,1) then
            ? "Digits must not repeat in guess.":?
            OK=0:exit for
          end if 
        next
        if OK=0 then exit for
      next
    end if
  until OK
  guesses +=1
  if guess=secret then
    ? "You won after "+str(guesses)+" guesses!"
    delay 3000:end
  end if
  bulls=0
  cows=0
  for i=1 to 4
    c=mid(secret,i,1)
    if mid(guess,i,1)=c then
      bulls +=1
    elif instr(guess,c)
      cows +=1
    end if
  next
  ?"You got "+str(bulls)+" bull(s) and "+str(cows)+ " cow(s).":?
wend

Galileo

  • Guest
Re: Yabasic code collection
« Reply #13 on: January 01, 2017, 04:32:49 PM »
First of all, wish everyone good luck in the new year that now begins.

Second, announce that I have created a single compressed file with all the programs I have wanted to share with the community. The link is the same as I indicated in my first post. This file will be updated periodically with new programs.

Greetings.

B+

  • Guest
Re: Yabasic code collection
« Reply #14 on: January 01, 2017, 04:59:45 PM »
Hi Galileo, we seem to be required to sign up for something from your link in original post.
« Last Edit: January 01, 2017, 05:01:26 PM by B+ »