Advertisement
mikjaer

Untitled

Aug 30th, 2021
3,475
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
XBasic 1.69 KB | None | 0 0
  1. 0010 PROC compare(guess$,secret$)
  2. 0020   correct:=0
  3. 0021   misplaced:=0
  4. 0030   FOR i:=1 TO 4 DO
  5. 0040     IF guess$(i:i)=secret$(i:i) THEN
  6. 0050       correct:=correct+1
  7. 0051       guess$(i:i):=" "
  8. 0052       secret$(i:i):=" "
  9. 0060     ENDIF
  10. 0070   ENDFOR i
  11. 0071   FOR i:=1 TO 4 DO
  12. 0072     FOR c:=1 TO 4 DO
  13. 0073       IF guess$(i:i)<>" " AND guess$(i:i)=secret$(c:c) THEN
  14. 0074         misplaced:=misplaced+1
  15. 0075         guess$(i:i):=" "
  16. 0076         secret$(c:c):=" "
  17. 0077       ENDIF
  18. 0078     ENDFOR c
  19. 0079   ENDFOR i
  20. 0080   PRINT AT CURROW-1,24: "Correct: ",correct," Misplaced: ",misplaced
  21. 0090 ENDPROC compare
  22. 0100 FUNC isin(v,s$)
  23. 0110   FOR i:=1 TO LEN(s$) DO
  24. 0120     IF VAL(s$(i:i))=v THEN RETURN TRUE
  25. 0130   ENDFOR i
  26. 0140   RETURN FALSE
  27. 0150 ENDFUNC isin
  28. 0160 FUNC validate(guess$)
  29. 0170   IF LEN(guess$)<>4 THEN
  30. 0180     RETURN FALSE
  31. 0190   ENDIF
  32. 0200   FOR i:=1 TO 4 DO
  33. 0210     IF NOT guess$(i:i) IN "0123456789" THEN RETURN FALSE
  34. 0220   ENDFOR i
  35. 0230   RETURN TRUE
  36. 0240 ENDFUNC validate
  37. 0250 secret$:=""
  38. 0260 FOR i:=1 TO 4 DO
  39. 0270   REPEAT
  40. 0280     candidate:=INT(RND*8)+1
  41. 0290   UNTIL NOT isin(candidate,secret$)
  42. 0300   secret$:=secret$+STR$(candidate)
  43. 0310   PRINT "Generated value numer ",i," which was ",candidate
  44. 0320 ENDFOR i
  45. 0330 PAGE
  46. 0340 PRINT "Welcome to Mastermind by RetroComputing with Mike"
  47. 0350 guesses:=0
  48. 0360 REPEAT
  49. 0370   INPUT "Guess the code:": guess$
  50. 0380   IF NOT validate(guess$) THEN
  51. 0390     PRINT "Your guess should consists of 4 numbers"
  52. 0400   ELSE
  53. 0410     guesses:=guesses+1
  54. 0420     compare(guess$,secret$)
  55. 0430   ENDIF
  56. 0440 UNTIL guess$=secret$
  57. 0450 PRINT "Congratulations, you guessed the secret in ",guesses," attempts"
  58.  
  59.  
  60.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement