Morpion solitaire: Difference between revisions
Content added Content deleted
(→{{header|Wren}}: Removed a duplicated line.) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 56: | Line 56: | ||
=={{header|C}}== |
=={{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. |
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. |
||
< |
<syntaxhighlight lang="c">#include <ncurses.h> |
||
#include <stdlib.h> |
#include <stdlib.h> |
||
#include <unistd.h> |
#include <unistd.h> |
||
Line 280: | Line 280: | ||
endwin(); |
endwin(); |
||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Go}}== |
=={{header|Go}}== |
||
{{libheader|goncurses}} |
{{libheader|goncurses}} |
||
{{trans|C}} |
{{trans|C}} |
||
< |
<syntaxhighlight lang="go">package main |
||
import ( |
import ( |
||
Line 552: | Line 552: | ||
gc.CBreak(false) |
gc.CBreak(false) |
||
gc.Echo(true) |
gc.Echo(true) |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Icon}} and {{header|Unicon}}== |
=={{header|Icon}} and {{header|Unicon}}== |
||
Line 563: | Line 563: | ||
=={{header|J}}== |
=={{header|J}}== |
||
With this program as the file m.ijs |
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. turn will be a verb with GRID as y, returning GRID. Therefor: |
||
NB. morpion is move to the power of infinity---convergence. |
NB. morpion is move to the power of infinity---convergence. |
||
Line 671: | Line 671: | ||
NB. example |
NB. example |
||
smoutput play'' |
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. |
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> |
<pre> |
||
Line 808: | Line 808: | ||
{{libheader|nim-ncurses}} |
{{libheader|nim-ncurses}} |
||
< |
<syntaxhighlight lang="nim">import os, random, sequtils |
||
import ncurses |
import ncurses |
||
Line 971: | Line 971: | ||
endwin() |
endwin() |
||
play()</ |
play()</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 994: | Line 994: | ||
Picks a move at random from all possible moves at each step. A sample game is shown. |
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. |
The largest score found so far (from just random play) is 92, also shown below. |
||
< |
<syntaxhighlight lang="perl">#!/usr/bin/perl |
||
use strict; # https://rosettacode.org/wiki/Morpion_solitaire |
use strict; # https://rosettacode.org/wiki/Morpion_solitaire |
||
Line 1,050: | Line 1,050: | ||
=~ s/.{60}\K /\n/gr, "\n"; |
=~ s/.{60}\K /\n/gr, "\n"; |
||
tr/O/X/; |
tr/O/X/; |
||
print $_, "move count: $count\n";</ |
print $_, "move count: $count\n";</syntaxhighlight> |
||
This runs on a 30x30 grid (as advised by Talk page). |
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 |
Each move is shown as the end points of a line, startcolumn,startrow->endcolumn,endrow where row and column numbers |
||
Line 1,149: | Line 1,149: | ||
Uses the same kind of block shift/or technology I used in "Forest fire" and have used |
Uses the same kind of block shift/or technology I used in "Forest fire" and have used |
||
for Conway's Life. |
for Conway's Life. |
||
< |
<syntaxhighlight lang="perl">#!/usr/bin/perl |
||
use strict; # https://rosettacode.org/wiki/Morpion_solitaire |
use strict; # https://rosettacode.org/wiki/Morpion_solitaire |
||
Line 1,199: | Line 1,199: | ||
print join(' ', map s/ .* /->/r =~ s!\d+! ($& % 31).','.int $& / 31 !ger, |
print join(' ', map s/ .* /->/r =~ s!\d+! ($& % 31).','.int $& / 31 !ger, |
||
@moves) =~ s/.{60}\K /\n/gr, "\n"; |
@moves) =~ s/.{60}\K /\n/gr, "\n"; |
||
print $_, "move count: $count\n";</ |
print $_, "move count: $count\n";</syntaxhighlight> |
||
=={{header|Phix}}== |
=={{header|Phix}}== |
||
Line 1,206: | Line 1,206: | ||
=={{header|Racket}}== |
=={{header|Racket}}== |
||
< |
<syntaxhighlight lang="racket">#lang racket |
||
(module rules racket/base |
(module rules racket/base |
||
(require racket/match) |
(require racket/match) |
||
Line 1,306: | Line 1,306: | ||
p |
p |
||
(define bmp (pict->bitmap p)) |
(define bmp (pict->bitmap p)) |
||
(send bmp save-file "images/morpion.png" 'png))</ |
(send bmp save-file "images/morpion.png" 'png))</syntaxhighlight> |
||
'''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: |
'''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: |
||
< |
<syntaxhighlight lang="racket">(module render racket |
||
(require racket/match |
(require racket/match |
||
racket/draw |
racket/draw |
||
Line 1,410: | Line 1,410: | ||
[(hash-has-key? p# k) #\.] |
[(hash-has-key? p# k) #\.] |
||
[else #\space]))) |
[else #\space]))) |
||
(printf "~s~%~a points ~a lines~%" l (hash-count p#) (length l))))</ |
(printf "~s~%~a points ~a lines~%" l (hash-count p#) (length l))))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,442: | Line 1,442: | ||
<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>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> |
<br>The default games is <tt> 5T </tt> |
||
< |
<syntaxhighlight 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. */ |
signal on syntax; signal on noValue /*handle possible REXX program errors. */ |
||
/* [↓] handle the user options (if any)*/ |
/* [↓] handle the user options (if any)*/ |
||
Line 1,617: | Line 1,617: | ||
x= xx |
x= xx |
||
do y=yy-1 by -1; x=x+1; if @.x.y==empty then leave; z= z||@.x.y; end |
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 & > */</ |
return ?win(z) /*──────count diag wins: up & <, down & > */</syntaxhighlight> |
||
This REXX program makes use of '''LINESIZE''' REXX program (or BIF) which is used to determine the screen width (or linesize) of the terminal (console). |
This REXX program makes use of '''LINESIZE''' REXX program (or BIF) which is used to determine the screen width (or linesize) of the terminal (console). |
||
<br>The '''LINESIZE.REX''' REXX program is included here ──► [[LINESIZE.REX]].<br> |
<br>The '''LINESIZE.REX''' REXX program is included here ──► [[LINESIZE.REX]].<br> |
||
Line 1,694: | Line 1,694: | ||
{{libheader|Wren-fmt}} |
{{libheader|Wren-fmt}} |
||
An embedded program so we can use the ncurses library. |
An embedded program so we can use the ncurses library. |
||
< |
<syntaxhighlight lang="ecmascript">/* morpion_solitaire.wren */ |
||
import "random" for Random |
import "random" for Random |
||
Line 1,935: | Line 1,935: | ||
Ncurses.nocbreak() |
Ncurses.nocbreak() |
||
Ncurses.echo() |
Ncurses.echo() |
||
Ncurses.endwin()</ |
Ncurses.endwin()</syntaxhighlight> |
||
<br> |
<br> |
||
We now embed the above script in the following C program, build and run it. |
We now embed the above script in the following C program, build and run it. |
||
< |
<syntaxhighlight lang="c">/* gcc morpion_solitaire.c -o morpion_solitaire -lncurses -lwren -lm */ |
||
#include <stdio.h> |
#include <stdio.h> |
||
Line 2,100: | Line 2,100: | ||
free(script); |
free(script); |
||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |