Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
M
MLOD Mini Projet
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Yoann Bordin
MLOD Mini Projet
Commits
f3c8496d
Commit
f3c8496d
authored
Nov 02, 2020
by
Yoann Bordin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Score gestion
parent
b84b5014
Changes
6
Show whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
268 additions
and
262 deletions
+268
-262
tetris-graph.c
tetris-graph.c
+33
-9
tetris-graph.h
tetris-graph.h
+9
-1
tetris-main.c
tetris-main.c
+31
-9
tetris-main.exe
tetris-main.exe
+0
-0
tetris-struct.c
tetris-struct.c
+162
-217
tetris-struct.h
tetris-struct.h
+33
-26
No files found.
tetris-graph.c
View file @
f3c8496d
...
...
@@ -22,8 +22,6 @@ void drawSquare(int x, int y, Color color){
SQUARE_SIZE
,
SQUARE_SIZE
,
color
);
}
void
drawPiece
(
Piece
p
){
...
...
@@ -47,14 +45,40 @@ void displayScore(int score){
DrawText
(
TextFormat
(
"%4i"
,
score
),
430
,
80
,
20
,
RAYWHITE
);
}
void
gameInstructions
(
Grid
grid
,
Piece
*
pList
){
Piece
piece
=
generePiece
(
pList
);
void
displaySquare
(
Square
sq
,
int
pIndex
,
int
pListIndex
){
char
text0
[
5
];
char
text1
[
5
];
drawPiece
(
piece
);
itoa
(
sq
->
posX
,
text0
,
10
);
itoa
(
sq
->
posY
,
text1
,
10
);
DrawText
(
text0
,
20
+
80
*
pIndex
,
20
+
80
*
pListIndex
,
20
,
RAYWHITE
);
DrawText
(
text1
,
60
+
80
*
pIndex
,
20
+
80
*
pListIndex
,
20
,
RAYWHITE
);
}
while
(
canMove
(
piece
,
grid
)){
movePieceVert
(
piece
,
1
);
void
displayPiece
(
Piece
p
,
int
pListIndex
){
for
(
int
i
=
0
;
i
<
p
.
size
;
i
++
){
displaySquare
(
p
.
squares
[
i
],
i
,
pListIndex
);
}
}
addPieceToGrid
(
piece
,
grid
);
void
displayPieceList
(
Piece
*
pList
,
int
size
){
for
(
int
i
=
0
;
i
<
size
;
i
++
){
displayPiece
(
pList
[
i
],
i
);
}
}
void
displayColumn
(
Square
*
line
,
int
index
,
int
height
){
for
(
int
y
=
0
;
y
<
height
;
y
++
){
if
(
line
[
y
]
!=
NULL
){
DrawText
(
"1"
,
500
+
30
*
index
,
20
+
30
*
y
,
20
,
RAYWHITE
);
}
}
}
void
displayGrid
(
Grid
g
){
for
(
int
x
=
0
;
x
<
g
.
width
;
x
++
){
displayColumn
(
g
.
grid
[
x
],
x
,
g
.
height
);
}
}
tetris-graph.h
View file @
f3c8496d
...
...
@@ -9,13 +9,21 @@
#define GRID_PWIDTH SQUARE_SIZE*GRID_WIDTH
#define GRID_PHEIGHT SQUARE_SIZE*GRID_HEIGHT
// Init functions
void
InitDisplay
();
void
InitGame
();
// Draw functions
void
drawSquare
(
int
x
,
int
y
,
Color
color
);
void
drawPiece
(
Piece
p
);
void
drawGrid
(
Grid
g
);
// Display functions
void
displayScore
(
int
score
);
void
gameInstructions
(
Grid
grid
,
Piece
*
pList
);
\ No newline at end of file
// Display functions (debug only)
void
displaySquare
(
Square
sq
,
int
pIndex
,
int
pListIndex
);
void
displayPiece
(
Piece
p
,
int
pListIndex
);
void
displayPieceList
(
Piece
*
pList
,
int
size
);
void
displayColumn
(
Square
*
line
,
int
index
,
int
height
);
void
displayGrid
(
Grid
g
);
\ No newline at end of file
tetris-main.c
View file @
f3c8496d
...
...
@@ -10,6 +10,7 @@ int main(void){
bool
isFixed
=
true
;
int
score
=
0
;
int
level
=
1
;
int
frameCounter
=
0
;
...
...
@@ -34,7 +35,7 @@ int main(void){
// Nouvelles pièces
if
(
isFixed
){
p
=
genere
Piece
(
pieceList
);
p
=
spawn
Piece
(
pieceList
);
movePieceHztl
(
p
,
3
);
isFixed
=
false
;
}
...
...
@@ -46,15 +47,15 @@ int main(void){
if
(
IsKeyPressed
(
KEY_RIGHT
)
&&
canMoveHztl
(
grid
,
p
,
1
)){
movePieceHztl
(
p
,
1
);
}
if
(
IsKeyDown
(
KEY_DOWN
)
&&
canMove
Down
(
grid
,
p
,
1
)){
if
(
IsKeyDown
(
KEY_DOWN
)
&&
canMove
Vert
(
grid
,
p
,
1
)){
movePieceVert
(
p
,
1
);
}
if
(
IsKeyPressed
(
KEY_Q
)){
// A en AZERTY
while
(
rotate
Border
Left
(
p
,
false
)){
while
(
rotate
Collide
Left
(
p
,
false
)){
movePieceHztl
(
p
,
1
);
}
while
(
rotate
Border
Right
(
p
,
false
)){
while
(
rotate
Collide
Right
(
p
,
false
)){
movePieceHztl
(
p
,
-
1
);
}
while
(
rotateCollideGrid
(
grid
,
p
,
false
)){
...
...
@@ -63,11 +64,12 @@ int main(void){
rotatePiece
(
grid
,
p
,
false
);
}
if
(
IsKeyPressed
(
KEY_W
)){
// Z en AZERTY
while
(
rotate
Border
Left
(
p
,
true
)){
while
(
rotate
Collide
Left
(
p
,
true
)){
movePieceHztl
(
p
,
1
);
}
while
(
rotate
Border
Right
(
p
,
true
)){
while
(
rotate
Collide
Right
(
p
,
true
)){
movePieceHztl
(
p
,
-
1
);
}
while
(
rotateCollideGrid
(
grid
,
p
,
true
)){
...
...
@@ -77,9 +79,10 @@ int main(void){
rotatePiece
(
grid
,
p
,
true
);
}
int
score_prec
=
score
;
// Déplacer pièce toutes les 1s
if
(
frameCounter
%
20
==
0
){
if
(
canMove
(
p
,
grid
)){
if
(
frameCounter
%
(
60
/
level
)
==
0
){
if
(
canMove
Vert
(
grid
,
p
,
1
)){
movePieceVert
(
p
,
1
);
}
else
{
...
...
@@ -91,6 +94,11 @@ int main(void){
}
}
// Gestion des niveaux
if
(
score_prec
%
10
>
score
%
10
&&
level
<
10
){
level
++
;
}
drawPiece
(
p
);
drawGrid
(
grid
);
displayGrid
(
grid
);
...
...
@@ -99,6 +107,20 @@ int main(void){
EndDrawing
();
}
for
(
int
x
=
0
;
x
<
grid
.
width
;
x
++
){
for
(
int
y
=
0
;
y
<
grid
.
height
;
y
++
){
free
(
grid
.
grid
[
x
][
y
]);
}
free
(
grid
.
grid
[
x
]);
}
free
(
grid
.
grid
);
for
(
int
i
=
0
;
i
<
p
.
size
;
i
++
){
free
(
p
.
squares
[
i
]);
}
free
(
p
.
squares
);
CloseWindow
();
return
EXIT_SUCCESS
;
...
...
tetris-main.exe
View file @
f3c8496d
No preview for this file type
tetris-struct.c
View file @
f3c8496d
...
...
@@ -33,117 +33,28 @@ Square squareInit(){
return
sq
;
}
void
setSquareColor
(
Square
sq
,
int
colorIndex
){
Color
colorList
[
7
]
=
{
SKYBLUE
,
YELLOW
,
PURPLE
,
ORANGE
,
BLUE
,
RED
,
GREEN
};
sq
->
color
=
colorList
[
colorIndex
];
}
void
setColor
(
Piece
p
,
int
colorIndex
){
for
(
int
i
=
0
;
i
<
p
.
size
;
i
++
){
setSquareColor
(
p
.
squares
[
i
],
colorIndex
);
}
}
bool
gameOver
(
Grid
grid
){
return
height
(
grid
)
==
grid
.
height
;
}
int
height
(
Grid
g
){
int
height
=
0
;
int
height_p
;
for
(
int
i
=
0
;
i
<
g
.
width
;
i
++
){
height_p
=
heightColumn
(
g
.
grid
[
i
],
g
.
height
);
if
(
height_p
>
height
){
height
=
height_p
;
}
}
return
height
;
}
int
heightColumn
(
Square
*
column
,
int
size
){
int
height
=
size
;
while
(
height
>
0
&&
column
[
size
-
height
]
==
NULL
){
height
--
;
}
return
height
;
}
bool
canMove
(
Piece
piece
,
Grid
grid
){
for
(
int
i
=
0
;
i
<
piece
.
size
;
i
++
){
if
(
!
canMoveSquare
(
piece
.
squares
[
i
],
grid
)){
return
false
;
}
}
return
true
;
}
bool
canMoveSquare
(
Square
sq
,
Grid
g
){
int
x
=
sq
->
posX
;
int
y
=
sq
->
posY
;
Square
copySquare
(
Square
sq
){
Square
newSq
=
squareInit
();
char
text3
[
5
]
;
itoa
(
y
,
text3
,
50
)
;
DrawText
(
text3
,
400
,
20
,
20
,
RED
)
;
newSq
->
color
=
sq
->
color
;
newSq
->
posX
=
sq
->
posX
;
newSq
->
posY
=
sq
->
posY
;
if
(
y
<
g
.
height
-
1
){
if
(
y
<
g
.
height
-
heightColumn
(
g
.
grid
[
x
],
g
.
height
)
-
1
){
return
true
;
}
return
false
;
}
return
false
;
return
newSq
;
}
void
rotateSquare
(
Square
sq
,
Square
ref
,
bool
rotation
){
int
refX
=
ref
->
posX
;
int
refY
=
ref
->
posY
;
// Keep memory
int
posX
=
sq
->
posX
;
int
posY
=
sq
->
posY
;
void
setSquareColor
(
Square
sq
,
int
colorIndex
){
Color
colorList
[
7
]
=
{
SKYBLUE
,
YELLOW
,
PURPLE
,
ORANGE
,
BLUE
,
RED
,
GREEN
};
if
(
rotation
){
sq
->
posX
=
refX
-
posY
+
refY
;
sq
->
posY
=
refY
+
posX
-
refX
;
}
else
{
sq
->
posX
=
refX
+
posY
-
refY
;
sq
->
posY
=
refY
-
posX
+
refX
;
}
sq
->
color
=
colorList
[
colorIndex
];
}
void
rotatePiece
(
Grid
g
,
Piece
p
,
bool
rotation
){
void
setPieceColor
(
Piece
p
,
int
colorIndex
){
for
(
int
i
=
0
;
i
<
p
.
size
;
i
++
){
if
(
i
!=
1
){
rotateSquare
(
p
.
squares
[
i
],
p
.
squares
[
1
],
rotation
);
}
}
}
void
addPieceToGrid
(
Piece
piece
,
Grid
grid
){
for
(
int
i
=
0
;
i
<
piece
.
size
;
i
++
){
addSquareToGrid
(
piece
.
squares
[
i
],
grid
);
setSquareColor
(
p
.
squares
[
i
],
colorIndex
);
}
}
void
addSquareToGrid
(
Square
sq
,
Grid
g
){
Square
newSq
=
copySquare
(
sq
);
g
.
grid
[
newSq
->
posX
][
newSq
->
posY
]
=
newSq
;
}
int
randInt
(
int
lower
,
int
upper
){
srand
(
time
(
0
));
int
num
=
(
rand
()
%
(
upper
-
lower
+
1
))
+
lower
;
return
num
;
}
Square
getSquare
(
char
*
line
,
int
index
){
Square
sq
=
squareInit
();
int
k
=
0
;
// Compteur de '('
...
...
@@ -174,15 +85,6 @@ Piece getPieceFromLine(char* line){
for
(
int
i
=
0
;
i
<
p
.
size
;
i
++
){
sq
=
getSquare
(
line
,
i
);
p
.
squares
[
i
]
=
sq
;
char
text0
[
5
];
char
text1
[
5
];
itoa
(
sq
->
posX
,
text0
,
10
);
itoa
(
sq
->
posY
,
text1
,
10
);
DrawText
(
text0
,
50
,
100
+
50
*
i
,
20
,
RAYWHITE
);
DrawText
(
text1
,
100
,
100
+
50
*
i
,
20
,
RAYWHITE
);
}
return
p
;
}
...
...
@@ -211,29 +113,27 @@ Piece* getPiecesData(char* fileName){
for
(
int
i
=
0
;
i
<
numPieces
;
i
++
){
fgets
(
line
,
30
,
dataFile
);
pieceList
[
i
]
=
getPieceFromLine
(
line
);
setColor
(
pieceList
[
i
],
i
);
set
Piece
Color
(
pieceList
[
i
],
i
);
}
free
(
line
);
return
pieceList
;
}
Square
copySquare
(
Square
sq
){
Square
newSq
=
squareInit
();
newSq
->
color
=
sq
->
color
;
newSq
->
posX
=
sq
->
posX
;
newSq
->
posY
=
sq
->
posY
;
int
randInt
(
int
lower
,
int
upper
){
srand
(
time
(
0
));
int
num
=
(
rand
()
%
(
upper
-
lower
+
1
))
+
lower
;
return
n
ewSq
;
return
n
um
;
}
Piece
genere
Piece
(
Piece
*
pieceList
){
Piece
spawn
Piece
(
Piece
*
pieceList
){
int
numPiece
=
randInt
(
0
,
6
);
Piece
pieceFromList
=
pieceList
[
numPiece
];
Piece
newPiece
=
pieceInit
();
// Copy the piece from the piece list
for
(
int
i
=
0
;
i
<
newPiece
.
size
;
i
++
){
newPiece
.
squares
[
i
]
=
copySquare
(
pieceFromList
.
squares
[
i
]);
}
...
...
@@ -241,40 +141,16 @@ Piece generePiece(Piece* pieceList){
return
newPiece
;
}
void
displaySquare
(
Square
sq
,
int
pIndex
,
int
pListIndex
){
char
text0
[
5
];
char
text1
[
5
];
itoa
(
sq
->
posX
,
text0
,
10
);
itoa
(
sq
->
posY
,
text1
,
10
);
DrawText
(
text0
,
20
+
80
*
pIndex
,
20
+
80
*
pListIndex
,
20
,
RAYWHITE
);
DrawText
(
text1
,
60
+
80
*
pIndex
,
20
+
80
*
pListIndex
,
20
,
RAYWHITE
);
}
void
displayPiece
(
Piece
p
,
int
pListIndex
){
for
(
int
i
=
0
;
i
<
p
.
size
;
i
++
){
displaySquare
(
p
.
squares
[
i
],
i
,
pListIndex
);
}
}
bool
canMoveVert
(
Grid
g
,
Piece
p
,
int
moveNum
){
for
(
int
i
=
0
;
i
<
p
.
size
;
i
++
){
int
x
=
p
.
squares
[
i
]
->
posX
;
int
y
=
p
.
squares
[
i
]
->
posY
+
moveNum
;
void
displayPieceList
(
Piece
*
pList
,
int
size
){
for
(
int
i
=
0
;
i
<
size
;
i
++
){
displayPiece
(
pList
[
i
],
i
);
if
(
y
>=
g
.
height
||
g
.
grid
[
x
][
y
]
!=
NULL
){
return
false
;
}
}
void
moveSquareVert
(
Square
sq
,
int
shift
){
sq
->
posY
+=
shift
;
}
void
movePieceVert
(
Piece
piece
,
int
shift
){
for
(
int
i
=
0
;
i
<
piece
.
size
;
i
++
){
moveSquareVert
(
piece
.
squares
[
i
],
shift
);
}
char
text3
[
5
];
itoa
(
shift
,
text3
,
20
);
DrawText
(
text3
,
450
,
20
,
20
,
RED
);
return
true
;
}
bool
canMoveHztl
(
Grid
g
,
Piece
p
,
int
moveNum
){
...
...
@@ -288,16 +164,18 @@ bool canMoveHztl(Grid g, Piece p, int moveNum){
}
return
true
;
}
bool
canMoveDown
(
Grid
g
,
Piece
p
,
int
moveNum
){
for
(
int
i
=
0
;
i
<
p
.
size
;
i
++
){
int
x
=
p
.
squares
[
i
]
->
posX
;
int
y
=
p
.
squares
[
i
]
->
posY
+
moveNum
;
if
(
y
<
0
||
y
>=
g
.
height
||
g
.
grid
[
x
][
y
]
!=
NULL
){
return
false
;
}
void
moveSquareVert
(
Square
sq
,
int
shift
){
sq
->
posY
+=
shift
;
}
void
movePieceVert
(
Piece
piece
,
int
shift
){
for
(
int
i
=
0
;
i
<
piece
.
size
;
i
++
){
moveSquareVert
(
piece
.
squares
[
i
],
shift
);
}
return
true
;
char
text3
[
5
];
itoa
(
shift
,
text3
,
20
);
DrawText
(
text3
,
450
,
20
,
20
,
RED
);
}
void
moveSquareHztl
(
Square
sq
,
int
shift
){
...
...
@@ -310,104 +188,66 @@ void movePieceHztl(Piece p, int shift){
}
}
int
removeLinesIfCompleted
(
Grid
grid
){
int
nbLinesRemoved
=
0
;
bool
isLineCompleted
;
int
y
=
0
;
while
(
y
<
grid
.
height
){
isLineCompleted
=
true
;
for
(
int
x
=
0
;
x
<
grid
.
width
;
x
++
){
if
(
grid
.
grid
[
x
][
y
]
==
NULL
){
isLineCompleted
=
false
;
}
}
bool
rotateCollideLeft
(
Piece
p
,
bool
rotation
){
int
refX
=
p
.
squares
[
1
]
->
posX
;
int
refY
=
p
.
squares
[
1
]
->
posY
;
if
(
isLineCompleted
){
nbLinesRemoved
++
;
int
lineToRemove
=
y
;
for
(
int
i
=
0
;
i
<
p
.
size
;
i
++
){
int
posY
=
p
.
squares
[
i
]
->
posY
;
int
newPosX
;
//free
for
(
int
x
=
0
;
x
<
grid
.
width
;
x
++
){
free
(
grid
.
grid
[
x
][
lineToRemove
]);
}
//shift
for
(
int
x
=
0
;
x
<
grid
.
width
;
x
++
){
for
(
int
i
=
lineToRemove
;
i
>
0
;
i
--
){
grid
.
grid
[
x
][
i
]
=
grid
.
grid
[
x
][
i
-
1
];
}
}
//initTop
for
(
int
x
=
0
;
x
<
grid
.
width
;
x
++
){
grid
.
grid
[
x
][
0
]
=
malloc
(
sizeof
(
square_s
));
grid
.
grid
[
x
][
0
]
=
NULL
;
}
if
(
rotation
){
newPosX
=
refX
-
posY
+
refY
;
}
else
{
y
++
;
}
newPosX
=
refX
+
posY
-
refY
;
}
return
nbLinesRemoved
;
}
void
displayColumn
(
Square
*
line
,
int
index
,
int
height
){
for
(
int
y
=
0
;
y
<
height
;
y
++
){
if
(
line
[
y
]
!=
NULL
){
DrawText
(
"1"
,
500
+
30
*
index
,
20
+
30
*
y
,
20
,
RAYWHITE
);
}
if
(
newPosX
<
0
){
return
true
;
}
}
void
displayGrid
(
Grid
g
){
for
(
int
x
=
0
;
x
<
g
.
width
;
x
++
){
displayColumn
(
g
.
grid
[
x
],
x
,
g
.
height
);
}
return
false
;
}
bool
rotate
BorderLef
t
(
Piece
p
,
bool
rotation
){
bool
rotate
CollideRigh
t
(
Piece
p
,
bool
rotation
){
int
refX
=
p
.
squares
[
1
]
->
posX
;
int
refY
=
p
.
squares
[
1
]
->
posY
;
for
(
int
i
=
0
;
i
<
p
.
size
;
i
++
){
int
posX
=
p
.
squares
[
i
]
->
posX
;
int
posY
=
p
.
squares
[
i
]
->
posY
;
int
newPosX
,
newPosY
;
int
newPosY
;
if
(
rotation
){
newPosX
=
refX
-
posY
+
refY
;
newPosY
=
refY
+
posX
-
refX
;
newPosY
=
refY
-
posX
+
refX
;
}
else
{
newPosX
=
refX
+
posY
-
refY
;
newPosY
=
refY
-
posX
+
refX
;
newPosY
=
refY
+
posX
-
refX
;
}
if
(
newPos
X
<
0
){
if
(
newPos
Y
<
0
){
return
true
;
}
}
return
false
;
}
bool
rotate
BorderRight
(
Piece
p
,
bool
rotation
){
bool
rotate
CollideTop
(
Piece
p
,
bool
rotation
){
int
refX
=
p
.
squares
[
1
]
->
posX
;
int
refY
=
p
.
squares
[
1
]
->
posY
;
for
(
int
i
=
0
;
i
<
p
.
size
;
i
++
){
int
posX
=
p
.
squares
[
i
]
->
posX
;
int
posY
=
p
.
squares
[
i
]
->
posY
;
int
newPosX
,
newPosY
;
int
newPosX
;
if
(
rotation
){
newPosX
=
refX
-
posY
+
refY
;
newPosY
=
refY
+
posX
-
refX
;
newPosX
=
refX
+
posY
-
refY
;
}
else
{
newPosX
=
refX
+
posY
-
refY
;
newPosY
=
refY
-
posX
+
refX
;
newPosX
=
refX
-
posY
+
refY
;
}
if
(
newPosX
>=
GRID_WIDTH
){
if
(
newPosX
<
0
){
return
true
;
}
}
...
...
@@ -438,3 +278,108 @@ bool rotateCollideGrid(Grid g, Piece p, bool rotation){
}
return
false
;
}
void
rotateSquare
(
Square
sq
,
Square
ref
,
bool
rotation
){
int
refX
=
ref
->
posX
;
int
refY
=
ref
->
posY
;
// Keep memory
int
posX
=
sq
->
posX
;
int
posY
=
sq
->
posY
;
if
(
rotation
){
sq
->
posX
=
refX
-
posY
+
refY
;
sq
->
posY
=
refY
+
posX
-
refX
;
}
else
{
sq
->
posX
=
refX
+
posY
-
refY
;
sq
->
posY
=
refY
-
posX
+
refX
;
}
}
void
rotatePiece
(
Grid
g
,
Piece
p
,
bool
rotation
){
for
(
int
i
=
0
;
i
<
p
.
size
;
i
++
){
if
(
i
!=
1
){
rotateSquare
(
p
.
squares
[
i
],
p
.
squares
[
1
],
rotation
);
}
}
}
void
addSquareToGrid
(
Square
sq
,
Grid
g
){
Square
newSq
=
copySquare
(
sq
);
g
.
grid
[
newSq
->
posX
][
newSq
->
posY
]
=
newSq
;
}
void
addPieceToGrid
(
Piece
piece
,
Grid
grid
){
for
(
int
i
=
0
;
i
<
piece
.
size
;
i
++
){
addSquareToGrid
(
piece
.
squares
[
i
],
grid
);
}
}
int
removeLinesIfCompleted
(
Grid
grid
){
int
nbLinesRemoved
=
0
;
bool
isLineCompleted
;
int
y
=
0
;
while
(
y
<
grid
.
height
){
isLineCompleted
=
true
;
for
(
int
x
=
0
;
x
<
grid
.
width
;
x
++
){
if
(
grid
.
grid
[
x
][
y
]
==
NULL
){
isLineCompleted
=
false
;
}
}
if
(
isLineCompleted
){
nbLinesRemoved
++
;
int
lineToRemove
=
y
;
//free
for
(
int
x
=
0
;
x
<
grid
.
width
;
x
++
){
free
(
grid
.
grid
[
x
][
lineToRemove
]);
}
//shift
for
(
int
x
=
0
;
x
<
grid
.
width
;
x
++
){
for
(
int
i
=
lineToRemove
;
i
>
0
;
i
--
){
grid
.
grid
[
x
][
i
]
=
grid
.
grid
[
x
][
i
-
1
];
}
}
//initTop
for
(
int
x
=
0
;
x
<
grid
.
width
;
x
++
){
grid
.
grid
[
x
][
0
]
=
malloc
(
sizeof
(
square_s
));
grid
.
grid
[
x
][
0
]
=
NULL
;
}
}
else
{
y
++
;
}
}
return
nbLinesRemoved
;
}
int
heightColumn
(
Square
*
column
,
int
size
){
int
height
=
size
;
while
(
height
>
0
&&
column
[
size
-
height
]
==
NULL
){
height
--
;
}
return
height
;
}
int
height
(
Grid
g
){
int
height
=
0
;
int
height_p
;
for
(
int
i
=
0
;
i
<
g
.
width
;
i
++
){
height_p
=
heightColumn
(
g
.
grid
[
i
],
g
.
height
);
if
(
height_p
>
height
){
height
=
height_p
;
}
}
return
height
;
}
bool
gameOver
(
Grid
grid
){
return
height
(
grid
)
==
grid
.
height
;
}
\ No newline at end of file
tetris-struct.h
View file @
f3c8496d
...
...
@@ -30,48 +30,55 @@ typedef struct Piece{
int
size
;
}
Piece
;
// Init functions
Grid
gridInit
();
Grid
initSquares
(
Grid
g
);
Piece
pieceInit
();
Square
squareInit
();
// Copy functions
Square
copySquare
(
Square
sq
);
bool
gameOver
(
Grid
grid
);
int
height
(
Grid
g
);
int
heightColumn
(
Square
*
column
,
int
size
);
bool
canMove
(
Piece
piece
,
Grid
grid
);
bool
canMoveSquare
(
Square
sq
,
Grid
g
);
void
rotateSquare
(
Square
sq
,
Square
ref
,
bool
rotation
);
void
rotatePiece
(
Grid
g
,
Piece
p
,
bool
rotation
);
// False makes a counterclockwise rotation and true makes a clockwise rotation
// Set functions
void
setSquareColor
(
Square
sq
,
int
colorIndex
);
void
setPieceColor
(
Piece
p
,
int
colorIndex
);
void
addPieceToGrid
(
Piece
piece
,
Grid
grid
);
void
addSquareToGrid
(
Square
sq
,
Grid
g
);
int
randInt
(
int
lower
,
int
upper
);
// Piece generation and data reading
Square
getSquare
(
char
*
line
,
int
index
);
Piece
getPieceFromLine
(
char
*
line
);
Piece
*
getPiecesData
(
char
*
fileName
);
Piece
generePiece
(
Piece
*
pieceList
);
void
displaySquare
(
Square
sq
,
int
pIndex
,
int
pListIndex
);
void
displayPiece
(
Piece
p
,
int
pListIndex
);
void
displayPieceList
(
Piece
*
pList
,
int
size
);
int
randInt
(
int
lower
,
int
upper
);
Piece
spawnPiece
(
Piece
*
pieceList
);
// Bool verifications for moving
bool
canMoveVert
(
Grid
g
,
Piece
p
,
int
moveNum
);
bool
canMoveHztl
(
Grid
g
,
Piece
p
,
int
moveNum
);
bool
canMoveDown
(
Grid
g
,
Piece
p
,
int
moveNum
);
// Moving functions
void
moveSquareVert
(
Square
sq
,
int
shift
);
void
movePieceVert
(
Piece
piece
,
int
shift
);
bool
canMoveHztl
(
Grid
g
,
Piece
p
,
int
moveNum
);
void
moveSquareHztl
(
Square
sq
,
int
shift
);
void
movePieceHztl
(
Piece
p
,
int
shift
);
int
removeLinesIfCompleted
(
Grid
grid
);
// Bool verifications for rotating
bool
rotateCollideLeft
(
Piece
p
,
bool
rotation
);
bool
rotateCollideRight
(
Piece
p
,
bool
rotation
);
bool
rotateCollideTop
(
Piece
p
,
bool
rotation
);
// To debug
bool
rotateCollideGrid
(
Grid
g
,
Piece
p
,
bool
rotation
);
void
displayColumn
(
Square
*
line
,
int
index
,
int
height
);
void
displayGrid
(
Grid
g
);
// Rotating functions
void
rotateSquare
(
Square
sq
,
Square
ref
,
bool
rotation
);
void
rotatePiece
(
Grid
g
,
Piece
p
,
bool
rotation
);
// False makes a counterclockwise rotation and true makes a clockwise rotation
bool
rotateBorderLeft
(
Piece
p
,
bool
rotation
);
bool
rotateBorderRight
(
Piece
p
,
bool
rotation
);
bool
rotateCollideGrid
(
Grid
g
,
Piece
p
,
bool
rotation
);
\ No newline at end of file
// Grid adding functions
void
addSquareToGrid
(
Square
sq
,
Grid
g
);
void
addPieceToGrid
(
Piece
piece
,
Grid
grid
);
// Line completed detection
int
removeLinesIfCompleted
(
Grid
grid
);
// Returns the number of lines completed
// Game over detection
int
heightColumn
(
Square
*
column
,
int
size
);
int
height
(
Grid
g
);
bool
gameOver
(
Grid
grid
);
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment