Skip to content

Commit bc89bbb

Browse files
committed
fix failing test
1 parent fea523a commit bc89bbb

File tree

1 file changed

+167
-166
lines changed

1 file changed

+167
-166
lines changed

tool/rlc/test/examples/sudoku.rl

Lines changed: 167 additions & 166 deletions
Original file line numberDiff line numberDiff line change
@@ -6,98 +6,98 @@ import action
66
# This is a class that rappresents a sudoku board,
77
# that is, a 9x9 grid that can contains digits.
88
cls Board:
9-
# The following line is a field declaration.
10-
# It speficies inside a Board there is a array
11-
# of 9x9 elements of type BInt<0, 10>, and such
12-
# array is named slots.
13-
14-
# a BInt<min, max> is a bounded integer,
15-
# it is used to specify the min and max
16-
# value that can placed inside the object.
17-
# In this case, it allows to insert into
18-
# the slots any number between 1 and 9,
19-
# and uses 0 as the value to rapresent
20-
# emtpyness
21-
BInt<0, 10>[9][9] slots
22-
23-
# This is a member function declaration
24-
# it declares a function called is_full,
25-
# with return type Bool, and zero
26-
# arguments
27-
# returns true when all the slots in the
28-
# game have been filled with a number.
29-
fun is_full() -> Bool:
30-
return self.count_empty() == 0
31-
32-
# count_empty returns the number of slots
33-
# that have not yet been filled with a
34-
# number
35-
fun count_empty() -> Int:
36-
let empty = 0
37-
let i = 0
38-
while i != 9:
39-
let y = 0
40-
while y != 9:
41-
if self.slots[y][i].value == 0:
42-
empty = empty + 1
43-
y = y + 1
44-
i = i + 1
45-
return empty
46-
47-
# returns true if the 3x3 region of the board
48-
# that contains a given cell does not contain
49-
# any cell with the given number.
50-
fun region_is_missing(Int row, Int column, Int number) -> Bool:
51-
let i = (row / 3)
52-
while i != (row / 3) + 3:
53-
let y = (column / 3)
54-
while y != (column / 3) + 3:
55-
if self.slots[y][i].value == number:
56-
return false
57-
y = y + 1
58-
i = i + 1
59-
return true
60-
61-
# returns true in the given column
62-
# does not contain the given number
63-
fun column_is_missing(Int column, Int number) -> Bool:
64-
let i = 0
65-
while i != 9:
66-
if self.slots[column][i].value == number:
67-
return false
68-
i = i + 1
69-
return true
70-
71-
# returns true in the given row
72-
# does not contain the given number
73-
fun row_is_missing(Int row, Int number) -> Bool:
74-
let i = 0
75-
while i != 9:
76-
if self.slots[i][row].value == number:
77-
return false
78-
i = i + 1
79-
return true
80-
81-
# returns true if marking the given cell
82-
# with the given number is valid
83-
fun can_mark(Int row, Int column, Int number) -> Bool:
84-
return self.row_is_missing(row, number) and self.column_is_missing(column, number) and self.region_is_missing(row, column, number) and self.slots[column][row] == 0
85-
86-
# returns true if there is at least a cell
87-
# that can be filled a number
88-
fun at_least_one_action_is_possible() -> Bool:
89-
let i = 0
90-
while i != 9:
91-
let y = 0
92-
while y != 9:
93-
let val = 1
94-
while val != 10:
95-
if self.can_mark(i, y, val):
96-
return true
97-
val = val + 1
98-
y = y + 1
99-
i = i + 1
100-
return false
9+
# The following line is a field declaration.
10+
# It speficies inside a Board there is a array
11+
# of 9x9 elements of type BInt<0, 10>, and such
12+
# array is named slots.
13+
14+
# a BInt<min, max> is a bounded integer,
15+
# it is used to specify the min and max
16+
# value that can placed inside the object.
17+
# In this case, it allows to insert into
18+
# the slots any number between 1 and 9,
19+
# and uses 0 as the value to rapresent
20+
# emtpyness
21+
BInt<0, 10>[9][9] slots
22+
23+
# This is a member function declaration
24+
# it declares a function called is_full,
25+
# with return type Bool, and zero
26+
# arguments
27+
# returns true when all the slots in the
28+
# game have been filled with a number.
29+
fun is_full() -> Bool:
30+
return self.count_empty() == 0
31+
32+
# count_empty returns the number of slots
33+
# that have not yet been filled with a
34+
# number
35+
fun count_empty() -> Int:
36+
let empty = 0
37+
let i = 0
38+
while i != 9:
39+
let y = 0
40+
while y != 9:
41+
if self.slots[y][i].value == 0:
42+
empty = empty + 1
43+
y = y + 1
44+
i = i + 1
45+
return empty
46+
47+
# returns true if the 3x3 region of the board
48+
# that contains a given cell does not contain
49+
# any cell with the given number.
50+
fun region_is_missing(Int row, Int column, Int number) -> Bool:
51+
let i = (row / 3)
52+
while i != (row / 3) + 3:
53+
let y = (column / 3)
54+
while y != (column / 3) + 3:
55+
if self.slots[y][i].value == number:
56+
return false
57+
y = y + 1
58+
i = i + 1
59+
return true
60+
61+
# returns true in the given column
62+
# does not contain the given number
63+
fun column_is_missing(Int column, Int number) -> Bool:
64+
let i = 0
65+
while i != 9:
66+
if self.slots[column][i].value == number:
67+
return false
68+
i = i + 1
69+
return true
70+
71+
# returns true in the given row
72+
# does not contain the given number
73+
fun row_is_missing(Int row, Int number) -> Bool:
74+
let i = 0
75+
while i != 9:
76+
if self.slots[i][row].value == number:
77+
return false
78+
i = i + 1
79+
return true
80+
81+
# returns true if marking the given cell
82+
# with the given number is valid
83+
fun can_mark(Int row, Int column, Int number) -> Bool:
84+
return self.row_is_missing(row, number) and self.column_is_missing(column, number) and self.region_is_missing(row, column, number) and self.slots[column][row] == 0
85+
86+
# returns true if there is at least a cell
87+
# that can be filled a number
88+
fun at_least_one_action_is_possible() -> Bool:
89+
let i = 0
90+
while i != 9:
91+
let y = 0
92+
while y != 9:
93+
let val = 1
94+
while val != 10:
95+
if self.can_mark(i, y, val):
96+
return true
97+
val = val + 1
98+
y = y + 1
99+
i = i + 1
100+
return false
101101

102102
# The next line of code introduces a Action Function.
103103
# Action Functions are pieces of codes that are written
@@ -114,85 +114,85 @@ cls Board:
114114
# random sudoku board is generated by writing random
115115
# numbers are written into it. The second part is the part
116116
# where the user tries to solve the game.
117+
117118
@classes
118119
act play() -> Game:
119-
# This is a variable declaration. It allocates a local
120-
# variable named board, of type Board. The variable is
121-
# introduced by the keyword frm, which specifies that
122-
# the variable is of interest not only to this function,
123-
# to the users of sudoku in general. This is of course
124-
# the case, since players need to look inside the sudoku
125-
# board to decide what to do.
126-
frm board : Board
127-
frm i = 0
128-
frm game_began = false
129-
while i != 20:
130-
# The following line introduces a action statement.
131-
# Actions statements are programmers can declare
132-
# the existance of a input that must be provided by
133-
# the user
134-
#
135-
# In this case are are declaring the existance of
136-
# of the action random_mark, which is invoked with
137-
# random arguments so that it can fill random cells
138-
# of the board before the player plays.
139-
#
140-
# The action has 3 arguments, x, y and the number to
141-
# be picked by the user.
142-
act random_mark(BInt<0, 9> x, BInt<0, 9> y, BInt<1, 10> number) {
143-
# The brackes after a action statement encode the
144-
# requirements of a action.
145-
board.can_mark(x.value, y.value, number.value)
146-
}
147-
# After a action statement has been executed, x, y and
148-
# number are now set the user wishes, so we can
149-
# mark the suggested point.
150-
board.slots[y.value][x.value] = number.value
151-
i = i + 1
152-
153-
# after we are done configurating the board we can
154-
# set to true the variable game_began, so that
155-
# outside users can know if we are still configuring
156-
# board or not
157-
game_began = true
158-
while !board.is_full() and board.at_least_one_action_is_possible():
159-
# while the game is not done, we allow the player to
160-
# mark cells.
161-
act mark(BInt<0, 9> x, BInt<0, 9> y, BInt<1, 10> number) {
162-
board.can_mark(x.value, y.value, number.value)
163-
}
164-
board.slots[y.value][x.value] = number.value
120+
# This is a variable declaration. It allocates a local
121+
# variable named board, of type Board. The variable is
122+
# introduced by the keyword frm, which specifies that
123+
# the variable is of interest not only to this function,
124+
# to the users of sudoku in general. This is of course
125+
# the case, since players need to look inside the sudoku
126+
# board to decide what to do.
127+
frm board : Board
128+
frm i = 0
129+
frm game_began = false
130+
while i != 20:
131+
# The following line introduces a action statement.
132+
# Actions statements are programmers can declare
133+
# the existance of a input that must be provided by
134+
# the user
135+
#
136+
# In this case are are declaring the existance of
137+
# of the action random_mark, which is invoked with
138+
# random arguments so that it can fill random cells
139+
# of the board before the player plays.
140+
#
141+
# The action has 3 arguments, x, y and the number to
142+
# be picked by the user.
143+
144+
# The brackes after a action statement encode the
145+
# requirements of a action.
146+
act random_mark(BInt<0, 9> x, BInt<0, 9> y, BInt<1, 10> number) { board.can_mark(x.value, y.value, number.value) }
147+
148+
# After a action statement has been executed, x, y and
149+
# number are now set the user wishes, so we can
150+
# mark the suggested point.
151+
board.slots[y.value][x.value] = number.value
152+
i = i + 1
153+
154+
# after we are done configurating the board we can
155+
# set to true the variable game_began, so that
156+
# outside users can know if we are still configuring
157+
# board or not
158+
game_began = true
159+
while !board.is_full() and board.at_least_one_action_is_possible():
160+
# while the game is not done, we allow the player to
161+
# mark cells.
162+
act mark(BInt<0, 9> x, BInt<0, 9> y, BInt<1, 10> number) { board.can_mark(x.value, y.value, number.value) }
163+
164+
board.slots[y.value][x.value] = number.value
165165

166166
# When using the machine learning layer
167167
# delivered by the rlc package you need
168168
# to provide a function that give a game
169169
# returns the current player.
170170
fun get_current_player(Game g) -> Int:
171-
# -4 is a special number to specify
172-
# that the game is done.
173-
if g.is_done():
174-
return -4
175-
# -1 is a special number to specify
176-
# that the next action must be taken
177-
# randomly
178-
if !g.game_began:
179-
return -1
180-
181-
# the first player has id 0.
182-
return 0
171+
# -4 is a special number to specify
172+
# that the game is done.
173+
if g.is_done():
174+
return -4
175+
# -1 is a special number to specify
176+
# that the next action must be taken
177+
# randomly
178+
if !g.game_began:
179+
return -1
180+
181+
# the first player has id 0.
182+
return 0
183183

184184
# We need to specify the maximal number
185185
# of players, so that the machine learning
186186
# can configure itself with the correct
187187
# number of agents
188188
fun get_num_players() -> Int:
189-
return 1
189+
return 1
190190

191191
# To avoid games that never terminate, we provide
192192
# a function that suggest a maximal number of actions
193193
# 200 is greatly overestimated.
194194
fun max_game_lenght() -> Int:
195-
return 200
195+
return 200
196196

197197
# For the machine learning to learn we need
198198
# to specify the score of the given player,
@@ -203,23 +203,24 @@ fun max_game_lenght() -> Int:
203203
# and we interpolate between the two depending
204204
# on how many number it has managed to write
205205
# otherwise
206-
fun score(Game g, Int player_id) -> Float:
207-
return 1.0 - float(g.board.count_empty()) / 81.0
206+
fun score(Game g, Int player_id) -> Float:
207+
return 1.0 - float(g.board.count_empty()) / 81.0
208208

209209
# this function is looked for by the machine
210210
# learning components when they wish to print
211211
# a human readable version of the game, so
212212
# that the user may understand what is going on
213213
fun pretty_print(Game g):
214-
let i = 0
215-
while i != 9:
216-
let to_print : String
217-
let y = 0
218-
while y != 9:
219-
if g.board.slots[y][i].value == 0:
220-
to_print.append("_")
221-
else:
222-
to_print.append(to_string(g.board.slots[y][i].value))
223-
y = y + 1
224-
print(to_print)
225-
i = i + 1
214+
let i = 0
215+
while i != 9:
216+
let to_print : String
217+
let y = 0
218+
while y != 9:
219+
if g.board.slots[y][i].value == 0:
220+
to_print.append("_")
221+
else:
222+
to_print.append(to_string(g.board.slots[y][i].value))
223+
y = y + 1
224+
print(to_print)
225+
i = i + 1
226+

0 commit comments

Comments
 (0)