You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
# This is a class that rappresents a sudoku board,
7
7
# that is, a 9x9 grid that can contains digits.
8
8
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
101
101
102
102
# The next line of code introduces a Action Function.
103
103
# Action Functions are pieces of codes that are written
@@ -114,85 +114,85 @@ cls Board:
114
114
# random sudoku board is generated by writing random
115
115
# numbers are written into it. The second part is the part
116
116
# where the user tries to solve the game.
117
+
117
118
@classes
118
119
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
0 commit comments