Author Topic: carta forbice sasso  (Read 3931 times)

0 Members and 1 Guest are viewing this topic.

Offline Kiara87

  • Forum Regular
  • Posts: 164
    • View Profile
carta forbice sasso
« on: September 11, 2020, 07:14:31 pm »
Hello everyone
place this game known to all
the game is called rock paper scissors
place here the programs he created to learn programming
I accept your criticism and your advice to improve the code and improve myself more and more
the game consists of choosing via input
1-Carta, 2-Forbice, 3,Sasso
anyone who knows the game knows how to win in this game

italiano
salve a tutti
lascio qui questo mio progetto
essendo ancora un principiante in questo campo il codice sarà un po contorto
magari non è alla perfezione ma cercherò di migliorarlo
penso che per imparare bisogna condividere i propri proggetti e confrontarli con altri
magari proverò aggiungere lizard è spock
e magari migliorare il codice

Code: QB64: [Select]
  1. DIM FNames(3) AS STRING
  2. LOCATE 1, 1: PRINT "É"; STRING$(78, 205); "»" 'string$ scrive 78 volte il valore in ascii del 205
  3. LOCATE 2, 1: PRINT "º"; TAB(80); "º" 'tab significa fai un spazio di 80 caratteri una tabbulazione
  4. LOCATE 3, 1: PRINT "È"; STRING$(78, 205); "¼" 'string$ scrive 78 volte il valore in ascii del 205
  5. LOCATE 2, 30: PRINT "=== Carta Forbice Sasso ==="
  6. PRINT "Premi qualsiasi tasto per iniziare..."
  7. ricomincia:
  8. tasto$ = INPUT$(1)
  9. tentativi = 6
  10. sconfitta = 0
  11. vincita = 0
  12.  
  13. 1
  14. LOCATE 1, 1: PRINT "É"; STRING$(78, 205); "»" 'string$ scrive 78 volte il valore in ascii del 205
  15. LOCATE 2, 1: PRINT "º"; TAB(80); "º" 'tab significa fai un spazio di 80 caratteri una tabbulazione
  16. LOCATE 3, 1: PRINT "È"; STRING$(78, 205); "¼" 'string$ scrive 78 volte il valore in ascii del 205
  17. LOCATE 2, 5: PRINT "hai"; tentativi; " tentativi, per vincere la partita devi vincere almeno 4 volte"
  18. LOCATE 7, 30
  19. PRINT "pc "; sconfitta; " tu "; vincita
  20.  
  21. FNames(1) = "carta"
  22. FNames(2) = "forbice"
  23. FNames(3) = "sasso"
  24.  
  25. FOR x = 1 TO 9
  26.  
  27.     COLOR 7
  28.     LOCATE 5, 10
  29.     INPUT " fai la tua scelta, 1-Carta, 2-Forbice, 3-Sasso "; x
  30.     CLS
  31.     'x = INT(RND * 3) + 1
  32.     x2 = INT(RND * 3) + 1
  33.  
  34.     IF tentativi <= 0 AND vincita > sconfitta THEN
  35.         LOCATE 15, 35
  36.         PRINT "hai vinto la partita "
  37.         SLEEP 1
  38.         PRINT " premi il tasto -> s due volte per continuare oppure un altro tasto per uscire"
  39.         tasto$ = INPUT$(1)
  40.         IF tasto$ = "s" THEN GOTO ricomincia
  41.         END
  42.     ELSEIF tentativi <= 0 AND sconfitta > vincita THEN
  43.  
  44.         LOCATE 15, 35
  45.         PRINT "hai perso la partita "
  46.         SLEEP 1
  47.         PRINT " premi il tasto -> s due volte per continuare oppure un altro tasto per uscire"
  48.         tasto$ = INPUT$(1)
  49.  
  50.         IF tasto$ = "s" THEN GOTO ricomincia
  51.         END
  52.     END IF
  53.  
  54.     IF x = x2 THEN
  55.         LOCATE 10, 1
  56.         PRINT " TU "; FNames(x)
  57.         PRINT " PC "; FNames(x2)
  58.         COLOR 3
  59.         PRINT " sono uguali pareggio "
  60.         tentativi = tentativi + 1
  61.  
  62.     ELSEIF x = 1 AND x2 = 2 THEN
  63.         LOCATE 10, 1
  64.         PRINT "TU "; FNames(x)
  65.         PRINT " PC "; FNames(x2)
  66.         PRINT " LA "; FNames(x); " viene tagliata dalle "; FNames(x2)
  67.         COLOR 4
  68.         PRINT " Hai perso "
  69.         sconfitta = sconfitta + 1
  70.     ELSEIF x = 2 AND x2 = 1 THEN
  71.         LOCATE 10, 1
  72.         PRINT "TU "; FNames(x)
  73.         PRINT " PC "; FNames(x2)
  74.         PRINT " LE "; FNames(x); " tagliano "; " la "; FNames(x2)
  75.         COLOR 10
  76.         PRINT " Hai vinto "
  77.         vincita = vincita + 1
  78.     ELSEIF x = 1 AND x2 = 3 THEN
  79.         LOCATE 10, 1
  80.         PRINT "TU "; FNames(x)
  81.         PRINT " PC "; FNames(x2)
  82.         PRINT " La "; FNames(x); " copre il "; FNames(x2)
  83.         COLOR 10
  84.         PRINT " Hai vinto "
  85.         vincita = vincita + 1
  86.     ELSEIF x = 3 AND x2 = 2 THEN
  87.         LOCATE 10, 1
  88.         PRINT "TU "; FNames(x)
  89.         PRINT " PC "; FNames(x2)
  90.         PRINT " IL "; FNames(x); " Distrugge le "; FNames(x2)
  91.         COLOR 10
  92.         PRINT " Hai vinto "
  93.         vincita = vincita + 1
  94.     ELSEIF x = 2 AND x2 = 3 THEN
  95.         LOCATE 10, 1
  96.         PRINT "TU "; FNames(x)
  97.         PRINT " PC "; FNames(x2)
  98.         PRINT " le "; FNames(x); " vengono distrutte dal "; FNames(x2)
  99.         COLOR 4
  100.         PRINT " Hai perso "
  101.         sconfitta = sconfitta + 1
  102.     ELSEIF x = 3 AND x2 = 1 THEN
  103.         LOCATE 10, 1
  104.         PRINT "TU "; FNames(x)
  105.         PRINT " PC "; FNames(x2)
  106.         PRINT " il "; FNames(x); " viene coperto dalla "; FNames(x2)
  107.         COLOR 4
  108.         PRINT " Hai perso"
  109.         sconfitta = sconfitta + 1
  110.     ELSEIF x > 3 OR x <= 0 THEN
  111.         LOCATE 10, 5
  112.         PRINT " scelta non valida"
  113.         LOCATE 12, 5
  114.         PRINT "puoi scegliere una delle 3 opzioni -> 1-Carta, 2-Forbice, 3-Sasso"
  115.         'ELSEIF x <= 0 THEN
  116.         ' LOCATE 10, 1
  117.         ' PRINT " Devi scegliere un opzione tra Carta Forbice oppure Sasso"
  118.     END IF
  119.     tentativi = tentativi - 1
  120.     GOTO 1
  121.  
  122.  
  123.  
  124.  
« Last Edit: September 11, 2020, 08:06:35 pm by Kiara87 »
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: carta forbice sasso
« Reply #1 on: September 11, 2020, 08:17:20 pm »
Yes, I have one of those :)
Code: QB64: [Select]
  1. 1 computer$ = MID$("rps", INT(RND * 3) + 1, 1)
  2. INPUT "Human please enter r for rock, p for paper, s for scissors any other looses "; human$
  3. PRINT "Computer picked: "; computer$
  4. IF human$ = computer$ THEN PRINT "Tie" + CHR$(10): GOTO 1
  5. IF INSTR(INSTR("rps", computer$), "rpsr", human$) = INSTR("rps", computer$) + 1 THEN PRINT "Human wins" + CHR$(10) ELSE PRINT "Computer wins" + CHR$(10)
  6.  

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: carta forbice sasso
« Reply #2 on: September 11, 2020, 10:39:38 pm »
I don't know about this:
 
I wonder if you ever get to next..PNG


Do you ever get to next x?

Offline Kiara87

  • Forum Regular
  • Posts: 164
    • View Profile
Re: carta forbice sasso
« Reply #3 on: September 12, 2020, 03:54:35 am »
I don't know about this:
 
I wonder if you ever get to next..PNG


Do you ever get to next x?

you're right but I put the goto for the reason not to delete the frame
being that it is a game that must only end if you win or lose
going to the next of x is useless

I just used the next loop to call the x of the variable

or can you use a gosub?

as in this case I have given a small modification
Code: QB64: [Select]
  1. DIM FNames(3) AS STRING
  2. LOCATE 1, 1: PRINT "É"; STRING$(78, 205); "»" 'string$ scrive 78 volte il valore in ascii del 205
  3. LOCATE 2, 1: PRINT "º"; TAB(80); "º" 'tab significa fai un spazio di 80 caratteri una tabbulazione
  4. LOCATE 3, 1: PRINT "È"; STRING$(78, 205); "¼" 'string$ scrive 78 volte il valore in ascii del 205
  5. LOCATE 2, 30: PRINT "=== Carta Forbice Sasso ==="
  6. PRINT "Premi qualsiasi tasto per iniziare..."
  7. ricomincia:
  8. tasto$ = INPUT$(1)
  9. tentativi = 6
  10. sconfitta = 0
  11. vincita = 0
  12.  
  13. 1
  14. LOCATE 1, 1: PRINT "É"; STRING$(78, 205); "»" 'string$ scrive 78 volte il valore in ascii del 205
  15. LOCATE 2, 1: PRINT "º"; TAB(80); "º" 'tab significa fai un spazio di 80 caratteri una tabbulazione
  16. LOCATE 3, 1: PRINT "È"; STRING$(78, 205); "¼" 'string$ scrive 78 volte il valore in ascii del 205
  17. LOCATE 2, 5: PRINT "hai"; tentativi; " tentativi, per vincere la partita devi vincere almeno 4 volte"
  18. LOCATE 7, 30
  19. PRINT "pc "; sconfitta; " tu "; vincita
  20.  
  21. FNames(1) = "carta"
  22. FNames(2) = "forbice"
  23. FNames(3) = "sasso"
  24.  
  25. FOR x = 1 TO 9
  26.  
  27.     COLOR 7
  28.     LOCATE 5, 10
  29.     INPUT " fai la tua scelta, 1-Carta, 2-Forbice, 3-Sasso "; x
  30.     CLS
  31.     'x = INT(RND * 3) + 1
  32.     x2 = INT(RND * 3) + 1
  33.  
  34.     IF tentativi <= 0 AND vincita > sconfitta THEN
  35.         LOCATE 15, 35
  36.         PRINT "hai vinto la partita "
  37.         LOCATE 16, 35
  38.         PRINT "pc "; sconfitta; " tu "; vincita
  39.         SLEEP 1
  40.         PRINT " premi il tasto -> s due volte per continuare oppure un altro tasto per uscire"
  41.         tasto$ = INPUT$(1)
  42.         IF LCASE$(tasto$) = "s" THEN GOTO ricomincia
  43.         END
  44.     ELSEIF tentativi <= 0 AND sconfitta > vincita THEN
  45.  
  46.         LOCATE 15, 35
  47.         PRINT "hai perso la partita "
  48.         LOCATE 16, 35
  49.         PRINT "pc "; sconfitta; " tu "; vincita
  50.         SLEEP 1
  51.         PRINT " premi il tasto -> s due volte per continuare oppure un altro tasto per uscire"
  52.         tasto$ = INPUT$(1)
  53.  
  54.         IF LCASE$(tasto$) = "s" THEN GOTO ricomincia
  55.         END
  56.     ELSEIF tentativi <= 0 AND sconfitta = vincita THEN
  57.  
  58.         LOCATE 15, 35
  59.         PRINT "hai Paregiato la Partita "
  60.         LOCATE 16, 35
  61.         PRINT "pc "; sconfitta; " tu "; vincita
  62.         SLEEP 1
  63.         PRINT " premi il tasto -> s due volte per continuare oppure un altro tasto per uscire"
  64.         tasto$ = INPUT$(1)
  65.  
  66.         IF LCASE$(tasto$) = "s" THEN GOTO ricomincia
  67.         END
  68.  
  69.     END IF
  70.  
  71.     IF x = x2 THEN
  72.         LOCATE 10, 1
  73.         PRINT " TU "; FNames(x)
  74.         PRINT " PC "; FNames(x2)
  75.         COLOR 3
  76.         PRINT " sono uguali pareggio "
  77.         tentativi = tentativi + 1
  78.  
  79.     ELSEIF x = 1 AND x2 = 2 THEN
  80.         LOCATE 10, 1
  81.         PRINT "TU "; FNames(x)
  82.         PRINT " PC "; FNames(x2)
  83.         PRINT " LA "; FNames(x); " viene tagliata dalle "; FNames(x2)
  84.         COLOR 4
  85.         PRINT " Hai perso "
  86.         sconfitta = sconfitta + 1
  87.     ELSEIF x = 2 AND x2 = 1 THEN
  88.         LOCATE 10, 1
  89.         PRINT "TU "; FNames(x)
  90.         PRINT " PC "; FNames(x2)
  91.         PRINT " LE "; FNames(x); " tagliano "; " la "; FNames(x2)
  92.         COLOR 10
  93.         PRINT " Hai vinto "
  94.         vincita = vincita + 1
  95.     ELSEIF x = 1 AND x2 = 3 THEN
  96.         LOCATE 10, 1
  97.         PRINT "TU "; FNames(x)
  98.         PRINT " PC "; FNames(x2)
  99.         PRINT " La "; FNames(x); " copre il "; FNames(x2)
  100.         COLOR 10
  101.         PRINT " Hai vinto "
  102.         vincita = vincita + 1
  103.     ELSEIF x = 3 AND x2 = 2 THEN
  104.         LOCATE 10, 1
  105.         PRINT "TU "; FNames(x)
  106.         PRINT " PC "; FNames(x2)
  107.         PRINT " IL "; FNames(x); " Distrugge le "; FNames(x2)
  108.         COLOR 10
  109.         PRINT " Hai vinto "
  110.         vincita = vincita + 1
  111.     ELSEIF x = 2 AND x2 = 3 THEN
  112.         LOCATE 10, 1
  113.         PRINT "TU "; FNames(x)
  114.         PRINT " PC "; FNames(x2)
  115.         PRINT " le "; FNames(x); " vengono distrutte dal "; FNames(x2)
  116.         COLOR 4
  117.         PRINT " Hai perso "
  118.         sconfitta = sconfitta + 1
  119.     ELSEIF x = 3 AND x2 = 1 THEN
  120.         LOCATE 10, 1
  121.         PRINT "TU "; FNames(x)
  122.         PRINT " PC "; FNames(x2)
  123.         PRINT " il "; FNames(x); " viene coperto dalla "; FNames(x2)
  124.         COLOR 4
  125.         PRINT " Hai perso"
  126.         sconfitta = sconfitta + 1
  127.     ELSEIF x > 3 OR x <= 0 THEN
  128.         LOCATE 10, 5
  129.         PRINT " scelta non valida"
  130.         LOCATE 12, 5
  131.         PRINT "puoi scegliere una delle 3 opzioni -> 1-Carta, 2-Forbice, 3-Sasso"
  132.         'ELSEIF x <= 0 THEN
  133.         ' LOCATE 10, 1
  134.         ' PRINT " Devi scegliere un opzione tra Carta Forbice oppure Sasso"
  135.     ELSE
  136.         LOCATE 12, 5
  137.         PRINT "TU "; FNames(x)
  138.         PRINT " PC "; FNames(x2)
  139.  
  140.     END IF
  141.  
  142.     tentativi = tentativi - 1
  143.     GOSUB 1
  144.  
  145.     RETURN
se avessi solo un'ora per salvare il mondo, passerei 55 minuti per definire bene il problema e 5 a trovare la soluzione

Offline Kiara87

  • Forum Regular
  • Posts: 164
    • View Profile
Re: carta forbice sasso
« Reply #4 on: September 12, 2020, 03:57:40 am »
Yes, I have one of those :)
Code: QB64: [Select]
  1. 1 computer$ = MID$("rps", INT(RND * 3) + 1, 1)
  2. INPUT "Human please enter r for rock, p for paper, s for scissors any other looses "; human$
  3. PRINT "Computer picked: "; computer$
  4. IF human$ = computer$ THEN PRINT "Tie" + CHR$(10): GOTO 1
  5. IF INSTR(INSTR("rps", computer$), "rpsr", human$) = INSTR("rps", computer$) + 1 THEN PRINT "Human wins" + CHR$(10) ELSE PRINT "Computer wins" + CHR$(10)
  6.  

thanks also for your version
in a few lines of gorgeous code
now I will take a look at it to study it
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: carta forbice sasso
« Reply #5 on: September 12, 2020, 12:30:14 pm »
thanks also for your version
in a few lines of gorgeous code
now I will take a look at it to study it

Good luck with line 5. ;-) Get that and you've learned something!  :)

Offline Kiara87

  • Forum Regular
  • Posts: 164
    • View Profile
Re: carta forbice sasso
« Reply #6 on: September 12, 2020, 05:30:42 pm »
Good luck with line 5. ;-) Get that and you've learned something!  :)

IF INSTR(INSTR("rps", computer$), "rpsr", human$) = INSTR("rps", computer$) + 1 THEN PRINT "Human wins" + CHR$(10) ELSE PRINT "Computer wins" + CHR$(10)

I did not know that  instr it could be written twice after the if condition
obviously I discovered something new

therefore translated it would be
if in the instr string that in the second string checks if there is that string
so for this reason it is written twice

what does this character do?
+ CHR $ (10)
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: carta forbice sasso
« Reply #7 on: September 12, 2020, 06:54:09 pm »
;-))

Code: QB64: [Select]
  1. S$ = "What does this character do?" + CHR$(10) + "OK so what just happened?" + CHR$(10) + "Want to see it again? ;)"
  2.  
  3.  

Hey @Kiara87, remember INSTR returns a number, so what that complex string is doing is comparing the computers number with the humans. But also when INSTR is given 3 arguments the first specifies where in the string (2nd argument) to start looking for a match to the last argument.
« Last Edit: September 12, 2020, 07:22:45 pm by bplus »

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Re: carta forbice sasso
« Reply #8 on: September 14, 2020, 07:50:33 pm »
Hi kiara87
good step foward modular programming, very less GOTO and spaghetti code!

as exercise to improve the use of loops
in your code
translate each GOTO label to a WHILE WEND loop changing the flow of program, cancel the FOR NEXT that is not just your goal
chunk down to a littler block of code those lines that do the same thing
follow the example of Fellippe Heitoir about loops writing his code https://www.qb64.org/forum/index.php?topic=2992.msg122441#msg122441

For the 5linescode of Bplus, study each detail of each line of code, it is compressed code in few lines!

/ IT
un nuon passo verso la programmazione modulare, molti meno GOTO e codice spaghetti.

come esercizio per migliorare l'uso dei cicli
prendi il tuo codice e traduci ogni ciclo GOTO etichetti (vedi GOTO 1 e GOTO ricomincia in un ciclo WHILE WEND, questo ti spingerà a cambiare il flusso del tuo programma, cancella quel ciclo FOR NEXT che sembra usato fuori dal proprio scopo di esistenza, suddividi in pezzi di codice più piccolo quelle linee di codice che fanno la stessa cosa.
segui gli esempi di Fellippe Heitoir sui cicli e scrivi il suo codice https://www.qb64.org/forum/index.php?topic=2992.msg122441#msg122441

Per la gara in 5 linee di Bplus, studia attentamente ogni dettaglio di ciascuna riga, è codice compresso in poche righe
Programming isn't difficult, only it's  consuming time and coffee

Offline Kiara87

  • Forum Regular
  • Posts: 164
    • View Profile
Re: carta forbice sasso
« Reply #9 on: September 16, 2020, 03:49:44 pm »
Hi kiara87
good step foward modular programming, very less GOTO and spaghetti code!

as exercise to improve the use of loops
in your code
translate each GOTO label to a WHILE WEND loop changing the flow of program, cancel the FOR NEXT that is not just your goal
chunk down to a littler block of code those lines that do the same thing
follow the example of Fellippe Heitoir about loops writing his code https://www.qb64.org/forum/index.php?topic=2992.msg122441#msg122441

For the 5linescode of Bplus, study each detail of each line of code, it is compressed code in few lines!

/ IT
un nuon passo verso la programmazione modulare, molti meno GOTO e codice spaghetti.

come esercizio per migliorare l'uso dei cicli
prendi il tuo codice e traduci ogni ciclo GOTO etichetti (vedi GOTO 1 e GOTO ricomincia in un ciclo WHILE WEND, questo ti spingerà a cambiare il flusso del tuo programma, cancella quel ciclo FOR NEXT che sembra usato fuori dal proprio scopo di esistenza, suddividi in pezzi di codice più piccolo quelle linee di codice che fanno la stessa cosa.
segui gli esempi di Fellippe Heitoir sui cicli e scrivi il suo codice https://www.qb64.org/forum/index.php?topic=2992.msg122441#msg122441

Per la gara in 5 linee di Bplus, studia attentamente ogni dettaglio di ciascuna riga, è codice compresso in poche righe

grazie per i consigli cercherò di migliorare come hai scritto nei tuoi ottimi consigli
il ciclo for lo messo per studiare un po il Pattern del ciclo
perchè volevo capire come hai letto nel programma che hai creato per me --> https://www.qb64.org/forum/index.php?topic=2891.msg121548#msg121548
volevo capire come fai a leggere dal ciclo for il file e come colori gli elementi
è in questo caso mi son chiesto vedendo un video su youtube forse devo studiare il Pattern  del ciclo for ?
cmq ancora non sono riuscito a finire il programma
sto cercando di studiare il linguaggio ovviamente ci vuole del tempo
grazie anche a te qualcosa lho capita è qualcosa no
magari quello che spieghi tu capisco meglio essendo che sei italiano
senza togliendo nulla ad altri programmatori qui che sono bravissimi ma visto che non so parlare inglese
non capisco più di tanto
se avessi solo un'ora per salvare il mondo, passerei 55 minuti per definire bene il problema e 5 a trovare la soluzione