Mastermind: Difference between revisions
Content added Content deleted
(Mastermind in FreeBASIC) |
|||
Line 1,187: | Line 1,187: | ||
. |
. |
||
call new</lang> |
call new</lang> |
||
=={{header|FreeBASIC}}== |
|||
<lang freebasic>'--- Declaration of global variables --- |
|||
Dim As String colors(1 To 4) => {"A", "B", "C", "D"} |
|||
Dim Shared As Integer nr, ub', numlet=4, lencod=4 |
|||
Dim Shared As String*4 master, pz |
|||
ub = Ubound(colors) |
|||
nr = 0 |
|||
'--- SUBroutines and FUNCtions --- |
|||
Sub Encabezado |
|||
Dim As String dup |
|||
Color 11: Print "Welcome to Mastermind" |
|||
Print "=====================" + Chr(13) + Chr(10) : Color 15 |
|||
Print "You will need to guess a random code." |
|||
Print "For each guess, you will receive a hint:" |
|||
Print "X - denotes a correct letter," |
|||
Print "O - denotes a letter in the original" |
|||
Print " string but a different position." |
|||
Print "You have 12 attempts." |
|||
Print "Duplicates are not allowed." + Chr(10) |
|||
Print "Good luck!" + Chr(10) + Chr(10) : Color 7 |
|||
End Sub |
|||
Sub showresult(test() As String, place1 As Byte, place2 As Byte, place3 As Byte) |
|||
Dim As Integer r, n1, n2, n3 |
|||
Print Using "##: "; nr; |
|||
For r = 1 To Ubound(test) |
|||
Print test(r); |
|||
Next R |
|||
Print " : "; |
|||
For n1 = 1 To place1 |
|||
Print "X"; " "; |
|||
Next N1 |
|||
For n2 = 1 To place2 |
|||
Print "O"; " "; |
|||
Next N2 |
|||
For n3 = 1 To place3 |
|||
Print "-"; " "; |
|||
Next N3 |
|||
Print : Print |
|||
End Sub |
|||
Sub Inicio |
|||
Dim As Integer mind(ub), rands(ub) |
|||
Dim As Integer n, aleat |
|||
Dim As Boolean repeat = false |
|||
For n = 1 To ub |
|||
While true |
|||
aleat = (Rnd * (ub-1)) + 1 |
|||
If rands(aleat) <> 1 Then |
|||
mind(n) = aleat |
|||
rands(aleat) = 1 |
|||
Exit While |
|||
End If |
|||
Wend |
|||
Next n |
|||
For n = 1 To ub |
|||
Mid(master,n,1) = Chr(64 + mind(n)) |
|||
pz &= Chr(64 + mind(n)) |
|||
Next n |
|||
End Sub |
|||
'--- Main Program --- |
|||
Randomize Timer |
|||
Cls |
|||
Dim As Integer guesses = 12 |
|||
Encabezado |
|||
Inicio |
|||
Color 15: Print pz : Color 7 |
|||
Do |
|||
Dim As Integer n, p, d, x, posic |
|||
Dim As Integer places(1 To 2), place1, place2, place3 |
|||
Dim As String*4 testbegin |
|||
Dim As String test(ub), mastertemp, tmp |
|||
Dim As Boolean flag = True |
|||
For p = 1 To Ubound(places) |
|||
places(p) = 0 |
|||
Next p |
|||
nr += 1 |
|||
Input "Your guess (ABCD)? " , testbegin |
|||
For d = 1 To Ubound(test) |
|||
test(d) = Mid(testbegin,d,1) |
|||
Next d |
|||
For n = 1 To Ubound(test) |
|||
If Ucase(test(n)) <> Mid(master,n,1) Then flag = False |
|||
Next n |
|||
If flag = True Then |
|||
Color 10: Print !"\nWell done! You guess correctly." : Sleep : Exit Do |
|||
Else |
|||
For x = 1 To Len(master) |
|||
If Ucase(test(x)) = Mid(master,x,1) Then places(1) += 1 |
|||
Next x |
|||
mastertemp = master |
|||
For p = 1 To Ubound(test) |
|||
posic = Instr(mastertemp, Ucase(test(p))) |
|||
If posic > 0 Then |
|||
tmp = mastertemp |
|||
mastertemp = Left(tmp,posic-1) + Mid(tmp, posic+1, Len(tmp)-1) |
|||
places(2) += 1 |
|||
End If |
|||
Next p |
|||
End If |
|||
place1 = places(1) |
|||
place2 = places(2) - place1 |
|||
place3 = Len(master) - (place1 + place2) |
|||
showresult(test(), place1, place2, place3) |
|||
Loop Until nr = guesses |
|||
Color 14: Print "The correct combination was: "; pz |
|||
Color 7: Print !"\nEnd of game" |
|||
Sleep</lang> |
|||
=={{header|Go}}== |
=={{header|Go}}== |