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
5688b2c1
Commit
5688b2c1
authored
4 years ago
by
ik1g19
Browse files
Options
Downloads
Patches
Plain Diff
progress on challenge 2, reordered nextPos params
parent
5a07d324
Branches
Branches containing commit
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
+22
-17
22 additions, 17 deletions
cw/src/Challenges.hs
with
22 additions
and
17 deletions
cw/src/Challenges.hs
+
22
−
17
View file @
5688b2c1
...
...
@@ -7,7 +7,7 @@
-- DO NOT MODIFY THE FOLLOWING LINES OF CODE
module
Challenges
(
WordSearchGrid
,
Placement
,
Posn
,
Orientation
(
..
),
solveWordSearch
,
createWordSearch
,
LamMacroExpr
(
..
),
LamExpr
(
..
),
prettyPrint
,
parseLamMacro
,
cpsTransform
,
innerRedn1
,
outerRedn1
,
compareInnerOuter
,
generatePos
)
where
cpsTransform
,
innerRedn1
,
outerRedn1
,
compareInnerOuter
)
where
-- Import standard library and parsing definitions from Hutton 2016, Chapter 13
-- We import System.Random - make sure that your installation has it installed - use stack ghci and stack ghc
...
...
@@ -74,7 +74,7 @@ findPlacement css (x,y) s | checkWordDir css (x,y) Forward s = Just ((
checkWordDir
::
WordSearchGrid
->
(
Int
,
Int
)
->
Orientation
->
String
->
Bool
checkWordDir
css
(
x
,
y
)
dir
(
l
:
[]
)
|
nextElem
css
(
x
,
y
)
dir
==
Just
l
=
True
|
otherwise
=
False
checkWordDir
css
(
x
,
y
)
dir
(
l
:
ls
)
|
nextElem
css
(
x
,
y
)
dir
==
Just
l
=
checkWordDir
css
(
nextPos
(
x
,
y
)
dir
)
dir
ls
checkWordDir
css
(
x
,
y
)
dir
(
l
:
ls
)
|
nextElem
css
(
x
,
y
)
dir
==
Just
l
=
checkWordDir
css
(
nextPos
dir
(
x
,
y
))
dir
ls
|
otherwise
=
False
...
...
@@ -82,15 +82,15 @@ checkWordDir css (x,y) dir (l:ls) | nextElem css (x,y) dir == Just l = checkWo
--------------------pattern matching for traversing the grid--------------------
--returns position of movement in a given direction
nextPos
::
(
Int
,
Int
)
->
Orientation
->
(
Int
,
Int
)
nextPos
(
x
,
y
)
Forward
=
(
x
+
1
,
y
)
nextPos
(
x
,
y
)
Back
=
(
x
-
1
,
y
)
nextPos
(
x
,
y
)
Up
=
(
x
,
y
-
1
)
nextPos
(
x
,
y
)
Down
=
(
x
,
y
+
1
)
nextPos
(
x
,
y
)
UpForward
=
(
x
+
1
,
y
-
1
)
nextPos
(
x
,
y
)
UpBack
=
(
x
-
1
,
y
-
1
)
nextPos
(
x
,
y
)
DownForward
=
(
x
+
1
,
y
+
1
)
nextPos
(
x
,
y
)
DownBack
=
(
x
-
1
,
y
+
1
)
nextPos
::
Orientation
->
(
Int
,
Int
)
->
(
Int
,
Int
)
nextPos
Forward
(
x
,
y
)
=
(
x
+
1
,
y
)
nextPos
Back
(
x
,
y
)
=
(
x
-
1
,
y
)
nextPos
Up
(
x
,
y
)
=
(
x
,
y
-
1
)
nextPos
Down
(
x
,
y
)
=
(
x
,
y
+
1
)
nextPos
UpForward
(
x
,
y
)
=
(
x
+
1
,
y
-
1
)
nextPos
UpBack
(
x
,
y
)
=
(
x
-
1
,
y
-
1
)
nextPos
DownForward
(
x
,
y
)
=
(
x
+
1
,
y
+
1
)
nextPos
DownBack
(
x
,
y
)
=
(
x
-
1
,
y
+
1
)
elemAt
::
[[
a
]]
->
(
Int
,
Int
)
->
a
...
...
@@ -102,7 +102,7 @@ nextElem :: [[a]] -> (Int,Int) -> Orientation -> Maybe a
nextElem
css
(
x
,
y
)
dir
|
x'
<
0
||
y'
<
0
||
x'
>
length
css
-
1
||
y'
>
length
css
-
1
=
Nothing
|
otherwise
=
Just
(
elemAt
css
(
x'
,
y'
))
where
(
x'
,
y'
)
=
nextPos
(
x
,
y
)
dir
(
x'
,
y'
)
=
nextPos
dir
(
x
,
y
)
-- Two examples for you to try out, the first of which is in the instructions
...
...
@@ -126,8 +126,7 @@ createWordSearch ss den = do gen <- getStdGen
where
charInInput
=
sum
(
map
length
ss
)
longestWordLen
=
max
(
map
length
ss
)
dim
=
head
[
x
|
x
<-
[
0
..
],
x
^
2
>
(
charInInput
/
den
),
x
>=
longestWordLen
]
--calculates needed dimension of grid according
--to the density
dim
=
head
[
x
|
x
<-
[
0
..
],
x
^
2
>
(
charInInput
/
den
),
x
>=
longestWordLen
]
--calculates needed dimension of grid according to the density
createGrid
::
[
String
]
->
Int
->
StdGen
->
WordSearchGrid
...
...
@@ -159,13 +158,19 @@ checkDir rg s (x,y) dir =
--adds an individual string to a given grid
--returns new grid and new generator
insertString
::
RandGrid
->
String
->
StdGen
->
(
RandGrid
,
StdGen
)
insertString
rg
s
gen
|
|
length
vDirs
==
0
=
insertString
rg
s
newGen
|
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
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
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