Mastermind: Difference between revisions
m
syntax highlighting fixup automation
(Mastermind in FreeBASIC) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 58:
{{trans|Python}}
<
[String] output_arr
Line 110:
print(‘------------------------------------’)
play_game()</
{{out}}
Line 140:
=={{header|Action!}}==
<
DEFINE MAXCOLORS="20"
DEFINE MINLENGTH="4"
Line 306:
PrintF(", try again (%B tries):%E",tries)
OD
RETURN</
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Mastermind.png Screenshot from Atari 8-bit computer]
Line 332:
{{works with|Ada|Ada|2012}}
<
with Ada.Numerics.Discrete_Random;
with Ada.Strings.Fixed;
Line 493:
end;
end MasterMind;
</syntaxhighlight>
{{out}}
<pre>How many colors? 9
Line 546:
{{works with|GNU APL}}
<
⍝ Define the alphabet
Line 676:
Mastermind
)OFF</
=={{header|AutoHotkey}}==
<
ww := floor(w/2-2), hh := floor(h/2-2)
grid := [], dx := w*4.5
Line 846:
GuiControl,, Radio%A_Index%, % pegs[A_Index]
return
;-----------------------------------------------------------------------</
=={{header|C++}}==
<
#include <algorithm>
#include <ctime>
Line 997:
return 0;
}
</syntaxhighlight>
{{out}}
<pre>
Line 1,028:
[https://easylang.online/apps/mastermind.html Run it]
<syntaxhighlight lang="text">col[] = [ 802 990 171 229 950 808 ]
len code[] 4
len guess[] 4
Line 1,186:
.
.
call new</
=={{header|FreeBASIC}}==
<
Dim As String colors(1 To 4) => {"A", "B", "C", "D"}
Dim Shared As Integer nr, ub', numlet=4, lencod=4
Line 1,302:
Color 14: Print "The correct combination was: "; pz
Color 7: Print !"\nEnd of game"
Sleep</
=={{header|Go}}==
<
import (
Line 1,494:
none := m.holes - black - white
return blacks[:black] + whites[:white] + nones[:none], black == m.holes
}</
{{out|note=using Knuth's five-guess algorithm}}
<pre>
Line 1,519:
You can try it [http://paulo-jorente.de/webgames/repos/mastermind/ here].
<
class Mastermind {
constructor() {
Line 1,752:
mm.check()
});
</syntaxhighlight>
To test you'll need a HTML file
Line 1,888:
=={{header|Julia}}==
GUI version, uses the Gtk toolkit.
<
struct Guess
Line 2,097:
mastermindapp()
</syntaxhighlight>
=={{header|Kotlin}}==
{{trans|C++}}
<
import java.util.Random
Line 2,218:
val m = Mastermind(4, 8, 12, false)
m.play()
}</
Sample input/output (showing last 2 guesses only):
Line 2,258:
=={{header|Lua}}==
Based on C++
<
math.randomseed( os.time() )
local black, white, none, code = "X", "O", "-"
Line 2,337:
if arg[4] ~= nil and arg[4] == "true" or arg[4] == "false" then rept = ( arg[4] == "true" ) end
play()
</syntaxhighlight>
{{out}}
<pre>
Line 2,364:
=={{header|Nim}}==
{{trans|Python}}
<
proc encode(correct, guess: string): string =
Line 2,414:
randomize()
playGame()</
{{out}}
Line 2,442:
=={{header|Perl}}==
{{trans|Raku}}
<
print 'Enter pool size, puzzle size, attempts allowed: ';
Line 2,493:
sub lose { print 'Too bad, you ran out of guesses. The solution was: ' . join(' ',@puzzle) . "\n"; exit }
</syntaxhighlight>
Sample output, omitting redundant instructions.
{{out}}
Line 2,515:
=={{header|Phix}}==
OTT GUI solution, fully configurable with solver.
<
constant SET_LIMIT = 1_000_000 -- above this, it uses random sampling.
Line 3,083:
IupClose()
end procedure
main()</
=={{header|Prolog}}==
<
mastermind(Colours, Length, Guesses, Duplicates) :-
Line 3,187:
N > 0,
N1 is N - 1,
truncate_list(T, N1, R).</
{{out}}
<pre>
Line 3,238:
{{works with|cpython|3.7.3}}
Tested in Python 3.7.3. Includes input verification.
<
import random
Line 3,299:
play_game()
</syntaxhighlight>
{{out}}
<pre>
Line 3,328:
{{works with|Rakudo|2017.01}}
By default, plays classic Mastermind using letters in place of colors. ( 4 chosen from 6, no repeats, 10 guess limit. ) Pass in parameters to modify the game. Enter a string of --length (default 4) letters with or without spaces. Guesses accept lower or upper case.
<syntaxhighlight lang="raku"
Int :$colors where 1 < * < 21 = 6, Int :$length where 3 < * < 11 = 4,
Int :$guesses where 7 < * < 21 = 10, Bool :$repeat = False
Line 3,386:
sub lose { say 'Too bad, you ran out of guesses. The solution was: ', $puzzle; exit }
}</
{{out|Sample output}}
<pre>Valid letter, but wrong position: ○ - Correct letter and position: ●
Line 3,403:
=={{header|REXX}}==
More checks could have been added (for illegal inputs and illegal options).
<
parse arg let wid mxG oRep seed _ /*obtain optional arguments from the CL*/
arg . . . rep . /*get uppercase 4th argument " " " */
Line 3,480:
if val > mx then call ser ? "has a value greater than " mx /*◄■■■■■■ optional vetting.*/
if ?=='REP' & \datatype(val,W) then call ser "Value for REPEATS isn't valid: " oRep /*◄■■■■■■ optional vetting.*/
return 1</
'''output'''
<pre>
Line 3,537:
=={{header|Ring}}==
<
# Project : Mastermind
Line 3,631:
next
see nl
</syntaxhighlight>
Output:
<pre>
Line 3,647:
=={{header|Rust}}==
{{libheader|rand}}
<
use rand::prelude::*;
Line 3,809:
res.sort_by(|a, b| b.cmp(a));
res.into_iter().collect()
}</
{{out}}
<pre>
Line 3,862:
=={{header|SQL}}==
<
-- Create Table
-- Distinct combination
Line 3,933:
check_white(guesses.*, mastermind.*)
FROM guesses, mastermind
</syntaxhighlight>
=={{header|Wren}}==
Line 3,939:
{{libheader|Wren-ioutil}}
{{libheader|Wren-str}}
<
import "/ioutil" for Input
import "/str" for Str
Line 4,044:
var m = Mastermind.new(4, 8, 12, false)
m.play()</
{{out}}
Line 4,105:
=={{header|zkl}}==
{{trans|C++}}
<
fcn init(code_len,guess_count){
var codeLen =code_len.max(4).min(10);
Line 4,151:
return(black==codeLen,"X"*black + "O"*white)
}
}(4,12).play();</
{{out}}
<pre>
|