Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
P
puzzleplayer
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Floris
puzzleplayer
Commits
e1d09b6a
Commit
e1d09b6a
authored
3 months ago
by
Floris
Browse files
Options
Downloads
Plain Diff
Merge branch 'Sudoku'
parents
6c1a2688
b13062b7
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
PuzzlePlayer/Sudoku.cs
+54
-38
54 additions, 38 deletions
PuzzlePlayer/Sudoku.cs
with
54 additions
and
38 deletions
PuzzlePlayer/Sudoku.cs
+
54
−
38
View file @
e1d09b6a
...
@@ -14,6 +14,7 @@ namespace PuzzlePlayer_Namespace
...
@@ -14,6 +14,7 @@ namespace PuzzlePlayer_Namespace
{
{
private
static
int
boardLength
;
private
static
int
boardLength
;
private
static
int
rootBoardLength
;
private
static
int
rootBoardLength
;
private
static
Random
random
=
new
Random
();
public
Sudoku
(
int
boardSize
=
9
)
public
Sudoku
(
int
boardSize
=
9
)
{
{
boardState
=
GetClearBoard
(
boardSize
,
boardSize
);
boardState
=
GetClearBoard
(
boardSize
,
boardSize
);
...
@@ -27,6 +28,11 @@ namespace PuzzlePlayer_Namespace
...
@@ -27,6 +28,11 @@ namespace PuzzlePlayer_Namespace
public
override
void
Draw
(
Graphics
gr
,
Rectangle
r
)
public
override
void
Draw
(
Graphics
gr
,
Rectangle
r
)
{
{
StringFormat
format
=
new
StringFormat
{
LineAlignment
=
StringAlignment
.
Center
,
Alignment
=
StringAlignment
.
Center
,
};
Size
tilesize
=
new
Size
(
r
.
Width
/
boardState
.
GetLength
(
0
),
r
.
Height
/
boardState
.
GetLength
(
1
));
Size
tilesize
=
new
Size
(
r
.
Width
/
boardState
.
GetLength
(
0
),
r
.
Height
/
boardState
.
GetLength
(
1
));
Pen
border
=
new
Pen
(
Color
.
Black
,
2
);
Pen
border
=
new
Pen
(
Color
.
Black
,
2
);
gr
.
FillRectangle
(
Brushes
.
Beige
,
r
.
X
,
r
.
Y
,
tilesize
.
Width
*
boardState
.
GetLength
(
0
),
tilesize
.
Height
*
boardState
.
GetLength
(
1
));
gr
.
FillRectangle
(
Brushes
.
Beige
,
r
.
X
,
r
.
Y
,
tilesize
.
Width
*
boardState
.
GetLength
(
0
),
tilesize
.
Height
*
boardState
.
GetLength
(
1
));
...
@@ -47,8 +53,9 @@ namespace PuzzlePlayer_Namespace
...
@@ -47,8 +53,9 @@ namespace PuzzlePlayer_Namespace
(
boardState
[
i
,
j
]).
ToString
(),
(
boardState
[
i
,
j
]).
ToString
(),
new
Font
(
"Arial"
,
tilesize
.
Width
/
2
),
new
Font
(
"Arial"
,
tilesize
.
Width
/
2
),
Brushes
.
Black
,
Brushes
.
Black
,
(
float
)(
r
.
X
+
i
*
tilesize
.
Width
+
tilesize
.
Width
/
4
),
(
float
)(
r
.
X
+
(
i
+
0.27
)
*
tilesize
.
Width
+
tilesize
.
Width
/
4
),
(
float
)(
r
.
Y
+
j
*
tilesize
.
Height
+
tilesize
.
Height
/
4
)
(
float
)(
r
.
Y
+
(
j
+
0.33
)
*
tilesize
.
Height
+
tilesize
.
Height
/
4
),
format
);
);
}
}
...
@@ -74,21 +81,22 @@ namespace PuzzlePlayer_Namespace
...
@@ -74,21 +81,22 @@ namespace PuzzlePlayer_Namespace
FillBox
(
i
,
i
);
FillBox
(
i
,
i
);
}
}
Fill
Sudoku
(
0
,
rootBoardLength
);
Solve
Sudoku
();
RemoveSpaces
(
54
);
RemoveSpaces
(
(
int
)(
boardLength
*
boardLength
-
30
)
);
}
}
public
override
SOLUTIONS
Solve
(
bool
j
)
public
override
SOLUTIONS
Solve
(
bool
b
)
{
{
FillSudoku
(
0
,
rootBoardLength
);
if
(
SolveSudoku
())
{
return
SOLUTIONS
.
UNIQUE
;
return
SOLUTIONS
.
UNIQUE
;
}
return
SOLUTIONS
.
NONE
;
}
}
private
int
RandomNumber
(
int
number
)
private
int
RandomNumber
(
int
number
)
{
{
Random
random
=
new
Random
();
return
(
int
)
Math
.
Floor
((
double
)(
random
.
NextDouble
()
*
number
+
1
));
return
(
int
)
Math
.
Floor
((
double
)(
random
.
NextDouble
()
*
number
+
1
));
}
}
...
@@ -113,7 +121,7 @@ namespace PuzzlePlayer_Namespace
...
@@ -113,7 +121,7 @@ namespace PuzzlePlayer_Namespace
private
bool
DoAllChecks
(
int
i
,
int
j
,
int
num
)
private
bool
DoAllChecks
(
int
i
,
int
j
,
int
num
)
{
{
return
(
BoxFlag
(
i
-
i
%
rootBoardLength
,
j
-
j
%
rootBoardLength
,
num
)
||
Row
Flag
(
i
,
num
)
||
Col
Flag
(
j
,
num
));
return
(
BoxFlag
(
i
-
i
%
rootBoardLength
,
j
-
j
%
rootBoardLength
,
num
)
||
Col
Flag
(
i
,
num
)
||
Row
Flag
(
j
,
num
));
}
}
private
bool
BoxFlag
(
int
row
,
int
col
,
int
num
)
private
bool
BoxFlag
(
int
row
,
int
col
,
int
num
)
...
@@ -131,7 +139,7 @@ namespace PuzzlePlayer_Namespace
...
@@ -131,7 +139,7 @@ namespace PuzzlePlayer_Namespace
return
false
;
return
false
;
}
}
private
bool
Row
Flag
(
int
i
,
int
num
)
private
bool
Col
Flag
(
int
i
,
int
num
)
{
{
for
(
int
j
=
0
;
j
<
boardLength
;
j
++)
for
(
int
j
=
0
;
j
<
boardLength
;
j
++)
{
{
...
@@ -143,7 +151,7 @@ namespace PuzzlePlayer_Namespace
...
@@ -143,7 +151,7 @@ namespace PuzzlePlayer_Namespace
return
false
;
return
false
;
}
}
private
bool
Col
Flag
(
int
j
,
int
num
)
private
bool
Row
Flag
(
int
j
,
int
num
)
{
{
for
(
int
i
=
0
;
i
<
boardLength
;
i
++)
for
(
int
i
=
0
;
i
<
boardLength
;
i
++)
{
{
...
@@ -155,36 +163,34 @@ namespace PuzzlePlayer_Namespace
...
@@ -155,36 +163,34 @@ namespace PuzzlePlayer_Namespace
return
false
;
return
false
;
}
}
private
bool
FillSudoku
(
int
row
,
int
col
)
// Heilige piramide
private
bool
SolveSudoku
()
{
{
if
(
row
==
boardLength
-
1
&&
col
==
boardLength
)
for
(
int
row
=
0
;
row
<
boardLength
;
row
++)
return
true
;
if
(
col
==
boardLength
)
{
{
row
++;
for
(
int
col
=
0
;
col
<
boardLength
;
col
++)
col
=
0
;
}
if
(
boardState
[
row
,
col
]
!=
-
1
)
{
return
FillSudoku
(
row
,
col
+
1
);
}
for
(
int
num
=
1
;
num
<=
boardLength
;
num
++)
{
if
(!
DoAllChecks
(
row
,
col
,
num
))
{
{
boardState
[
row
,
col
]
=
num
;
if
(
boardState
[
row
,
col
]
==
emptySpace
)
if
(
FillSudoku
(
row
,
col
+
1
))
{
{
return
true
;
for
(
int
num
=
1
;
num
<=
boardLength
;
num
++)
{
if
(!
DoAllChecks
(
row
,
col
,
num
))
{
boardState
[
row
,
col
]
=
num
;
if
(
SolveSudoku
())
{
return
true
;
}
boardState
[
row
,
col
]
=
emptySpace
;
}
}
return
false
;
}
}
boardState
[
row
,
col
]
=
-
1
;
}
}
}
}
return
fals
e
;
return
tru
e
;
}
}
private
static
string
BoardToString
(
int
[,]
board
)
private
static
string
BoardToString
(
int
[,]
board
)
...
@@ -293,10 +299,20 @@ namespace PuzzlePlayer_Namespace
...
@@ -293,10 +299,20 @@ namespace PuzzlePlayer_Namespace
{
{
for
(
int
i
=
0
;
i
<
k
;
i
++)
for
(
int
i
=
0
;
i
<
k
;
i
++)
{
{
int
row
=
RandomNumber
(
boardLength
)
-
1
;
int
row
;
int
col
=
RandomNumber
(
boardLength
)
-
1
;
int
col
;
boardState
[
row
,
col
]
=
emptySpace
;
row
=
RandomNumber
(
boardLength
)
-
1
;
col
=
RandomNumber
(
boardLength
)
-
1
;
if
(
boardState
[
row
,
col
]
!=
emptySpace
)
{
boardState
[
row
,
col
]
=
emptySpace
;
}
else
{
i
--;
}
}
}
}
}
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment