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
228c5bfe
Commit
228c5bfe
authored
3 years ago
by
dario.genga
Browse files
Options
Downloads
Patches
Plain Diff
Add print of the game
parent
78dfd10b
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
+1
-0
1 addition, 0 deletions
main.c
puissance.c
+92
-0
92 additions, 0 deletions
puissance.c
puissance.h
+4
-0
4 additions, 0 deletions
puissance.h
with
97 additions
and
0 deletions
main.c
+
1
−
0
View file @
228c5bfe
...
...
@@ -11,6 +11,7 @@ int main() {
puissance
game
;
game_init
(
&
game
,
RAND_AI
,
DEFAULT_ROW
,
DEFAULT_COL
);
print_game
(
game
);
game_destroy
(
&
game
);
...
...
This diff is collapsed.
Click to expand it.
puissance.c
+
92
−
0
View file @
228c5bfe
...
...
@@ -5,20 +5,109 @@
#include
"puissance.h"
#include
<stdio.h>
#include
<stdlib.h>
#include
<string.h>
void
game_init
(
puissance
*
p
,
GameMode
mode
,
int
row
,
int
col
)
{
p
->
mode
=
mode
;
p
->
current_player
=
PLAYER_ONE
;
p
->
row
=
row
;
p
->
col
=
col
;
// Allocate memory for the data
p
->
data
=
malloc
(
col
*
sizeof
(
int
*
));
for
(
int
i
=
0
;
i
<
col
;
i
++
)
{
p
->
data
[
i
]
=
malloc
(
row
*
sizeof
(
int
*
));
}
// Set a default value for the data
for
(
int
r
=
0
;
r
<
row
;
r
++
)
{
for
(
int
c
=
0
;
c
<
col
;
c
++
)
{
p
->
data
[
r
][
c
]
=
-
1
;
}
}
}
void
print_top
(
char
*
display
,
int
col
)
{
// Print top
strcat
(
display
,
"┌"
);
for
(
int
i
=
1
;
i
<=
col
*
2
-
1
;
i
++
)
{
if
(
i
%
2
==
1
)
{
strcat
(
display
,
"─"
);
}
else
{
strcat
(
display
,
"┬"
);
}
}
strcat
(
display
,
"┐
\n
"
);
}
void
print_bot
(
char
*
display
,
int
col
)
{
// Print bot
strcat
(
display
,
"└"
);
for
(
int
i
=
1
;
i
<=
col
*
2
-
1
;
i
++
)
{
if
(
i
%
2
==
1
)
{
strcat
(
display
,
"─"
);
}
else
{
strcat
(
display
,
"┴"
);
}
}
strcat
(
display
,
"┘
\n
"
);
// Print col numbers
for
(
int
i
=
1
;
i
<=
col
;
i
++
)
{
char
i_has_string
[
256
];
sprintf
(
i_has_string
,
"%d"
,
i
);
strcat
(
display
,
" "
);
strcat
(
display
,
i_has_string
);
}
}
void
print_row_separator
(
char
*
display
,
int
col
)
{
strcat
(
display
,
"├"
);
for
(
int
i
=
1
;
i
<=
col
*
2
-
1
;
i
++
)
{
if
(
i
%
2
==
1
)
{
strcat
(
display
,
"─"
);
}
else
{
strcat
(
display
,
"┼"
);
}
}
strcat
(
display
,
"┤
\n
"
);
}
void
print_game
(
puissance
p
)
{
char
display
[
1024
];
// Print the top of the game
print_top
(
display
,
p
.
col
);
// Print the rest of the game (except bottom)
for
(
int
row_index
=
0
;
row_index
<
p
.
row
;
row_index
++
)
{
for
(
int
col_index
=
0
;
col_index
<
p
.
col
;
col_index
++
)
{
// Print each row
strcat
(
display
,
"│"
);
if
(
p
.
data
[
row_index
][
col_index
]
==
PLAYER_ONE
)
{
strcat
(
display
,
PLAYER_ONE_STRING
);
}
else
if
(
p
.
data
[
row_index
][
col_index
]
==
PLAYER_TWO
)
{
strcat
(
display
,
PLAYER_TWO_STRING
);
}
else
{
strcat
(
display
,
" "
);
}
// Close the row before going to the next line
if
(
col_index
==
p
.
col
-
1
)
{
strcat
(
display
,
"│
\n
"
);
}
}
// Print the row separator (except when we are at the bottom)
if
(
row_index
!=
p
.
row
-
1
)
{
print_row_separator
(
display
,
p
.
col
);
}
}
// Print the bottom then display the game
print_bot
(
display
,
p
.
col
);
printf
(
"%s
\n
"
,
display
);
}
void
verify_game
(
puissance
p
)
{
...
...
@@ -44,6 +133,9 @@ bool smart_play(puissance *p) {
}
void
game_destroy
(
puissance
*
p
)
{
for
(
int
i
=
0
;
i
<
p
->
col
;
i
++
)
{
free
(
p
->
data
[
i
]);
}
free
(
p
->
data
);
p
=
NULL
;
}
...
...
This diff is collapsed.
Click to expand it.
puissance.h
+
4
−
0
View file @
228c5bfe
...
...
@@ -13,6 +13,8 @@
#define COL_MIN 4
#define DEFAULT_ROW 6
#define DEFAULT_COL 7
#define PLAYER_ONE_STRING "O"
#define PLAYER_TWO_STRING "X"
typedef
enum
{
RAND_AI
,
...
...
@@ -48,4 +50,6 @@ bool random_play(puissance *p);
bool
smart_play
(
puissance
*
p
);
void
game_destroy
(
puissance
*
p
);
// TODO: add get top of col
#endif
\ No newline at end of file
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