Mastermind: Difference between revisions

m
syntax highlighting fixup automation
(Mastermind in FreeBASIC)
m (syntax highlighting fixup automation)
Line 58:
{{trans|Python}}
 
<langsyntaxhighlight lang="11l">F encode(correct, guess)
[String] output_arr
 
Line 110:
print(‘------------------------------------’)
 
play_game()</langsyntaxhighlight>
 
{{out}}
Line 140:
 
=={{header|Action!}}==
<langsyntaxhighlight Actionlang="action!">DEFINE MINCOLORS="2"
DEFINE MAXCOLORS="20"
DEFINE MINLENGTH="4"
Line 306:
PrintF(", try again (%B tries):%E",tries)
OD
RETURN</langsyntaxhighlight>
{{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}}
 
<langsyntaxhighlight Adalang="ada">with Ada.Text_IO;
with Ada.Numerics.Discrete_Random;
with Ada.Strings.Fixed;
Line 493:
end;
end MasterMind;
</syntaxhighlight>
</lang>
{{out}}
<pre>How many colors? 9
Line 546:
{{works with|GNU APL}}
 
<langsyntaxhighlight APLlang="apl">#!/usr/local/bin/apl -s -f --
 
⍝ Define the alphabet
Line 676:
 
Mastermind
)OFF</langsyntaxhighlight>
 
=={{header|AutoHotkey}}==
<langsyntaxhighlight AutoHotkeylang="autohotkey">w := h := 32, maxRows := 10, numPegs := 8
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
;-----------------------------------------------------------------------</langsyntaxhighlight>
 
=={{header|C++}}==
<langsyntaxhighlight lang="cpp">#include <iostream>
#include <algorithm>
#include <ctime>
Line 997:
return 0;
}
</syntaxhighlight>
</lang>
{{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</langsyntaxhighlight>
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight 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
Line 1,302:
Color 14: Print "The correct combination was: "; pz
Color 7: Print !"\nEnd of game"
Sleep</langsyntaxhighlight>
 
 
=={{header|Go}}==
 
<langsyntaxhighlight Golang="go">package main
 
import (
Line 1,494:
none := m.holes - black - white
return blacks[:black] + whites[:white] + nones[:none], black == m.holes
}</langsyntaxhighlight>
{{out|note=using Knuth's five-guess algorithm}}
<pre>
Line 1,519:
You can try it [http://paulo-jorente.de/webgames/repos/mastermind/ here].
 
<langsyntaxhighlight lang="javascript">
class Mastermind {
constructor() {
Line 1,752:
mm.check()
});
</syntaxhighlight>
</lang>
 
To test you'll need a HTML file
Line 1,888:
=={{header|Julia}}==
GUI version, uses the Gtk toolkit.
<langsyntaxhighlight lang="julia">using Gtk, Colors, Cairo, Graphics
 
struct Guess
Line 2,097:
 
mastermindapp()
</syntaxhighlight>
</lang>
 
=={{header|Kotlin}}==
{{trans|C++}}
<langsyntaxhighlight lang="scala">// version 1.2.51
 
import java.util.Random
Line 2,218:
val m = Mastermind(4, 8, 12, false)
m.play()
}</langsyntaxhighlight>
 
Sample input/output (showing last 2 guesses only):
Line 2,258:
=={{header|Lua}}==
Based on C++
<langsyntaxhighlight lang="lua">
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>
</lang>
{{out}}
<pre>
Line 2,364:
=={{header|Nim}}==
{{trans|Python}}
<langsyntaxhighlight Nimlang="nim">import random, sequtils, strformat, strutils
 
proc encode(correct, guess: string): string =
Line 2,414:
 
randomize()
playGame()</langsyntaxhighlight>
 
{{out}}
Line 2,442:
=={{header|Perl}}==
{{trans|Raku}}
<langsyntaxhighlight lang="perl">use List::Util qw(any);
 
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>
</lang>
Sample output, omitting redundant instructions.
{{out}}
Line 2,515:
=={{header|Phix}}==
OTT GUI solution, fully configurable with solver.
<langsyntaxhighlight Phixlang="phix">-- demo/rosetta/mastermind.exw
constant SET_LIMIT = 1_000_000 -- above this, it uses random sampling.
 
Line 3,083:
IupClose()
end procedure
main()</langsyntaxhighlight>
 
=={{header|Prolog}}==
 
<langsyntaxhighlight Prologlang="prolog">mastermind :- mastermind(7, 4, 8, no_duplicates).
 
mastermind(Colours, Length, Guesses, Duplicates) :-
Line 3,187:
N > 0,
N1 is N - 1,
truncate_list(T, N1, R).</langsyntaxhighlight>
{{out}}
<pre>
Line 3,238:
{{works with|cpython|3.7.3}}
Tested in Python 3.7.3. Includes input verification.
<langsyntaxhighlight lang="python">
import random
 
Line 3,299:
play_game()
 
</syntaxhighlight>
</lang>
{{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" perl6line>sub MAIN (
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 }
}</langsyntaxhighlight>
{{out|Sample output}}
<pre>Valid letter, but wrong position: ○ - Correct letter and position: ●
Line 3,403:
=={{header|REXX}}==
More checks could have been added &nbsp; (for illegal inputs and illegal options).
<langsyntaxhighlight lang="rexx">/*REXX pgm scores mastermind game with a human or CBLFs (Carbon Based Life Forms). */
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</langsyntaxhighlight>
'''output'''
<pre>
Line 3,537:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
# Project : Mastermind
 
Line 3,631:
next
see nl
</syntaxhighlight>
</lang>
Output:
<pre>
Line 3,647:
=={{header|Rust}}==
{{libheader|rand}}
<langsyntaxhighlight lang="rust">extern crate rand;
 
use rand::prelude::*;
Line 3,809:
res.sort_by(|a, b| b.cmp(a));
res.into_iter().collect()
}</langsyntaxhighlight>
{{out}}
<pre>
Line 3,862:
 
=={{header|SQL}}==
<langsyntaxhighlight lang="sql">
-- Create Table
-- Distinct combination
Line 3,933:
check_white(guesses.*, mastermind.*)
FROM guesses, mastermind
</syntaxhighlight>
</lang>
 
=={{header|Wren}}==
Line 3,939:
{{libheader|Wren-ioutil}}
{{libheader|Wren-str}}
<langsyntaxhighlight lang="ecmascript">import "random" for Random
import "/ioutil" for Input
import "/str" for Str
Line 4,044:
 
var m = Mastermind.new(4, 8, 12, false)
m.play()</langsyntaxhighlight>
 
{{out}}
Line 4,105:
=={{header|zkl}}==
{{trans|C++}}
<langsyntaxhighlight lang="zkl">class MasterMind{
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();</langsyntaxhighlight>
{{out}}
<pre>
10,327

edits