Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
W
Worldgeneration PoC
Manage
Activity
Members
Labels
Plan
Issues
0
Issue boards
Milestones
Iterations
Wiki
Requirements
Jira
Code
Merge requests
0
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package Registry
Container Registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
ASD-Project-S2-2024-klas-B
Worldgeneration PoC
Commits
df1ad16a
Commit
df1ad16a
authored
10 months ago
by
Wai Leung Chan
Browse files
Options
Downloads
Patches
Plain Diff
Add Recursive Division PoC
parent
be9a12f1
Branches
recursive-division
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/main/java/org/asdb/DungeonGenerator.java
+102
-0
102 additions, 0 deletions
src/main/java/org/asdb/DungeonGenerator.java
with
102 additions
and
0 deletions
src/main/java/org/asdb/DungeonGenerator.java
0 → 100644
+
102
−
0
View file @
df1ad16a
package
org.asdb
;
public
class
DungeonGenerator
{
public
static
void
main
(
String
[]
args
)
{
DungeonGenerator
dungeonGenerator
=
new
DungeonGenerator
(
150
,
30
);
// Example size: 40x20
dungeonGenerator
.
generateDungeon
();
}
private
char
[][]
dungeonMap
;
private
int
width
;
private
int
height
;
public
DungeonGenerator
(
int
width
,
int
height
)
{
this
.
width
=
width
;
this
.
height
=
height
;
this
.
dungeonMap
=
new
char
[
height
][
width
];
}
public
void
generateDungeon
()
{
// Fill the dungeon with walls
for
(
int
i
=
0
;
i
<
height
;
i
++)
{
for
(
int
j
=
0
;
j
<
width
;
j
++)
{
dungeonMap
[
i
][
j
]
=
'.'
;
}
}
// Divide the dungeon recursively using the Recursive Division algorithm
divideSpace
(
0
,
0
,
width
-
1
,
height
-
1
,
3
);
// Print the dungeon map
for
(
int
i
=
0
;
i
<
height
;
i
++)
{
for
(
int
j
=
0
;
j
<
width
;
j
++)
{
System
.
out
.
print
(
dungeonMap
[
i
][
j
]);
}
System
.
out
.
println
();
}
}
private
void
divideSpace
(
int
x1
,
int
y1
,
int
x2
,
int
y2
,
int
divisions
)
{
// Check if we reached the desired number of divisions
if
(
divisions
==
0
)
{
// Draw a horizontal line through the middle of the space
int
midY
=
(
y1
+
y2
)
/
2
;
for
(
int
j
=
x1
;
j
<=
x2
;
j
++)
{
dungeonMap
[
midY
][
j
]
=
'#'
;
}
// Open passages in the walls
int
passageX
=
getRandomNumberInRange
(
x1
,
x2
-
2
);
// Adjusted range to ensure space for 3 pixels
for
(
int
k
=
passageX
;
k
<=
passageX
+
2
;
k
++)
{
dungeonMap
[
midY
][
k
]
=
'.'
;
}
return
;
}
// Decide whether to divide horizontally or vertically
boolean
divideHorizontally
=
Math
.
random
()
<
0.5
;
// If we are dividing horizontally
if
(
divideHorizontally
)
{
// Determine the position of the dividing line
int
splitY
=
getRandomNumberInRange
(
y1
+
1
,
y2
-
1
);
// Draw the dividing line
for
(
int
j
=
x1
;
j
<=
x2
;
j
++)
{
dungeonMap
[
splitY
][
j
]
=
'#'
;
}
// Open passages in the walls
int
passageX
=
getRandomNumberInRange
(
x1
,
x2
);
// Adjusted range
dungeonMap
[
splitY
][
passageX
]
=
'.'
;
// Recur on the resulting subspaces
divideSpace
(
x1
,
y1
,
x2
,
splitY
-
1
,
divisions
-
1
);
divideSpace
(
x1
,
splitY
+
1
,
x2
,
y2
,
divisions
-
1
);
}
else
{
// If we are dividing vertically
// Determine the position of the dividing line
int
splitX
=
getRandomNumberInRange
(
x1
+
1
,
x2
-
1
);
// Draw the dividing line
for
(
int
i
=
y1
;
i
<=
y2
;
i
++)
{
dungeonMap
[
i
][
splitX
]
=
'#'
;
}
// Open passages in the walls
int
passageY
=
getRandomNumberInRange
(
y1
,
y2
-
2
);
// Adjusted range to ensure space for 3 pixels
for
(
int
k
=
passageY
;
k
<=
passageY
+
2
;
k
++)
{
dungeonMap
[
k
][
splitX
]
=
'.'
;
}
// Recur on the resulting subspaces
divideSpace
(
x1
,
y1
,
splitX
-
1
,
y2
,
divisions
-
1
);
divideSpace
(
splitX
+
1
,
y1
,
x2
,
y2
,
divisions
-
1
);
}
}
// Helper method to generate a random number within a range
private
int
getRandomNumberInRange
(
int
min
,
int
max
)
{
return
(
int
)
(
Math
.
random
()
*
((
max
-
min
)
+
1
))
+
min
;
}
}
\ No newline at end of file
This diff is collapsed.
Click to expand it.
Preview
0%
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