Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
C
COMP1206-Mathdoku
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Model registry
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Nimrod Abramovich
COMP1206-Mathdoku
Commits
41cb6d55
Commit
41cb6d55
authored
5 years ago
by
Nimrod Abramovich
Browse files
Options
Downloads
Patches
Plain Diff
grid values & cage generator added
parent
4b4264b2
No related branches found
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
bin/coursework/RandomGenerator.class
+0
-0
0 additions, 0 deletions
bin/coursework/RandomGenerator.class
src/coursework/RandomGenerator.java
+317
-0
317 additions, 0 deletions
src/coursework/RandomGenerator.java
with
317 additions
and
0 deletions
bin/coursework/RandomGenerator.class
0 → 100644
+
0
−
0
View file @
41cb6d55
File added
This diff is collapsed.
Click to expand it.
src/coursework/RandomGenerator.java
0 → 100644
+
317
−
0
View file @
41cb6d55
package
coursework
;
import
java.util.ArrayList
;
import
java.util.HashSet
;
import
java.util.List
;
import
java.util.Random
;
public
class
RandomGenerator
{
private
static
int
N
=
6
;
private
static
int
[][]
grid
=
new
int
[
N
][
N
];
private
static
List
<
Integer
>
uncagedGrids
=
new
ArrayList
<
Integer
>();
private
static
List
<
Integer
>
possibleMoves
;
public
static
void
main
(
String
[]
args
)
{
generate
();
}
private
static
void
generate
()
{
generateGridValues
();
shuffleGridValues
();
generateCages
();
}
private
static
void
generateCages
()
{
Random
random
=
new
Random
();
addAllGrids
();
while
(
uncagedGrids
.
size
()
>
0
)
{
int
cageProbability
=
random
.
nextInt
(
100
);
int
cageSize
;
if
(
cageProbability
<
50
)
{
cageSize
=
2
;
}
else
if
(
cageProbability
<
85
)
{
cageSize
=
3
;
}
else
{
cageSize
=
4
;
}
if
(
uncagedGrids
.
size
()
<=
cageSize
)
{
cageSize
=
uncagedGrids
.
size
();
}
if
(
cageSize
==
0
)
{
break
;
}
int
[]
cageIds
=
new
int
[
cageSize
];
int
id
=
uncagedGrids
.
get
(
0
);
cageIds
[
0
]
=
id
;
uncagedGrids
.
remove
(
0
);
for
(
int
i
=
0
;
i
<
cageSize
-
1
;
i
++)
{
int
cornerGrid
=
isCornerGrid
(
id
);
int
edgeGrid
=
isEdgeGrid
(
id
);
possibleMoves
=
new
ArrayList
<
Integer
>();
possibleMoves
.
add
(
id
+
1
);
possibleMoves
.
add
(
id
-
1
);
possibleMoves
.
add
(
id
+
N
);
possibleMoves
.
add
(
id
-
N
);
if
(
cornerGrid
!=
-
1
)
{
if
(
cornerGrid
==
0
)
{
topLeftCorner
(
id
);
}
else
if
(
cornerGrid
==
1
)
{
topRightCorner
(
id
);
}
else
if
(
cornerGrid
==
2
)
{
bottomRightCorner
(
id
);
}
else
if
(
cornerGrid
==
3
)
{
bottomLeftCorner
(
id
);
}
}
else
if
(
edgeGrid
!=
-
1
)
{
if
(
edgeGrid
==
0
)
{
leftEdge
(
id
);
}
else
if
(
edgeGrid
==
1
)
{
rightEdge
(
id
);
}
else
if
(
edgeGrid
==
2
)
{
topEdge
(
id
);
}
else
if
(
edgeGrid
==
3
)
{
bottomEdge
(
id
);
}
}
else
{
centre
(
id
);
}
if
(
possibleMoves
.
size
()
>=
1
)
{
id
=
possibleMoves
.
get
(
random
.
nextInt
(
possibleMoves
.
size
()));
}
cageIds
[
i
+
1
]
=
id
;
if
(
uncagedGrids
.
contains
(
id
))
{
uncagedGrids
.
remove
(
uncagedGrids
.
indexOf
(
id
));
}
}
String
operator
=
"+"
;
int
value
=
0
;
//Cage cage = new Cage(cageIds, value, operator);
}
}
private
static
void
centre
(
int
id
)
{
if
(!
uncagedGrids
.
contains
(
id
+
N
))
possibleMoves
.
remove
(
possibleMoves
.
indexOf
(
id
+
N
));
if
(!
uncagedGrids
.
contains
(
id
-
N
))
possibleMoves
.
remove
(
possibleMoves
.
indexOf
(
id
-
N
));
if
(!
uncagedGrids
.
contains
(
id
+
1
))
possibleMoves
.
remove
(
possibleMoves
.
indexOf
(
id
+
1
));
if
(!
uncagedGrids
.
contains
(
id
-
1
))
possibleMoves
.
remove
(
possibleMoves
.
indexOf
(
id
-
1
));
}
private
static
void
topLeftCorner
(
int
id
)
{
possibleMoves
.
remove
(
possibleMoves
.
indexOf
(
id
-
1
));
possibleMoves
.
remove
(
possibleMoves
.
indexOf
(
id
-
N
));
if
(!
uncagedGrids
.
contains
(
id
+
1
))
possibleMoves
.
remove
(
possibleMoves
.
indexOf
(
id
+
1
));
if
(!
uncagedGrids
.
contains
(
id
+
N
))
possibleMoves
.
remove
(
possibleMoves
.
indexOf
(
id
+
N
));
}
private
static
void
topRightCorner
(
int
id
)
{
possibleMoves
.
remove
(
possibleMoves
.
indexOf
(
id
-
N
));
possibleMoves
.
remove
(
possibleMoves
.
indexOf
(
id
+
1
));
if
(!
uncagedGrids
.
contains
(
id
-
1
))
possibleMoves
.
remove
(
possibleMoves
.
indexOf
(
id
-
1
));
if
(!
uncagedGrids
.
contains
(
id
+
N
))
possibleMoves
.
remove
(
possibleMoves
.
indexOf
(
id
+
N
));
}
private
static
void
bottomRightCorner
(
int
id
)
{
possibleMoves
.
remove
(
possibleMoves
.
indexOf
(
id
+
N
));
possibleMoves
.
remove
(
possibleMoves
.
indexOf
(
id
+
1
));
if
(!
uncagedGrids
.
contains
(
id
-
1
))
possibleMoves
.
remove
(
possibleMoves
.
indexOf
(
id
-
1
));
if
(!
uncagedGrids
.
contains
(
id
-
N
))
possibleMoves
.
remove
(
possibleMoves
.
indexOf
(
id
-
N
));
}
private
static
void
bottomLeftCorner
(
int
id
)
{
possibleMoves
.
remove
(
possibleMoves
.
indexOf
(
id
+
N
));
possibleMoves
.
remove
(
possibleMoves
.
indexOf
(
id
-
1
));
if
(!
uncagedGrids
.
contains
(
id
+
1
))
possibleMoves
.
remove
(
possibleMoves
.
indexOf
(
id
+
1
));
if
(!
uncagedGrids
.
contains
(
id
-
N
))
possibleMoves
.
remove
(
possibleMoves
.
indexOf
(
id
-
N
));
}
private
static
void
leftEdge
(
int
id
)
{
possibleMoves
.
remove
(
possibleMoves
.
indexOf
(
id
-
1
));
if
(!
uncagedGrids
.
contains
(
id
+
N
))
possibleMoves
.
remove
(
possibleMoves
.
indexOf
(
id
+
N
));
if
(!
uncagedGrids
.
contains
(
id
-
N
))
possibleMoves
.
remove
(
possibleMoves
.
indexOf
(
id
-
N
));
if
(!
uncagedGrids
.
contains
(
id
+
1
))
possibleMoves
.
remove
(
possibleMoves
.
indexOf
(
id
+
1
));
}
private
static
void
topEdge
(
int
id
)
{
possibleMoves
.
remove
(
possibleMoves
.
indexOf
(
id
-
N
));
if
(!
uncagedGrids
.
contains
(
id
+
N
))
possibleMoves
.
remove
(
possibleMoves
.
indexOf
(
id
+
N
));
if
(!
uncagedGrids
.
contains
(
id
+
1
))
possibleMoves
.
remove
(
possibleMoves
.
indexOf
(
id
+
1
));
if
(!
uncagedGrids
.
contains
(
id
-
1
))
possibleMoves
.
remove
(
possibleMoves
.
indexOf
(
id
-
1
));
}
private
static
void
rightEdge
(
int
id
)
{
possibleMoves
.
remove
(
possibleMoves
.
indexOf
(
id
+
1
));
if
(!
uncagedGrids
.
contains
(
id
-
N
))
possibleMoves
.
remove
(
possibleMoves
.
indexOf
(
id
-
N
));
if
(!
uncagedGrids
.
contains
(
id
+
N
))
possibleMoves
.
remove
(
possibleMoves
.
indexOf
(
id
+
N
));
if
(!
uncagedGrids
.
contains
(
id
-
1
))
possibleMoves
.
remove
(
possibleMoves
.
indexOf
(
id
-
1
));
}
private
static
void
bottomEdge
(
int
id
)
{
possibleMoves
.
remove
(
possibleMoves
.
indexOf
(
id
+
N
));
if
(!
uncagedGrids
.
contains
(
id
-
N
))
possibleMoves
.
remove
(
possibleMoves
.
indexOf
(
id
-
N
));
if
(!
uncagedGrids
.
contains
(
id
-
1
))
possibleMoves
.
remove
(
possibleMoves
.
indexOf
(
id
-
1
));
if
(!
uncagedGrids
.
contains
(
id
+
1
))
possibleMoves
.
remove
(
possibleMoves
.
indexOf
(
id
+
1
));
}
private
static
void
addAllGrids
()
{
for
(
int
i
=
0
;
i
<
N
*
N
;
i
++)
{
uncagedGrids
.
add
(
i
);
}
}
private
static
int
isEdgeGrid
(
int
gridId
)
{
if
(
gridId
%
N
==
0
)
// left edge
return
0
;
if
(
gridId
%
N
==
N
-
1
)
// right edge
return
1
;
if
((
int
)
gridId
/
N
==
0
)
// top edge
return
2
;
if
((
int
)
gridId
/
N
==
N
-
1
)
// bottom edge
return
3
;
return
-
1
;
// not an edge grid
}
private
static
int
isCornerGrid
(
int
gridId
)
{
if
((
int
)
gridId
/
N
==
0
&&
gridId
%
N
==
0
)
// top left corner
return
0
;
if
((
int
)
gridId
/
N
==
N
-
1
&&
gridId
%
N
==
N
-
1
)
// bottom right corner
return
2
;
if
((
int
)
gridId
/
N
==
0
&&
gridId
%
N
==
N
-
1
)
// top right corner
return
1
;
if
((
int
)
gridId
/
N
==
N
-
1
&&
gridId
%
N
==
0
)
// bottom left corner
return
3
;
return
-
1
;
// not a corner
}
private
static
void
generateGridValues
()
{
int
counter
=
1
;
for
(
int
i
=
0
;
i
<
N
;
i
++)
{
for
(
int
j
=
0
;
j
<
N
;
j
++)
{
grid
[
i
][
j
]
=
counter
;
counter
++;
if
(
counter
==
N
+
1
)
{
counter
=
1
;
}
}
counter
--;
if
(
counter
==
0
)
{
counter
=
N
;
}
}
}
private
static
void
shuffleGridValues
()
{
Random
random
=
new
Random
();
for
(
int
i
=
0
;
i
<
15
;
i
++)
{
int
[]
colsSwap
=
new
int
[
2
];
int
[]
rowsSwap
=
new
int
[
2
];
if
(
colsSwap
[
0
]
==
colsSwap
[
1
])
{
colsSwap
=
generateValues
(
random
);
}
if
(
rowsSwap
[
0
]
==
rowsSwap
[
1
])
{
rowsSwap
=
generateValues
(
random
);
}
swapColumns
(
colsSwap
[
0
],
colsSwap
[
1
]);
swapRows
(
rowsSwap
[
0
],
rowsSwap
[
1
]);
if
(
i
==
7
)
{
transpose
();
}
}
}
private
static
int
[]
generateValues
(
Random
random
)
{
return
new
int
[]
{
random
.
nextInt
(
N
),
random
.
nextInt
(
N
)
};
}
private
static
void
swapColumns
(
int
colA
,
int
colB
)
{
int
[][]
columns
=
new
int
[
N
][
2
];
for
(
int
i
=
0
;
i
<
N
;
i
++)
{
columns
[
i
][
0
]
=
grid
[
i
][
colA
];
columns
[
i
][
1
]
=
grid
[
i
][
colB
];
}
for
(
int
i
=
0
;
i
<
N
;
i
++)
{
grid
[
i
][
colA
]
=
columns
[
i
][
1
];
grid
[
i
][
colB
]
=
columns
[
i
][
0
];
}
}
private
static
void
swapRows
(
int
rowA
,
int
rowB
)
{
int
[][]
rows
=
new
int
[
N
][
2
];
for
(
int
i
=
0
;
i
<
N
;
i
++)
{
rows
[
i
][
0
]
=
grid
[
rowA
][
i
];
rows
[
i
][
1
]
=
grid
[
rowB
][
i
];
}
for
(
int
i
=
0
;
i
<
N
;
i
++)
{
grid
[
rowA
][
i
]
=
rows
[
i
][
1
];
grid
[
rowB
][
i
]
=
rows
[
i
][
0
];
}
}
private
static
void
transpose
()
{
int
[][]
temp
=
new
int
[
N
][
N
];
for
(
int
i
=
0
;
i
<
N
;
i
++)
{
for
(
int
j
=
0
;
j
<
N
;
j
++)
{
temp
[
j
][
i
]
=
grid
[
i
][
j
];
}
}
grid
=
temp
;
}
private
static
void
output
()
{
for
(
int
i
=
0
;
i
<
N
;
i
++)
{
for
(
int
j
=
0
;
j
<
N
;
j
++)
{
System
.
out
.
print
(
grid
[
i
][
j
]
+
" "
);
}
System
.
out
.
print
(
"\n"
);
}
System
.
out
.
print
(
"\n"
);
}
}
\ No newline at end of file
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