Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
M
Mathe-Skribbl
Manage
Activity
Members
Labels
Plan
Issues
0
Issue boards
Milestones
Wiki
Requirements
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
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Ben Eltschig
Mathe-Skribbl
Commits
e14669a4
Commit
e14669a4
authored
4 years ago
by
Max Wehmeier
Browse files
Options
Downloads
Patches
Plain Diff
added _isCorrect and _isClose
parent
5e6d5f1b
2 merge requests
!5
Custom server
,
!2
Add isclose iscorrect
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
client/logic/SkribblServer.js
+116
-3
116 additions, 3 deletions
client/logic/SkribblServer.js
with
116 additions
and
3 deletions
client/logic/SkribblServer.js
+
116
−
3
View file @
e14669a4
...
@@ -266,12 +266,125 @@ export default class SkribblServer {
...
@@ -266,12 +266,125 @@ export default class SkribblServer {
}
}
/**
/**
* Checks whether a given guess is close to the given word.
* Checks whether a given guess is close to the given word. A guess counts as close, if one letter is wrong,
* two letters are swapped or the guess has one letter too much or too little.
* @param {string} guess
* @param {string} guess
* @param {string} word
* @param {string} word
* @todo implement
*/
*/
static
_isClose
(
guess
,
word
){
static
_isClose
(
guess
,
word
)
{
guess
=
guess
.
toLowerCase
();
word
=
word
.
toLowerCase
();
word
.
replace
(
/-/g
,
"
"
);
guess
.
replace
(
/-/g
,
"
"
);
//if equal
if
(
guess
==
word
)
{
return
true
;
}
//either one letter wrong or two letters swapped
if
(
guess
.
length
==
word
.
length
)
{
let
wordArray
=
[];
let
guessArray
=
[];
//makes the string into an array
for
(
var
i
=
0
;
i
<
word
.
length
;
i
++
)
{
wordArray
[
i
]
=
word
.
charAt
(
i
);
guessArray
[
i
]
=
guess
.
charAt
(
i
);
}
//Counts the mistakes and their position
let
errorCounter
=
0
;
let
errorPos
=
0
;
for
(
var
i
=
0
;
i
<
wordArray
.
length
;
i
++
)
{
if
(
wordArray
[
i
]
!=
guessArray
[
i
])
{
if
(
errorCounter
==
0
)
{
errorPos
=
i
;
}
if
(
errorCounter
==
1
)
{
//if a second mistake occurs, either the letters are swapped or it is not correct
//but there could be a third mistake, so in the case of a swap, true is not directly returned.
if
((
wordArray
[
i
]
!=
guessArray
[
errorPos
])
||
(
wordArray
[
errorPos
]
!=
guessArray
[
i
]))
{
return
false
;
}
}
if
(
errorCounter
>=
2
)
{
//with two or more mistakes, the word is not close
return
false
;
}
errorCounter
++
;
}
}
//if it hasnt returned false by now, the guess is close
return
true
;
}
//if one letter too much
if
(
guess
.
length
-
1
==
word
.
length
)
{
let
wordArray
=
[];
let
guessArray
=
[];
for
(
var
i
=
0
;
i
<
word
.
length
;
i
++
)
{
wordArray
[
i
]
=
word
.
charAt
(
i
);
guessArray
[
i
]
=
guess
.
charAt
(
i
);
}
guessArray
[
guessArray
.
length
]
=
guess
.
charAt
(
guessArray
.
length
);
let
errorCounter
=
0
;
//also the offset
for
(
var
i
=
0
;
i
<
wordArray
.
length
;
i
++
)
{
if
(
wordArray
[
i
]
!=
guessArray
[
i
+
errorCounter
])
{
errorCounter
++
;
if
(
errorCounter
>=
2
)
{
return
false
;
}
}
}
return
true
;
}
//if one letter too little
if
(
guess
.
length
+
1
==
word
.
length
)
{
let
wordArray
=
[];
let
guessArray
=
[];
for
(
var
i
=
0
;
i
<
guess
.
length
;
i
++
)
{
wordArray
[
i
]
=
word
.
charAt
(
i
);
guessArray
[
i
]
=
guess
.
charAt
(
i
);
}
wordArray
[
wordArray
.
length
]
=
word
.
charAt
(
wordArray
.
length
);
let
errorCounter
=
0
;
//also the offset
for
(
var
i
=
0
;
i
<
guessArray
.
length
;
i
++
)
{
if
(
wordArray
[
i
+
errorCounter
]
!=
guessArray
[
i
])
{
errorCounter
++
;
if
(
errorCounter
>=
2
)
{
return
false
;
}
}
}
return
true
;
}
return
false
;
return
false
;
}
}
/**
* Checks if the guess is correct. It is not case-sensitive.
* @param {string} guess
* @param {string} word
*/
static
_isCorrect
(
guess
,
word
)
{
guess
=
guess
.
toLowerCase
();
word
=
word
.
toLowerCase
();
word
.
replace
(
/-/g
,
"
"
);
guess
.
replace
(
/-/g
,
"
"
);
if
(
guess
==
word
)
{
return
true
;
}
else
{
return
true
;
}
}
}
}
\ 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