Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
wordle
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
Package registry
Model registry
Operate
Environments
Terraform modules
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
This is an archived project. Repository and other project resources are read-only.
Show more breadcrumbs
repos.tanguy.cavagna
sequencial programming
wordle
Commits
738d08fb
Commit
738d08fb
authored
3 years ago
by
tanguy.cavagna
Browse files
Options
Downloads
Patches
Plain Diff
Better control logic and added correct letters in next try by default
parent
6e1be2f9
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
ui/ui.c
+8
-5
8 additions, 5 deletions
ui/ui.c
wordle/wordle.c
+43
-20
43 additions, 20 deletions
wordle/wordle.c
with
51 additions
and
25 deletions
ui/ui.c
+
8
−
5
View file @
738d08fb
...
...
@@ -237,7 +237,14 @@ void toggle_help() {
void
show_gameboard
(
int
tries_count
,
int
word_length
,
char
**
tries
,
int
**
validations
,
int
score
)
{
for
(
int
i
=
0
;
i
<
tries_count
;
i
++
)
{
// Draw current try letters
for
(
size_t
j
=
0
;
j
<
strlen
(
tries
[
i
]);
j
++
)
{
for
(
int
j
=
0
;
j
<
word_length
;
j
++
)
{
// Placeholder
if
(
tries
[
i
][
j
]
==
'\0'
)
{
mvaddch
(
GAMEBOARD_Y_OFFSET
+
i
,
TERM_MID_COLS
-
word_length
+
(
j
*
2
),
'_'
);
continue
;
}
// Letters
if
(
validations
[
i
][
j
]
==
1
)
attron
(
COLOR_PAIR
(
ORANGE_PAIR
));
if
(
validations
[
i
][
j
]
==
2
)
...
...
@@ -247,10 +254,6 @@ void show_gameboard(int tries_count, int word_length, char **tries, int **valida
attroff
(
COLOR_PAIR
(
GREEN_PAIR
));
attroff
(
COLOR_PAIR
(
ORANGE_PAIR
));
}
// Draw remaning try letters placeholder
for
(
size_t
j
=
0
;
j
<
word_length
-
strlen
(
tries
[
i
]);
j
++
)
mvaddstr
(
GAMEBOARD_Y_OFFSET
+
i
,
TERM_MID_COLS
+
(
word_length
/
2
)
-
(
j
*
2
),
" _"
);
}
move
(
GAMEBOARD_Y_OFFSET
-
2
,
TERM_MID_COLS
-
word_length
-
3
);
...
...
This diff is collapsed.
Click to expand it.
wordle/wordle.c
+
43
−
20
View file @
738d08fb
...
...
@@ -20,6 +20,7 @@ void initialize_game() {
_game_finished
=
false
;
current_try_id
=
0
;
current_try_letter_id
=
0
;
score
=
0
;
// Tries and answer setup
chosen_word
=
get_random_word
();
...
...
@@ -125,9 +126,9 @@ void render_game() {
bool
validate_letter
(
int
key
)
{
return
(
islower
(
key
)
||
isupper
(
key
));
}
/**
* @brief
Validate
the la
st guess
* @brief
Control
the
p
la
cement of each letter
*/
int
*
validate_guess
(
char
answer
[
WORD_LENGHT
],
char
try
[
WORD_LENGHT
])
{
int
*
letter_placement_control
(
char
answer
[
WORD_LENGHT
],
char
try
[
WORD_LENGHT
])
{
int
*
validation
=
calloc
(
WORD_LENGHT
,
sizeof
(
int
));
char
cpy
[
WORD_LENGHT
+
1
];
strcpy
(
cpy
,
answer
);
...
...
@@ -153,6 +154,36 @@ int *validate_guess(char answer[WORD_LENGHT], char try[WORD_LENGHT]) {
return
validation
;
}
/**
* @brief Is a try valid
*
* @param try
* @return true
* @return false
*/
bool
validate_guess
(
char
try
[
WORD_LENGHT
])
{
// Length check
if
(
strlen
(
try
)
<
WORD_LENGHT
)
return
false
;
// Presence check
char
**
bank
=
get_bank
();
bool
present
=
false
;
for
(
int
i
=
0
;
i
<
get_bank_size
();
i
++
)
{
if
(
!
strcmp
(
bank
[
i
],
try
))
present
=
true
;
}
if
(
!
present
)
{
current_try_letter_id
=
0
;
for
(
int
i
=
0
;
i
<
WORD_LENGHT
;
i
++
)
tries
[
current_try_id
][
i
]
=
'\0'
;
return
false
;
}
return
true
;
}
/**
* @brief Handle controls
*
...
...
@@ -169,33 +200,25 @@ void handle_controls(int key) {
break
;
case
KEYBIND_GUESS
:
// Length check
if
(
strlen
(
tries
[
current_try_id
])
<
WORD_LENGHT
)
if
(
!
validate_guess
(
tries
[
current_try_id
]))
break
;
// Presence check
char
**
bank
=
get_bank
();
bool
present
=
false
;
for
(
int
i
=
0
;
i
<
get_bank_size
();
i
++
)
{
if
(
!
strcmp
(
bank
[
i
],
tries
[
current_try_id
]))
present
=
true
;
}
if
(
!
present
)
{
current_try_letter_id
=
0
;
for
(
int
i
=
0
;
i
<
WORD_LENGHT
;
i
++
)
tries
[
current_try_id
][
i
]
=
'\0'
;
break
;
}
// Validations
int
*
tmp
=
validate_guess
(
chosen_word
,
tries
[
current_try_id
]);
int
*
tmp
=
letter_placement_control
(
chosen_word
,
tries
[
current_try_id
]);
for
(
int
i
=
0
;
i
<
WORD_LENGHT
;
i
++
)
validations
[
current_try_id
][
i
]
=
tmp
[
i
];
free
(
tmp
);
current_try_id
+=
1
;
current_try_letter_id
=
0
;
// Place correct letters from previous in the current one
if
(
current_try_id
<
TRIES_COUNT
)
{
for
(
int
i
=
0
;
i
<
WORD_LENGHT
;
i
++
)
{
if
(
validations
[
current_try_id
-
1
][
i
]
==
LETTER_PLACED
)
tries
[
current_try_id
][
i
]
=
tries
[
current_try_id
-
1
][
i
];
}
}
break
;
}
}
...
...
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