UoD.dcl 7.13 KB
Newer Older
1
definition module Trax.UoD
Bas Lijnse's avatar
Bas Lijnse committed
2

3 4 5 6 7
import iTasks.WF.Definition
from   iTasks.Extensions.User import :: User
import PlatformExts.Tuple
from   StdClass import class zero, class ~
import Data.Maybe
8 9
import Data.GenFDomain
import Data.GenEq, Data.GenLexOrd, Control.GenMap
10

11
derive class iTask TraxSt, Coordinate, TileEdge, LineColor
Bas Lijnse's avatar
Bas Lijnse committed
12 13 14 15 16

:: TraxTile                             // a tile connects two edges:
	= { end1 :: !TileEdge               //    the red line at one end and
	  , end2 :: !TileEdge               //    the red line at the other end
	  }
17 18 19 20 21 22 23 24 25 26
derive   gEditor    TraxTile
derive   gText      TraxTile
derive   JSONEncode TraxTile
derive   JSONDecode TraxTile
derive   gEq        TraxTile
derive   gFDomain   TraxTile
instance fromTuple  TileEdge TileEdge TraxTile
instance toTuple    TileEdge TileEdge TraxTile
instance ==         TraxTile
instance toString   TraxTile
Bas Lijnse's avatar
Bas Lijnse committed
27 28 29 30 31 32 33 34 35 36 37 38 39 40

horizontal :: TraxTile                  // tile with a straight horizontal red line
vertical   :: TraxTile                  // tile with a straight vertical   red line
northwest  :: TraxTile                  // tile with an elbow red line at north-west
northeast  :: TraxTile                  // tile with an elbow red line at north-east
southeast  :: TraxTile                  // tile with an elbow red line at south-east
southwest  :: TraxTile                  // tile with an elbow red line at south-west

/** other_edge @tile @edge = @edge`:
        the tile connects @edge and @edge`.
*/
other_edge :: !TraxTile !TileEdge -> TileEdge

:: TileEdge                             // an edge is either at:
41
	= North	                            //    the north side of a tile, or at
42
	| East                              //    the east  side of a tile, or at
43
	| South                             //    the south side of a tile, or at
44 45 46 47 48 49
	| West                              //    the west  side of a tile
derive   gFDomain  TileEdge
derive   gLexOrd   TileEdge
instance ==        TileEdge
instance <         TileEdge
instance ~         TileEdge
Bas Lijnse's avatar
Bas Lijnse committed
50

51 52 53
:: LineColor                            // a line color is either:
	= RedLine                           //    red, or
	| WhiteLine                         //    white
54 55 56
derive   gFDomain  LineColor
instance ==        LineColor
instance ~         LineColor
Bas Lijnse's avatar
Bas Lijnse committed
57

58 59 60
:: Coordinate                           // a coordinate consists of:
 = { col :: !Int                        //   a column-coordinate
   , row :: !Int                        //   a row-coordinate
61
   }
Bas Lijnse's avatar
Bas Lijnse committed
62 63 64
instance ==        Coordinate
instance <         Coordinate
instance zero      Coordinate
65 66
instance fromTuple Int Int Coordinate
instance toTuple   Int Int Coordinate
67 68 69 70 71 72 73 74

/** col @{col} = col.
*/
col :: !Coordinate -> Int

/** row @{row} = row.
*/
row :: !Coordinate -> Int
Bas Lijnse's avatar
Bas Lijnse committed
75

76
:: Trax
77 78 79 80 81 82 83 84 85
derive   gEditor    Trax
derive   gText      Trax
derive   JSONEncode Trax
derive   JSONDecode Trax
derive   gEq        Trax
instance ==         Trax
instance zero       Trax

class tiles a :: !a -> [(Coordinate,TraxTile)]
Bas Lijnse's avatar
Bas Lijnse committed
86 87 88 89

/** tiles @trax = @tiles`:
	   @tiles` is a finite map of all current tiles of @trax.
*/
90 91
instance tiles Trax
instance tiles TraxSt
Bas Lijnse's avatar
Bas Lijnse committed
92

93 94
/** no_of_tiles @trax:
        returns the current number of tiles in @trax.
Bas Lijnse's avatar
Bas Lijnse committed
95
*/
96
no_of_tiles :: !Trax -> Int
Bas Lijnse's avatar
Bas Lijnse committed
97 98 99 100

/** bounds @trax = ((@minx,@maxx),(@miny,@maxy)):
        returns the mimimum x-coordinate @minx and minimum y-coordinate @miny
        and the maximum x-coordinate @maxx and maximum y-coordinate @maxy of @trax.
101
        It is assumed that (no_of_tiles @trax > 0).
Bas Lijnse's avatar
Bas Lijnse committed
102 103 104 105 106
*/
bounds :: !Trax -> (!(!Int,!Int), !(!Int,!Int))

/** dimension @trax = (@nr_of_cols,@nr_of_rows):
       returns the @nr_of_cols and @nr_of_rows of the collection of @trax.
107
       It is assumed that (no_of_tiles @trax > 0).
Bas Lijnse's avatar
Bas Lijnse committed
108 109 110 111
*/
dimension :: !Trax -> (!Int,!Int)

/** add_tile @coordinate @tile @trax = @trax`:
112 113 114
        only if (tile_at @trax @coordinate) = Nothing and the line colors of @tile match with the
        line endings of the neighbouring tiles of @coordinate in @trax, then (@coordinate,@tile) 
        is added to @trax, resulting in @trax`.
Bas Lijnse's avatar
Bas Lijnse committed
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
        In any other case, @trax` = @trax.
*/
add_tile :: !Coordinate !TraxTile !Trax -> Trax

/** tile_at @trax @coordinate = Nothing:
       when no tile is present at @coordinate in @trax.
    tile_at @trax @coordinate = Just @t:
       returns tile @t which is present at @coordinate in @trax.
*/
tile_at :: !Trax !Coordinate -> Maybe TraxTile

/** free_coordinates @trax = @free:
       computes the coordinates in which a new tile can be placed.
       These coordinates are all free direct neighbours of all tiles in @trax.
*/
free_coordinates :: !Trax -> [Coordinate]

:: LineColors

/** linecolors @trax @coordinate = @colors:
       computes of a potential tile at @coordinate in @trax the corresponding @colors of the line-endings.
       tile_at @trax @coordinate should be Nothing.
*/
linecolors :: !Trax !Coordinate -> LineColors

140 141
/** possible_tiles @trax @coordinate = @tiles:
		returns those @tiles that constitute a legal move in @trax at @coordinate.
Bas Lijnse's avatar
Bas Lijnse committed
142
*/
143
possible_tiles :: !Trax !Coordinate -> [TraxTile]
Bas Lijnse's avatar
Bas Lijnse committed
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170

:: Line

/** is_loop @path = True:
       holds only if @path is a closed loop.
    is_loop @path = False:
       @path is not a closed loop.
*/
is_loop :: !Line -> Bool

/** cut_loop @path = @path`:
       turns the infinite @path, forming a loop, into a finite @path` that contains all tiles.
*/
cut_loop :: !Line -> Line

/** loops @trax = Nothing:
       @trax contains no loop of RedLine or WhiteLine.
    loops @trax = @loops:
       @trax contains @loops, each indicating their color and path.
*/
loops :: !Trax -> [(LineColor,Line)]

/** winning_lines @trax = @lines:
       returns all winning @lines that start either at the west or north edge of @trax.
*/
winning_lines :: !Trax -> [(LineColor,Line)]

171
:: TraxSt
172 173 174 175
 = { trax   :: !Trax              // the current set of placed tiles
   , names  :: ![User]            // the current two players
   , turn   :: !Bool
   , choice :: !Maybe Coordinate
176 177
   }

178 179 180 181 182 183 184 185 186 187
/** mandatory_moves @trax @coordinate = Just @trax`:
       assumes that the tile at @coordinate in @trax is the most recently placed tile.
       It performs the mandatory moves that require filling empty places next to this
       tile, and all subsequent other empty places, resulting in @trax`.
    mandatory_moves @trax @coordinate = Nothing:
       at least one mandatory move occurred that resulted in an illegal configuration:
       an empty tile with three or four of the same line colors.
*/
mandatory_moves :: !Trax !Coordinate -> Maybe Trax

188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
/** game_over @st:
		returns True only if the given configuration in @st.trax contains one or more
		lines that connect opposite board edges, or one or more closed loops.
*/
game_over :: !TraxSt -> Bool

/** start_with_this @tile @st = @st`:
		@st` has @tile added to @st.trax, assuming that @st.trax is empty; 
		@st` toggles @st.turn, allowed the other player to put a tile.
*/
start_with_this :: !TraxTile !TraxSt -> TraxSt

/** setcell @choice @st:
		sets @st.choice to (Just @choice).
*/
setcell :: !Coordinate !TraxSt -> TraxSt

/** settile @c @t @st = @st`:
		@st` has tile @t added to @st.trax on coordinate @c.
		In addition, all the mandatory moves have been played as well.
		The turn is given to the other player.
*/
settile :: !Coordinate !TraxTile !TraxSt -> TraxSt