Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
T
TP_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
Show more breadcrumbs
sabrina.lapaire
TP_Puissance4
Commits
8bd5fe3b
Commit
8bd5fe3b
authored
3 weeks ago
by
sabrina.lapaire
Browse files
Options
Downloads
Patches
Plain Diff
Modifier twoPlayers.c
parent
b5370a3c
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/twoPlayers.c
+15
-181
15 additions, 181 deletions
src/twoPlayers.c
with
15 additions
and
181 deletions
src/twoPlayers.c
+
15
−
181
View file @
8bd5fe3b
#include
"twoPlayers.h"
int
current_line
(
struct
board
board
,
int
no_col
){
int
no_line
=
board
.
line
-
1
;
while
(
board
.
data
[
no_line
][
no_col
]
!=
Vide
){
no_line
--
;
}
return
no_line
;
}
bool
is_full_board
(
struct
board
board
){
for
(
int
i
=
0
;
i
<
board
.
line
;
i
++
){
for
(
int
j
=
0
;
j
<
board
.
col
;
j
++
){
if
(
board
.
data
[
i
][
j
]
==
Vide
){
return
false
;
}
}
}
return
true
;
}
bool
row_of_four
(
struct
board
*
board
){
int
sum
=
0
;
int
i
=
0
;
int
old_i
=
-
1
;
while
(
i
<
board
->
col
){
old_i
=
i
;
i
++
;
if
(
board
->
data
[
board
->
last_pos_x
][
i
]
==
board
->
last_symbole
&&
board
->
data
[
board
->
last_pos_x
][
old_i
]
==
board
->
last_symbole
){
sum
++
;
}
}
if
(
sum
>=
4
){
return
true
;
}
return
false
;
}
bool
col_of_four
(
struct
board
*
board
){
int
sum
=
0
;
int
i
=
board
->
line
-
1
;
int
old_i
=
-
1
;
while
(
i
>=
0
){
old_i
=
i
;
if
(
board
->
data
[
i
][
board
->
last_pos_y
]
==
board
->
last_symbole
&&
board
->
data
[
old_i
][
board
->
last_pos_y
]
==
board
->
last_symbole
){
sum
++
;
}
i
--
;
}
if
(
sum
>=
4
){
return
true
;
}
return
false
;
}
bool
diag_of_four_left_right
(
struct
board
*
board
){
int
sum
=
1
;
// Nous commençons à 1 car le dernier symbole est déjà compté
int
current_x
=
board
->
last_pos_x
;
int
current_y
=
board
->
last_pos_y
;
int
oldx
=
-
1
;
int
oldy
=
-
1
;
// Vérification de la diagonale supérieure gauche à la diagonale inférieure droite
while
(
current_x
>
0
&&
current_y
>
0
)
{
oldx
=
current_x
;
oldy
=
current_y
;
current_x
--
;
current_y
--
;
if
(
board
->
data
[
current_x
][
current_y
]
==
board
->
last_symbole
&&
board
->
data
[
oldx
][
oldy
]
==
board
->
last_symbole
)
{
sum
++
;
// Increment only if we find consecutive symbols
}
}
// Réinitialisation des coordonnées
current_x
=
board
->
last_pos_x
;
current_y
=
board
->
last_pos_y
;
// Vérification de la diagonale inférieure droite à la diagonale supérieure gauche
while
(
current_x
<
board
->
line
-
1
&&
current_y
<
board
->
col
-
1
)
{
oldx
=
current_x
;
oldy
=
current_y
;
current_x
++
;
current_y
++
;
if
(
board
->
data
[
current_x
][
current_y
]
==
board
->
last_symbole
&&
board
->
data
[
oldx
][
oldy
]
==
board
->
last_symbole
)
{
sum
++
;
// Increment only if we find consecutive symbols
}
}
// Vérifie si sum est exactement égal à 4
if
(
sum
==
4
)
{
return
true
;
// We found 4 consecutive symbols
}
return
false
;
// No 4 consecutive symbols found
}
bool
diag_of_four_right_left
(
struct
board
*
board
){
int
sum
=
1
;
// On commence à 1 car le dernier symbole est déjà compté
int
current_x
=
board
->
last_pos_x
;
int
current_y
=
board
->
last_pos_y
;
int
oldx
=
-
1
;
int
oldy
=
-
1
;
// Vérification de la diagonale montante gauche à la diagonale descendante droite
while
(
current_x
>=
0
&&
current_y
<
board
->
col
-
1
)
{
oldx
=
current_x
;
oldy
=
current_y
;
current_x
--
;
current_y
++
;
if
(
board
->
data
[
current_x
][
current_y
]
==
board
->
last_symbole
&&
board
->
data
[
oldx
][
oldy
]
==
board
->
last_symbole
)
{
sum
++
;
// Incrémenter seulement si les symboles sont consécutifs
}
}
// Réinitialisation des coordonnées
current_x
=
board
->
last_pos_x
;
current_y
=
board
->
last_pos_y
;
// Vérification de la diagonale descendante gauche à la diagonale montante droite
while
(
current_x
<
board
->
line
-
1
&&
current_y
>=
0
)
{
oldx
=
current_x
;
oldy
=
current_y
;
current_x
++
;
current_y
--
;
if
(
board
->
data
[
current_x
][
current_y
]
==
board
->
last_symbole
&&
board
->
data
[
oldx
][
oldy
]
==
board
->
last_symbole
)
{
sum
++
;
// Incrémenter seulement si les symboles sont consécutifs
}
}
// Vérifie si sum est exactement égal à 4
if
(
sum
==
4
)
{
return
true
;
// Si 4 symboles consécutifs sont trouvés, retourner true
}
return
false
;
// Sinon, retourner false
}
bool
winner
(
struct
board
board
){
if
(
row_of_four
(
&
board
)
||
diag_of_four_left_right
(
&
board
)
||
diag_of_four_right_left
(
&
board
)
||
col_of_four
(
&
board
)){
return
true
;
}
return
false
;
}
void
play
(
struct
board
board
){
int
no_col
=
0
;
int
no_line
=
0
;
int
no_round
=
1
;
while
(
!
is_full_board
(
board
)){
printf
(
"Column number? (starts at 1):"
);
scanf
(
"%d"
,
&
no_col
);
no_line
=
current_line
(
board
,
no_col
-
1
);
if
(
no_round
%
2
!=
0
){
board
.
data
[
no_line
][
no_col
-
1
]
=
Croix
;
board
.
last_symbole
=
Croix
;
}
else
{
board
.
data
[
no_line
][
no_col
-
1
]
=
Cercle
;
board
.
last_symbole
=
Cercle
;
}
board
.
last_pos_x
=
no_line
;
board
.
last_pos_y
=
no_col
-
1
;
print_game
(
&
board
);
if
(
winner
(
board
)){
if
(
board
.
last_symbole
==
Croix
){
printf
(
"Player one won
\n
"
);
}
else
if
(
board
.
last_symbole
==
Cercle
){
printf
(
"Player two won
\n
"
);
}
break
;
}
no_round
++
;
}
if
(
is_full_board
(
board
)){
printf
(
"It is a draw
\n
"
);
}
}
#include
<stdio.h>
#include
<stdlib.h>
#include
<stdbool.h>
#include
"board.h"
#ifndef twoplayers_h
#define twoplayers_h
int
current_line
(
struct
board
board
,
int
no_col
);
bool
is_full_board
(
struct
board
board
);
bool
row_of_four
(
struct
board
*
board
);
bool
diag_of_four_left_right
(
struct
board
*
board
);
bool
diag_of_four_right_left
(
struct
board
*
board
);
bool
winner
(
struct
board
board
);
void
play
(
struct
board
board
);
#endif
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