Morpion solitaire/Unicon: Difference between revisions

Content added Content deleted
m (→‎Other Interesting Results: ... more trimming)
m (Fixed syntax highlighting.)
 
Line 23: Line 23:
For more see: Morpion -h|-help
For more see: Morpion -h|-help


== Basic Solution ==
===Basic Solution===
The code need to solve the task requirements is found in this section. Just delete the $define below. The basic code will play a single random game, show an ASCII art grid, and produce a log in Pentasol compatible notation. On any given run your most likely to see a game with scores in the 20's or 60's.
The code need to solve the task requirements is found in this section. Just delete the $define below. The basic code will play a single random game, show an ASCII art grid, and produce a log in Pentasol compatible notation. On any given run your most likely to see a game with scores in the 20's or 60's.
=== Main and Core Game Play Procedures ===
===Main and Core Game Play Procedures===
<lang Unicon>link printf,strings,options
<syntaxhighlight lang="unicon">link printf,strings,options


$define MORPVER "1.7g" # version
$define MORPVER "1.7g" # version
Line 215: Line 215:
/MG.rseed := &random # seed for this game
/MG.rseed := &random # seed for this game
if MG.move := ?ScanGrid(MG).pool then return MG
if MG.move := ?ScanGrid(MG).pool then return MG
end</lang>
end</syntaxhighlight>


=== Game Logging ===
===Game Logging===
<lang Unicon>procedure WriteMoveLog(MG) #: write move log wrapper
<syntaxhighlight lang="unicon">procedure WriteMoveLog(MG) #: write move log wrapper
if \M_GameSave then {
if \M_GameSave then {
savegame := sprintf("Games/%s/%i-L%i",M_GameType,*MG.history,M_Limit)
savegame := sprintf("Games/%s/%i-L%i",M_GameType,*MG.history,M_Limit)
Line 268: Line 268:
m.line[m.move,2]-m.coff+MG.coff,m.line[m.move,1]-m.roff+MG.roff,
m.line[m.move,2]-m.coff+MG.coff,m.line[m.move,1]-m.roff+MG.roff,
m.direction,(d < 0,"-")|(d = 0,"")|"+",abs(d))
m.direction,(d < 0,"-")|(d = 0,"")|"+",abs(d))
end</lang>
end</syntaxhighlight>


== Extended Framework ==
===Extended Framework===
None of the code below is needed to satisfy the basic task. It supports the extended framework which includes, command line options, reading and replaying games from a saved file, running mass simulations to glean the best games, and some code for detailed analysis if I ever get around to experimenting with other than random strategy.
None of the code below is needed to satisfy the basic task. It supports the extended framework which includes, command line options, reading and replaying games from a saved file, running mass simulations to glean the best games, and some code for detailed analysis if I ever get around to experimenting with other than random strategy.
=== Interface, Parameters, Globals ===
===Interface, Parameters, Globals===


<lang Unicon>$ifdef EXTENDED
<syntaxhighlight lang="unicon">$ifdef EXTENDED


# --- Interface, Parameters, Additional Globals ---
# --- Interface, Parameters, Additional Globals ---
Line 410: Line 410:
\t-?|-h|-help\tthis help text")
\t-?|-h|-help\tthis help text")
stop()
stop()
end</lang>
end</syntaxhighlight>


=== Multigame Simulation and Monitoring Support ===
===Multigame Simulation and Monitoring Support===
<syntaxhighlight lang="unicon">
<lang Unicon>
procedure MultiMorphion(N,MG) #: Simulate N games using MG
procedure MultiMorphion(N,MG) #: Simulate N games using MG
etime := -&time
etime := -&time
Line 489: Line 489:
every fprintf(M_Output," %s","-"|&storage) ; fprintf(M_Output,"\n\n")
every fprintf(M_Output," %s","-"|&storage) ; fprintf(M_Output,"\n\n")
fprintf(M_Output,"Icon/Unicon version %s\n",&version)
fprintf(M_Output,"Icon/Unicon version %s\n",&version)
end </lang>
end </syntaxhighlight>


=== Game Replayer ===
===Game Replayer===
<lang Unicon>procedure ReplayMorpion() #: Handle recorded games
<syntaxhighlight lang="unicon">procedure ReplayMorpion() #: Handle recorded games
Replayer(M := ReadMoveLog(M_ReplayFile)) # read game and save data
Replayer(M := ReadMoveLog(M_ReplayFile)) # read game and save data
M_Strategy := Replayer
M_Strategy := Replayer
Line 543: Line 543:
stop()
stop()
}
}
end</lang>
end</syntaxhighlight>


=== Game Reader ===
===Game Reader===
<lang Unicon>procedure ReadMoveLog(MG) #: read move log wrapper
<syntaxhighlight lang="unicon">procedure ReadMoveLog(MG) #: read move log wrapper
fprintf(M_Output,"Reading recorded game from: %s\n",M_ReplayFile)
fprintf(M_Output,"Reading recorded game from: %s\n",M_ReplayFile)
f := open(M_ReplayFile ,"r") |
f := open(M_ReplayFile ,"r") |
Line 596: Line 596:
}
}
return M
return M
end</lang>
end</syntaxhighlight>


=== Detailed Move Logging (for analysis) ===
===Detailed Move Logging (for analysis)===
<lang Unicon>procedure PrintDetails(MG) #: print the log
<syntaxhighlight lang="unicon">procedure PrintDetails(MG) #: print the log
fprintf(M_Output,"Detailed Move Log\n")
fprintf(M_Output,"Detailed Move Log\n")
every fprintf(M_Output,"%i : %s\n",i := 1 to *MG.log,MG.log[i])
every fprintf(M_Output,"%i : %s\n",i := 1 to *MG.log,MG.log[i])
Line 619: Line 619:
log ||:= sprintf(" Metric=%i",M_Eval(MG)) # append score (opt)
log ||:= sprintf(" Metric=%i",M_Eval(MG)) # append score (opt)
put(MG.log,log) # log the move
put(MG.log,log) # log the move
end</lang>
end</syntaxhighlight>


=== Strategy Support ===
===Strategy Support===
<lang Unicon># No useful examples at this time
<syntaxhighlight lang="unicon"># No useful examples at this time


procedure Scorefail(MG);end #: dummy M_Eval always fails
procedure Scorefail(MG);end #: dummy M_Eval always fails
$endif</lang>
$endif</syntaxhighlight>


{{libheader|Icon Programming Library}}
{{libheader|Icon Programming Library}}