Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
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
anthony.bouillan
puissance4
Commits
dc0a241f
Commit
dc0a241f
authored
2 months ago
by
anthony.bouillan
Browse files
Options
Downloads
Patches
Plain Diff
Use memory allocation when creating board
parent
83bf4eb8
No related branches found
No related tags found
No related merge requests found
Pipeline
#38995
failed
2 months ago
Stage: build
Stage: test
Stage: clean
Changes
2
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
Makefile
+1
-1
1 addition, 1 deletion
Makefile
puissance4.c
+46
-10
46 additions, 10 deletions
puissance4.c
with
47 additions
and
11 deletions
Makefile
+
1
−
1
View file @
dc0a241f
...
...
@@ -12,7 +12,7 @@ clean:
$(
RM
)
-f
puissance4
*
.o
*
.cand
run
:
puissance4
./
$<
./
$<
3 5 6
tests
:
puissance4
$(
MAKE
)
-C
testbed
...
...
This diff is collapsed.
Click to expand it.
puissance4.c
+
46
−
10
View file @
dc0a241f
...
...
@@ -2,11 +2,13 @@
#include
<stdlib.h>
#include
<stdbool.h>
void
init_board
(
int
rows
,
int
cols
,
int
board
[
rows
][
cols
]
);
void
print_board
(
int
rows
,
int
cols
,
int
board
[
rows
][
cols
]
);
struct
coordinate
add_token
(
int
col
,
int
player
,
int
rows
,
int
cols
,
int
board
[
rows
][
cols
]
);
bool
is_player_winning
(
int
col
,
int
row
,
int
rows
,
int
cols
,
int
board
[
rows
][
cols
]
);
void
init_board
(
int
rows
,
int
cols
,
int
**
board
);
void
print_board
(
int
rows
,
int
cols
,
int
**
board
);
struct
coordinate
add_token
(
int
col
,
int
player
,
int
rows
,
int
**
board
);
bool
is_player_winning
(
int
col
,
int
row
,
int
rows
,
int
cols
,
int
**
board
);
void
flush_input
();
int
**
create_board
(
int
rows
,
int
cols
);
int
destroy_board
(
int
rows
,
int
**
board
);
enum
STATE
{
...
...
@@ -38,7 +40,11 @@ int main(int argc, char const* argv[]) {
int
rows
=
atoi
(
argv
[
2
]);
int
cols
=
atoi
(
argv
[
3
]);
int
board
[
rows
][
cols
];
int
**
board
=
create_board
(
rows
,
cols
);
if
(
board
==
NULL
)
{
fprintf
(
stderr
,
"Error: Unable to allocate memory for the board.
\n
"
);
return
EXIT_FAILURE
;
}
init_board
(
rows
,
cols
,
board
);
printf
(
"Board size is %dx%d (rows x col)"
,
rows
,
cols
);
...
...
@@ -54,7 +60,7 @@ int main(int argc, char const* argv[]) {
if
(
col
<
1
||
col
>
cols
)
{
continue
;
}
coordinate
=
add_token
(
col
,
player
%
2
,
rows
,
cols
,
board
);
coordinate
=
add_token
(
col
,
player
%
2
,
rows
,
board
);
print_board
(
rows
,
cols
,
board
);
if
(
is_player_winning
(
coordinate
.
x
,
coordinate
.
y
,
rows
,
cols
,
board
))
{
printf
(
"Player %s won!
\n
"
,
player
%
2
==
0
?
"one"
:
"two"
);
...
...
@@ -67,10 +73,14 @@ int main(int argc, char const* argv[]) {
player
++
;
}
if
(
destroy_board
(
rows
,
board
)
==
EXIT_FAILURE
)
{
fprintf
(
stderr
,
"Error: Unable to free memory for the board.
\n
"
);
return
EXIT_FAILURE
;
}
return
EXIT_SUCCESS
;
}
struct
coordinate
add_token
(
int
col
,
int
player
,
int
rows
,
int
cols
,
int
board
[
rows
][
cols
]
)
{
struct
coordinate
add_token
(
int
col
,
int
player
,
int
rows
,
int
**
board
)
{
struct
coordinate
coordinate
=
{
0
,
0
};
for
(
int
i
=
rows
-
1
;
i
>=
0
;
i
--
)
{
if
(
board
[
i
][
col
-
1
]
==
EMPTY
)
{
...
...
@@ -83,8 +93,34 @@ struct coordinate add_token(int col, int player, int rows, int cols, int board[r
return
coordinate
;
}
int
**
create_board
(
int
rows
,
int
cols
)
{
int
**
board
=
malloc
(
rows
*
sizeof
(
int
*
));
if
(
board
==
NULL
)
{
return
NULL
;
}
for
(
int
i
=
0
;
i
<
rows
;
i
++
)
{
board
[
i
]
=
malloc
(
cols
*
sizeof
(
int
));
if
(
board
[
i
]
==
NULL
)
{
for
(
int
j
=
0
;
j
<
i
;
j
++
)
{
free
(
board
[
j
]);
}
free
(
board
);
return
NULL
;
}
}
return
board
;
}
int
destroy_board
(
int
rows
,
int
**
board
)
{
for
(
int
i
=
0
;
i
<
rows
;
i
++
)
{
free
(
board
[
i
]);
}
free
(
board
);
return
EXIT_SUCCESS
;
}
void
init_board
(
int
rows
,
int
cols
,
int
board
[
rows
][
cols
]
)
{
void
init_board
(
int
rows
,
int
cols
,
int
**
board
)
{
for
(
int
i
=
0
;
i
<
rows
;
i
++
)
{
for
(
int
j
=
0
;
j
<
cols
;
j
++
)
{
board
[
i
][
j
]
=
EMPTY
;
...
...
@@ -97,7 +133,7 @@ void flush_input() {
while
((
c
=
getchar
())
!=
'\n'
&&
c
!=
EOF
);
}
bool
is_player_winning
(
int
row
,
int
col
,
int
rows
,
int
cols
,
int
board
[
rows
][
cols
]
)
{
bool
is_player_winning
(
int
row
,
int
col
,
int
rows
,
int
cols
,
int
**
board
)
{
int
current_piece
=
board
[
row
][
col
];
for
(
int
i
=
0
;
i
<
rows
-
3
;
i
++
)
{
...
...
@@ -155,7 +191,7 @@ char transform_state_to_char(enum STATE state) {
}
}
void
print_board
(
int
rows
,
int
cols
,
int
board
[
rows
][
cols
]
)
{
void
print_board
(
int
rows
,
int
cols
,
int
**
board
)
{
// Top border of the board
printf
(
"
\n
┌"
);
for
(
int
col
=
0
;
col
<
cols
;
col
++
)
{
...
...
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