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
664a7b4c
Commit
664a7b4c
authored
4 years ago
by
ik1g19
Browse files
Options
Downloads
Patches
Plain Diff
createWordSearch working, index exception being thrown
parent
a3612da4
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
+12
-11
12 additions, 11 deletions
cw/src/Challenges.hs
with
12 additions
and
11 deletions
cw/src/Challenges.hs
+
12
−
11
View file @
664a7b4c
...
@@ -117,20 +117,20 @@ exWords1'2 = [ "BANANA", "ORANGE", "MELON", "RASPBERRY","APPLE","PLUM","GRAPE" ]
...
@@ -117,20 +117,20 @@ exWords1'2 = [ "BANANA", "ORANGE", "MELON", "RASPBERRY","APPLE","PLUM","GRAPE" ]
-- Challenge 2 --
-- Challenge 2 --
--internal grid values are either a character or a placeholder for a random letter
--internal grid values are either a character or a placeholder for a random letter
data
GridVal
=
Letter
Char
|
Rand
data
GridVal
=
Letter
Char
|
Rand
deriving
Eq
type
RandGrid
=
[[
GridVal
]]
type
RandGrid
=
[[
GridVal
]]
createWordSearch
::
[
String
]
->
Double
->
IO
WordSearchGrid
createWordSearch
::
[
String
]
->
Double
->
IO
WordSearchGrid
createWordSearch
ss
den
=
do
gen
<-
get
StdGen
--initial generator
createWordSearch
ss
den
=
do
gen
<-
new
StdGen
--initial generator
return
(
createGrid
ss
dim
gen
)
return
(
createGrid
dim
gen
ss
)
where
where
charInInput
=
sum
(
map
length
ss
)
charInInput
=
fromIntegral
$
sum
$
map
length
ss
::
Double
longestWordLen
=
max
(
map
length
ss
)
longestWordLen
=
fromIntegral
$
foldl1
max
$
map
length
ss
::
Double
dim
=
head
[
x
|
x
<-
[
0
..
],
x
^
2
>
(
charInInput
/
den
),
x
>=
longestWordLen
]
--calculates needed dimension of grid according to the density
dim
=
floor
$
head
[
x
|
x
<-
[
0
..
],
x
^
2
>
(
charInInput
/
den
),
x
>=
longestWordLen
]
--calculates needed dimension of grid according to the density
createGrid
::
Int
->
StdGen
->
[
String
]
->
WordSearchGrid
createGrid
::
Int
->
StdGen
->
[
String
]
->
WordSearchGrid
createGrid
dim
gen
ss
=
randToWord
finalGrid
(
charsFromStrs
ss
)
gen'
createGrid
dim
gen
ss
=
randToWord
(
charsFromStrs
ss
)
gen'
finalGrid
where
where
tempGrid
=
replicate
dim
(
replicate
dim
Rand
)
--fills grid with random values
tempGrid
=
replicate
dim
(
replicate
dim
Rand
)
--fills grid with random values
(
finalGrid
,
gen'
)
=
addStrsToGrid
tempGrid
gen
ss
--final grid after all strings added
(
finalGrid
,
gen'
)
=
addStrsToGrid
tempGrid
gen
ss
--final grid after all strings added
...
@@ -156,7 +156,7 @@ rmdups = map head . group . sort
...
@@ -156,7 +156,7 @@ rmdups = map head . group . sort
randToWord
::
[
Char
]
->
StdGen
->
RandGrid
->
WordSearchGrid
randToWord
::
[
Char
]
->
StdGen
->
RandGrid
->
WordSearchGrid
randToWord
cs
gen
[]
=
[]
randToWord
cs
gen
[]
=
[]
randToWord
cs
gen
(
row
:
rs
)
=
let
(
newRow
,
newGen
)
=
rowConvert
cs
gen
row
randToWord
cs
gen
(
row
:
rs
)
=
let
(
newRow
,
newGen
)
=
rowConvert
cs
gen
row
in
newRow
:
randToWord
cs
newGen
in
newRow
:
randToWord
cs
newGen
rs
rowConvert
::
[
Char
]
->
StdGen
->
[
GridVal
]
->
([
Char
],
StdGen
)
rowConvert
::
[
Char
]
->
StdGen
->
[
GridVal
]
->
([
Char
],
StdGen
)
rowConvert
cs
gen
[]
=
(
[]
,
gen
)
rowConvert
cs
gen
[]
=
(
[]
,
gen
)
...
@@ -171,7 +171,7 @@ rowConvert cs gen (Rand:xs) = let (rows,gen') = rowConvert cs newGen xs
...
@@ -171,7 +171,7 @@ rowConvert cs gen (Rand:xs) = let (rows,gen') = rowConvert cs newGen xs
--adds list of strings to given grid one by one
--adds list of strings to given grid one by one
addStrsToGrid
::
RandGrid
->
StdGen
->
[
String
]
->
(
RandGrid
,
StdGen
)
addStrsToGrid
::
RandGrid
->
StdGen
->
[
String
]
->
(
RandGrid
,
StdGen
)
addStrsToGrid
rg
gen
(
s
:
[]
)
=
(
newGrid
,
newG
en
)
addStrsToGrid
rg
gen
(
s
:
[]
)
=
insertString
rg
s
g
en
addStrsToGrid
rg
gen
(
s
:
ss
)
=
addStrsToGrid
newGrid
newGen
ss
addStrsToGrid
rg
gen
(
s
:
ss
)
=
addStrsToGrid
newGrid
newGen
ss
where
where
(
newGrid
,
newGen
)
=
insertString
rg
s
gen
(
newGrid
,
newGen
)
=
insertString
rg
s
gen
...
@@ -186,7 +186,7 @@ validDirs rg s (x,y) = map fst $ filter ( \(_,b) -> b == True ) (zip dirs (map (
...
@@ -186,7 +186,7 @@ validDirs rg s (x,y) = map fst $ filter ( \(_,b) -> b == True ) (zip dirs (map (
--checks whether an orientation for a string at a given position in a grid is valid
--checks whether an orientation for a string at a given position in a grid is valid
checkDir
::
RandGrid
->
String
->
Posn
->
Orientation
->
Bool
checkDir
::
RandGrid
->
String
->
Posn
->
Orientation
->
Bool
checkDir
rg
s
(
x
,
y
)
dir
|
let
(
x'
,
y'
)
=
posns
!!
length
s
-
1
,
checkDir
rg
s
(
x
,
y
)
dir
|
let
(
x'
,
y'
)
=
posns
!!
(
length
s
-
1
)
,
x'
<
0
||
x'
>
length
rg
-
1
||
x'
<
0
||
x'
>
length
rg
-
1
||
y'
<
0
||
y'
>
length
rg
-
1
=
False
y'
<
0
||
y'
>
length
rg
-
1
=
False
|
foldl
(
&&
)
True
(
map
(
\
(
a
,
b
)
->
Letter
a
==
b
||
b
==
Rand
)
$
zip
s
lettersGrid
)
=
True
|
foldl
(
&&
)
True
(
map
(
\
(
a
,
b
)
->
Letter
a
==
b
||
b
==
Rand
)
$
zip
s
lettersGrid
)
=
True
...
@@ -206,7 +206,8 @@ insertString rg s gen | elemAt rg (x,y) /= Rand &&
...
@@ -206,7 +206,8 @@ insertString rg s gen | elemAt rg (x,y) /= Rand &&
where
where
(
(
x
,
y
),
newGen
)
=
generatePos
gen
(
length
rg
)
(
(
x
,
y
),
newGen
)
=
generatePos
gen
(
length
rg
)
vDirs
=
validDirs
rg
s
(
x
,
y
)
vDirs
=
validDirs
rg
s
(
x
,
y
)
(
randomDir
,
_
)
=
vDirs
!!
randomR
(
0
,
length
vDirs
-
1
)
gen
::
(
Int
,
StdGen
)
randomDir
=
let
(
index
,
_
)
=
randomR
(
0
,
length
vDirs
-
1
)
gen
in
vDirs
!!
index
addToGrid
::
Orientation
->
String
->
RandGrid
->
Posn
->
RandGrid
addToGrid
::
Orientation
->
String
->
RandGrid
->
Posn
->
RandGrid
addToGrid
dir
(
c
:
[]
)
rg
(
x'
,
y'
)
=
insertAt2D
(
Letter
c
)
(
x'
,
y'
)
rg
addToGrid
dir
(
c
:
[]
)
rg
(
x'
,
y'
)
=
insertAt2D
(
Letter
c
)
(
x'
,
y'
)
rg
...
...
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