Here it is:
[code=thinbasic] '---******************************************************
'---* Game of MasterMind *
'---* This version uses a four digit *
'---* code instead of using different *
'---* colors. *
'---******************************************************
'---******************************************************
'---* This is a console script so it has to have *
'---* the uses "console" to work *
'---******************************************************
uses "console"
'---******************************************************
'---* Here are all the variables used in the script *
'---******************************************************
dim Quit as long
dim myNum as string
dim guess as string
dim Playing as long
dim Play_again as long
dim Result as string
dim n as long
dim Num_check as string
dim Num_only as long
dim Player_guess as long
dim build_code as long
dim x as long
dim pos_correct as long
dim dig_correct as long
dim y as string
dim z as long
dim a_guess as string
dim guess_number as long
Intro() 'tell about game & how to play
Quit = %false 'variable changed to %true when time to quit while loop
while Quit = %False 'while loop where game played
guess_number = 1 'begin counting the number of guesses to win
myNum = "" 'variable to hold computer's number
randomize 'seeds the rnd function
myNum = int(rnd(0,9)) 'get first digit
while len(myNum) < 4 'while loop to build 4 digit number - quit when we have 4
x = int(rnd(0,9)) 'pick next random digit
if instr(myNum,x) = 0 then 'Make sure each digit is unique
myNum = myNum + x 'if it is add to computer's number
end if
wend 'move on when finished
console_writeline("I've picked a number") 'tell player computer has number
console_writeline("") 'then print blank line
Playing = %false 'variable = %true when player has
while Playing = %false 'guessed number. There is no offer for player to quit.
Num_only = %false 'variable = %true if player guess is acceptable
while Num_only = %false 'loop to check guess
Console_SetInputMode(%Console_ENABLE_PROCESSED_INPUT) 'lets player use backspace to edit guess before enter key pressed
console_writeline("Guess Number: " + guess_number) 'tell player the number of guess
console_write("What is your guess? ") 'ask for guess
guess = console_readline() 'get the guess
a_guess = left$(guess,(len(guess)-1)) 'take off cr/enter character from end of guess
Num_check = VERIFY(a_guess, "0123456789") 'make sure guess has only digits
select case Num_check
case <> 0
console_writeline("Your guess MUST be numbers ONLY!") 'in case player put a wrong character in guess
console_writeline("")
case else
N = len(a_guess)
if N = 4 then
Num_only = %true
else
console_writeline("Your guess must be 4 digit!") 'don't let guess be < 4 or > 4 digits
console_writeline("Try again")
console_writeline("")
end if
end select
wend
pos_correct = 0 'keep count of number in correct position
for x = 1 to 4 'look at all 4 digits and
if mid$(a_guess,x,1) = mid$(myNum,x,1) then 'add to count when needed
pos_correct += 1
end if
next
dig_correct = 0 'keep count of all digits that are correct
for x = 1 to 4 'even if they aren't in correct position
y = mid$(myNum,x,1)
for z = 1 to 4 'loop to keep count of digits that are correct
if y = mid$(a_guess,z,1) then
dig_correct += 1
exit for 'exit for loop when correct digit found
end if
next
next
if a_guess = myNum then 'see if player guessed computer's number
console_writeline("")
console_writeline("You guessed correctly! You WON!!!!!") 'tell player about win and
console_writeline("")
console_writeline("You took " + guess_number + " guesses") 'how many guesses it took
console_writeline("")
Playing = %true
else
console_writeline("")
guess_number += 1 'if not a win then increment the guess number by one
console_writeline("You have " + dig_correct + " digits correct") 'give number of digits correct
console_writeline("You have " + pos_correct + " digits in the correct position") 'and number of digits in correct position
console_writeline("")
end if
wend
console_write("Play again? ( (Y)es or (N)o )") 'after win ask if player wants to play again or quit
Play_again = %false
WHILE Play_again = %FALSE
'---Read keyboard input
Result = Console_inKeyb
'---Handle returned string
select case Result
case "n","N" 'all this if player says no
console_cls
Console_Writeline("")
console_writeline("GOODBYE!!!")
n = sleep(1000)
Play_again = %TRUE
Quit = %true
case "y","Y"
guess_number = 1 'all this if player says yes
console_cls
Play_again = %true
end select
wend
wend
function Intro() 'this function gives instructions about how to play
console_writeline("A Guessing Game!")
console_writeline("")
console_writeline("I will choose a 4-digit number.")
console_writeline("You will try to guess the number.")
Console_writeline("All I will tell you is how many digits are correct")
console_writeline("and how many are in the correct position.")
console_writeline("When you have guessed the number, you win")
console_writeline("")
end function
'---******************************************************
'---* End of script *
'---* That's all there is *
'---******************************************************
[/code]
Thanks
Sandy
Bookmarks