Skip to content

Commit

Permalink
[fix] some updates in tictactopa
Browse files Browse the repository at this point in the history
 * patching for nullary lambda
 * correcting a bug in ColSet.specialize
 * removing the warnings, add the flag warn-error root
 * completing documentation of funaction handler
  • Loading branch information
Mathieu Barbin committed Mar 28, 2011
1 parent 3a80167 commit 90a7efd
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 31 deletions.
6 changes: 3 additions & 3 deletions multitub/multitub.opa
Original file line number Original file line Diff line number Diff line change
@@ -1,5 +1,5 @@
/* /*
* OPA design pattern collection. (c) MLstate - 2010 * OPA design pattern collection. (c) MLstate - 2011
* *
* The Mutlitub pattern, for OPA-S3. * The Mutlitub pattern, for OPA-S3.
* @author Mathieu Barbin * @author Mathieu Barbin
Expand Down Expand Up @@ -130,7 +130,7 @@ type Multitub.C.interface('state) = {
* may be imperative, there are no reason to forbid it, e.g. wrt performances * may be imperative, there are no reason to forbid it, e.g. wrt performances
* depending on the kind of the application. * depending on the kind of the application.
**/ **/
init : void -> 'state init : -> 'state


/** /**
* Handle messages received from the server, or from some funaction in the page. * Handle messages received from the server, or from some funaction in the page.
Expand Down Expand Up @@ -184,7 +184,7 @@ type Multitub.S.interface('state) = {
* The initializer is not functionnal because the state of the server * The initializer is not functionnal because the state of the server
* may be imperative. * may be imperative.
**/ **/
init : void -> 'state init : -> 'state


/** /**
* Some initialization may be necessary once the server knows the client. * Some initialization may be necessary once the server knows the client.
Expand Down
1 change: 0 additions & 1 deletion src/client.opa
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -238,7 +238,6 @@ clear() =
token = ClientToken.token(location) token = ClientToken.token(location)
do ClientToken.clear(token) do ClientToken.clear(token)
void void
columns = pred
do Loop.for2((0, GameParameters.dimensions.columns - 1), (0, GameParameters.dimensions.lines - 1), clear_token) do Loop.for2((0, GameParameters.dimensions.columns - 1), (0, GameParameters.dimensions.lines - 1), clear_token)
void void


Expand Down
11 changes: 2 additions & 9 deletions src/colset.opa
Original file line number Original file line Diff line number Diff line change
@@ -1,5 +1,5 @@
/* /*
* Tictactopa. (c) MLstate - 2010 * Tictactopa. (c) MLstate - 2011
* @author Mathieu Barbin * @author Mathieu Barbin
**/ **/


Expand Down Expand Up @@ -78,14 +78,7 @@ type ColSet.t = int


specialize(setA, setB) = specialize(setA, setB) =
inter = inter(setA, setB) inter = inter(setA, setB)
if is_empty(inter) then setA else setB if is_empty(inter) then setA else inter

priority_inter(list : list(ColSet.t), set : ColSet.t) =
fold_inter(set, acc) = specialize(acc, set)
match list with
| { nil } -> empty
| ~{ hd tl } ->
List.fold(fold_inter, tl, hd)


/** /**
* Pick a random elt in the set. * Pick a random elt in the set.
Expand Down
23 changes: 18 additions & 5 deletions src/funaction.opa
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -9,17 +9,30 @@


@client @package Funaction = {{ @client @package Funaction = {{


onclick_token(s_channel : Multitub.S.channel, c_channel : Multitub.C.channel, location : Grid.location, _) = /**
i = location.column * The client has perform a click on the grid.
j = location.line * In this case, we pass through the client tube, and send to it the [click] message
* No direct interaction with the server tube with this event.
**/
onclick_token(_ : Multitub.S.channel, c_channel : Multitub.C.channel, location : Grid.location, _) =
do Multitub.send_client(c_channel, {funaction = { click = location } }) do Multitub.send_client(c_channel, {funaction = { click = location } })
void void


restart(s_channel : Multitub.S.channel, c_channel : Multitub.C.channel, _) = /**
* The client has perform a click on the "start a new game" button.
* In this case, we pass through the client tube, and send to it the [restart] message
* No direct interaction with the server tube with this event.
**/
restart(_ : Multitub.S.channel, c_channel : Multitub.C.channel, _) =
do Multitub.send_client(c_channel, {funaction = {restart}}) do Multitub.send_client(c_channel, {funaction = {restart}})
void void


level(s_channel : Multitub.S.channel, c_channel : Multitub.C.channel, dom_level : dom, _) = /**
* The client has perform a click on the level toggle button.
* In this case, we pass direclty through the server tube, and send to it the [ia_parameters] message
* No direct interaction with the client tube with this event.
**/
level(s_channel : Multitub.S.channel, _ : Multitub.C.channel, dom_level : dom, _) =
value = Dom.get_value(dom_level) value = Dom.get_value(dom_level)
level = int_of_string_unsafe(value) level = int_of_string_unsafe(value)
do Multitub.send_server(s_channel, {ia_parameters = { ~level }}) do Multitub.send_server(s_channel, {ia_parameters = { ~level }})
Expand Down
14 changes: 9 additions & 5 deletions src/game.opa
Original file line number Original file line Diff line number Diff line change
@@ -1,5 +1,5 @@
/* /*
* Tictactopa. (c) MLstate - 2010 * Tictactopa. (c) MLstate - 2011
* @author Mathieu Barbin * @author Mathieu Barbin
**/ **/


Expand Down Expand Up @@ -249,9 +249,9 @@ type Game.state = {
* Status detection: * Status detection:
* From a non {free} case, follow from a location in a given direction as long as * From a non {free} case, follow from a location in a given direction as long as
* the content does not change, or the value exceed goal. In this case, return the * the content does not change, or the value exceed goal. In this case, return the
* corresponding player. * corresponding player, else return [none]
**/ **/
follow(grid : Game.grid, i, j, direction : Grid.location) = follow(grid : Game.grid, i, j, direction : Grid.location) : Game.winner =
goal = GameParameters.goal goal = GameParameters.goal
columns = GameParameters.dimensions.columns columns = GameParameters.dimensions.columns
lines = GameParameters.dimensions.lines lines = GameParameters.dimensions.lines
Expand Down Expand Up @@ -280,7 +280,7 @@ type Game.state = {
* Follow in every direction (4), from every non-free location. * Follow in every direction (4), from every non-free location.
* Stops with the first success * Stops with the first success
**/ **/
status(grid : Game.grid) = status(grid : Game.grid) : Game.winner =
h = { column = 1 ; line = 0 } h = { column = 1 ; line = 0 }
v = { column = 0 ; line = 1} v = { column = 0 ; line = 1}
du = { column = 1 ; line = 1 } du = { column = 1 ; line = 1 }
Expand Down Expand Up @@ -368,9 +368,13 @@ type Game.state = {
GameUtils.count(grid, snd) GameUtils.count(grid, snd)
total = GameParameters.dimensions.columns * GameParameters.dimensions.lines total = GameParameters.dimensions.columns * GameParameters.dimensions.lines
match GameUtils.status(grid) with match GameUtils.status(grid) with
| ({some=player}) as winner -> | ({some=_}) as winner ->
{ winner = winner } { winner = winner }
| _ -> | _ ->
/*
In case [GameUtils.status = none], it could means that the game is over with exaeco,
or if the grid is not full, that the game is still in progress.
*/
if total == n_fst + n_snd if total == n_fst + n_snd
then then
{ winner = none } { winner = none }
Expand Down
20 changes: 14 additions & 6 deletions src/grid.opa
Original file line number Original file line Diff line number Diff line change
@@ -1,5 +1,5 @@
/* /*
* Tictactopa. (c) MLstate - 2010 * Tictactopa. (c) MLstate - 2011
* @author Mathieu Barbin * @author Mathieu Barbin
**/ **/


Expand Down Expand Up @@ -127,16 +127,24 @@ type Grid.t('content) = {
**/ **/
dimensions(grid : Grid.t) = grid.dimensions dimensions(grid : Grid.t) = grid.dimensions


/**
* Get the content of a location in the content of a grid.
* Low level function, not exported.
* @errors(Unspecified in case of index out of bounds)
**/
@private getij_t(t : llarray(llarray('content)), column : Grid.column, line : Grid.line) =
t_line = LowLevelArray.get(t, column)
content = LowLevelArray.get(t_line, line)
content

/** /**
* Get the content of a location in a grid. * Get the content of a location in a grid.
* Interface usefull for loops. * Interface usefull for loops.
* @errors(Unspecified in case of index out of bounds) * @errors(Unspecified in case of index out of bounds)
**/ **/
getij(grid : Grid.t, column : Grid.column, line : Grid.line) = getij(grid : Grid.t, column : Grid.column, line : Grid.line) =
t = grid.t t = grid.t
t_line = LowLevelArray.get(t, column) getij_t(t, column, line)
content = LowLevelArray.get(t_line, line)
content


/** /**
* Get the content of a location in a grid. * Get the content of a location in a grid.
Expand Down Expand Up @@ -193,7 +201,7 @@ type Grid.t('content) = {
dimensions = grid.dimensions dimensions = grid.dimensions
columns = dimensions.columns columns = dimensions.columns
lines = dimensions.lines lines = dimensions.lines
t = grid.t grid_t = grid.t
column = location.column column = location.column
line = location.line line = location.line
min_c = max(0, column - dist) min_c = max(0, column - dist)
Expand All @@ -207,7 +215,7 @@ type Grid.t('content) = {
else else
if ((i == column) && (j == line)) then aux(i, succ(j), acc) if ((i == column) && (j == line)) then aux(i, succ(j), acc)
else else
acc = fold(getij(grid, i, j), acc) acc = fold(getij_t(grid_t, i, j), acc)
aux(i, succ(j), acc) aux(i, succ(j), acc)
aux(min_c, min_l, acc) aux(min_c, min_l, acc)


Expand Down
4 changes: 2 additions & 2 deletions src/ia.opa
Original file line number Original file line Diff line number Diff line change
@@ -1,5 +1,5 @@
/* /*
* Tictactopa. (c) MLstate - 2010 * Tictactopa. (c) MLstate - 2011
* @author Mathieu Barbin * @author Mathieu Barbin
*/ */


Expand Down Expand Up @@ -79,7 +79,7 @@ IA_Winning = {{
p = ( p : Game.player ) <: Game.content p = ( p : Game.player ) <: Game.content
GameContent.equal(player, p) GameContent.equal(player, p)
| _ -> false | _ -> false
grid = Grid.setij(grid, i, j, {free}) _grid = Grid.setij(grid, i, j, {free})
res res


compute(grid : Game.grid, win : IA.winning_grid) : IA.winning_grid = compute(grid : Game.grid, win : IA.winning_grid) : IA.winning_grid =
Expand Down
1 change: 1 addition & 0 deletions tictactopa.opack
Original file line number Original file line Diff line number Diff line change
@@ -1,4 +1,5 @@
# sources for tictactopa # sources for tictactopa
--warn-error root
src/colset.opa src/colset.opa
src/grid.opa src/grid.opa
src/game.opa src/game.opa
Expand Down

0 comments on commit 90a7efd

Please sign in to comment.