MapKit JS how to assign mapkit.Coordinates() from variable as digit - webview

I am using MapKit JS in Filemaker and can get the coordinates from an input in Filemaker Web View like this.
"const punkt = '" & Substitute ( MYMAP::longlat ; ¶ ; ", " ) & "';" & ¶ &
The input is e.g.: 59.436549, 10.629371 but I can not get this into mapkit definition. I get the latitude ang longtitude value and make sure it is a digit. But then I need the comma (,).
var punkt // this is 59.658985, 10.790869
var punktxy = punkt.split(",");
var x = parseFloat(punktxy[0]);
var x = parseFloat(punktxy[1);
When I hardcode it works:
new mapkit.Coordinate(59.658985, 10.790869)
But I can not get this right. Probably because it is type text when I concatenate it like this:
new mapkit.Coordinate(x + ',' + y)
This does not work either:
new mapkit.Coordinate(x,y)
It is probably a string? How do I get the value correct? This is probably a javascript basic question but I am lost here.
Here is my webviewer code that includes the javascript from the text fields. Notice I am putting the x and y values into the javascript. That is why I need 3 js files since I can't seem to get this right: mapkit.Coordinate(59.658985, 10.790869)
webviewer:
// Load your specific implementation of MapKit JS ""; "const
punkt = '" & Substitute ( ARTSFUNN::Lokalitet ; ¶ ; ", " ) & "';" & ¶
& GetLayoutObjectAttribute ( "map1.js" ; "content" ) & ARTSFUNN::Lat
& " , " & ARTSFUNN::Long & GetLayoutObjectAttribute ( "map2.js" ;
"content" ) & ARTSFUNN::Lat & " , " & ARTSFUNN::Long &
GetLayoutObjectAttribute ( "map3.js" ; "content" ); "";
map1.js:
mapkit.init({
authorizationCallback: done => {
done(
"<<$$JWT.TOKEN>>"
);
}
});
var punktxy = punkt.split(",");
var x = parseFloat(punktxy[0]);
var x = parseFloat(punktxy[1);
var xy = (x + ','+ y); // doesn't work
var MarkerAnnotation = mapkit.MarkerAnnotation,
clickAnnotation;
var borch = new mapkit.CoordinateRegion(
new mapkit.Coordinate(
map2.js:
),
new mapkit.CoordinateSpan(0.005, 0.005)
);
var map = new mapkit.Map('map');
map.region = borch;
map.mapType = "hybrid";
map.setCenterAnimated(new mapkit.Coordinate(
map3.js:
), true);
console.log(map);
update## - just testing and now this seems to work!
Define the Lat and Long in WebViewer:
"var x = '" & Substitute ( ARTSFUNN::Lat ; ¶ ; ", " ) & "';" & ¶ &
"var y = '" & Substitute ( ARTSFUNN::Long ; ¶ ; ", " ) & "';" & ¶ &
Use the values in the include map.js:
var x = parseFloat(x);
var y = parseFloat(y);
// does not work if I don't convert to digit before use!
var bor = new mapkit.CoordinateRegion(
new mapkit.Coordinate(x,y),
new mapkit.CoordinateSpan(0.005, 0.005)
);
var map = new mapkit.Map('map');
map.region = bor;
map.mapType = "hybrid";
map.setCenterAnimated(new mapkit.Coordinate(x,y), true);

This is not really an answer (unless your question is purely about the "as digit" part), but I can't post this much code in a comment.
At the beginning of your question, you used a field named MYMAP::longlat which apparently held both coordinates separated by a carriage return. And you used Substitute() to replace the return with a comma.
Now you are using two separate fields, ARTSFUNN::Lat and ARTSFUNN::Lon - yet you're still applying the same Substitute operation to both. This does not seem necessary.
More importantly, you're doing:
"var x = '" & Substitute ( ARTSFUNN::Lat ; ¶ ; ", " ) & "';" & ¶ &
"var y = '" & Substitute ( ARTSFUNN::Long ; ¶ ; ", " ) & "';" & ¶ &
which would produce a result like:
var x = '59.436549';
var y = '10.629371';
where both variables are clearly strings which you then have to convert to numbers.
I believe you should be doing:
"var x = " & ARTSFUNN::Lat & ";¶var y = " & ARTSFUNN::Long & ";¶"
to produce:
var x = 59.436549;
var y = 10.629371;
which can then be used directly by:
new mapkit.Coordinate(x,y)
The same result can be obtained using your original field in:
"var x = " & GetValue ( MYMAP::longlat ; 2 ) & ";¶var y = " & GetValue ( MYMAP::longlat ; 1 ) & ";¶"
(assuming longitude is the first value listed in the field).

Related

How to transpose a 2D list with repeating objects

I have been trying to write some VBA in Excel to transpose a 2D list, based on searching for the first character "{".
Before:
After:
My code:
With Sheets("Results").Range(Cell1:="A1", Cell2:="A39")
Set a = .Find("{", After:=Range("A" & lRow))
Set b = a
c = a.Address
Do While Not .FindNext(b) Is Nothing And a.Address <> .FindNext(b).Address
c = c & "," & .FindNext(b).Address
rangeToMoveCell1 = a.Address
rangeToMoveCell2 = .FindNext(b).Address
MsgBox ("rangeToMoveCell1: " & rangeToMoveCell1 & vbNewLine & "rangeToMoveCell2: " & rangeToMoveCell2)
Sheets("Results").Range(Cell1:=rangeToMoveCell1, Cell2:=rangeToMoveCell2).Copy
Sheets("Results").Range(Cell1:=rangeToMoveCell1, Cell2:=rangeToMoveCell2).Offset(-3, 1).PasteSpecial Transpose:=True
Sheets("Results").Range(Cell1:=rangeToMoveCell1, Cell2:=rangeToMoveCell2).Clear
Set b = .FindNext(b)
Loop
End With
I've come up with this and it works, except it does not process the last find:
With Sheets("Results").Range(Cell1:="A1", Cell2:="A" & lRow)
Set a = .Find("{", After:=Range("B" & lRow))
Set b = a
c = a.Offset(3).Address
Do While Not .FindNext(b) Is Nothing And a.Address <> .FindNext(b).Address
Set nextFind = .FindNext(b)
Set d = nextFind
'MsgBox ("d: " & d)
e = nextFind.Offset(-1, 1).Address
Sheets("Results").Range(Cell1:=c, Cell2:=e).Copy
Sheets("Results").Range(Cell1:=c, Cell2:=e).Offset(-3, 2).PasteSpecial Transpose:=True
Sheets("Results").Range(Cell1:=c, Cell2:=e).EntireRow.Delete
Sheets("Results").Range(c).EntireRow.Insert
Sheets("Results").Range(c).EntireRow.Insert
c = nextFind.Offset(3).Address
Set b = .FindNext(b)
Loop
End With

Need DXL code to arrange attribute lines into table (converting DOORS data to LaTeX source)

I have a DXL script which parses all data in DOORS columns into a LaTeX -compatible text source file. What I can't figure out is how to re-order some data into a tabular - compatible format. The attributes in question are DXL links to a reference DOORS module, so there is one line (separated by a line-feed) per link in each cell. Currently I loop thru all columns for each object (row), with the code snippet (part of the full script)
for col in doorsModule do {
var_name = title( col )
if( ! main( col ) && search( regexp "Absolute Number", var_name, 0 ) == false )
{
// oss is my output stream variable
if ( length(text(col, obj) ) > 0 )
{
oss << "\\textbf{";
oss << var_name; // still the column title here
oss << "}\t"
var_name = text( col, obj );
oss << var_name;
oss << "\n\n";
c++;
}
}
}
Examples of the contents of a cell, where I have separately parsed the Column Name to bold and collected it prior to collecting the cell contents. All four lines are the contents of a single cell.
\textbf{LinkedItemName}
DISTANCE
MinSpeed
MaxSpeed
Time
\textbf{Unit}
m
km/h
km/h
minutes
\textbf{Driver1}
100
30
80
20
\textbf{Driver2}
50
20
60
10
\textbf{Driver3}
60
30
60
30
What I want to do is re-arrange the data so that I can write the source code for a table, to wit:
\textbf{LinkedItemName} & \textbf{Unit} & \textbf{Driver1} & \textbf{Driver2} & \textbf{Driver3} \\
DISTANCE & m & 100 & 50 & 60 \\
MinSpeed & km/h & 30 & 20 & 30 \\
MaxSpeed & km/h & 80 & 60 & 60 \\
Time & minutes & 20 & 10 & 30 \\
I know in advance the exact Attribute names I'm "collecting." I can't figure out how to manipulate the data returned from each cell (regex or otherwise) to create my desired final output. I'm guessing some regex code (in DXL) might be able to assign the contents of each line within a cell to a series of variables, but don't quite see how.
Combination of regex and string assembly seems to work. Here's a sample bit of code (some of which is straight from the DOORS DXL Reference Manual)
int idx = 0
Array thewords = create(1,1)
Array thelen = create(1,1)
Regexp getaline = regexp2 ".*"
// matches any character except newline
string txt1 = "line 1\nline two\nline three\n"
// 3 line string
while (!null txt1 && getaline txt1) {
int ilen = length(txt1[match 0])
print "ilen is " ilen "\n"
put(thelen, ilen, idx, 0)
putString(thewords,txt1[match 0],0,idx)
idx ++
// match 0 is whole of match
txt1 = txt1[end 0 + 2:] // move past newline
}
int jj
// initialize to simplify adding the "&"
int lenone = (int get(thelen,0,0) )
string foo = (string get(thewords, 0, 0,lenone ) )
int lenout
for (jj = 1; jj < idx; jj++) {
lenout = (int get(thelen,jj,0) )
foo = foo "&" (string get(thewords, 0, jj,lenout ) )
}
foo = foo "\\\\"
// foo is now "line 1&line two&line three\\ " (without quotes) as LaTeX wants

How to resize side by side controls in MS ACCESS 2007

I have a form in which there are various fields, for ex. Two textboxes are side by side.
These 2 textbox have anchor property left,top and other right,top.
Now when I resize the form the controls are aligned to left and the other textbox to right.
But when as screen is maximized it leaves a blank space in between these two textboxes.
So then I made the anchor property of both textbox to both,both the controls overlapped.
PS: working on MS ACCESS 2007.
anchoring property above is Horizontal, Vertical
EDIT : In Normal window
_______________________Min Max Close_
| First_Name TEXTBOX Last_Name TEXTBOX |
|_______________________________|
When Maximized to whole screen it gives me
_____________________________________Min Max Close_
| First_Name TEXTBOX ............................. Last_Name TEXTBOX |
|______________________________________________|
And I need this way as below
_____________________________________Min Max Close_
| F i r s t_N a m e T E X T B O X ........ L a s t_N a m e T E X T B O X |
|______________________________________________|
I am trying to explain by doing all this as I am not allowed to upload a image, Sorry for that....
Paste the following code into your form, change the field names, and see what happens. The two fields will 'grow' as you increase the form width, yet maintain their Anchor. Note: I updated on 3/3 to handle the field labels.
Option Compare Database
Option Explicit
Dim fviInsideWidth As Integer
Dim fviSaveInsideWidth As Integer
Dim fviFormWidth As Integer
Dim fviFldWidth As Integer
Dim fviFieldGap As Integer
Dim fviRemainder As Integer
Dim fviLblWidth As Integer
Dim fviRLblToTxt As Integer
Dim fvstrLLabel As String
Dim fvstrRLabel As String
Private Sub Form_Open(Cancel As Integer)
fviSaveInsideWidth = Me.InsideWidth
fviInsideWidth = Me.InsideWidth
fviFormWidth = Me.Width
fviFldWidth = Me.fldLeft.Width + Me.fldRight.Width
fviRemainder = fviInsideWidth - fviFldWidth
fviFieldGap = Me.fldRight.Left - (Me.fldLeft.Left + Me.fldLeft.Width)
fvstrLLabel = Me.fldLeft.Controls.Item(0).Name
fvstrRLabel = Me.fldRight.Controls.Item(0).Name
fviLblWidth = Me.Controls(fvstrRLabel).Width
fviRLblToTxt = Me.fldRight.Left - Me.Controls(fvstrRLabel).Left
'Debug.Print "Open - InsideWidth = " & fviInsideWidth & " Fields: " & fviFldWidth & " Remainder: " & fviRemainder
'Debug.Print "Open - Form Width = " & Me.Width & vbTab & "Diff = " & fviInsideWidth - fviFormWidth
End Sub
Private Sub Form_Close()
Me.fldLeft.Width = fviFldWidth
Me.fldRight.Width = fviFldWidth
Me.InsideWidth = fviSaveInsideWidth
End Sub
Private Sub Form_Resize()
Dim ifldWidth As Integer
Dim ifrmWidth As Integer
fviInsideWidth = Me.InsideWidth
ifrmWidth = fviInsideWidth - 1110
Me.Width = ifrmWidth
ifldWidth = Int((fviInsideWidth - fviRemainder) / 2)
Me.fldLeft.Width = ifldWidth
Me.fldRight.Left = Me.fldLeft.Left + Me.fldLeft.Width + fviFieldGap
Me.Controls(fvstrRLabel).Left = Me.fldRight.Left - fviRLblToTxt
Me.fldRight.Width = ifldWidth
'Debug.Print "Resize - InsideWidth = " & fviInsideWidth & vbTab & "Form Width = " & Me.Width & " Flds: " & ifldWidth & " Right=" & Me.fldLeft.Left + Me.fldLeft.Width + fviFieldGap
'Debug.Print "Resize Form: " & Me.Width & " Flds: " & ifldWidth
Me.Repaint
End Sub

Parsing a TeX-like language with lpeg

I am struggling to get my head around LPEG. I have managed to produce one grammar which does what I want, but I have been beating my head against this one and not getting far. The idea is to parse a document which is a simplified form of TeX. I want to split a document into:
Environments, which are \begin{cmd} and \end{cmd} pairs.
Commands which can either take an argument like so: \foo{bar} or can be bare: \foo.
Both environments and commands can have parameters like so: \command[color=green,background=blue]{content}.
Other stuff.
I also would like to keep track of line number information for error handling purposes. Here's what I have so far:
lpeg = require("lpeg")
lpeg.locale(lpeg)
-- Assume a lot of "X = lpeg.X" here.
-- Line number handling from http://lua-users.org/lists/lua-l/2011-05/msg00607.html
-- with additional print statements to check they are working.
local newline = P"\r"^-1 * "\n" / function (a) print("New"); end
local incrementline = Cg( Cb"linenum" )/ function ( a ) print("NL"); return a + 1 end , "linenum"
local setup = Cg ( Cc ( 1) , "linenum" )
nl = newline * incrementline
space = nl + lpeg.space
-- Taken from "Name-value lists" in http://www.inf.puc-rio.br/~roberto/lpeg/
local identifier = (R("AZ") + R("az") + P("_") + R("09"))^1
local sep = lpeg.S(",;") * space^0
local value = (1-lpeg.S(",;]"))^1
local pair = lpeg.Cg(C(identifier) * space ^0 * "=" * space ^0 * C(value)) * sep^-1
local list = lpeg.Cf(lpeg.Ct("") * pair^0, rawset)
local parameters = (P("[") * list * P("]")) ^-1
-- And the rest is mine
anything = C( (space^1 + (1-lpeg.S("\\{}")) )^1) * Cb("linenum") / function (a,b) return { text = a, line = b } end
begin_environment = P("\\begin") * Ct(parameters) * P("{") * Cg(identifier, "environment") * Cb("environment") * P("}") / function (a,b) return { params = a[1], environment = b } end
end_environment = P("\\end{") * Cg(identifier) * P("}")
texlike = lpeg.P{
"document";
document = setup * V("stuff") * -1,
stuff = Cg(V"environment" + anything + V"bracketed_stuff" + V"command_with" + V"command_without")^0,
bracketed_stuff = P"{" * V"stuff" * P"}" / function (a) return a end,
command_with =((P("\\") * Cg(identifier) * Ct(parameters) * Ct(V"bracketed_stuff"))-P("\\end{")) / function (i,p,n) return { command = i, parameters = p, nodes = n } end,
command_without = (( P("\\") * Cg(identifier) * Ct(parameters) )-P("\\end{")) / function (i,p) return { command = i, parameters = p } end,
environment = Cg(begin_environment * Ct(V("stuff")) * end_environment) / function (b,stuff, e) return { b = b, stuff = stuff, e = e} end
}
It almost works!
> texlike:match("\\foo[one=two]thing\\bar")
{
command = "foo",
parameters = {
{
one = "two",
},
},
}
{
line = 1,
text = "thing",
}
{
command = "bar",
parameters = {
},
}
But! First, I can't get the line number handling part to work at all. The function within incrementline is never fired.
I also can't quite work out how nested capture information is passed to handling functions (which is why I have scattered Cg, C and Ct semirandomly over the grammar). This means that only one item is returned from within a command_with:
> texlike:match("\\foo{text \\command moretext}")
{
command = "foo",
nodes = {
{
line = 1,
text = "text ",
},
},
parameters = {
},
}
I would also love to be able to check that the environment start and ends match up but when I tried to do so, my back references from "begin" were not in scope by the time I got to "end". I don't know where to go from here.
Late answer but hopefully it'll offer some insight if you're still looking for a solution or wondering what the problem was.
There are a couple of issues with your grammar, some of which can be tricky to spot.
Your line increment here looks incorrect:
local incrementline = Cg( Cb"linenum" ) /
function ( a ) print("NL"); return a + 1 end,
"linenum"
It looks like you meant to create a named capture group and not an anonymous group. The backcapture linenum is essentially being used like a variable. The problem is because this is inside an anonymous capture, linenum will not update properly -- function(a) will always receive 1 when called. You need to move the closing ) to the end so "linenum" is included:
local incrementline = Cg( Cb"linenum" /
function ( a ) print("NL"); return a + 1 end,
"linenum")
Relevant LPeg documentation for Cg capture.
The second problem is with your anything non-terminal rule:
anything = C( (space^1 + (1-lpeg.S("\\{}")) )^1) * Cb("linenum") ...
There are several things to be careful here. First, a named Cg capture (from incrementline rule once it's fixed) doesn't produce anything unless it's in a table or you backref it. The second major thing is that it has an adhoc scope like a variable. More precisely, its scope ends once you close it in an outer capture -- like what you're doing here:
C( (space^1 + (...) )^1)
Which means by the time you reference its backcapture with * Cb("linenum"), that's already too late -- the linenum you really want already closed its scope.
I always found LPeg's re syntax a bit easier to grok so I've rewritten the grammar with that instead:
local grammar_cb =
{
fold = pairfold,
resetlinenum = resetlinenum,
incrementlinenum = incrementlinenum, getlinenum = getlinenum,
error = error
}
local texlike_grammar = re.compile(
[[
document <- '' -> resetlinenum {| docpiece* |} !.
docpiece <- {| envcmd |} / {| cmd |} / multiline
beginslash <- cmdslash 'begin'
endslash <- cmdslash 'end'
envcmd <- beginslash paramblock? {:beginenv: envblock :} (!endslash docpiece)*
endslash openbrace {:endenv: =beginenv :} closebrace / &beginslash {} -> error .
envblock <- openbrace key closebrace
cmd <- cmdslash {:command: identifier :} (paramblock? cmdblock)?
cmdblock <- openbrace {:nodes: {| docpiece* |} :} closebrace
paramblock <- opensq ( {:parameters: {| parampairs |} -> fold :} / whitesp) closesq
parampairs <- parampair (sep parampair)*
parampair <- key assign value
key <- whitesp { identifier }
value <- whitesp { [^],;%s]+ }
multiline <- (nl? text)+
text <- {| {:text: (!cmd !closebrace !%nl [_%w%p%s])+ :} {:line: '' -> getlinenum :} |}
identifier <- [_%w]+
cmdslash <- whitesp '\'
assign <- whitesp '='
sep <- whitesp ','
openbrace <- whitesp '{'
closebrace <- whitesp '}'
opensq <- whitesp '['
closesq <- whitesp ']'
nl <- {%nl+} -> incrementlinenum
whitesp <- (nl / %s)*
]], grammar_cb)
The callback functions are straight-forwardly defined as:
local function pairfold(...)
local t, kv = {}, ...
if #kv % 2 == 1 then return ... end
for i = #kv, 2, -2 do
t[ kv[i - 1] ] = kv[i]
end
return t
end
local incrementlinenum, getlinenum, resetlinenum do
local line = 1
function incrementlinenum(nl)
assert(not nl:match "%S")
line = line + #nl
end
function getlinenum() return line end
function resetlinenum() line = 1 end
end
Testing the grammar with a non-trivial tex-like str with multiple lines:
local test1 = [[\foo{text \bar[color = red, background = black]{
moretext \baz{
even
more text} }
this time skipping multiple
lines even, such wow!}]]
Produces the follow AST in lua-table format:
{
command = "foo",
nodes = {
{
text = "text",
line = 1
},
{
parameters = {
color = "red",
background = "black"
},
command = "bar",
nodes = {
{
text = " moretext",
line = 2
},
{
command = "baz",
nodes = {
{
text = "even ",
line = 3
},
{
text = "more text",
line = 4
}
}
}
}
},
{
text = "this time skipping multiple",
line = 7
},
{
text = "lines even, such wow!",
line = 9
}
}
}
And a second test for begin/end environments:
local test2 = [[\begin[p1
=apple,
p2=blue]{scope} scope foobar
\end{scope} global foobar]]
Which seems to give approximately what you're looking for:
{
{
{
text = " scope foobar",
line = 3
},
parameters = {
p1 = "apple",
p2 = "blue"
},
beginenv = "scope",
endenv = "scope"
},
{
text = " global foobar",
line = 4
}
}

Very simple RogueLike in F#, making it more "functional"

I have some existing C# code for a very, very simple RogueLike engine. It is deliberately naive in that I was trying to do the minimum amount as simply as possible. All it does is move an # symbol around a hardcoded map using the arrow keys and System.Console:
//define the map
var map = new List<string>{
" ",
" ",
" ",
" ",
" ############################### ",
" # # ",
" # ###### # ",
" # # # # ",
" #### #### # # # ",
" # # # # # # ",
" # # # # # # ",
" #### #### ###### # ",
" # = # ",
" # = # ",
" ############################### ",
" ",
" ",
" ",
" ",
" "
};
//set initial player position on the map
var playerX = 8;
var playerY = 6;
//clear the console
Console.Clear();
//send each row of the map to the Console
map.ForEach( Console.WriteLine );
//create an empty ConsoleKeyInfo for storing the last key pressed
var keyInfo = new ConsoleKeyInfo( );
//keep processing key presses until the player wants to quit
while ( keyInfo.Key != ConsoleKey.Q ) {
//store the player's current location
var oldX = playerX;
var oldY = playerY;
//change the player's location if they pressed an arrow key
switch ( keyInfo.Key ) {
case ConsoleKey.UpArrow:
playerY--;
break;
case ConsoleKey.DownArrow:
playerY++;
break;
case ConsoleKey.LeftArrow:
playerX--;
break;
case ConsoleKey.RightArrow:
playerX++;
break;
}
//check if the square that the player is trying to move to is empty
if( map[ playerY ][ playerX ] == ' ' ) {
//ok it was empty, clear the square they were standing on before
Console.SetCursorPosition( oldX, oldY );
Console.Write( ' ' );
//now draw them at the new square
Console.SetCursorPosition( playerX, playerY );
Console.Write( '#' );
} else {
//they can't move there, change their location back to the old location
playerX = oldX;
playerY = oldY;
}
//wait for them to press a key and store it in keyInfo
keyInfo = Console.ReadKey( true );
}
I was playing around with doing it in F#, initially I was trying to write it using functional concepts, but turned out I was a bit over my head, so I did pretty much a straight port - it's not really an F# program (though it compiles and runs) it's a procedural program written in F# syntax:
open System
//define the map
let map = [ " ";
" ";
" ";
" ";
" ############################### ";
" # # ";
" # ###### # ";
" # # # # ";
" #### #### # # # ";
" # # # # # # ";
" # # # # # # ";
" #### #### ###### # ";
" # = # ";
" # = # ";
" ############################### ";
" ";
" ";
" ";
" ";
" " ]
//set initial player position on the map
let mutable playerX = 8
let mutable playerY = 6
//clear the console
Console.Clear()
//send each row of the map to the Console
map |> Seq.iter (printfn "%s")
//create an empty ConsoleKeyInfo for storing the last key pressed
let mutable keyInfo = ConsoleKeyInfo()
//keep processing key presses until the player wants to quit
while not ( keyInfo.Key = ConsoleKey.Q ) do
//store the player's current location
let mutable oldX = playerX
let mutable oldY = playerY
//change the player's location if they pressed an arrow key
if keyInfo.Key = ConsoleKey.UpArrow then
playerY <- playerY - 1
else if keyInfo.Key = ConsoleKey.DownArrow then
playerY <- playerY + 1
else if keyInfo.Key = ConsoleKey.LeftArrow then
playerX <- playerX - 1
else if keyInfo.Key = ConsoleKey.RightArrow then
playerX <- playerX + 1
//check if the square that the player is trying to move to is empty
if map.Item( playerY ).Chars( playerX ) = ' ' then
//ok it was empty, clear the square they were standing on
Console.SetCursorPosition( oldX, oldY )
Console.Write( ' ' )
//now draw them at the new square
Console.SetCursorPosition( playerX, playerY )
Console.Write( '#' )
else
//they can't move there, change their location back to the old location
playerX <- oldX
playerY <- oldY
//wait for them to press a key and store it in keyInfo
keyInfo <- Console.ReadKey( true )
So my question is, what do I need to learn in order to rewrite this more functionally, can you give me some hints, a vague overview, that kind of thing.
I'd prefer a shove in the right direction rather than just seeing some code, but if that's the easiest way for you to explain it to me then fine, but in that case can you please also explain the "why" rather the "how" of it?
Game programming in general will test your ability to manage complexity. I find that functional programming encourages you to break problems your solving into smaller pieces.
The first thing you want to do is turn your script into a bunch of functions by separating all the different concerns. I know it sounds silly but the very act of doing this will make the code more functional (pun intended.) Your main concern is going to be state management. I used a record to manage the position state and a tuple to manage the running state. As your code gets more advanced you will need objects to manage state cleanly.
Try adding more to this game and keep breaking the functions apart as they grow. Eventually you will need objects to manage all the functions.
On a game programming note don't change state to something else and then change it back if it fails some test. You want minimal state change. So for instance below I calculate the newPosition and then only change the playerPosition if this future position passes.
open System
// use a third party vector class for 2D and 3D positions
// or write your own for pratice
type Pos = {x: int; y: int}
with
static member (+) (a, b) =
{x = a.x + b.x; y = a.y + b.y}
let drawBoard map =
//clear the console
Console.Clear()
//send each row of the map to the Console
map |> List.iter (printfn "%s")
let movePlayer (keyInfo : ConsoleKeyInfo) =
match keyInfo.Key with
| ConsoleKey.UpArrow -> {x = 0; y = -1}
| ConsoleKey.DownArrow -> {x = 0; y = 1}
| ConsoleKey.LeftArrow -> {x = -1; y = 0}
| ConsoleKey.RightArrow -> {x = 1; y = 0}
| _ -> {x = 0; y = 0}
let validPosition (map:string list) position =
map.Item(position.y).Chars(position.x) = ' '
//clear the square player was standing on
let clearPlayer position =
Console.SetCursorPosition(position.x, position.y)
Console.Write( ' ' )
//draw the square player is standing on
let drawPlayer position =
Console.SetCursorPosition(position.x, position.y)
Console.Write( '#' )
let takeTurn map playerPosition =
let keyInfo = Console.ReadKey true
// check to see if player wants to keep playing
let keepPlaying = keyInfo.Key <> ConsoleKey.Q
// get player movement from user input
let movement = movePlayer keyInfo
// calculate the players new position
let newPosition = playerPosition + movement
// check for valid move
let validMove = newPosition |> validPosition map
// update drawing if move was valid
if validMove then
clearPlayer playerPosition
drawPlayer newPosition
// return state
if validMove then
keepPlaying, newPosition
else
keepPlaying, playerPosition
// main game loop
let rec gameRun map playerPosition =
let keepPlaying, newPosition = playerPosition |> takeTurn map
if keepPlaying then
gameRun map newPosition
// setup game
let startGame map playerPosition =
drawBoard map
drawPlayer playerPosition
gameRun map playerPosition
//define the map
let map = [ " ";
" ";
" ";
" ";
" ############################### ";
" # # ";
" # ###### # ";
" # # # # ";
" #### #### # # # ";
" # # # # # # ";
" # # # # # # ";
" #### #### ###### # ";
" # = # ";
" # = # ";
" ############################### ";
" ";
" ";
" ";
" ";
" " ]
//initial player position on the map
let playerPosition = {x = 8; y = 6}
startGame map playerPosition
That's a nice little game :-). In functional programming, you'd want to avoid using mutable state (as others pointed out) and you'd also want to write the core of your game as a function that doesn't have any side-effects (e.g. reading from console and writing).
The key part of the game is the function that controls the position. You could refactor your code to have a function with the type signature:
val getNextPosition : (int * int) -> ConsoleKey -> option<int * int>
The function returns None if the game should quit. Otherwise it returns Some(posX, posY) where posX and posY are your new locations for the # symbol. By doing the change, you get a nice functional core and the function getNextPosition is also easy to test (because it always returns the same result for the same inputs).
To use the function, the best option is to write the looping using recursion. The structure of the main function would look like this:
let rec playing pos =
match getNextPosition pos (Console.ReadKey()) with
| None -> () // Quit the game
| Some(newPos) ->
// This function redraws the screen (this is a side-effect,
// but it is localized to a single function)
redrawScreen pos newPos
playing newPos
Being a game, and using the Console, there is state and side-effects here which are inherent. But the key thing you'll want to do is eliminate those mutables. Using a recursive loop instead of a while loop will help you do that since then you can pass your state as arguments to each recursive call. Other than that, the main thing I can see to take advantage of F# features here is using pattern matching instead of if/then statements and switches, though that would be a mainly aesthetic improvement.
I'll try and avoid being overly specific - if I end up going too far in the other direction and this is too vague, let me know and I'll try improve it a little.
When making a functional program that has some sort of state, the basic mechanism you want to implement is something like:
(currentState, input) => newState
Then you can write a small wrapper around that to handle fetching input and drawing output.

Resources