Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
poo2019numeric
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
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
francois.hofer
poo2019numeric
Commits
4a6a79cc
Commit
4a6a79cc
authored
5 years ago
by
fefe
Browse files
Options
Downloads
Patches
Plain Diff
All functions finished ( but not tested and missises just rungame())
parent
312f7d00
Branches
Branches containing commit
Tags
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/main/java/ch/hepia/Hangman/Hangman.java
+81
-7
81 additions, 7 deletions
src/main/java/ch/hepia/Hangman/Hangman.java
with
81 additions
and
7 deletions
src/main/java/ch/hepia/Hangman/Hangman.java
+
81
−
7
View file @
4a6a79cc
public
class
Hangman
{
/*private String currentWord;
private String guessWord;*/
private
static
String
check
(
String
currentWord
,
String
guessWord
,
char
letter
){
StringBuilder
newStr
=
new
StringBuilder
(
currentWord
);
for
(
int
i
=
0
;
i
<
guessWord
.
length
();
i
++){
if
(
guessWord
.
charAt
(
i
)
==
letter
){
private
String
secretWord
;
private
String
currWord
;
private
int
attemp
;
private
int
failedAttempt
;
private
boolean
lastTry
;
private
Hangman
(
String
word
){
this
.
secretWord
=
word
;
this
.
currWord
=
"_"
.
repeat
(
word
.
length
());
this
.
attemp
=
0
;
this
.
failedAttempt
=
0
;
this
.
lastTry
=
true
;
}
public
static
Hangman
withSecretWord
(
String
word
){
return
new
Hangman
(
word
);
}
private
String
currentWord
(){
return
this
.
currWord
;
}
public
void
printCurrentWord
(){
System
.
out
.
println
(
"Current Word:"
+
this
.
currentWord
());
}
private
int
attemps
(){
return
this
.
attemp
;
}
private
String
secretWord
(){
return
this
.
secretWord
;
}
private
boolean
lastTry
(){
return
this
.
lastTry
;
}
public
int
failedAttempts
(){
return
this
.
failedAttempt
;
}
public
void
printGallow
(){
printHang
(
this
.
failedAttempts
());
}
public
void
printGallowIfError
(){
if
(!
this
.
lastTry
()){
printHang
(
this
.
failedAttempts
());
}
}
private
String
check
(
char
letter
){
StringBuilder
newStr
=
new
StringBuilder
(
this
.
currentWord
());
for
(
int
i
=
0
;
i
<
this
.
secretWord
().
length
();
i
++){
if
(
this
.
secretWord
().
charAt
(
i
)
==
letter
){
newStr
.
setCharAt
(
i
,
letter
);
}
}
return
newStr
.
toString
();
}
public
Hangman
tryWith
(
char
letter
){
Hangman
newH
=
this
;
newH
.
attemp
++;
String
checked
=
check
(
letter
);
if
(
newH
.
currentWord
().
equals
(
checked
)){
newH
.
failedAttempt
++;
newH
.
lastTry
=
false
;
return
newH
;
}
newH
.
lastTry
=
true
;
newH
.
currWord
=
checked
;
return
newH
;
}
public
boolean
isFinished
(){
if
(
this
.
failedAttempts
()
==
9
||
!
this
.
currentWord
().
contains
(
"_"
)){
return
true
;
}
return
false
;
}
public
boolean
hasWon
(){
if
(
this
.
failedAttempts
()==
9
){
return
false
;
}
return
true
;
}
private
static
void
printHang
(
int
i
){
String
i0
=
""
;
String
i1
=
"----------\n|/\n|\n|\n|\n|\n|\n|"
;
...
...
@@ -26,7 +97,10 @@ public class Hangman {
System
.
out
.
println
(
drawHangMan
[
i
]);
}
public
static
void
main
(
String
[]
args
){
printHang
(
2
);
Hangman
h
=
Hangman
.
withSecretWord
(
"mystère"
);
String
currentWord
=
h
.
currentWord
();
}
...
...
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