Logo Interactive Grid Based Game.

This is a simple grid based game, similar to draughts, which runs within Logo and uses a central text file for the map and player pieces. All graphics are drawn direct to the Logo screen.

The program contains several elements.

  1. setup: This sets the board up and gives the players' pieces their start positions. Both players can run this program at the start.
  2. drawgrid: This generates the background map grid.
  3. makemap draws the map right at the start.
  4. readmap reads the map from the textfile.
  5. writemap writes the map to the textfile.
  6. playermap: This draws the pieces onto the grid.
  7. slide: allows players to slide a piece one drid position N,S,E or W.
  8. hop: allows players to hop over one piece (as in draughts). The piece hopped over is removed if it belongs to the opposition.
  9. checklegal: checks if a slide move is legal.
  10. checkhop1:checks that there is a piece to be hopped over.
  11. checkhop2: checks that there is any empty position to hop into.

To play the game:

  1. Create a network folder called logogame on the drive which matches the drive name in the program. (W: If need be change this by using 'EDall' in seek and destroy mode.)
  2. Each player then runs setup.
  3. Each player then makes moves like mad until no legal moves are left.
  4. The player controlling the biggest area of the board is the winner.
  5. You can use command line macros.
  6. Most errors will not stop the game so just ignore them.
  7. If the 'file open error' occurs then just close it at the command line and carry on.

Complete gridgame.lgo listing.

Making the board and pieces.

makemap

This creates the game. It writes the game information to a text file called "W:\\logogame\\mygame\\mpboard.txt
Make sure that you are writing to a extant directory! The easiest way to change the directory code is to open the whole gridgame.lgo file in Edall and use find/replace in seek and destroy mode. (ie substitute W: for F:)

This file is a primitive database. In the current form of the game it contains 132 numbers. The board is a 11x12 grid. All positions are set to 0. Then red 1's and blue 2's are added. Finally it is written to the server.

to makemap
make "mpboard (array :arraysize 0)
repeat :arraysize [ setitem repcount-1 :mpboard 0]
repeat (:xmax+1)[
setitem repcount-1 :mpboard 1
setitem repcount-1+:xmax+1 :mpboard 1
setitem repcount-1+(:ymax-1)*(:xmax+1) :mpboard 2
setitem repcount-1+(:ymax)*(:xmax+1) :mpboard 2 ]
writemap
end

playermap

This draws the pieces onto the board. It uses drawgrid first. Bitmap images of the units could be used instead etc. but this would be slower.

to playermap
readmap
drawgrid
repeat :ymax+1 [
make "y repcount-1
repeat :xmax+1 [
make "x repcount-1
make "value item :y*(:xmax+1)+:x :mpboard
if :value=0 [
setpc (list 0 255 0)]
if :value=1 [
setpc (list 255 0 0)]
if :value=2 [
setpc (list 0 0 255 )]
pu home
setxy :x*80-400 :y*50-300
pd
setpensize [5 5]
circle 5
setpensize [1 1]
fill
]]
end

drawgrid

This generates a grid on which to play the game. It uses LOGO's inbuilt drawing commands as these run much quicker than loading bitmaps. It also uses labels to generate the grid coordinates.

to drawgrid
cs
setpc (list 0 0 0)
pu setxy -400 -300 pd
;draw verticals
repeat (:xmax+1) [
rt 90
label repcount-1
lt 90
fd 50*(:ymax)
pu fd 20 pd
rt 90
label repcount-1 pu
pu fd 80 rt 90 fd 50*(:ymax)+20 rt 180 pd
]
; draw horizontals
repeat (:ymax+1) [
pu home setxy -400 -300 pd
pu fd (repcount-1)*50 rt 90 pd
label repcount-1
fd 80*(:xmax)
label repcount-1]
end

setup

This calls all the programs needed to get the game going.

to setup
make "ymax 11
make "xmax 9
make "arraysize (:ymax+1)*(:xmax+1)
cs ht
makemap
drawgrid
playermap
end

To run setup type: setup

Moving the pieces around.

slide

This allows the player to slide one grid position in the N,S,E or W directions. Illegal moves are ignored. After a legal move the map is redrawn.

to slide :x :y :direction
make "red1x :x
make "red1y :y
readmap
if :direction="w [
make "red1x :x-1]
if :direction="e [
make "red1x :x+1 ]
if :direction="n [
make "red1y :y+1 ]
if :direction="s [
make "red1y :y-1 ]
;check for legal move
make "piece checklegal :x :y :red1x :red1y
if :piece>0 [
;update the maze by removing red
setitem :x+:y*(:xmax+1) :mpboard 0
;update the maze with new redman location
setitem :red1x+:red1y*(:xmax+1) :mpboard :piece
writemap
playermap
]
end

To use slide type: slide STARTX STARTY "DIRECTION eg slide 2 3 "n

hop

Allows the player to hop over a piece in the N, S, E or W directions. The new positions are calculated and then the legality of the move is checked. If the move is legal then the pieces are adjusted and the board is redrawn. Illegal moves are either ignored or will result in an error message that won't stop the game.

to hop :x :y :direction
make "red1x :x
make "red1y :y
make "red2x :x
make "red2y :y
readmap
if :direction="w [
make "red2x :x-2
make "red1x :x-1]
if :direction="e [
make "red2x :x+2
make "red1x :x+1 ]
if :direction="n [
make "red2y :y+2
make "red1y :y+1 ]
if :direction="s [
make "red2y :y-2
make "red1y :y-1 ]
;check for legal move need to redo this
make "piecemoved checkhop2 :x :y :red2x :red2y
make "piecejumped checkhop1 :x :y :red1x :red1y
if :piecejumped>0 [
if :piecemoved>0 [
;update the maze by removing red
setitem :x+:y*(:xmax+1) :mpboard 0
;update the maze with new piece location
setitem :red2x+:red2y*(:xmax+1) :mpboard :piecemoved
;sort out the piece jumped
ifelse :piecemoved=:piecejumped [][
;update the maze by removing jumped piece
setitem :red1x+:red1y*(:xmax+1) :mpboard 0]
writemap
playermap
]]
end

To use hop type: hop STARTX STARTY "DIRECTION eg hop 2 3 "n

Checking that moves are legal.

checklegal

to checklegal :xin :yin :x1in :y1in
readmap
make "outvalue 0
make "datafound item (:x1in+:y1in*(:xmax+1)) :mpboard
;check that the destination is empty
if :datafound=0 [
make "datafound item (:xin+:yin*(:xmax+1)) :mpboard
make "outvalue :datafound ]
; output is the piece moved
output :outvalue
end

checkhop1 and 2

to checkhop1 :xin :yin :x1in :y1in
readmap
make "outvalue 0
make "datafound item (:xin+:yin*(:xmax+1)) :mpboard
;check there is a piece to move
if :datafound>0 [
make "datafound item (:x1in+:y1in*(:xmax+1)) :mpboard
make "outvalue :datafound ]
;returns value of place jumped
output :outvalue
end
to checkhop2 :xin :yin :x2in :y2in
readmap
make "outvalue 0
make "datafound item (:x2in+:y2in*(:xmax+1)) :mpboard
if :datafound=0 [
make "datafound item (:xin+:yin*(:xmax+1)) :mpboard
make "outvalue :datafound ]
;outputs value of piece moved
output :outvalue
end

Reading and writing to the server

readmap

Reads the :mpboard array from the text file.

to readmap
openread "F:\\logogame\\mygame\\mpboard.txt
setread "F:\\logogame\\mygame\\mpboard.txt
repeat :arraysize [setitem repcount-1 :mpboard first readlist ]
close "F:\\logogame\\mygame\\mpboard.txt
end

writemap

Writes the :mpboard array to a text file.

to writemap
openwrite "F:\\logogame\\mygame\\mpboard.txt
setwrite "F:\\logogame\\mygame\\mpboard.txt
repeat :arraysize [print item repcount-1 :mpboard]
setwrite[]
close "F:\\logogame\\mygame\\mpboard.txt
end

NB the commonest crash occurs when the players clash for access to the txt file. This will result in a 'file already open' error. To resolve this you must use the command line code below to manually close the file.

close "F:\\logogame\\mygame\\mpboard.txt

Screenshots and MACROS

Moving 3 pieces side by side

repeat 3 [ slide repcount 1 "n]

before


after


Hopping 3 side by side

repeat 3 [ hop repcount 0 "n]

before


after


Extension ideas.

'Fog of War' could be used so that you can only 'see' positions within 2 grid moves of your own pieces. A scoring function could be added and squares marked to show which player last visited, and therefore controls, them.

Last updated 24th February 2010