Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
progseq-puissance4
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
dario.genga
progseq-puissance4
Commits
543897ff
Commit
543897ff
authored
3 years ago
by
dario.genga
Browse files
Options
Downloads
Patches
Plain Diff
Add horizontal verification
Various changes have been done for better game result verification.
parent
a9b9a972
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
main.c
+6
-6
6 additions, 6 deletions
main.c
puissance.c
+63
-23
63 additions, 23 deletions
puissance.c
puissance.h
+2
-1
2 additions, 1 deletion
puissance.h
with
71 additions
and
30 deletions
main.c
+
6
−
6
View file @
543897ff
...
...
@@ -13,16 +13,16 @@ int main() {
puissance
game
;
game_init
(
&
game
,
RAND_AI
,
DEFAULT_ROW
,
DEFAULT_COL
);
manual_play
(
&
game
,
1
);
manual_play
(
&
game
,
0
);
manual_play
(
&
game
,
1
);
manual_play
(
&
game
,
4
);
print_game
(
game
);
manual_play
(
&
game
,
0
);
manual_play
(
&
game
,
2
);
manual_play
(
&
game
,
0
);
manual_play
(
&
game
,
3
);
print_game
(
game
);
manual_play
(
&
game
,
0
);
manual_play
(
&
game
,
2
);
print_game
(
game
);
manual_play
(
&
game
,
6
);
print_game
(
game
);
manual_play
(
&
game
,
1
);
print_game
(
game
);
game_destroy
(
&
game
);
...
...
This diff is collapsed.
Click to expand it.
puissance.c
+
63
−
23
View file @
543897ff
...
...
@@ -118,52 +118,92 @@ void print_game(puissance p) {
}
}
GameResult
vertical_game_check
(
puissance
p
,
int
last_col_index_played
)
{
GameResult
get_winning_player
(
puissance
*
p
)
{
// Return the player who have won.
if
(
p
->
current_player
==
PLAYER_ONE
)
{
p
->
state
=
PLAYER_ONE_WIN
;
}
else
{
p
->
state
=
PLAYER_TWO_WIN
;
}
return
p
->
state
;
}
GameResult
vertical_game_check
(
puissance
*
p
,
int
last_col_index_played
)
{
if
(
p
->
state
!=
ONGOING
)
{
return
p
->
state
;
}
bool
four_aligned
=
false
;
// Get the row index
int
last_row_index_played
=
get_available_row_index
(
&
p
,
last_col_index_played
);
if
(
last_row_index_played
!=
EMPTY_CELL_VALUE
)
{
last_row_index_played
+=
1
;
// Adjust the index
}
int
last_row_index_played
=
get_available_row_index
(
p
,
last_col_index_played
)
+
1
;
// Verify if we have enough vertical space.
if
(
last_row_index_played
+
NB_
SAME_VALUE_ALIGNED
_FOR_WIN
-
1
<
p
.
row
)
{
int
last_played_value
=
p
.
data
[
last_row_index_played
][
last_col_index_played
];
if
(
last_row_index_played
+
NB_
VERIFICATION
_FOR_WIN
<
p
->
row
)
{
int
last_played_value
=
p
->
data
[
last_row_index_played
][
last_col_index_played
];
four_aligned
=
true
;
// Verify if the aligned value are the same
for
(
int
i
=
1
;
i
<=
NB_
SAME_VALUE_ALIGNED
_FOR_WIN
-
1
;
i
++
)
{
if
(
last_played_value
!=
p
.
data
[
last_row_index_played
+
i
][
last_col_index_played
])
{
for
(
int
i
=
1
;
i
<=
NB_
VERIFICATION
_FOR_WIN
;
i
++
)
{
if
(
last_played_value
!=
p
->
data
[
last_row_index_played
+
i
][
last_col_index_played
])
{
four_aligned
=
false
;
break
;
}
}
if
(
four_aligned
)
{
// Return the player who have won.
if
(
p
.
current_player
==
PLAYER_ONE
)
{
return
PLAYER_ONE_WIN
;
}
else
{
return
PLAYER_TWO_WIN
;
return
get_winning_player
(
p
);
}
}
return
ONGOING
;
}
GameResult
horizontal_game_check
(
puissance
*
p
,
int
last_col_index_played
)
{
if
(
p
->
state
!=
ONGOING
)
{
return
p
->
state
;
}
bool
four_aligned
=
false
;
// Get the row index
int
last_row_index_played
=
get_available_row_index
(
p
,
last_col_index_played
)
+
1
;
int
last_played_value
=
p
->
data
[
last_row_index_played
][
last_col_index_played
];
// Verify the left
if
(
last_col_index_played
-
NB_VERIFICATION_FOR_WIN
>=
0
)
{
four_aligned
=
true
;
for
(
int
i
=
1
;
i
<=
NB_VERIFICATION_FOR_WIN
;
i
++
)
{
if
(
last_played_value
!=
p
->
data
[
last_row_index_played
][
last_col_index_played
-
i
])
{
four_aligned
=
false
;
break
;
}
}
}
// Verify the right
if
(
last_col_index_played
+
NB_VERIFICATION_FOR_WIN
<
p
->
col
&&
four_aligned
==
false
)
{
four_aligned
=
true
;
for
(
int
i
=
1
;
i
<=
NB_VERIFICATION_FOR_WIN
;
i
++
)
{
if
(
last_played_value
!=
p
->
data
[
last_row_index_played
][
last_col_index_played
+
i
])
{
four_aligned
=
false
;
break
;
}
}
}
if
(
four_aligned
)
{
return
get_winning_player
(
p
);
}
return
ONGOING
;
}
GameResult
verify_game
(
puissance
*
p
,
int
last_col_index_played
)
{
GameResult
result
=
ONGOING
;
// Vertical check
result
=
vertical_game_check
(
*
p
,
last_col_index_played
);
if
(
result
!=
ONGOING
)
{
p
->
state
=
result
;
return
result
;
}
vertical_game_check
(
p
,
last_col_index_played
);
// Horizontal check
horizontal_game_check
(
p
,
last_col_index_played
);
p
->
state
=
result
;
return
result
;
return
p
->
state
;
}
GameResult
display_game_result
(
GameResult
gr
)
{
...
...
This diff is collapsed.
Click to expand it.
puissance.h
+
2
−
1
View file @
543897ff
...
...
@@ -15,8 +15,9 @@
#define DEFAULT_COL 7
#define PLAYER_ONE_STRING "X"
#define PLAYER_TWO_STRING "O"
#define EMPTY_CELL_VALUE -
1
#define EMPTY_CELL_VALUE -
2
#define NB_SAME_VALUE_ALIGNED_FOR_WIN 4
#define NB_VERIFICATION_FOR_WIN (NB_SAME_VALUE_ALIGNED_FOR_WIN - 1)
typedef
enum
{
RAND_AI
,
...
...
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