Morpion solitaire: Difference between revisions

→‎{{header|Wren}}: Minor tidy and fixed problem with 'mvprintw' now requiring a format specifier.
(Added Wren)
(→‎{{header|Wren}}: Minor tidy and fixed problem with 'mvprintw' now requiring a format specifier.)
 
(3 intermediate revisions by 3 users not shown)
Line 56:
=={{header|C}}==
Console play with ncurses. Length and touching rules can be changed at the begining of source code. 'q' key to quit, space key to toggle auto move, anykey for next move. Play is random. I got nowhere near the record 177 moves, but did approach the worst-possible (20) quite often.
<langsyntaxhighlight Clang="c">#include <ncurses.h>
#include <stdlib.h>
#include <unistd.h>
Line 280:
endwin();
return 0;
}</langsyntaxhighlight>
 
=={{header|Go}}==
{{libheader|goncurses}}
{{trans|C}}
<langsyntaxhighlight lang="go">package main
 
import (
Line 552:
gc.CBreak(false)
gc.Echo(true)
}</langsyntaxhighlight>
 
=={{header|Icon}} and {{header|Unicon}}==
Line 563:
=={{header|J}}==
With this program as the file m.ijs
<syntaxhighlight lang="j">
<lang J>
NB. turn will be a verb with GRID as y, returning GRID. Therefor:
NB. morpion is move to the power of infinity---convergence.
Line 671:
NB. example
smoutput play''
</syntaxhighlight>
</lang>
load the file into a j session to play an initial game and report the number of turns. We can play a game providing a vector of move numbers at which to report the output.
<pre>
Line 808:
{{libheader|nim-ncurses}}
 
<langsyntaxhighlight Nimlang="nim">import os, random, sequtils
import ncurses
 
Line 971:
endwin()
 
play()</langsyntaxhighlight>
 
{{out}}
Line 994:
Picks a move at random from all possible moves at each step. A sample game is shown.
The largest score found so far (from just random play) is 92, also shown below.
<syntaxhighlight lang="perl">use strict;
<lang perl>#!/usr/bin/perl
 
use strict; # https://rosettacode.org/wiki/Morpion_solitaire
use warnings;
use List::Util qw( none );
Line 1,050 ⟶ 1,048:
=~ s/.{60}\K /\n/gr, "\n";
tr/O/X/;
print $_, "move count: $count\n";</langsyntaxhighlight>
This runs on a 30x30 grid (as advised by Talk page).
Each move is shown as the end points of a line, startcolumn,startrow->endcolumn,endrow where row and column numbers
Line 1,145 ⟶ 1,143:
move count: 92
</pre>
 
A faster, shorter version without the single step display.
<br>
Uses the same kind of block shift/or technology I used in "Forest fire" and have used
for Conway's Life.
<syntaxhighlight lang="perl">use strict;
<lang perl>#!/usr/bin/perl
 
use strict; # https://rosettacode.org/wiki/Morpion_solitaire
use warnings;
use List::Utilfeature qw( none )'bitwise';
use List::Util 'none';
 
local $_ = <<END;
Line 1,169 ⟶ 1,166:
$_ = tr/X./ /r . tr/./ /r . tr/X./ /r; # expand to 30x30 and spaces
 
my($count, @moves, %used) = 0;
my %used;
my $count = 0;
while( 1 )
{
Line 1,177 ⟶ 1,172:
for my $i ( 1, 30 .. 32 ) # directions 1 - 30 / 31 | 32 \
{
my $combined = tr/X \n/A\0/r |.
(substr $_, $i) =~ tr/X \n/B\0/r |.
(substr $_, 2 * $i) =~ tr/X \n/D\0/r |.
(substr $_, 3 * $i) =~ tr/X \n/H\0/r |.
(substr $_, 4 * $i) =~ tr/X \n/P\0/r;
while( $combined =~ /[OW\[\]\^]/g ) # exactly four Xs and one space
Line 1,199 ⟶ 1,194:
print join(' ', map s/ .* /->/r =~ s!\d+! ($& % 31).','.int $& / 31 !ger,
@moves) =~ s/.{60}\K /\n/gr, "\n";
print $_, "move count: $count\n";</langsyntaxhighlight>
 
=={{header|Phix}}==
Line 1,206 ⟶ 1,201:
=={{header|Racket}}==
 
<langsyntaxhighlight lang="racket">#lang racket
(module rules racket/base
(require racket/match)
Line 1,306 ⟶ 1,301:
p
(define bmp (pict->bitmap p))
(send bmp save-file "images/morpion.png" 'png))</langsyntaxhighlight>
 
 
'''Intermission:''' The <code>render</code> submodule just does drawing, and is not part of the solving. But the <code>main</code> module uses it, so we put it in here:
 
<langsyntaxhighlight lang="racket">(module render racket
(require racket/match
racket/draw
Line 1,410 ⟶ 1,405:
[(hash-has-key? p# k) #\.]
[else #\space])))
(printf "~s~%~a points ~a lines~%" l (hash-count p#) (length l))))</langsyntaxhighlight>
{{out}}
 
Line 1,442 ⟶ 1,437:
<br>This program allows the <tt> D </tt> or <tt> T </tt> forms of the game, and allows any board size (grid size) of three or higher.
<br>The default games is <tt> 5T </tt>
<langsyntaxhighlight lang="rexx">/*REXX program plays Morpion solitaire (with grid output), the default is the 5T version*/
signal on syntax; signal on noValue /*handle possible REXX program errors. */
/* [↓] handle the user options (if any)*/
Line 1,617 ⟶ 1,612:
x= xx
do y=yy-1 by -1; x=x+1; if @.x.y==empty then leave; z= z||@.x.y; end
return ?win(z) /*──────count diag wins: up & <, down & > */</langsyntaxhighlight>
This REXX program makes use of &nbsp; '''LINESIZE''' &nbsp; REXX program (or BIF) which is used to determine the screen width (or linesize) of the terminal (console).
<br>The &nbsp; '''LINESIZE.REX''' &nbsp; REXX program is included here ──► [[LINESIZE.REX]].<br>
Line 1,694 ⟶ 1,689:
{{libheader|Wren-fmt}}
An embedded program so we can use the ncurses library.
<langsyntaxhighlight ecmascriptlang="wren">/* morpion_solitaireMorpion_solitaire.wren */
 
import "random" for Random
Line 1,935 ⟶ 1,930:
Ncurses.nocbreak()
Ncurses.echo()
Ncurses.endwin()</langsyntaxhighlight>
<br>
We now embed the above script in the following C program, build and run it.
<langsyntaxhighlight lang="c">/* gcc morpion_solitaireMorpion_solitaire.c -o morpion_solitaireMorpion_solitaire -lncurses -lwren -lm */
 
#include <stdio.h>
Line 1,982 ⟶ 1,977:
int x = (int)wrenGetSlotDouble(vm, 2);
const char *str = wrenGetSlotString(vm, 3);
mvprintw(y, x, "%s", str);
}
 
Line 2,012 ⟶ 2,007:
if (isStatic && strcmp(signature, "nocbreak()") == 0) return C_nocbreak;
if (isStatic && strcmp(signature, "echo()") == 0) return C_echo;
if (isStatic && strcmp(signature, "noecho()") == 0) return C_noecho;
if (isStatic && strcmp(signature, "refresh()") == 0) return C_refresh;
if (isStatic && strcmp(signature, "getch()") == 0) return C_getch;
Line 2,080 ⟶ 2,074:
WrenVM* vm = wrenNewVM(&config);
const char* module = "main";
const char* fileName = "morpion_solitaireMorpion_solitaire.wren";
char *script = readFile(fileName);
WrenInterpretResult result = wrenInterpret(vm, module, script);
Line 2,101 ⟶ 2,095:
free(script);
return 0;
}</langsyntaxhighlight>
9,476

edits