Author Topic: Guess the Animal  (Read 7176 times)

0 Members and 1 Guest are viewing this topic.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Guess the Animal
« Reply #15 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!

Offline Kiara87

  • Forum Regular
  • Posts: 164
    • View Profile
Re: Guess the Animal
« Reply #16 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
se avessi solo un'ora per salvare il mondo, passerei 55 minuti per definire bene il problema e 5 a trovare la soluzione

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Guess the Animal
« Reply #17 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.

« Last Edit: November 08, 2020, 09:31:18 pm by bplus »

Offline MidnightOwl

  • Newbie
  • Posts: 5
    • View Profile
Re: Guess the Animal
« Reply #18 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]

Offline MidnightOwl

  • Newbie
  • Posts: 5
    • View Profile
Re: Guess the Animal
« Reply #19 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

Offline Kiara87

  • Forum Regular
  • Posts: 164
    • View Profile
Re: Guess the Animal
« Reply #20 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
se avessi solo un'ora per salvare il mondo, passerei 55 minuti per definire bene il problema e 5 a trovare la soluzione

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Guess the Animal
« Reply #21 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.

 
Midnight owl animal version.PNG

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.





« Last Edit: November 09, 2020, 11:13:46 am by bplus »

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Guess the Animal
« Reply #22 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.  

Offline Kiara87

  • Forum Regular
  • Posts: 164
    • View Profile
Re: Guess the Animal
« Reply #23 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
se avessi solo un'ora per salvare il mondo, passerei 55 minuti per definire bene il problema e 5 a trovare la soluzione

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Guess the Animal
« Reply #24 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.