QB64.org Forum

Active Forums => Programs => Topic started by: bplus on November 08, 2020, 12:47:24 pm

Title: Guess the Animal
Post by: bplus on November 08, 2020, 12:47:24 pm
This is an interesting program and might be good exercise to convert it to more modern ways with QB64.

In 70 lines or so this little program starts learning.

Define the problem: wouldn't it be nice to save it's learning so you don't have to start over the next time you run the program? wouldn't it be nice if your answers didn't have to be in all capital letters? wouldn't it be nice to get a little practice using an array? Wouldn't it be nice to lose all the line numbers?

Here, I've taken TempodiBasic's find and divided the program into 4 or 5 sections with spaces to start making sense of what it is doing and how.

Code: QB64: [Select]
  1. 10 PRINT TAB(32); "ANIMAL"
  2. 20 PRINT TAB(15); "CREATIVE COMPUTING  MORRISTOWN, NEW JERSEY"
  3. 40 PRINT "PLAY 'GUESS THE ANIMAL'"
  4. 50 PRINT "THINK OF AN ANIMAL AND THE COMPUTER WILL TRY TO GUESS IT."
  5. 70 DIM A$(200)
  6. 80 FOR I = 0 TO 3
  7.    90 READ A$(I)
  8. 100 NEXT I
  9. 110 N = VAL(A$(0))
  10.  
  11. 120 REM          MAIN CONTROL SECTION
  12. 130 INPUT "ARE YOU THINKING OF AN ANIMAL"; A$
  13. 140 IF A$ = "LIST" THEN 600
  14. 150 IF LEFT$(A$, 1) <> "Y" THEN 120
  15. 160 K = 1
  16. 170 GOSUB 390
  17. 180 IF LEN(A$(K)) = 0 THEN 999
  18. 190 IF LEFT$(A$(K), 2) = "\Q" THEN 170
  19. 200 PRINT "IS IT A "; RIGHT$(A$(K), LEN(A$(K)) - 2);
  20. 210 INPUT A$
  21. 220 A$ = LEFT$(A$, 1)
  22. 230 IF LEFT$(A$, 1) = "Y" THEN PRINT "WHY NOT TRY ANOTHER ANIMAL?": GOTO 120
  23. 240 INPUT "THE ANIMAL YOU WERE THINKING OF WAS A "; V$
  24. 250 PRINT "PLEASE TYPE IN A QUESTION THAT WOULD DISTINGUISH A"
  25. 260 PRINT V$; " FROM A "; RIGHT$(A$(K), LEN(A$(K)) - 2)
  26. 270 INPUT X$
  27. 280 PRINT "FOR A "; V$; " THE ANSWER WOULD BE ";
  28. 290 INPUT A$
  29. 300 A$ = LEFT$(A$, 1): IF A$ <> "Y" AND A$ <> "N" THEN 280
  30. 310 IF A$ = "Y" THEN B$ = "N"
  31. 320 IF A$ = "N" THEN B$ = "Y"
  32. 330 Z1 = VAL(A$(0))
  33. 340 A$(0) = STR$(Z1 + 2)
  34. 350 A$(Z1) = A$(K)
  35. 360 A$(Z1 + 1) = "\A" + V$
  36. 370 A$(K) = "\Q" + X$ + "\" + A$ + STR$(Z1 + 1) + "\" + B$ + STR$(Z1) + "\"
  37. 380 GOTO 120
  38.  
  39.  
  40. 390 REM     SUBROUTINE TO PRINT QUESTIONS
  41. 400 Q$ = A$(K)
  42. 410 FOR Z = 3 TO LEN(Q$)
  43.    415 IF MID$(Q$, Z, 1) <> "\" THEN PRINT MID$(Q$, Z, 1);
  44. 417 NEXT Z
  45. 420 INPUT C$
  46. 430 C$ = LEFT$(C$, 1)
  47. 440 IF C$ <> "Y" AND C$ <> "N" THEN 410
  48. 450 T$ = "\" + C$
  49. 455 FOR X = 3 TO LEN(Q$) - 1
  50.    460 IF MID$(Q$, X, 2) = T$ THEN 480
  51. 470 NEXT X
  52. 475 STOP
  53. 480 FOR Y = X + 1 TO LEN(Q$)
  54.    490 IF MID$(Q$, Y, 1) = "\" THEN 510
  55. 500 NEXT Y
  56. 505 STOP
  57. 510 K = VAL(MID$(Q$, X + 2, Y - X - 2))
  58. 520 RETURN
  59.  
  60.  
  61.  
  62. 530 DATA "4","\QDOES IT SWIM\Y2\N3\","\AFISH","\ABIRD"
  63.  
  64.  
  65. 600 PRINT: PRINT "ANIMALS I ALREADY KNOW ARE:"
  66. 605 X = 0
  67. 610 FOR I = 1 TO 200
  68.    620 IF LEFT$(A$(I), 2) <> "\A" THEN 650
  69.    624 PRINT TAB(15 * X);
  70.    630 FOR Z = 3 TO LEN(A$(I))
  71.        640 IF MID$(A$(I), Z, 1) <> "\" THEN PRINT MID$(A$(I), Z, 1);
  72.    642 NEXT Z
  73.    645 X = X + 1: IF X = 4 THEN X = 0: PRINT
  74. 650 NEXT I
  75. 660 PRINT
  76. 670 PRINT
  77. 680 GOTO 120
  78. 999 END
  79.  
  80.  
  81.  

Divide and conquer!
Title: Re: Guess the Animal
Post by: Kiara87 on November 08, 2020, 05:17:17 pm
This is an interesting program and might be good exercise to convert it to more modern ways with QB64.

In 70 lines or so this little program starts learning.

Define the problem: wouldn't it be nice to save it's learning so you don't have to start over the next time you run the program? wouldn't it be nice if your answers didn't have to be in all capital letters? wouldn't it be nice to get a little practice using an array? Wouldn't it be nice to lose all the line numbers?

Here, I've taken TempodiBasic's find and divided the program into 4 or 5 sections with spaces to start making sense of what it is doing and how.

Code: QB64: [Select]
  1. 10 PRINT TAB(32); "ANIMAL"
  2. 20 PRINT TAB(15); "CREATIVE COMPUTING  MORRISTOWN, NEW JERSEY"
  3. 40 PRINT "PLAY 'GUESS THE ANIMAL'"
  4. 50 PRINT "THINK OF AN ANIMAL AND THE COMPUTER WILL TRY TO GUESS IT."
  5. 70 DIM A$(200)
  6. 80 FOR I = 0 TO 3
  7.    90 READ A$(I)
  8. 100 NEXT I
  9. 110 N = VAL(A$(0))
  10.  
  11. 120 REM          MAIN CONTROL SECTION
  12. 130 INPUT "ARE YOU THINKING OF AN ANIMAL"; A$
  13. 140 IF A$ = "LIST" THEN 600
  14. 150 IF LEFT$(A$, 1) <> "Y" THEN 120
  15. 160 K = 1
  16. 170 GOSUB 390
  17. 180 IF LEN(A$(K)) = 0 THEN 999
  18. 190 IF LEFT$(A$(K), 2) = "\Q" THEN 170
  19. 200 PRINT "IS IT A "; RIGHT$(A$(K), LEN(A$(K)) - 2);
  20. 210 INPUT A$
  21. 220 A$ = LEFT$(A$, 1)
  22. 230 IF LEFT$(A$, 1) = "Y" THEN PRINT "WHY NOT TRY ANOTHER ANIMAL?": GOTO 120
  23. 240 INPUT "THE ANIMAL YOU WERE THINKING OF WAS A "; V$
  24. 250 PRINT "PLEASE TYPE IN A QUESTION THAT WOULD DISTINGUISH A"
  25. 260 PRINT V$; " FROM A "; RIGHT$(A$(K), LEN(A$(K)) - 2)
  26. 270 INPUT X$
  27. 280 PRINT "FOR A "; V$; " THE ANSWER WOULD BE ";
  28. 290 INPUT A$
  29. 300 A$ = LEFT$(A$, 1): IF A$ <> "Y" AND A$ <> "N" THEN 280
  30. 310 IF A$ = "Y" THEN B$ = "N"
  31. 320 IF A$ = "N" THEN B$ = "Y"
  32. 330 Z1 = VAL(A$(0))
  33. 340 A$(0) = STR$(Z1 + 2)
  34. 350 A$(Z1) = A$(K)
  35. 360 A$(Z1 + 1) = "\A" + V$
  36. 370 A$(K) = "\Q" + X$ + "\" + A$ + STR$(Z1 + 1) + "\" + B$ + STR$(Z1) + "\"
  37. 380 GOTO 120
  38.  
  39.  
  40. 390 REM     SUBROUTINE TO PRINT QUESTIONS
  41. 400 Q$ = A$(K)
  42. 410 FOR Z = 3 TO LEN(Q$)
  43.    415 IF MID$(Q$, Z, 1) <> "\" THEN PRINT MID$(Q$, Z, 1);
  44. 417 NEXT Z
  45. 420 INPUT C$
  46. 430 C$ = LEFT$(C$, 1)
  47. 440 IF C$ <> "Y" AND C$ <> "N" THEN 410
  48. 450 T$ = "\" + C$
  49. 455 FOR X = 3 TO LEN(Q$) - 1
  50.    460 IF MID$(Q$, X, 2) = T$ THEN 480
  51. 470 NEXT X
  52. 475 STOP
  53. 480 FOR Y = X + 1 TO LEN(Q$)
  54.    490 IF MID$(Q$, Y, 1) = "\" THEN 510
  55. 500 NEXT Y
  56. 505 STOP
  57. 510 K = VAL(MID$(Q$, X + 2, Y - X - 2))
  58. 520 RETURN
  59.  
  60.  
  61.  
  62. 530 DATA "4","\QDOES IT SWIM\Y2\N3\","\AFISH","\ABIRD"
  63.  
  64.  
  65. 600 PRINT: PRINT "ANIMALS I ALREADY KNOW ARE:"
  66. 605 X = 0
  67. 610 FOR I = 1 TO 200
  68.    620 IF LEFT$(A$(I), 2) <> "\A" THEN 650
  69.    624 PRINT TAB(15 * X);
  70.    630 FOR Z = 3 TO LEN(A$(I))
  71.        640 IF MID$(A$(I), Z, 1) <> "\" THEN PRINT MID$(A$(I), Z, 1);
  72.    642 NEXT Z
  73.    645 X = X + 1: IF X = 4 THEN X = 0: PRINT
  74. 650 NEXT I
  75. 660 PRINT
  76. 670 PRINT
  77. 680 GOTO 120
  78. 999 END
  79.  
  80.  
  81.  

Divide and conquer!

these sources as an exercise I downloaded them

https://www.mediafire.com/file/psy37gm7dadtobb/Basic_Computer_Games_SourceCode.7z/file

so the solution to learn and translate a program from one code to another?

will help learning?

is this what you mean?
from basic code to QB64
OR other language to QB64?
Title: Re: Guess the Animal
Post by: TempodiBasic on November 08, 2020, 05:42:40 pm
Hi kiara87
I suppose that Bplus drives you to learning translating a source code to another structure so:
1. you must read Wiki to understand what does each keyword that you have to manage
2. you can see a general structure in the code
3. you can choose a more readable structure
4. you can change the structure  adding or modifying its way of working
It takes times but it is a good and hard way to learn
Good luck

Ciao kiara87
Io suppongo che Bplus ti guidi ad apprendere  traducendo un codice sorgente in un'altra struttura di codice così facendo:
1. tu sei costretta a leggere laWIKI per capire coda fa ogni parola che tu devi gestire
2. tu puoi vedere una struttura generale nel codice
3. tu puoi scegliere una struttura più leggibile
4. tu puoi cambiare la struttura aggiungendo o modificando il suo modo di funzionare
Richiede del tempo ma è un modo buono e duro di apprendere
Buona fortuna
Title: Re: Guess the Animal
Post by: Kiara87 on November 08, 2020, 05:59:28 pm
Hi kiara87
I suppose that Bplus drives you to learning translating a source code to another structure so:
1. you must read Wiki to understand what does each keyword that you have to manage
2. you can see a general structure in the code
3. you can choose a more readable structure
4. you can change the structure  adding or modifying its way of working
It takes times but it is a good and hard way to learn
Good luck

Ciao kiara87
Io suppongo che Bplus ti guidi ad apprendere  traducendo un codice sorgente in un'altra struttura di codice così facendo:
1. tu sei costretta a leggere laWIKI per capire coda fa ogni parola che tu devi gestire
2. tu puoi vedere una struttura generale nel codice
3. tu puoi scegliere una struttura più leggibile
4. tu puoi cambiare la struttura aggiungendo o modificando il suo modo di funzionare
Richiede del tempo ma è un modo buono e duro di apprendere
Buona fortuna

ciao @TempodiBasic 
allora avevo capito bene devo tradurre  il programma dal codice basic a QB64
però hai detto che ci vuole del tempo per apprendere con questo metodo
allora io mi chiedo se cè un metodo più veloce per imparare a programmare
questo magari e il metodo migliore tradurre un programma in altro linguaggio?
oppure non esiste un metodo migliore?
non sia per la fretta per imparare ma è quasi 1 anno che ci giro è cercando scorciatoie magari un metodo più veloce esiste
comunque farò anche come consigliato da bplus perche credo che lui abbia imparato cosi a programmare
grazie anche alla tua attenzione e tutti i tuoi suggerimenti e un tuo commento vale tanto con la tua traduzione in italiano
Title: Re: Guess the Animal
Post by: bplus on November 08, 2020, 06:29:48 pm
Oh man! this is harder than I thought. I tried this afternoon putting it in more modern form and picked up errors. Might be easier to rewrite this from scratch, for me anyway ;-))

Title: Re: Guess the Animal
Post by: Kiara87 on November 08, 2020, 06:37:06 pm
Oh man! this is harder than I thought. I tried this afternoon putting it in more modern form and picked up errors. Might be easier to rewrite this from scratch, for me anyway ;-))

you can post the work here to verify the changes made so to learn how you transformed the code
thanks
Title: Re: Guess the Animal
Post by: Dav on November 08, 2020, 06:54:08 pm
I once had a Basic code tool that converted old basic source (that needed line numbers), into prettier Qbasic source code.  It removed all line numbers that were not called on (like GOTO 110), and it cleaned up code in other ways too.  I used it often to translate old code into more modern code and it worked good.  It was called B2Q I think.  I'm going to look for it.  Could come in handy again....

- Dav.
Title: Re: Guess the Animal
Post by: bplus on November 08, 2020, 06:59:56 pm
Yeah Dav that was first thing I did. Then plugged in the list maker right in where it was called. Something happens with Question gosub when trying to redo the goto's inside it. It would help if I fully understand how it worked :)

Oh heck it might have been just a case of not being UCASE$() ;-))

Not use to seeing naked INPUT C$

OK removed non essential line numbers, moved the list of animals into main loop putting data at the end of program and capitalized all the INPUTs.

Looks like the original program is not parsing the A$() questions correctly either.

progress?
Code: QB64: [Select]
  1. PRINT TAB(32); "ANIMAL"
  2. PRINT TAB(15); "CREATIVE COMPUTING  MORRISTOWN, NEW JERSEY"
  3. PRINT "PLAY 'GUESS THE ANIMAL'"
  4. PRINT "THINK OF AN ANIMAL AND THE COMPUTER WILL TRY TO GUESS IT."
  5. DIM A$(200)
  6. FOR I = 0 TO 3
  7.     READ A$(I)
  8. N = VAL(A$(0))
  9. 120 REM          MAIN CONTROL SECTION
  10. INPUT "ARE YOU THINKING OF AN ANIMAL"; A$
  11. A$ = UCASE$(A$)
  12. IF A$ = "LIST" THEN
  13.     PRINT: PRINT "ANIMALS I ALREADY KNOW ARE:"
  14.     X = 0
  15.     FOR I = 1 TO 200
  16.         IF LEFT$(A$(I), 2) <> "\A" THEN _CONTINUE
  17.         PRINT TAB(15 * X);
  18.         FOR Z = 3 TO LEN(A$(I))
  19.             IF MID$(A$(I), Z, 1) <> "\" THEN PRINT MID$(A$(I), Z, 1);
  20.         NEXT Z
  21.         X = X + 1: IF X = 4 THEN X = 0: PRINT
  22.     NEXT I
  23.     PRINT
  24.     PRINT
  25.     GOTO 120
  26. IF LEFT$(A$, 1) <> "Y" THEN 120
  27. K = 1
  28. 170 GOSUB 390
  29. IF LEN(A$(K)) = 0 THEN END
  30. IF LEFT$(A$(K), 2) = "\Q" THEN 170
  31. PRINT "IS IT A "; RIGHT$(A$(K), LEN(A$(K)) - 2);
  32. A$ = UCASE$(LEFT$(A$, 1))
  33. IF LEFT$(A$, 1) = "Y" THEN PRINT "WHY NOT TRY ANOTHER ANIMAL?": GOTO 120
  34. INPUT "THE ANIMAL YOU WERE THINKING OF WAS A "; V$
  35. V$ = UCASE$(V$)
  36. PRINT "PLEASE TYPE IN A QUESTION THAT WOULD DISTINGUISH A"
  37. PRINT V$; " FROM A "; RIGHT$(A$(K), LEN(A$(K)) - 2)
  38. X$ = UCASE$(X$)
  39. 280 PRINT "FOR A "; V$; " THE ANSWER WOULD BE ";
  40. A$ = UCASE$(A$)
  41. A$ = LEFT$(A$, 1): IF A$ <> "Y" AND A$ <> "N" THEN 280
  42. IF A$ = "Y" THEN B$ = "N"
  43. IF A$ = "N" THEN B$ = "Y"
  44. Z1 = VAL(A$(0))
  45. A$(0) = STR$(Z1 + 2)
  46. A$(Z1) = A$(K)
  47. A$(Z1 + 1) = "\A" + V$
  48. A$(K) = "\Q" + X$ + "\" + A$ + STR$(Z1 + 1) + "\" + B$ + STR$(Z1) + "\"
  49. GOTO 120
  50. 390 REM     SUBROUTINE TO PRINT QUESTIONS
  51. Q$ = A$(K)
  52. 410 FOR Z = 3 TO LEN(Q$)
  53.     IF MID$(Q$, Z, 1) <> "\" THEN PRINT MID$(Q$, Z, 1);
  54. C$ = UCASE$(LEFT$(C$, 1))
  55. IF C$ <> "Y" AND C$ <> "N" THEN 410
  56. T$ = "\" + C$
  57. FOR X = 3 TO LEN(Q$) - 1
  58.     IF MID$(Q$, X, 2) = T$ THEN 480
  59. 480 FOR Y = X + 1 TO LEN(Q$)
  60.     IF MID$(Q$, Y, 1) = "\" THEN 510
  61. 510 K = VAL(MID$(Q$, X + 2, Y - X - 2))
  62. DATA "4","\QDOES IT SWIM\Y2\N3\","\AFISH","\ABIRD"
  63.  

Looks like will have to trim$ STR$(numbers) also.
Title: Re: Guess the Animal
Post by: Dav on November 08, 2020, 07:13:42 pm
 I ran the code through the remline  (https://www.qb64.org/wiki/Line_number)tool in the wiki, and here's that output. A wee bit better I guess...

- Dav

Code: QB64: [Select]
  1. PRINT TAB(32); "ANIMAL"
  2. PRINT TAB(15); "CREATIVE COMPUTING  MORRISTOWN, NEW JERSEY"
  3. PRINT "PLAY 'GUESS THE ANIMAL'"
  4. PRINT "THINK OF AN ANIMAL AND THE COMPUTER WILL TRY TO GUESS IT."
  5. DIM A$(200)
  6. FOR I = 0 TO 3
  7.   90 READ A$(I)
  8. N = VAL(A$(0))
  9.  
  10. 120 REM          MAIN CONTROL SECTION
  11. INPUT "ARE YOU THINKING OF AN ANIMAL"; A$
  12. IF A$ = "LIST" THEN 600
  13. IF LEFT$(A$, 1) <> "Y" THEN 120
  14. K = 1
  15. 170 GOSUB 390
  16. IF LEN(A$(K)) = 0 THEN 999
  17. IF LEFT$(A$(K), 2) = "\Q" THEN 170
  18. PRINT "IS IT A "; RIGHT$(A$(K), LEN(A$(K)) - 2);
  19. A$ = LEFT$(A$, 1)
  20. IF LEFT$(A$, 1) = "Y" THEN PRINT "WHY NOT TRY ANOTHER ANIMAL?": GOTO 120
  21. INPUT "THE ANIMAL YOU WERE THINKING OF WAS A "; V$
  22. PRINT "PLEASE TYPE IN A QUESTION THAT WOULD DISTINGUISH A"
  23. PRINT V$; " FROM A "; RIGHT$(A$(K), LEN(A$(K)) - 2)
  24. 280 PRINT "FOR A "; V$; " THE ANSWER WOULD BE ";
  25. A$ = LEFT$(A$, 1): IF A$ <> "Y" AND A$ <> "N" THEN 280
  26. IF A$ = "Y" THEN B$ = "N"
  27. IF A$ = "N" THEN B$ = "Y"
  28. Z1 = VAL(A$(0))
  29. A$(0) = STR$(Z1 + 2)
  30. A$(Z1) = A$(K)
  31. A$(Z1 + 1) = "\A" + V$
  32. A$(K) = "\Q" + X$ + "\" + A$ + STR$(Z1 + 1) + "\" + B$ + STR$(Z1) + "\"
  33. GOTO 120
  34.  
  35.  
  36. 390 REM     SUBROUTINE TO PRINT QUESTIONS
  37. Q$ = A$(K)
  38. 410 FOR Z = 3 TO LEN(Q$)
  39.   415 IF MID$(Q$, Z, 1) <> "\" THEN PRINT MID$(Q$, Z, 1);
  40. C$ = LEFT$(C$, 1)
  41. IF C$ <> "Y" AND C$ <> "N" THEN 410
  42. T$ = "\" + C$
  43. FOR X = 3 TO LEN(Q$) - 1
  44.   460 IF MID$(Q$, X, 2) = T$ THEN 480
  45. 480 FOR Y = X + 1 TO LEN(Q$)
  46.   490 IF MID$(Q$, Y, 1) = "\" THEN 510
  47. 510 K = VAL(MID$(Q$, X + 2, Y - X - 2))
  48.  
  49.  
  50.  
  51. DATA "4","\QDOES IT SWIM\Y2\N3\","\AFISH","\ABIRD"
  52.  
  53.  
  54. 600 PRINT: PRINT "ANIMALS I ALREADY KNOW ARE:"
  55. X = 0
  56. FOR I = 1 TO 200
  57.   620 IF LEFT$(A$(I), 2) <> "\A" THEN 650
  58.   624 PRINT TAB(15 * X);
  59.   630 FOR Z = 3 TO LEN(A$(I))
  60.       640 IF MID$(A$(I), Z, 1) <> "\" THEN PRINT MID$(A$(I), Z, 1);
  61.   642 NEXT Z
  62.   645 X = X + 1: IF X = 4 THEN X = 0: PRINT
  63. 650 NEXT I
  64. GOTO 120
  65. 999 END
  66.  
Title: Re: Guess the Animal
Post by: Kiara87 on November 08, 2020, 07:40:14 pm
I ran the code through the remline  (https://www.qb64.org/wiki/Line_number)tool in the wiki, and here's that output. A wee bit better I guess...

- Dav

Code: QB64: [Select]
  1. PRINT TAB(32); "ANIMAL"
  2. PRINT TAB(15); "CREATIVE COMPUTING  MORRISTOWN, NEW JERSEY"
  3. PRINT "PLAY 'GUESS THE ANIMAL'"
  4. PRINT "THINK OF AN ANIMAL AND THE COMPUTER WILL TRY TO GUESS IT."
  5. DIM A$(200)
  6. FOR I = 0 TO 3
  7.   90 READ A$(I)
  8. N = VAL(A$(0))
  9.  
  10. 120 REM          MAIN CONTROL SECTION
  11. INPUT "ARE YOU THINKING OF AN ANIMAL"; A$
  12. IF A$ = "LIST" THEN 600
  13. IF LEFT$(A$, 1) <> "Y" THEN 120
  14. K = 1
  15. 170 GOSUB 390
  16. IF LEN(A$(K)) = 0 THEN 999
  17. IF LEFT$(A$(K), 2) = "\Q" THEN 170
  18. PRINT "IS IT A "; RIGHT$(A$(K), LEN(A$(K)) - 2);
  19. A$ = LEFT$(A$, 1)
  20. IF LEFT$(A$, 1) = "Y" THEN PRINT "WHY NOT TRY ANOTHER ANIMAL?": GOTO 120
  21. INPUT "THE ANIMAL YOU WERE THINKING OF WAS A "; V$
  22. PRINT "PLEASE TYPE IN A QUESTION THAT WOULD DISTINGUISH A"
  23. PRINT V$; " FROM A "; RIGHT$(A$(K), LEN(A$(K)) - 2)
  24. 280 PRINT "FOR A "; V$; " THE ANSWER WOULD BE ";
  25. A$ = LEFT$(A$, 1): IF A$ <> "Y" AND A$ <> "N" THEN 280
  26. IF A$ = "Y" THEN B$ = "N"
  27. IF A$ = "N" THEN B$ = "Y"
  28. Z1 = VAL(A$(0))
  29. A$(0) = STR$(Z1 + 2)
  30. A$(Z1) = A$(K)
  31. A$(Z1 + 1) = "\A" + V$
  32. A$(K) = "\Q" + X$ + "\" + A$ + STR$(Z1 + 1) + "\" + B$ + STR$(Z1) + "\"
  33. GOTO 120
  34.  
  35.  
  36. 390 REM     SUBROUTINE TO PRINT QUESTIONS
  37. Q$ = A$(K)
  38. 410 FOR Z = 3 TO LEN(Q$)
  39.   415 IF MID$(Q$, Z, 1) <> "\" THEN PRINT MID$(Q$, Z, 1);
  40. C$ = LEFT$(C$, 1)
  41. IF C$ <> "Y" AND C$ <> "N" THEN 410
  42. T$ = "\" + C$
  43. FOR X = 3 TO LEN(Q$) - 1
  44.   460 IF MID$(Q$, X, 2) = T$ THEN 480
  45. 480 FOR Y = X + 1 TO LEN(Q$)
  46.   490 IF MID$(Q$, Y, 1) = "\" THEN 510
  47. 510 K = VAL(MID$(Q$, X + 2, Y - X - 2))
  48.  
  49.  
  50.  
  51. DATA "4","\QDOES IT SWIM\Y2\N3\","\AFISH","\ABIRD"
  52.  
  53.  
  54. 600 PRINT: PRINT "ANIMALS I ALREADY KNOW ARE:"
  55. X = 0
  56. FOR I = 1 TO 200
  57.   620 IF LEFT$(A$(I), 2) <> "\A" THEN 650
  58.   624 PRINT TAB(15 * X);
  59.   630 FOR Z = 3 TO LEN(A$(I))
  60.       640 IF MID$(A$(I), Z, 1) <> "\" THEN PRINT MID$(A$(I), Z, 1);
  61.   642 NEXT Z
  62.   645 X = X + 1: IF X = 4 THEN X = 0: PRINT
  63. 650 NEXT I
  64. GOTO 120
  65. 999 END
  66.  

as I see the code is always the same
Title: Re: Guess the Animal
Post by: Dav on November 08, 2020, 07:47:06 pm
as I see the code is always the same

Yes, you are correct.  However, with the unused lines numbers removed, it may now be visually easier to start modernizing the code (I would find it easier).

- Dav
Title: Re: Guess the Animal
Post by: bplus on November 08, 2020, 07:52:12 pm
as I see the code is always the same

I don't see line numbers on every line.

But I see REMline leaves allot more line numbers in than I did?
Title: Re: Guess the Animal
Post by: bplus on November 08, 2020, 07:53:36 pm
Dav did you pick up on the parsing problem of the original code, look at the ends of the questions?

 
Title: Re: Guess the Animal
Post by: Dav on November 08, 2020, 07:56:03 pm
Dav did you pick up on the parsing problem of the original code, look at the ends of the questions?

No I didn't - but I'll take a closer look at it....

- Dav
Title: Re: Guess the Animal
Post by: Kiara87 on November 08, 2020, 08:22:57 pm
I don't see line numbers on every line.

But I see REMline leaves allot more line numbers in than I did?

REMline   i don't know this instruction i was looking in the wiki but i can't understand what it is for 
Title: Re: Guess the Animal
Post by: bplus on November 08, 2020, 08:28:01 pm
REMline   i don't know this instruction i was looking in the wiki but i can't understand what it is for 

Found it searching Wiki: https://www.qb64.org/wiki/Line_number

First I searched remline, nothing, then remline.bas and hit!
Title: Re: Guess the Animal
Post by: Kiara87 on November 08, 2020, 08:33:22 pm
Found it searching Wiki: https://www.qb64.org/wiki/Line_number

First I searched remline, nothing, then remline.bas and hit!

only gives me error?
https://imgur.com/yXPNoeg
Title: Re: Guess the Animal
Post by: bplus on November 08, 2020, 09:30:02 pm
Quote
only gives me error?

REMline is a .bas program that needs the program name for the .BAS Source file to remove the line numbers from.

Store the remline.bas program in same folder where your .bas file is that you want to remove line numbers from.
RUN the program to compile it. (Make sure the Save EXE in same folder as Source is bulleted in the RUN menu of IDE)
Run the program again and input the name of the bas file to remove the numbers from (looks like it does quotes too.)

What I do is look for every GOTO and GOSUB and comment those line numbers then I remove all line numbers not commented then I remove comments to line numbers and make sure the program still works correctly.

Title: Re: Guess the Animal
Post by: MidnightOwl on November 09, 2020, 03:24:06 am
here is the program converted to QB64 code. I have done things like this so much that it was a piece of cake.
Code: QB64: [Select]
  1. [DATA "4","\QDOES IT SWIM\Y2\N3\","\AFISH","\ABIRD"
  2.  
  3. PRINT TAB(32); "ANIMAL"
  4. PRINT TAB(15); "CREATIVE COMPUTING  MORRISTOWN, NEW JERSEY"
  5. PRINT "PLAY 'GUESS THE ANIMAL'"
  6. PRINT "THINK OF AN ANIMAL AND THE COMPUTER WILL TRY TO GUESS IT."
  7. DIM SHARED A$(200), K, N, T$
  8. FOR I = 0 TO 3
  9.    90 READ A$(I)
  10. N = VAL(A$(0))
  11.  
  12. MAIN
  13.  
  14. '         MAIN CONTROL SECTION
  15. SUB MAIN ()
  16.     DO
  17.         DO
  18.             INPUT "ARE YOU THINKING OF AN ANIMAL (Y/N)"; A$: A$ = UCASE$(A$)
  19.         LOOP UNTIL A$ = "LIST" OR LEFT$(A$, 1) = "Y" OR LEFT$(A$, 1) = "N"
  20.         IF A$ = "LIST" THEN SHOWLIST: MAIN
  21.         IF LEFT$(A$, 1) = "N" THEN SYSTEM
  22.         K = 1
  23.         DO
  24.             SHOWQUESTION
  25.             IF LEN(A$(K)) = 0 THEN SYSTEM
  26.         LOOP WHILE LEFT$(A$(K), 2) = "\Q"
  27.         PRINT "IS IT A "; RIGHT$(A$(K), LEN(A$(K)) - 2);
  28.         INPUT A$
  29.         A$ = LEFT$(A$, 1)
  30.         IF LEFT$(A$, 1) = "Y" THEN PRINT "WHY NOT TRY ANOTHER ANIMAL?": MAIN
  31.         INPUT "THE ANIMAL YOU WERE THINKING OF WAS A "; V$
  32.         PRINT "PLEASE TYPE IN A QUESTION THAT WOULD DISTINGUISH A"
  33.         PRINT V$; " FROM A "; RIGHT$(A$(K), LEN(A$(K)) - 2)
  34.         INPUT X$
  35.         DO
  36.             PRINT "FOR A "; V$; " THE ANSWER WOULD BE ";
  37.             INPUT A$
  38.         A$ = LEFT$(A$, 1): LOOP UNTIL A$ = "Y" OR A$ = "N"
  39.         IF A$ = "Y" THEN B$ = "N"
  40.         IF A$ = "N" THEN B$ = "Y"
  41.         Z1 = VAL(A$(0))
  42.         A$(0) = STR$(Z1 + 2)
  43.         A$(Z1) = A$(K)
  44.         A$(Z1 + 1) = "\A" + V$
  45.         A$(K) = "\Q" + X$ + "\" + A$ + STR$(Z1 + 1) + "\" + B$ + STR$(Z1) + "\"
  46.     LOOP
  47.  
  48. '  SUBROUTINE TO PRINT QUESTIONS
  49. SUB SHOWQUESTION ()
  50.     Q$ = A$(K)
  51.     DO
  52.         FOR Z = 3 TO LEN(Q$)
  53.             IF MID$(Q$, Z, 1) = "\" THEN EXIT FOR ELSE PRINT MID$(Q$, Z, 1);
  54.         NEXT Z
  55.         INPUT C$: C$ = UCASE$(C$)
  56.         C$ = LEFT$(C$, 1)
  57.     LOOP UNTIL C$ = "Y" OR C$ = "N"
  58.     T$ = "\" + C$
  59.     FOR X = 3 TO LEN(Q$) - 1
  60.         IF MID$(Q$, X, 2) = T$ THEN EXIT FOR
  61.     NEXT X
  62.     FOR Y = X + 1 TO LEN(Q$)
  63.         IF MID$(Q$, Y, 1) = "\" THEN EXIT FOR
  64.     NEXT Y
  65.     K = VAL(MID$(Q$, X + 2, Y - X - 2))
  66.  
  67. SUB SHOWLIST ()
  68.     PRINT: PRINT "ANIMALS I ALREADY KNOW ARE:"
  69.     X = 0
  70.     FOR I = 1 TO 200
  71.        620 IF LEFT$(A$(I), 2) = "\A" THEN
  72.            624 PRINT TAB(15 * X);
  73.            630 FOR Z = 3 TO LEN(A$(I))
  74.                640 IF MID$(A$(I), Z, 1) <> "\" THEN PRINT MID$(A$(I), Z, 1);
  75.            642 NEXT Z
  76.            645 X = X + 1: IF X = 4 THEN X = 0: PRINT
  77.         END IF
  78.     NEXT I
  79.     PRINT
  80.     PRINT
  81.  
  82.  
  83.  
  84. /code]
Title: Re: Guess the Animal
Post by: MidnightOwl on November 09, 2020, 07:06:37 am
Here is a version that saves a data file so that the program doesn't forget what it learned
Code: QB64: [Select]
  1. DATA "4","\QDOES IT SWIM\Y2\N3\","\AFISH","\ABIRD"
  2.  
  3. PRINT TAB(32); "ANIMAL"
  4. PRINT TAB(15); "CREATIVE COMPUTING  MORRISTOWN, NEW JERSEY"
  5. PRINT "PLAY 'GUESS THE ANIMAL'"
  6. PRINT "THINK OF AN ANIMAL AND THE COMPUTER WILL TRY TO GUESS IT."
  7. DIM SHARED A$(200), K, N, T$
  8. ON ERROR GOTO NOFILE
  9. OPEN "ANIMAL.DAT" FOR INPUT AS #3
  10. FOR I = 0 TO 200
  11.     INPUT #3, A$(I)
  12. NOFILE:
  13. FOR I = 0 TO 3
  14.     READ A$(I)
  15.  
  16. N = VAL(A$(0))
  17.  
  18. MAIN
  19.  
  20. '         MAIN CONTROL SECTION
  21. SUB MAIN ()
  22.     DO
  23.         DO
  24.             INPUT "ARE YOU THINKING OF AN ANIMAL (Y/N)"; A$: A$ = UCASE$(A$)
  25.         LOOP UNTIL A$ = "LIST" OR LEFT$(A$, 1) = "Y" OR LEFT$(A$, 1) = "N"
  26.         IF A$ = "LIST" THEN SHOWLIST: MAIN
  27.         IF LEFT$(A$, 1) = "N" THEN KEEPDATA: SYSTEM
  28.         K = 1
  29.         DO
  30.             SHOWQUESTION
  31.             IF LEN(A$(K)) = 0 THEN KEEPDATA: SYSTEM
  32.         LOOP WHILE LEFT$(A$(K), 2) = "\Q"
  33.         PRINT "IS IT A "; RIGHT$(A$(K), LEN(A$(K)) - 2);
  34.         INPUT A$
  35.         A$ = LEFT$(A$, 1)
  36.         IF LEFT$(A$, 1) = "Y" THEN PRINT "WHY NOT TRY ANOTHER ANIMAL?": MAIN
  37.         INPUT "THE ANIMAL YOU WERE THINKING OF WAS A "; V$
  38.         PRINT "PLEASE TYPE IN A QUESTION THAT WOULD DISTINGUISH A"
  39.         PRINT V$; " FROM A "; RIGHT$(A$(K), LEN(A$(K)) - 2)
  40.         INPUT X$
  41.         DO
  42.             PRINT "FOR A "; V$; " THE ANSWER WOULD BE ";
  43.             INPUT A$
  44.         A$ = LEFT$(A$, 1): LOOP UNTIL A$ = "Y" OR A$ = "N"
  45.         IF A$ = "Y" THEN B$ = "N"
  46.         IF A$ = "N" THEN B$ = "Y"
  47.         Z1 = VAL(A$(0))
  48.         A$(0) = STR$(Z1 + 2)
  49.         A$(Z1) = A$(K)
  50.         A$(Z1 + 1) = "\A" + V$
  51.         A$(K) = "\Q" + X$ + "\" + A$ + STR$(Z1 + 1) + "\" + B$ + STR$(Z1) + "\"
  52.     LOOP
  53.  
  54. '  SUBROUTINE TO PRINT QUESTIONS
  55. SUB SHOWQUESTION ()
  56.     Q$ = A$(K)
  57.     DO
  58.         FOR Z = 3 TO LEN(Q$)
  59.             IF MID$(Q$, Z, 1) = "\" THEN EXIT FOR ELSE PRINT MID$(Q$, Z, 1);
  60.         NEXT Z
  61.         INPUT C$: C$ = UCASE$(C$)
  62.         C$ = LEFT$(C$, 1)
  63.     LOOP UNTIL C$ = "Y" OR C$ = "N"
  64.     T$ = "\" + C$
  65.     FOR X = 3 TO LEN(Q$) - 1
  66.         IF MID$(Q$, X, 2) = T$ THEN EXIT FOR
  67.     NEXT X
  68.     FOR Y = X + 1 TO LEN(Q$)
  69.         IF MID$(Q$, Y, 1) = "\" THEN EXIT FOR
  70.     NEXT Y
  71.     K = VAL(MID$(Q$, X + 2, Y - X - 2))
  72.  
  73. SUB SHOWLIST ()
  74.     PRINT: PRINT "ANIMALS I ALREADY KNOW ARE:"
  75.     X = 0
  76.     FOR I = 1 TO 200
  77.         IF LEFT$(A$(I), 2) = "\A" THEN
  78.             PRINT TAB(15 * X);
  79.             FOR Z = 3 TO LEN(A$(I))
  80.                 IF MID$(A$(I), Z, 1) <> "\" THEN PRINT MID$(A$(I), Z, 1);
  81.             NEXT Z
  82.             X = X + 1: IF X = 4 THEN X = 0: PRINT
  83.         END IF
  84.     NEXT I
  85.     PRINT
  86.     PRINT
  87.  
  88. '        SUBROUTINE TO SAVE DATA FILE
  89. SUB KEEPDATA ()
  90.     OPEN "ANIMAL.DAT" FOR OUTPUT AS #2
  91.     FOR I = 0 TO 200
  92.         WRITE #2, A$(I)
  93.     NEXT I
  94.     CLOSE #2
Title: Re: Guess the Animal
Post by: Kiara87 on November 09, 2020, 08:01:26 am
Here is a version that saves a data file so that the program doesn't forget what it learned
Code: QB64: [Select]
  1. DATA "4","\QDOES IT SWIM\Y2\N3\","\AFISH","\ABIRD"
  2.  
  3. PRINT TAB(32); "ANIMAL"
  4. PRINT TAB(15); "CREATIVE COMPUTING  MORRISTOWN, NEW JERSEY"
  5. PRINT "PLAY 'GUESS THE ANIMAL'"
  6. PRINT "THINK OF AN ANIMAL AND THE COMPUTER WILL TRY TO GUESS IT."
  7. DIM SHARED A$(200), K, N, T$
  8. ON ERROR GOTO NOFILE
  9. OPEN "ANIMAL.DAT" FOR INPUT AS #3
  10. FOR I = 0 TO 200
  11.     INPUT #3, A$(I)
  12. NOFILE:
  13. FOR I = 0 TO 3
  14.     READ A$(I)
  15.  
  16. N = VAL(A$(0))
  17.  
  18. MAIN
  19.  
  20. '         MAIN CONTROL SECTION
  21. SUB MAIN ()
  22.     DO
  23.         DO
  24.             INPUT "ARE YOU THINKING OF AN ANIMAL (Y/N)"; A$: A$ = UCASE$(A$)
  25.         LOOP UNTIL A$ = "LIST" OR LEFT$(A$, 1) = "Y" OR LEFT$(A$, 1) = "N"
  26.         IF A$ = "LIST" THEN SHOWLIST: MAIN
  27.         IF LEFT$(A$, 1) = "N" THEN KEEPDATA: SYSTEM
  28.         K = 1
  29.         DO
  30.             SHOWQUESTION
  31.             IF LEN(A$(K)) = 0 THEN KEEPDATA: SYSTEM
  32.         LOOP WHILE LEFT$(A$(K), 2) = "\Q"
  33.         PRINT "IS IT A "; RIGHT$(A$(K), LEN(A$(K)) - 2);
  34.         INPUT A$
  35.         A$ = LEFT$(A$, 1)
  36.         IF LEFT$(A$, 1) = "Y" THEN PRINT "WHY NOT TRY ANOTHER ANIMAL?": MAIN
  37.         INPUT "THE ANIMAL YOU WERE THINKING OF WAS A "; V$
  38.         PRINT "PLEASE TYPE IN A QUESTION THAT WOULD DISTINGUISH A"
  39.         PRINT V$; " FROM A "; RIGHT$(A$(K), LEN(A$(K)) - 2)
  40.         INPUT X$
  41.         DO
  42.             PRINT "FOR A "; V$; " THE ANSWER WOULD BE ";
  43.             INPUT A$
  44.         A$ = LEFT$(A$, 1): LOOP UNTIL A$ = "Y" OR A$ = "N"
  45.         IF A$ = "Y" THEN B$ = "N"
  46.         IF A$ = "N" THEN B$ = "Y"
  47.         Z1 = VAL(A$(0))
  48.         A$(0) = STR$(Z1 + 2)
  49.         A$(Z1) = A$(K)
  50.         A$(Z1 + 1) = "\A" + V$
  51.         A$(K) = "\Q" + X$ + "\" + A$ + STR$(Z1 + 1) + "\" + B$ + STR$(Z1) + "\"
  52.     LOOP
  53.  
  54. '  SUBROUTINE TO PRINT QUESTIONS
  55. SUB SHOWQUESTION ()
  56.     Q$ = A$(K)
  57.     DO
  58.         FOR Z = 3 TO LEN(Q$)
  59.             IF MID$(Q$, Z, 1) = "\" THEN EXIT FOR ELSE PRINT MID$(Q$, Z, 1);
  60.         NEXT Z
  61.         INPUT C$: C$ = UCASE$(C$)
  62.         C$ = LEFT$(C$, 1)
  63.     LOOP UNTIL C$ = "Y" OR C$ = "N"
  64.     T$ = "\" + C$
  65.     FOR X = 3 TO LEN(Q$) - 1
  66.         IF MID$(Q$, X, 2) = T$ THEN EXIT FOR
  67.     NEXT X
  68.     FOR Y = X + 1 TO LEN(Q$)
  69.         IF MID$(Q$, Y, 1) = "\" THEN EXIT FOR
  70.     NEXT Y
  71.     K = VAL(MID$(Q$, X + 2, Y - X - 2))
  72.  
  73. SUB SHOWLIST ()
  74.     PRINT: PRINT "ANIMALS I ALREADY KNOW ARE:"
  75.     X = 0
  76.     FOR I = 1 TO 200
  77.         IF LEFT$(A$(I), 2) = "\A" THEN
  78.             PRINT TAB(15 * X);
  79.             FOR Z = 3 TO LEN(A$(I))
  80.                 IF MID$(A$(I), Z, 1) <> "\" THEN PRINT MID$(A$(I), Z, 1);
  81.             NEXT Z
  82.             X = X + 1: IF X = 4 THEN X = 0: PRINT
  83.         END IF
  84.     NEXT I
  85.     PRINT
  86.     PRINT
  87.  
  88. '        SUBROUTINE TO SAVE DATA FILE
  89. SUB KEEPDATA ()
  90.     OPEN "ANIMAL.DAT" FOR OUTPUT AS #2
  91.     FOR I = 0 TO 200
  92.         WRITE #2, A$(I)
  93.     NEXT I
  94.     CLOSE #2

big thank you for transforming the program

this is what bplus intends to translate the code from one structure to another structure

obviously for those who do not have much practice it is more tiring

but i will try to study above and thanks for translating the program
Title: Re: Guess the Animal
Post by: bplus on November 09, 2020, 11:11:21 am
Here is a version that saves a data file so that the program doesn't forget what it learned
Code: QB64: [Select]
  1. DATA "4","\QDOES IT SWIM\Y2\N3\","\AFISH","\ABIRD"
  2.  
  3. PRINT TAB(32); "ANIMAL"
  4. PRINT TAB(15); "CREATIVE COMPUTING  MORRISTOWN, NEW JERSEY"
  5. PRINT "PLAY 'GUESS THE ANIMAL'"
  6. PRINT "THINK OF AN ANIMAL AND THE COMPUTER WILL TRY TO GUESS IT."
  7. DIM SHARED A$(200), K, N, T$
  8. ON ERROR GOTO NOFILE
  9. OPEN "ANIMAL.DAT" FOR INPUT AS #3
  10. FOR I = 0 TO 200
  11.     INPUT #3, A$(I)
  12. NOFILE:
  13. FOR I = 0 TO 3
  14.     READ A$(I)
  15.  
  16. N = VAL(A$(0))
  17.  
  18. MAIN
  19.  
  20. '         MAIN CONTROL SECTION
  21. SUB MAIN ()
  22.     DO
  23.         DO
  24.             INPUT "ARE YOU THINKING OF AN ANIMAL (Y/N)"; A$: A$ = UCASE$(A$)
  25.         LOOP UNTIL A$ = "LIST" OR LEFT$(A$, 1) = "Y" OR LEFT$(A$, 1) = "N"
  26.         IF A$ = "LIST" THEN SHOWLIST: MAIN
  27.         IF LEFT$(A$, 1) = "N" THEN KEEPDATA: SYSTEM
  28.         K = 1
  29.         DO
  30.             SHOWQUESTION
  31.             IF LEN(A$(K)) = 0 THEN KEEPDATA: SYSTEM
  32.         LOOP WHILE LEFT$(A$(K), 2) = "\Q"
  33.         PRINT "IS IT A "; RIGHT$(A$(K), LEN(A$(K)) - 2);
  34.         INPUT A$
  35.         A$ = LEFT$(A$, 1)
  36.         IF LEFT$(A$, 1) = "Y" THEN PRINT "WHY NOT TRY ANOTHER ANIMAL?": MAIN
  37.         INPUT "THE ANIMAL YOU WERE THINKING OF WAS A "; V$
  38.         PRINT "PLEASE TYPE IN A QUESTION THAT WOULD DISTINGUISH A"
  39.         PRINT V$; " FROM A "; RIGHT$(A$(K), LEN(A$(K)) - 2)
  40.         INPUT X$
  41.         DO
  42.             PRINT "FOR A "; V$; " THE ANSWER WOULD BE ";
  43.             INPUT A$
  44.         A$ = LEFT$(A$, 1): LOOP UNTIL A$ = "Y" OR A$ = "N"
  45.         IF A$ = "Y" THEN B$ = "N"
  46.         IF A$ = "N" THEN B$ = "Y"
  47.         Z1 = VAL(A$(0))
  48.         A$(0) = STR$(Z1 + 2)
  49.         A$(Z1) = A$(K)
  50.         A$(Z1 + 1) = "\A" + V$
  51.         A$(K) = "\Q" + X$ + "\" + A$ + STR$(Z1 + 1) + "\" + B$ + STR$(Z1) + "\"
  52.     LOOP
  53.  
  54. '  SUBROUTINE TO PRINT QUESTIONS
  55. SUB SHOWQUESTION ()
  56.     Q$ = A$(K)
  57.     DO
  58.         FOR Z = 3 TO LEN(Q$)
  59.             IF MID$(Q$, Z, 1) = "\" THEN EXIT FOR ELSE PRINT MID$(Q$, Z, 1);
  60.         NEXT Z
  61.         INPUT C$: C$ = UCASE$(C$)
  62.         C$ = LEFT$(C$, 1)
  63.     LOOP UNTIL C$ = "Y" OR C$ = "N"
  64.     T$ = "\" + C$
  65.     FOR X = 3 TO LEN(Q$) - 1
  66.         IF MID$(Q$, X, 2) = T$ THEN EXIT FOR
  67.     NEXT X
  68.     FOR Y = X + 1 TO LEN(Q$)
  69.         IF MID$(Q$, Y, 1) = "\" THEN EXIT FOR
  70.     NEXT Y
  71.     K = VAL(MID$(Q$, X + 2, Y - X - 2))
  72.  
  73. SUB SHOWLIST ()
  74.     PRINT: PRINT "ANIMALS I ALREADY KNOW ARE:"
  75.     X = 0
  76.     FOR I = 1 TO 200
  77.         IF LEFT$(A$(I), 2) = "\A" THEN
  78.             PRINT TAB(15 * X);
  79.             FOR Z = 3 TO LEN(A$(I))
  80.                 IF MID$(A$(I), Z, 1) <> "\" THEN PRINT MID$(A$(I), Z, 1);
  81.             NEXT Z
  82.             X = X + 1: IF X = 4 THEN X = 0: PRINT
  83.         END IF
  84.     NEXT I
  85.     PRINT
  86.     PRINT
  87.  
  88. '        SUBROUTINE TO SAVE DATA FILE
  89. SUB KEEPDATA ()
  90.     OPEN "ANIMAL.DAT" FOR OUTPUT AS #2
  91.     FOR I = 0 TO 200
  92.         WRITE #2, A$(I)
  93.     NEXT I
  94.     CLOSE #2

Nice! you fixed the parsing problem for asking the questions and converted everything to SUBs AND saved the new learning to file!  I recommend UCASE$ conversions for all INPUTs so can type INPUTs in lower case and will be automatically converted to upper and remain consistent to the other parts of program.

 

See? the dog lesson didn't take though it did ask the right question, it thinks it hasn't guessed it yet.

Ha! I've still not analysed the structure to the A$() array, I wanted to see how that N got used at the start, looks like MidnightOwl... no  he SHARED it along with some others with his SUBs.

Oh there's the data, right at the top, allready to go if a file hadn't been started yet!
Now what does file look like after I started dog?

Animal.DAT the saved lesson on DOG, I see N is increased to 6
Quote
" 6"
"\QDOES IT SWIM\Y2\N3\"
"\AFISH"
"\QDOES IT FLY\N 5\Y 4\"
"\ABIRD"
"\ADOG"

So N is left at A$(0) and tracks the last line number in the file or A$() array. I still need to go over structure for storing data in array. MidnightOwl had to solve all that.


So how do we get practice for Kiara using arrays and then SUBs and FUNCTIONs? This might have been too hard but maybe a demo of what you can do with some practice behind you.

All newbies need this practice, they are the bigger hurdles for novice.





Title: Re: Guess the Animal
Post by: bplus on November 09, 2020, 11:31:48 am
I just added UCASE$ to all INPUTs of MidnightOwl's transformed code:

Code: QB64: [Select]
  1.  
  2. DATA "4","\QDOES IT SWIM\Y2\N3\","\AFISH","\ABIRD"
  3.  
  4. PRINT TAB(32); "ANIMAL"
  5. PRINT TAB(15); "CREATIVE COMPUTING  MORRISTOWN, NEW JERSEY"
  6. PRINT "PLAY 'GUESS THE ANIMAL'"
  7. PRINT "THINK OF AN ANIMAL AND THE COMPUTER WILL TRY TO GUESS IT."
  8. DIM SHARED A$(200), K, N, T$
  9. ON ERROR GOTO NOFILE
  10. OPEN "ANIMAL.DAT" FOR INPUT AS #3
  11. FOR I = 0 TO 200
  12.     INPUT #3, A$(I)
  13. NOFILE:
  14. FOR I = 0 TO 3
  15.     READ A$(I)
  16.  
  17. N = VAL(A$(0))
  18.  
  19. MAIN
  20.  
  21. '         MAIN CONTROL SECTION
  22. SUB MAIN ()
  23.     DO
  24.         DO
  25.             INPUT "ARE YOU THINKING OF AN ANIMAL (Y/N)"; A$: A$ = UCASE$(A$)
  26.         LOOP UNTIL A$ = "LIST" OR LEFT$(A$, 1) = "Y" OR LEFT$(A$, 1) = "N"
  27.         IF A$ = "LIST" THEN SHOWLIST: MAIN
  28.         IF LEFT$(A$, 1) = "N" THEN KEEPDATA: SYSTEM
  29.         K = 1
  30.         DO
  31.             SHOWQUESTION
  32.             IF LEN(A$(K)) = 0 THEN KEEPDATA: SYSTEM
  33.         LOOP WHILE LEFT$(A$(K), 2) = "\Q"
  34.         PRINT "IS IT A "; RIGHT$(A$(K), LEN(A$(K)) - 2);
  35.         INPUT A$
  36.         A$ = UCASE$(LEFT$(A$, 1)) '<<<<<<<<<<<<<<  fix to ucase$
  37.         IF LEFT$(A$, 1) = "Y" THEN PRINT "WHY NOT TRY ANOTHER ANIMAL?": MAIN
  38.         INPUT "THE ANIMAL YOU WERE THINKING OF WAS A "; V$
  39.         V$ = UCASE$(V$) '<<<<<<<<<<<<<<  fix to ucase$
  40.         PRINT "PLEASE TYPE IN A QUESTION THAT WOULD DISTINGUISH A"
  41.         PRINT V$; " FROM A "; RIGHT$(A$(K), LEN(A$(K)) - 2)
  42.         INPUT X$
  43.         X$ = UCASE$(X$) '<<<<<<<<<<<<<<  fix to ucase$
  44.         DO
  45.             PRINT "FOR A "; V$; " THE ANSWER WOULD BE ";
  46.             INPUT A$
  47.             A$ = UCASE$(A$) '<<<<<<<<<<<<<<  fix to ucase$
  48.         A$ = LEFT$(A$, 1): LOOP UNTIL A$ = "Y" OR A$ = "N"
  49.         IF A$ = "Y" THEN B$ = "N"
  50.         IF A$ = "N" THEN B$ = "Y"
  51.         Z1 = VAL(A$(0))
  52.         A$(0) = STR$(Z1 + 2)
  53.         A$(Z1) = A$(K)
  54.         A$(Z1 + 1) = "\A" + V$
  55.         A$(K) = "\Q" + X$ + "\" + A$ + STR$(Z1 + 1) + "\" + B$ + STR$(Z1) + "\"
  56.     LOOP
  57.  
  58. '  SUBROUTINE TO PRINT QUESTIONS
  59. SUB SHOWQUESTION ()
  60.     Q$ = A$(K)
  61.     DO
  62.         FOR Z = 3 TO LEN(Q$)
  63.             IF MID$(Q$, Z, 1) = "\" THEN EXIT FOR ELSE PRINT MID$(Q$, Z, 1);
  64.         NEXT Z
  65.         INPUT C$: C$ = UCASE$(C$)
  66.         C$ = LEFT$(C$, 1)
  67.     LOOP UNTIL C$ = "Y" OR C$ = "N"
  68.     T$ = "\" + C$
  69.     FOR X = 3 TO LEN(Q$) - 1
  70.         IF MID$(Q$, X, 2) = T$ THEN EXIT FOR
  71.     NEXT X
  72.     FOR Y = X + 1 TO LEN(Q$)
  73.         IF MID$(Q$, Y, 1) = "\" THEN EXIT FOR
  74.     NEXT Y
  75.     K = VAL(MID$(Q$, X + 2, Y - X - 2))
  76.  
  77. SUB SHOWLIST ()
  78.     PRINT: PRINT "ANIMALS I ALREADY KNOW ARE:"
  79.     X = 0
  80.     FOR I = 1 TO 200
  81.         IF LEFT$(A$(I), 2) = "\A" THEN
  82.             PRINT TAB(15 * X);
  83.             FOR Z = 3 TO LEN(A$(I))
  84.                 IF MID$(A$(I), Z, 1) <> "\" THEN PRINT MID$(A$(I), Z, 1);
  85.             NEXT Z
  86.             X = X + 1: IF X = 4 THEN X = 0: PRINT
  87.         END IF
  88.     NEXT I
  89.     PRINT
  90.     PRINT
  91.  
  92. '        SUBROUTINE TO SAVE DATA FILE
  93. SUB KEEPDATA ()
  94.     OPEN "ANIMAL.DAT" FOR OUTPUT AS #2
  95.     FOR I = 0 TO 200
  96.         WRITE #2, A$(I)
  97.     NEXT I
  98.     CLOSE #2
  99.  
  100.  
Title: Re: Guess the Animal
Post by: Kiara87 on November 09, 2020, 03:42:14 pm
REMline is a .bas program that needs the program name for the .BAS Source file to remove the line numbers from.

Store the remline.bas program in same folder where your .bas file is that you want to remove line numbers from.
RUN the program to compile it. (Make sure the Save EXE in same folder as Source is bulleted in the RUN menu of IDE)
Run the program again and input the name of the bas file to remove the numbers from (looks like it does quotes too.)

What I do is look for every GOTO and GOSUB and comment those line numbers then I remove all line numbers not commented then I remove comments to line numbers and make sure the program still works correctly.

now i understand REMLINE  thanks
Title: Re: Guess the Animal
Post by: bplus on November 10, 2020, 09:01:46 pm
Complete makeover from 73 Lines of Code (LOC) to 64! More traces of old style removed including all CAPITALIZATION.

Code: QB64: [Select]
  1. _TITLE "Guess Animal - QB64 Conversion of Morristown Version" 'watching LOC, see code notes at bottom
  2. REDIM A$(200)
  3. L$ = CHR$(10) ' L is for line or next line
  4. PRINT L$ + SPC(35) + "Guess Animal" + L$ + L$ + SPC(11) + "Originally from: CREATIVE COMPUTING  MORRISTOWN, NEW JERSEY" + L$ + L$ + L$
  5. PRINT " Think of an animal and the computer will try and guess it." + L$
  6. IF _FILEEXISTS("ANIMAL.DAT") THEN
  7.     OPEN "ANIMAL.DAT" FOR INPUT AS #3
  8.     FOR I = 0 TO 200
  9.         INPUT #3, A$(I)
  10.     NEXT
  11.     CLOSE #3
  12.     FOR I = 0 TO 3
  13.         READ A$(I)
  14.     NEXT I
  15. N = VAL(A$(0))
  16.     DO
  17.         A$ = LCASE$(npt$(L$ + " Are you thinking of an animal (y/n/list)"))
  18.         IF A$ = "list" THEN
  19.             PRINT L$ + " Animals I already know are:" ' listing computers aimals
  20.             FOR I = 1 TO 200
  21.                 IF LEFT$(A$(I), 2) = "\A" THEN PRINT "   " + MID$(A$(I), 3),
  22.             NEXT I
  23.             PRINT L$ + L$
  24.         END IF
  25.     LOOP UNTIL LEFT$(A$, 1) = "y" OR LEFT$(A$, 1) = "n"
  26.     IF LEFT$(A$, 1) = "n" THEN EXIT DO 'bug out main DO doesn't want to play
  27.     K = 1
  28.     WHILE LEFT$(A$(K), 2) = "\Q"
  29.         Q$ = A$(K)
  30.         DO 'parse question and use as prompt to get c$
  31.             C$ = UCASE$(LEFT$(npt$(" " + MID$(Q$, 3, INSTR(3, Q$, "\") - 3)), 1))
  32.         LOOP UNTIL C$ = "Y" OR C$ = "N"
  33.         X = INSTR(3, LEFT$(Q$, LEN(Q$) - 1), "\" + C$)
  34.         Y = INSTR(X + 1, Q$, "\")
  35.         K = VAL(MID$(Q$, X + 2, Y - X - 2))
  36.         IF LEN(A$(K)) = 0 THEN EXIT DO ' bug out of main DO loop, out of questions
  37.     WEND
  38.     A$ = UCASE$(npt$(" Is it a " + RIGHT$(A$(K), LEN(A$(K)) - 2)))
  39.     IF LEFT$(A$, 1) = "Y" THEN PRINT " Why not try another animal?": _CONTINUE 'skip rest of this loop
  40.     V$ = LCASE$(npt$(" The animal you were thinking of is a "))
  41.     X$ = npt$(" Please type a question that would distinguish a " + V$ + " from a " + RIGHT$(A$(K), LEN(A$(K)) - 2))
  42.     DO
  43.         A$ = UCASE$(LEFT$(npt$(" For a " + V$ + " the answer would be "), 1))
  44.     LOOP UNTIL A$ = "Y" OR A$ = "N"
  45.     IF A$ = "Y" THEN B$ = "N" ELSE B$ = "Y"
  46.     Z1 = VAL(A$(0))
  47.     A$(0) = _TRIM$(STR$(Z1 + 2))
  48.     A$(Z1) = A$(K)
  49.     A$(Z1 + 1) = "\A" + V$
  50.     A$(K) = "\Q" + X$ + "\" + A$ + _TRIM$(STR$(Z1 + 1)) + "\" + B$ + _TRIM$(STR$(Z1)) + "\"
  51. OPEN "ANIMAL.DAT" FOR OUTPUT AS #2
  52. FOR I = 0 TO 200
  53.     WRITE #2, A$(I)
  54. DATA "4","\QDoes it live in water\Y2\N3\","\Afish","\Abird"
  55. FUNCTION npt$ (prompt$) ' nice way to get more variable data into the prompt!
  56.     PRINT prompt$;
  57.     INPUT npt$
  58.  
  59. '===================================== Code Notes down here to watch LOC of this app =======================================
  60.  
  61. ' 2020-11-10 b+ Makeover = 2nd go at Midnight Owl's great start on conversion
  62. ' note: first mod of Midnight's converstion added UCASE$ to all INPUTs solving a little user friendly problem.
  63. ' 2020-11-10 A step further was to revise PRINT prompt : INPUT A$ : UCASE$(A$) into a single function.
  64. ' Then one time call sub routines I put back into main code includeing the MAIN SUB!
  65. ' That includes: the LIST animals SUB for LIST option (which I made known in prompt).
  66. ' The SHOW QUESTION SUB and the KEEPDATA SUB.
  67. ' I also removed the ON error and used more modern check _FILEEXISTS
  68.  
  69. ' savings: admittedly some of this makes understanding of code less clear
  70. ' for X, Y use INSTR to skip FOR... NEXT loops
  71.  
  72. ' From this:
  73. ' T$ = "\" + C$
  74. ' FOR X = 3 TO LEN(Q$) - 1
  75. '     IF MID$(Q$, X, 2) = T$ THEN EXIT FOR
  76. ' NEXT X
  77.  
  78. ' to this
  79. ' X = INSTR(3, LEFT$(Q$, LEN(Q$) - 1), "\" + C$)
  80.  
  81. ' and from this
  82. ' FOR Y = X + 1 TO LEN(Q$)
  83. '     IF MID$(Q$, Y, 1) = "\" THEN EXIT FOR
  84. ' NEXT Y
  85. ' to this!
  86. ' Y = INSTR(X + 1, Q$, "\")
  87.  
  88. ' I decided to get rid of all the CAPITALIZATION and make normal looking sentences. So I modified the INPUT Function
  89. ' renaming it npt$ (input$ without vowels). Now if you want all caps use: UCASE$(npt$(prompt$)), same for lower
  90. ' case. Since humans swim I changed the first question: Does it live in water? Now how would you answer that for beaver?
  91. ' I hope not yes :) Nor do beavers or rabbits hibernate, I am learning myself!
  92. ' 2020-11-10 post 64 LOC down from original 73, not bad since it is complete makeover.
  93.  
  94.