Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
J
Jogo da Vida - Programação 1
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
Harbor Registry
Model registry
Operate
Environments
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
Leonardo Krambeck
Jogo da Vida - Programação 1
Commits
08fa1429
Commit
08fa1429
authored
5 years ago
by
Leonardo Krambeck
Browse files
Options
Downloads
Patches
Plain Diff
implementado todo o resto, apenas faltando corrigir checkNeighbours e dar o clear screen
parent
b63d1d1b
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
GameOfLife.c
+131
-21
131 additions, 21 deletions
GameOfLife.c
with
131 additions
and
21 deletions
GameOfLife.c
+
131
−
21
View file @
08fa1429
...
...
@@ -3,7 +3,7 @@
#include
<string.h>
typedef
struct
Generation
{
int
**
board
;
int
**
generation
;
int
rows
;
int
cols
;
}
generation_t
;
...
...
@@ -15,11 +15,22 @@ typedef struct Game {
int
totalGenerations
;
}
game_t
;
#define ALIVE 1
#define DEAD 0
void
startGame
(
game_t
*
game
,
char
**
argv
);
void
defineSizes
(
game_t
*
game
,
char
**
argv
);
void
mem
m
oryAlloc
(
game_t
*
game
);
void
memoryAlloc
(
game_t
*
game
);
void
firstGeneration
(
game_t
*
game
);
int
testSize
(
game_t
*
game
,
int
x
,
int
y
);
void
playGame
(
game_t
*
game
);
void
printGeneration
(
game_t
*
game
);
void
nextCellStatus
(
game_t
*
game
,
int
x
,
int
y
);
int
checkNeighbors
(
game_t
*
game
,
int
x
,
int
y
);
void
createLife
(
game_t
*
game
,
int
x
,
int
y
);
void
keepStatus
(
game_t
*
game
,
int
x
,
int
y
);
void
killCell
(
game_t
*
game
,
int
x
,
int
y
);
void
incrementGeneration
(
game_t
*
game
);
int
main
(
int
argc
,
char
**
argv
)
{
...
...
@@ -29,8 +40,7 @@ int main (int argc, char **argv)
{
startGame
(
&
game
,
argv
);
if
(
!
firstGeneration
(
&
game
)
)
return
2
;
firstGeneration
(
&
game
);
playGame
(
&
game
);
...
...
@@ -46,7 +56,9 @@ void startGame (game_t *game, char **argv)
{
defineSizes
(
game
,
argv
);
memmoryAlloc
(
game
);
memoryAlloc
(
game
);
game
->
generationNumber
=
1
;
printf
(
"How many generations do you want to simulate?
\n
"
);
scanf
(
"%d"
,
&
game
->
totalGenerations
);
...
...
@@ -63,29 +75,30 @@ void defineSizes (game_t *game, char **argv)
game
->
this
.
cols
=
cols
;
game
->
next
.
rows
=
rows
;
game
->
next
.
row
s
=
row
s
;
game
->
next
.
col
s
=
col
s
;
}
void
memmoryAlloc
(
game_t
*
game
)
/* Allocate the generations matrices with all cells DEAD (0). */
void
memoryAlloc
(
game_t
*
game
)
{
int
i
;
game
->
this
.
board
=
(
int
**
)
calloc
(
game
->
this
.
rows
,
sizeof
(
int
*
)
);
game
->
this
.
generation
=
(
int
**
)
calloc
(
game
->
this
.
rows
,
sizeof
(
int
*
)
);
for
(
i
=
0
;
i
<
game
->
this
.
rows
;
i
++
)
game
->
this
.
board
[
i
]
=
(
int
*
)
calloc
(
game
->
this
.
cols
,
sizeof
(
int
)
);
game
->
this
.
generation
[
i
]
=
(
int
*
)
calloc
(
game
->
this
.
cols
,
sizeof
(
int
)
);
game
->
next
.
board
=
(
int
**
)
calloc
(
game
->
next
.
rows
,
sizeof
(
int
*
)
);
game
->
next
.
generation
=
(
int
**
)
calloc
(
game
->
next
.
rows
,
sizeof
(
int
*
)
);
for
(
i
=
0
;
i
<
game
->
next
.
rows
;
i
++
)
game
->
next
.
board
[
i
]
=
(
int
*
)
calloc
(
game
->
next
.
cols
,
sizeof
(
int
)
);
game
->
next
.
generation
[
i
]
=
(
int
*
)
calloc
(
game
->
next
.
cols
,
sizeof
(
int
)
);
}
void
firstGeneration
(
game_t
*
game
)
{
int
i
,
alive
,
x
,
y
;
int
alive
,
x
,
y
,
i
=
0
;
printf
(
"
\n
"
);
printf
(
"How many alive cells do you want in the first generation?
\n
"
);
...
...
@@ -94,23 +107,120 @@ void firstGeneration (game_t *game)
printf
(
"
\n
"
);
printf
(
"Put the coordinates of the cells you want alive on the first generation:
\n
"
);
for
(
i
=
0
;
i
<
alive
;
i
++
)
while
(
i
<
alive
)
{
printf
(
"Cell [%d]: "
,
i
+
1
);
printf
(
"Cell [%d]: "
,
i
);
scanf
(
"%d %d"
,
&
x
,
&
y
);
x
--
;
y
--
;
if
(
testSize
(
game
,
x
,
y
)
)
createLife
(
game
);
{
game
->
this
.
generation
[
x
][
y
]
=
ALIVE
;
i
++
;
}
else
printf
(
"Invalid coordinates.
\n
"
);
}
}
int
testSize
(
game_t
*
game
,
int
x
,
int
y
)
{
if
(
(
x
<
0
)
||
(
y
<
0
)
||
(
x
>=
game
->
this
.
rows
)
||
(
y
>=
game
->
this
.
cols
)
)
return
0
;
return
1
;
}
void
playGame
(
game_t
*
game
)
{
int
i
,
j
;
printGeneration
(
game
);
while
(
game
->
generationNumber
<
game
->
totalGenerations
)
{
for
(
i
=
0
;
i
<
game
->
this
.
rows
;
i
++
)
for
(
j
=
0
;
j
<
game
->
this
.
cols
;
j
++
)
nextCellStatus
(
game
,
i
,
j
);
incrementGeneration
(
game
);
printGeneration
(
game
);
}
}
void
printGeneration
(
game_t
*
game
)
{
int
i
,
j
;
printf
(
"
\n
"
);
/*clrs*/
for
(
i
=
0
;
i
<
game
->
this
.
rows
;
i
++
)
{
for
(
j
=
0
;
j
<
game
->
this
.
cols
;
j
++
)
printf
(
"%d "
,
game
->
this
.
generation
[
i
][
j
]);
printf
(
"
\n
"
);
}
}
void
nextCellStatus
(
game_t
*
game
,
int
x
,
int
y
)
{
switch
(
checkNeighbors
(
game
,
x
,
y
)
)
{
case
3
:
createLife
(
game
,
x
,
y
);
break
;
case
2
:
keepStatus
(
game
,
x
,
y
);
break
;
default:
killCell
(
game
,
x
,
y
);
}
}
/* ------------- Here is the jump of the cat --------------*/
int
checkNeighbors
(
game_t
*
game
,
int
x
,
int
y
)
{
int
i
,
j
,
aliveCellCount
=
0
;
printf
(
"bruh
\n
"
);
for
(
i
=-
1
;
i
<=
1
;
i
++
)
for
(
j
=-
1
;
j
<=
1
;
j
++
)
if
(
!
(
(
x
+
i
<
0
)
||
(
y
+
j
<
0
)
||
(
x
+
i
>
game
->
this
.
rows
)
||
(
y
+
j
>
game
->
this
.
cols
)
)
)
if
(
(
x
!=
0
)
||
(
y
!=
0
)
)
if
(
game
->
this
.
generation
[
x
+
i
][
y
+
j
]
==
1
)
aliveCellCount
++
;
return
aliveCellCount
;
}
void
createLife
(
game_t
*
game
,
int
x
,
int
y
)
{
game
->
next
.
generation
[
x
][
y
]
=
ALIVE
;
}
void
keepStatus
(
game_t
*
game
,
int
x
,
int
y
)
{
game
->
next
.
generation
[
x
][
y
]
=
game
->
this
.
generation
[
x
][
y
];
}
void
killCell
(
game_t
*
game
,
int
x
,
int
y
)
{
game
->
next
.
generation
[
x
][
y
]
=
DEAD
;
}
void
incrementGeneration
(
game_t
*
game
)
{
int
i
,
j
;
for
(
i
=
0
;
i
<
game
->
next
.
rows
;
i
++
)
for
(
j
=
0
;
j
<
game
->
next
.
cols
;
j
++
)
game
->
this
.
generation
[
i
][
j
]
=
game
->
next
.
generation
[
i
][
j
];
game
->
generationNumber
++
;
}
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