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
930a6c79
Commit
930a6c79
authored
3 years ago
by
tanguy.cavagna
Browse files
Options
Downloads
Patches
Plain Diff
Reformated game loop
parent
c83dd25e
No related branches found
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
ui/ui.c
+1
-1
1 addition, 1 deletion
ui/ui.c
wordle/wordle.c
+64
-46
64 additions, 46 deletions
wordle/wordle.c
wordle/wordle.h
+2
-0
2 additions, 0 deletions
wordle/wordle.h
with
67 additions
and
47 deletions
ui/ui.c
+
1
−
1
View file @
930a6c79
...
@@ -186,7 +186,7 @@ void show_gameboard(int tries_count, int word_length, char **tries, int **valida
...
@@ -186,7 +186,7 @@ void show_gameboard(int tries_count, int word_length, char **tries, int **valida
// Draw remaning try letters placeholder
// Draw remaning try letters placeholder
for
(
size_t
j
=
0
;
j
<
word_length
-
strlen
(
tries
[
i
]);
j
++
)
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
),
"
_
"
);
mvaddstr
(
GAMEBOARD_Y_OFFSET
+
i
,
TERM_MID_COLS
+
(
word_length
/
2
)
-
(
j
*
2
),
"
_
"
);
}
}
refresh
();
refresh
();
}
}
\ No newline at end of file
This diff is collapsed.
Click to expand it.
wordle/wordle.c
+
64
−
46
View file @
930a6c79
...
@@ -11,35 +11,78 @@ int **validations;
...
@@ -11,35 +11,78 @@ int **validations;
int
current_try_id
=
0
;
int
current_try_id
=
0
;
int
current_try_letter_id
=
0
;
int
current_try_letter_id
=
0
;
/**
* @brief Initialize the game
*/
void
initialize_game
()
{
chosen_word
=
"SALUT"
;
// Must be randomly picked in a dictionary
tries
=
calloc
(
TRIES_COUNT
,
sizeof
(
char
*
));
validations
=
malloc
(
sizeof
(
int
*
)
*
TRIES_COUNT
);
for
(
int
i
=
0
;
i
<
TRIES_COUNT
;
i
++
)
{
validations
[
i
]
=
calloc
(
WORD_LENGHT
+
1
,
sizeof
(
int
));
tries
[
i
]
=
calloc
(
WORD_LENGHT
+
1
,
sizeof
(
char
));
// +1 because of \0
}
}
/**
/**
* @brief Terminate a game
* @brief Terminate a game
*/
*/
void
terminate_game
()
{
_game_finished
=
true
;
}
void
terminate_game
()
{
_game_finished
=
true
;
for
(
int
i
=
0
;
i
<
TRIES_COUNT
;
i
++
)
{
free
(
tries
[
i
]);
free
(
validations
[
i
]);
}
free
(
tries
);
free
(
validations
);
}
/**
/**
* @brief
Add a letter to the current try
* @brief
Condition(s) to lose a game
*
*
* @param letter
* @return true
* @return false
*/
*/
void
add_letter_to_try
(
char
letter
)
{
bool
lose_condition
()
{
return
(
current_try_id
>
TRIES_COUNT
-
1
);
}
tries
[
current_try_id
][
current_try_letter_id
]
=
letter
;
current_try_letter_id
+=
1
;
/**
* @brief Render the game for the selected gamemode
*/
void
render_game
()
{
switch
(
gamemode
)
{
case
GAMEMODE_SOLO
:
printw_center
(
"Solo"
,
5
);
break
;
case
GAMEMODE_1_V_1
:
printw_center
(
"Player 1"
,
5
);
break
;
case
GAMEMODE_COMPUTER
:
printw_center
(
"Computer assisted"
,
5
);
break
;
}
show_gameboard
(
TRIES_COUNT
,
WORD_LENGHT
,
tries
,
validations
);
}
}
/**
/**
* @brief Check if the given letter is a lowercase letter only
* @brief Check if the given letter is a lowercase letter only
(ASCII code)
*
*
* @param key
* @param key
* @return true
* @return true
* @return false
* @return false
*/
*/
bool
validate_letter
(
int
key
)
{
return
(
key
>=
97
&&
key
<=
122
);
}
bool
validate_letter
(
int
key
)
{
return
(
islower
(
key
)
||
isupper
(
key
)
);
}
/**
/**
* @brief Validate the last guess
* @brief Validate the last guess
*
*/
*/
int
*
validate_
last_
guess
(
char
answer
[
WORD_LENGHT
],
char
try
[
WORD_LENGHT
])
{
int
*
validate_guess
(
char
answer
[
WORD_LENGHT
],
char
try
[
WORD_LENGHT
])
{
int
a
[
WORD_LENGHT
]
=
{
0
,
2
,
0
,
1
,
0
};
int
a
[
WORD_LENGHT
]
=
{
0
,
2
,
0
,
1
,
0
};
int
*
validation
=
calloc
(
WORD_LENGHT
,
sizeof
(
int
));
int
*
validation
=
calloc
(
WORD_LENGHT
,
sizeof
(
int
));
...
@@ -68,7 +111,7 @@ void handle_controls(int key) {
...
@@ -68,7 +111,7 @@ void handle_controls(int key) {
if
(
strlen
(
tries
[
current_try_id
])
<
WORD_LENGHT
)
if
(
strlen
(
tries
[
current_try_id
])
<
WORD_LENGHT
)
break
;
break
;
int
*
tmp
=
validate_
last_
guess
(
chosen_word
,
tries
[
current_try_id
]);
int
*
tmp
=
validate_guess
(
chosen_word
,
tries
[
current_try_id
]);
for
(
int
i
=
0
;
i
<
WORD_LENGHT
;
i
++
)
for
(
int
i
=
0
;
i
<
WORD_LENGHT
;
i
++
)
validations
[
current_try_id
][
i
]
=
tmp
[
i
];
validations
[
current_try_id
][
i
]
=
tmp
[
i
];
free
(
tmp
);
free
(
tmp
);
...
@@ -85,58 +128,33 @@ void handle_controls(int key) {
...
@@ -85,58 +128,33 @@ void handle_controls(int key) {
void
set_gamemode
(
int
menu_gamemode
)
{
gamemode
=
menu_gamemode
;
}
void
set_gamemode
(
int
menu_gamemode
)
{
gamemode
=
menu_gamemode
;
}
void
launch_game
()
{
void
launch_game
()
{
chosen_word
=
"SALUT"
;
initialize_game
();
tries
=
calloc
(
TRIES_COUNT
,
sizeof
(
char
*
));
validations
=
malloc
(
sizeof
(
int
*
)
*
TRIES_COUNT
);
for
(
int
i
=
0
;
i
<
TRIES_COUNT
;
i
++
)
{
validations
[
i
]
=
calloc
(
WORD_LENGHT
+
1
,
sizeof
(
int
));
tries
[
i
]
=
calloc
(
WORD_LENGHT
+
1
,
sizeof
(
char
));
// +1 because of \0
}
while
(
!
game_finished
())
{
while
(
!
game_finished
())
{
// Guard clause
// Guard clause
if
(
current_try_id
>
TRIES_COUNT
-
1
)
{
if
(
lose_condition
()
)
{
terminate_game
();
terminate_game
();
continue
;
continue
;
}
}
// Render
// Render
switch
(
gamemode
)
{
render_game
();
case
GAMEMODE_SOLO
:
printw_center
(
"Solo"
,
5
);
break
;
case
GAMEMODE_1_V_1
:
printw_center
(
"Player 1"
,
5
);
break
;
case
GAMEMODE_COMPUTER
:
printw_center
(
"Computer assisted"
,
5
);
break
;
}
show_gameboard
(
TRIES_COUNT
,
WORD_LENGHT
,
tries
,
validations
);
// Next letter
// Key handling
// || Need to check letter by letter every time to be able to continue using
// || shortcut keys like Ctrl+h during a guess.
int
key
=
getch
();
int
key
=
getch
();
if
(
validate_letter
(
key
))
{
if
(
validate_letter
(
key
))
{
if
(
current_try_letter_id
<
WORD_LENGHT
)
if
(
current_try_letter_id
<
WORD_LENGHT
)
{
add_letter_to_try
((
char
)
key
-
32
);
// Offset letter to only put upercase
tries
[
current_try_id
][
current_try_letter_id
]
=
toupper
(
key
);
current_try_letter_id
+=
1
;
}
}
}
handle_controls
(
key
);
handle_controls
(
key
);
refresh
();
refresh
();
}
}
for
(
int
i
=
0
;
i
<
TRIES_COUNT
;
i
++
)
{
free
(
tries
[
i
]);
free
(
validations
[
i
]);
}
free
(
tries
);
free
(
validations
);
}
}
bool
game_finished
()
{
return
_game_finished
;
}
bool
game_finished
()
{
return
_game_finished
;
}
\ No newline at end of file
This diff is collapsed.
Click to expand it.
wordle/wordle.h
+
2
−
0
View file @
930a6c79
...
@@ -2,7 +2,9 @@
...
@@ -2,7 +2,9 @@
#define WORDLE_H_
#define WORDLE_H_
#include
"../ui/ui.h"
#include
"../ui/ui.h"
#include
<ctype.h>
#include
<stdbool.h>
#include
<stdbool.h>
#include
<string.h>
// Gamemodes
// Gamemodes
#define GAMEMODE_SOLO 0
#define GAMEMODE_SOLO 0
...
...
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