6STRIKES: Text mode word guessing game

Post Reply
User avatar
Dav
Posts: 279
Joined: Tue Dec 30, 2014 5:12 pm
My devices: iPad Mini, iPod Touch.
Location: North Carolina, USA
Contact:

6STRIKES: Text mode word guessing game

Post by Dav »

This is something I did today just to get used to some of smart Basic commands. It's an old school word guessing game sort of like hangman. Text mode only, no graphics, like an old Qbasic game. I'm not a game maker, but had fun making this. Works on my iPad Mini.

- Dav

EDIT: Try Dutchmans version also, it is improved.

Code: Select all

'==================================================
'6STRIKES.TXT - A Text Mode Word Guessing Game
'               By Dav, for Smart BASIC, JAN/2015
'==================================================
'
'ABOUT: This is an old school text mode game from
'       my Qbasic coding days.  I wanted to see 
'       how well it could be ported to Smart BASIC.
'       I also wanted to get familiar with some
'       of smart BASIC's commands.  So this little
'       game sort of got my feet wet there.
'
'PLAY:  The game is like Hangman - try to guess
'       the words by clicking on the letters.
'       Pick them all, solve the word.
'       If you pick a wrong letter, it's a strike.
'       Get 6 strikes and the game is over.

'       You could add GRAPHICS & MUSIC and make a
'       real game out of it if you wanted.  Enjoy.
'
'NOTE:  I've only tested this on my iPad Mini.
'
'       You can add more words to the data state-
'       ments at the bottom, but be sure to change
'       the 'totalwords' variable to match them.
'=================================================

init:

  set orientation top
  set output font color 1,1,1
  set output back color 0,128/255,128/255
  set output font size "56"
  set buttons font name "Verdana"
  set buttons font size 56

  '=== load words
  totalwords=15          '<< Total number of words in DATA
  dim word$(totalwords+1)
  for g=1 to totalwords
     read word$(g)
  next g

'==============================================

intro:

  text clear
  set output font color 0,0,0   'Turn screen/text black
  set output back color 0,0,0

  print
  print
  print "       6 STRIKES!"
  print "         By Dav"
  print 
  print "     Guess the word(s)"
  print "   Before Striking out!"
  print 
  print
  print "  High Score today:"; highscore
  print

  '=== fade in screen/text  from black
  for f = 0 to 128
     c=f/255
     set output back color 0,c,c
     set output font color c,c,c
     pause .005
  next f

  '=== continue fading in text...
  for f=129 to 255
     c= f/255
     set output font color c,c,c
     pause .005
  next f

  '=== Put a PLAY button to start game
  button "play" title " Play " at 280,700
  do 
     set output font color 0,1,(rnd(128)+120)/255
  until button_pressed("play")
  button "play" delete

  wordnum=1   'Start at word # 1

'=============================================

newword:

  text clear                    'Clear text
  set output font color 1,1,1   'Set text color
  call drawkeys(1)              'Redraw all letter
  strike=0                      'Reset strike counter

  '=== Build a guess$ word to use (ie: --- ----)
  '=== It's Based on word$() length
  r$=""
  for e = 0 to len(word$(wordnum))-1
     n$ = mid$(word$(wordnum),e,1)
     if n$ <> " " then
        r$ = r$ & "-"
     else
        r$ = r$ & " "
     end if
  next e
  guess$=r$

'=============================================

mainloop:

  text clear
  print " Word:"; wordnum; "   Score =";score
  print
  print 
  print "       "; guess$

  gosub drawstrikes

'=============================================

getaletter:

  for i = 65 to 90
     if button_pressed(chr$(i)) then
        button chr$(i) delete
		
        '=== Replace (-) in guess$ with letter
        g2$=""
        for e = 0 to len(word$(wordnum))-1
           nib$ = mid$(word$(wordnum),e,1)
           if nib$ = chr$(i) then
              g2$ = g2$ & chr$(i)
              found=1
           else
              g2$ = g2$ & mid$(guess$, e, 1)
           end if
        next e
        guess$=g2$   

        '=== If all letters guessed...
        if guess$=word$(wordnum) then
           gosub gotit
           wordnum=wordnum+ 1
           if wordnum>totalwords then goto wongame
           goto newword
        end if
        if found<>1 then strike=strike+1
        if strike=6 then goto lose
        found=0
        goto mainloop
		
     end if
  next i
  
  if button_pressed("quit") then goto theend
  
goto getaletter

'=============================================

gotit:

  text clear
  score =score+ len(word$(wordnum))
  print " Word:"; wordnum; "   Score =";score
  print
  print "       You got it!"
  print
  print "     ";word$(wordnum)
  print
  print "      Next word...."
  gosub flashfont
  return

'=============================================

lose:

  text clear
  print
  print
  print "     YOU STRUCK OUT!"
  print
  print "     The word was..."
  print
  print "      "; word$(wordnum)
  print
  gosub flashfont

'=============================================

theend:

  if score> highscore then highscore = score
  text clear
  call drawkeys(0)
  goto intro

'=============================================

wongame:

  call drawkeys(0)
  set output back color 0,25/255,125/255
  text clear

  for i= 1 to 100
    print
  next i
  print "     CONGRATULATIONS!"
  for u = 1 to 8
     print ! pause .15
  next u
  for g= 1 to 20
     set output font color rnd(255)/255,1, rnd(255)/255
     pause .1
  next g
  print "       YOU WON!!!"
  for u = 1 to 8
     print ! pause .15
  next u

  for g= 1 to 20
     set output font color rnd(255)/255,1, rnd(255)/255
     pause .1
  next g

  goto theend


'=====================================================
' FUNCTIONS / GOSUBS / DATA STATEMENTS ........
'=====================================================

drawstrikes:   'Draw the boxes with the X's

  u$=chr$(175)
  print ! print " ___ ___ ___ ___ ___ ___"
  if strike = 0 then print " | | | | | | | | | | | |"
  if strike = 1 then print " |X| | | | | | | | | | |"
  if strike = 2 then print " |X| |X| | | | | | | | |"
  if strike = 3 then print " |X| |X| |X| | | | | | |"
  if strike = 4 then print " |X| |X| |X| |X| | | | |"
  if strike = 5 then print " |X| |X| |X| |X| |X| | |"
  if strike = 6 then print " |X| |X| |X| |X| |X| |X|"

  for k = 1 to 6
     print " "; u$&u$&u$;
  next k

return

'====================================================

flashfont:   'Flashes the fonts on the screen

  for m=0 to 500
     set output font color 0,1,(rnd(128)+120)/255
     pause .005
  next m

return

'====================================================

def drawkeys(way)

  'Draws or Deletes Buttons, based on 'way'
  
  set buttons font name "Verdana"
  set buttons font size 56

  l=65  'Do all Letters A to Z (Ascii..)
  for y = 0 to 300 step 100
     for t = 30 to 700 step 100
        if l < 91 then 
           if way =1 then
              button chr$(l) title chr$(l) at t, 570+y 
           else
              button chr$(l) delete
           end if
        end if
        l=l+1
     next t
  next y
  
  if way = 1 then
    button "quit" title "QUIT" at 530,870
  else
    button "quit" delete
  end if

end def

'===================================================

data "SMART BASIC","VANILA BEAN","PROGRAMMING"
data "DECEMBER", "HAMBURGER", "OLYMPICS"
data "COMPUTER", "RHYTHM", "GORILLA"
data "SCIENTIFIC","VOLCANO", "HISTORY"
data "LANGUAGE", "WINDOWS", "MACINTOSH"
Attachments
6strikes-screenshot.jpg
6strikes-screenshot.jpg (49.42 KiB) Viewed 3433 times
Last edited by Dav on Sun Jan 11, 2015 8:34 pm, edited 1 time in total.

User avatar
Dutchman
Posts: 851
Joined: Mon May 06, 2013 9:21 am
My devices: iMac, iPad Air, iPhone
Location: Netherlands
Flag: Netherlands

Re: 6STRIKES: Text mode word guessing game

Post by Dutchman »

Nice game. Clever use of text mode.
I made some modifications. I hope you don't mond :D
- For me the game was upside down. So I added some code after 'Init:'
- I added OPTION BASE 1. That makes it easier to use.
- The words are now chosen randomly
- The list of words can now be extended easily. Add words, but keep "#" at the end.
- I added 'maxwords' which determines the number of words to guess. It is increased after each won game.
It is much easier to make modifications if you rely on functions and subroutines rather than GOTO commands to determine the program flow. ;)

Code: Select all

'==================================================
'6STRIKES.TXT - A Text Mode Word Guessing Game
'               By Dav, for Smart BASIC, JAN/2015
'               mod. by Dutchman
'==================================================
'
'ABOUT: This is an old school text mode game from
'       my Qbasic coding days.  I wanted to see 
'       how well it could be ported to Smart BASIC.
'       I also wanted to get familiar with some
'       of smart BASIC's commands.  So this little
'       game sort of got my feet wet there.
'
'PLAY:  The game is like Hangman - try to guess
'       the words by clicking on the letters.
'       Pick them all, solve the word.
'       If you pick a wrong letter, it's a strike.
'       Get 6 strikes and the game is over.

'       You could add GRAPHICS & MUSIC and make a
'       real game out of it if you wanted.  Enjoy.
'
'NOTE:  I've only tested this on my iPad Mini.
'
'       You can add more words to the data state-
'       ments at the bottom, but be sure to keep 
'       "#" at the end.
'=================================================
init:
  GET ORIENTATION p
  IF p=3 THEN SET ORIENTATION 3 ELSE SET ORIENTATION 1
  RANDOMIZE
  set output font color 1,1,1
  set output back color 0,128/255,128/255
  set output font size "56"
  set buttons font name "Verdana"
  set buttons font size 56
  OPTION BASE 1
  GOSUB LoadWords
'
'=========== S T A R T   G A M E ==============
' set initial number of words to guess
' maxwords will increase if words are guessed
  maxwords=1
Again:
  GOSUB Intro 
  zerofade=128 ' fade only at start
  GOSUB newword
mainloop:
  GOSUB InitScreen
  DO
    for i = 65 to 90
      found=0
      if button_pressed(chr$(i)) then GOSUB Newstate
      IF found THEN GoSub InitScreen
    '=== If all letters guessed...
      if guess$=word$(wordnum,1) then
        gosub gotit
        IF wordcount=maxwords AND wordcount<6 then
          maxwords+=1  ! GOTO theend
        ELSE 
          IF wordcount=6 THEN 
            GOSUB Wongame
             goto theend
          ENDIF
        ENDIF
        gosub newword
        gosub initscreen
      end if
      if strike=6 then 
        gosub lose
        GOTO theend
      ENDIF
  next i
  UNTIL button_pressed("quit")
  finish=1
TheEnd:
  if score> highscore then highscore = score
  text clear
  call drawkeys(0)
  IF finish=0 AND wordcount<maxwords THEN again
  TEXT CLEAR
END

'=====================================================
' FUNCTIONS / SUBROUTINES / DATA
'=====================================================
lose:
  text clear
  print
  print
  print "     YOU STRUCK OUT!"
  print
  print "     The word was..."
  print
  print "      "; word$(wordnum,1)
  print
  gosub flashfont
RETURN
'
Wongame:
  IF maxwords<totalwords THEN maxwords+=1
  call drawkeys(0)
  set output back color 0,25/255,125/255
  text clear
  for i= 1 to 100
    print
  next i
  print "     CONGRATULATIONS!"
  for u = 1 to 8
     print ! pause .15
  next u
  for g= 1 to 20
     set output font color rnd(255)/255,1, rnd(255)/255
     pause .1
  next g
  print "       YOU WON!!!"
  for u = 1 to 8
     print ! pause .15
  next u
  for g= 1 to 20
     set output font color rnd(255)/255,1, rnd(255)/255
     pause .1
  next g
RETURN
'
gotit:
  text clear
  score =score+1
  print " Word:"; wordcount; "   Score =";score
  print
  print "       You got it!"
  print
  print "     ";word$(wordnum,1)
  print
  print "      Next word...."
  gosub flashfont
return
'
flashfont:   'Flashes the fonts on the screen
  for m=0 to 50
     set output font color 0,1,(rnd(128)+120)/255
     pause .05
  next m
return
'
def drawkeys(way)
  'Draws or Deletes Buttons, based on 'way'
  set buttons font name "Verdana"
  set buttons font size 56
  l=65  'Do all Letters A to Z (Ascii..)
  for y = 0 to 300 step 100
     for t = 30 to 700 step 100
        if l < 91 then 
           if way =1 then
              button chr$(l) title chr$(l) at t, 570+y 
           else
              button chr$(l) delete
           end if
        end if
        l=l+1
     next t
  next y
  if way = 1 then
    button "quit" title "QUIT" at 530,870
  else
    button "quit" delete
  end if
end def
'
LoadWords:
  RESTORE TO Words
  totalwords=-1'read one too much in total
  WHILE count$<>"#"
    READ count$
    totalwords+=1'<< Total number of words in DATA
  END WHILE
  RESTORE TO words
  dim word$(totalwords,2)'name,usage
  for g=1 to totalwords
     read word$(g,1)
  next g
RETURN
'
intro:
  ' reset initial state
  FOR i=1 TO totalwords
    word$(i,2)=""
  NEXT i
  wordcount=0 ' number of words used
  text clear
  set output font color 0,0,0   'Turn screen/text black
  set output back color 0,0,0
  print
  print
  IF maxwords=1 THEN
    print "       6 STRIKES!"
    print "         By Dav"
  ELSE
    print "   Congratulations!"
    PRINT "     next level"
  ENDIF
  print 
  print "     Guess";maxwords;"word(s)"
  print "   Before Striking out!"
  print 
  print
  print "  High Score today:"; highscore
  print
  '=== fade in screen/text  from black
  for f = zerofade to 128
     c=f/255
     set output back color 0,c,c
     set output font color c,c,c
     pause .005
  next f
  '=== continue fading in text...
  for f=129 to 255
     c= f/255
     set output font color c,c,c
     pause .005
  next f
  '=== Put a PLAY button to start game
  button "play" title " Play " at 280,700
  do 
     set output font color 0,1,(rnd(128)+120)/255
  until button_pressed("play")
  button "play" delete
RETURN
'
newword:
  text clear                    'Clear text
  set output font color 1,1,1   'Set text color
  call drawkeys(1)              'Redraw all letter
  strike=0                      'Reset strike counter
  '=== pick non-used word
    pick=FLOOR(RND(totalwords-wordcount))+1
    empty=0 ! wordnum=0
    DO
      wordnum+=1
      IF word$(wordnum,2)="" THEN empty+=1
    UNTIL empty=pick
    wordcount+=1
    word$(wordnum,2)="#"
  '=== Build a guess$ word to use (ie: --- ----)
  '=== It's Based on word$() length
  r$=""
  for e = 1 to len(word$(wordnum,1))
     n$ = mid$(word$(wordnum,1),e,1)
     if n$ <> " " then
        r$ = r$ & "-"
     else
        r$ = r$ & " "
     end if
  next e
  guess$=r$
RETURN
'
Newstate:
button chr$(i) delete      
'=== Replace (-) in guess$ with letter
g2$=""
for e = 1 to len(word$(wordnum,1))
  nib$ = mid$(word$(wordnum,1),e,1)
  if nib$ = chr$(i) then
    g2$ = g2$ & chr$(i)
    found=1
  else
    g2$ = g2$ & mid$(guess$, e, 1)
  end if
next e
IF NOT found THEN strike+=1
guess$=g2$   
RETURN
'
InitScreen:
  text clear
  print " Word:"; wordcount; "   Score =";score
  print
  print 
  print "       "; guess$
  u$=chr$(175)
  print ! print " ___ ___ ___ ___ ___ ___"
  if strike = 0 then print " | | | | | | | | | | | |"
  if strike = 1 then print " |X| | | | | | | | | | |"
  if strike = 2 then print " |X| |X| | | | | | | | |"
  if strike = 3 then print " |X| |X| |X| | | | | | |"
  if strike = 4 then print " |X| |X| |X| |X| | | | |"
  if strike = 5 then print " |X| |X| |X| |X| |X| | |"
  if strike = 6 then print " |X| |X| |X| |X| |X| |X|"

  for k = 1 to 6
     print " "; u$&u$&u$;
  next k
RETURN
'
Words:
data "SMART BASIC","VANILA BEAN","PROGRAMMING"
data "DECEMBER", "HAMBURGER", "OLYMPICS"
data "COMPUTER", "RHYTHM", "GORILLA"
data "SCIENTIFIC","VOLCANO", "HISTORY"
data "LANGUAGE", "WINDOWS", "MACINTOSH"
data "WORMHOLE","#"'end symbol
Last edited by Dutchman on Sun Jan 11, 2015 4:36 pm, edited 1 time in total.

User avatar
Dutchman
Posts: 851
Joined: Mon May 06, 2013 9:21 am
My devices: iMac, iPad Air, iPhone
Location: Netherlands
Flag: Netherlands

Re: 6STRIKES: Text mode word guessing game

Post by Dutchman »

I changed the last part of the begintext, about adding words. I forgot that initially. :oops:

User avatar
Mr. Kibernetik
Site Admin
Posts: 4786
Joined: Mon Nov 19, 2012 10:16 pm
My devices: iPhone, iPad, MacBook
Location: Russia
Flag: Russia

Re: 6STRIKES: Text mode word guessing game

Post by Mr. Kibernetik »

Very interesting game! Thanks!

User avatar
Dutchman
Posts: 851
Joined: Mon May 06, 2013 9:21 am
My devices: iMac, iPad Air, iPhone
Location: Netherlands
Flag: Netherlands

Re: 6STRIKES: Text mode word guessing game

Post by Dutchman »

I have made some more modifications.
The program flow is now determined in the main loop. You'll find GOTO commands in the main loop only.
A kind of 'level' is added. The number of words to guess starts at 1 and increases if that is done.
If strikes=6 then the level restarts. The 'level' is determined by the variable 'maxwords'
This gives more parameters to calculate the score. I did not change that.
If 'maxwords' is set to 6 then the game ends as original.
TO DO: other languages :lol:

User avatar
Dav
Posts: 279
Joined: Tue Dec 30, 2014 5:12 pm
My devices: iPad Mini, iPod Touch.
Location: North Carolina, USA
Contact:

Re: 6STRIKES: Text mode word guessing game

Post by Dav »

Thanks Dutchman! I like your modifications. Much better. More like a real game now.

I see now getting screen orientation is very important, I will be aware of that in future programs. I agree, OPTION BASE 1 makes it easier for me too, I will use that from now on. The code looks nice and tidy now, I appreciate your contribution. :)

Does the font size look different on other iPads? Or does it scale to fit? (I'm kind of new to iPads).

Also, Thanks Mr. Kibernetik. I am having fun with smart Basic.

- Dav

User avatar
Dutchman
Posts: 851
Joined: Mon May 06, 2013 9:21 am
My devices: iMac, iPad Air, iPhone
Location: Netherlands
Flag: Netherlands

Re: 6STRIKES: Text mode word guessing game

Post by Dutchman »

On my iPad air the screen layout looks good. Well done :!:

User avatar
Mr. Kibernetik
Site Admin
Posts: 4786
Joined: Mon Nov 19, 2012 10:16 pm
My devices: iPhone, iPad, MacBook
Location: Russia
Flag: Russia

Re: 6STRIKES: Text mode word guessing game

Post by Mr. Kibernetik »

Dav wrote: I am having fun with smart Basic.
Very nice to hear that!

Post Reply