Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
P
prog3-coursework
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Package registry
Model registry
Operate
Terraform modules
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
ik1g19
prog3-coursework
Commits
9f39595a
Commit
9f39595a
authored
Dec 28, 2020
by
ik1g19
Browse files
Options
Downloads
Patches
Plain Diff
progress on challenge 2
parent
5688b2c1
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
cw/src/Challenges.hs
+16
-6
16 additions, 6 deletions
cw/src/Challenges.hs
with
16 additions
and
6 deletions
cw/src/Challenges.hs
+
16
−
6
View file @
9f39595a
...
...
@@ -161,16 +161,26 @@ insertString :: RandGrid -> String -> StdGen -> (RandGrid,StdGen)
insertString
rg
s
gen
|
elemAt
rg
(
x
,
y
)
/=
Rand
&&
elemAt
rg
(
x
,
y
)
/=
Letter
(
head
s
)
=
insertString
rg
s
newGen
--guard:if position is invalid, generate new position
|
length
vDirs
==
0
=
insertString
rg
s
newGen
--guard:if no valid orientations exist, generate new position
|
otherwise
|
otherwise
=
where
((
x
,
y
),
newGen
)
=
generatePos
gen
(
length
rg
)
vDirs
=
validDirs
rg
s
(
x
,
y
)
addToGrid
::
String
->
Orientation
->
RandGrid
addToGrid
s
dir
=
iterate
--iterate to get positions
--zip together with string
--map to add to grid
addToGrid
::
Orientation
->
String
->
(
Int
,
Int
)
->
RandGrid
addToGrid
dir
(
c
:
cs
)
(
x'
,
y'
)
=
addToGrid
dir
cs
(
nextPos
dir
(
x'
,
y'
))
charAdded
where
charAdded
=
insertAt2D
(
Letter
c
)
(
x'
,
y'
)
rg
--addToGrid dir = map (\(c,(m,n)) -> insertAt2D (Letter c) (m,n) rg) (zip s (take (length s) $ iterate (nextPos dir) (x,y)))
--inserts element at location in 2d array
insertAt2D
::
a
->
(
Int
,
Int
)
->
[[
a
]]
->
[[
a
]]
insertAt2D
newElement
(
x
,
y
)
grid
=
insertAt
a
x
(
grid
!!
y
)
--using code from https://stackoverflow.com/questions/43291442/haskell-insert-an-element-on-nth-position
insertAt
::
a
->
Int
->
[
a
]
->
[
a
]
insertAt
newElement
0
as
=
newElement
:
as
insertAt
newElement
i
(
a
:
as
)
=
a
:
insertAt
newElement
(
i
-
1
)
as
generatePos
::
StdGen
->
Int
->
((
Int
,
Int
),
StdGen
)
...
...
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