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
24e6e5ff
Commit
24e6e5ff
authored
Nov 01, 2020
by
Yoann Bordin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added basic rules
parent
60a52c28
Changes
7
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
225 additions
and
76 deletions
+225
-76
tetris-graph.c
tetris-graph.c
+35
-6
tetris-graph.h
tetris-graph.h
+7
-3
tetris-main.c
tetris-main.c
+46
-13
tetris-main.exe
tetris-main.exe
+0
-0
tetris-struct.c
tetris-struct.c
+124
-49
tetris-struct.h
tetris-struct.h
+12
-4
tetris.data
tetris.data
+1
-1
No files found.
tetris-graph.c
View file @
24e6e5ff
...
@@ -7,19 +7,48 @@ void InitGame(){
...
@@ -7,19 +7,48 @@ void InitGame(){
void
InitDisplay
(){
void
InitDisplay
(){
ClearBackground
(
BLACK
);
ClearBackground
(
BLACK
);
DrawLine
(
BORDER_GAP
,
BORDER_GAP
,
BORDER_GAP
,
BORDER_GAP
+
GRID_HEIGHT
,
RAYWHITE
);
DrawLine
(
BORDER_GAP
,
BORDER_GAP
+
GRID_HEIGHT
,
BORDER_GAP
+
GRID_WIDTH
,
BORDER_GAP
+
GRID_HEIGHT
,
RAYWHITE
);
const
int
posY
=
BORDER_GAP
+
GRID_PHEIGHT
;
DrawLine
(
BORDER_GAP
+
GRID_WIDTH
,
BORDER_GAP
+
GRID_HEIGHT
,
BORDER_GAP
+
GRID_WIDTH
,
BORDER_GAP
,
RAYWHITE
);
const
int
posX
=
BORDER_GAP
+
GRID_PWIDTH
;
DrawLine
(
BORDER_GAP
,
BORDER_GAP
,
BORDER_GAP
,
posY
,
RAYWHITE
);
DrawLine
(
BORDER_GAP
,
posY
,
posX
,
posY
,
RAYWHITE
);
DrawLine
(
posX
,
posY
,
posX
,
BORDER_GAP
,
RAYWHITE
);
}
}
void
drawSquare
(
Square
sq
){
void
drawSquare
(
Square
sq
){
DrawRectangle
(
sq
->
posX
*
SQUARE_SIZE
,
sq
->
posY
*
SQUARE_SIZE
,
if
(
sq
!=
NULL
){
SQUARE_SIZE
,
SQUARE_SIZE
,
DrawRectangle
(
BORDER_GAP
+
sq
->
posX
*
SQUARE_SIZE
,
sq
->
color
);
BORDER_GAP
+
sq
->
posY
*
SQUARE_SIZE
,
SQUARE_SIZE
,
SQUARE_SIZE
,
sq
->
color
);
}
}
}
void
drawPiece
(
Piece
p
){
void
drawPiece
(
Piece
p
){
for
(
int
i
=
0
;
i
<
p
.
size
;
i
++
){
for
(
int
i
=
0
;
i
<
p
.
size
;
i
++
){
drawSquare
(
p
.
squares
[
i
]);
drawSquare
(
p
.
squares
[
i
]);
}
}
}
void
displayGrid
(
Grid
g
){
for
(
int
i
=
0
;
i
<
g
.
width
;
i
++
){
for
(
int
j
=
0
;
j
<
g
.
height
;
j
++
){
drawSquare
(
g
.
grid
[
i
][
j
]);
}
}
}
void
gameInstructions
(
Grid
grid
,
Piece
*
pList
){
Piece
piece
=
generePiece
(
pList
);
drawPiece
(
piece
);
while
(
canMove
(
piece
,
grid
)){
movePieceVert
(
piece
,
1
);
}
addPieceToGrid
(
piece
,
grid
);
}
}
\ No newline at end of file
tetris-graph.h
View file @
24e6e5ff
...
@@ -5,11 +5,15 @@
...
@@ -5,11 +5,15 @@
#define BORDER_GAP 20
#define BORDER_GAP 20
#define SQUARE_SIZE 30
#define SQUARE_SIZE 30
#define GRID_WIDTH SQUARE_SIZE*10
#define GRID_HEIGHT SQUARE_SIZE*20
#define GRID_PWIDTH SQUARE_SIZE*GRID_WIDTH
#define GRID_PHEIGHT SQUARE_SIZE*GRID_HEIGHT
void
InitDisplay
();
void
InitDisplay
();
void
InitGame
();
void
InitGame
();
void
drawSquare
(
Square
sq
);
void
drawSquare
(
Square
sq
);
void
drawPiece
(
Piece
p
);
void
drawPiece
(
Piece
p
);
\ No newline at end of file
void
displayGrid
(
Grid
g
);
void
gameInstructions
(
Grid
grid
,
Piece
*
pList
);
\ No newline at end of file
tetris-main.c
View file @
24e6e5ff
#include "tetris-graph.h"
#include "tetris-graph.h"
#include <stdio.h>
int
main
(
void
){
int
main
(
void
){
InitGame
();
InitGame
();
Piece
*
pieceList
=
getPiecesData
();
Piece
*
pieceList
=
getPiecesData
(
"tetris.data"
);
Grid
grid
=
gridInit
();
Piece
p
=
pieceInit
();
bool
isFixed
=
true
;
int
frameCounter
=
0
;
while
(
!
WindowShouldClose
()){
while
(
!
WindowShouldClose
()
&&
!
gameOver
(
grid
)){
BeginDrawing
();
BeginDrawing
();
InitDisplay
();
InitDisplay
();
displayPieceList
(
pieceList
,
7
);
// Testing
char
text1
[
10
];
itoa
(
grid
.
height
,
text1
,
10
);
DrawText
(
text1
,
20
,
20
,
20
,
RED
);
char
text2
[
5
];
itoa
(
height
(
grid
),
text2
,
10
);
DrawText
(
text2
,
50
,
20
,
20
,
RED
);
if
(
!
gameOver
(
grid
)){
DrawText
(
"not game over"
,
100
,
20
,
20
,
RED
);
}
/* Grid grid = gridInit();
// Nouvelles pièces
while(!gameOver(grid)){
if
(
isFixed
){
gameInstructions(grid);
p
=
generePiece
(
pieceList
);
} */
movePieceHztl
(
p
,
3
);
Piece
p
=
generePiece
(
pieceList
);
isFixed
=
false
;
}
// Déplacer pièce toutes les 2s
if
(
frameCounter
%
10
==
0
){
if
(
canMove
(
p
,
grid
)){
movePieceVert
(
p
,
1
);
}
else
{
addPieceToGrid
(
p
,
grid
);
isFixed
=
true
;
}
}
drawPiece
(
p
);
drawPiece
(
p
);
displayGrid
(
grid
);
frameCounter
++
;
EndDrawing
();
EndDrawing
();
}
}
EndDrawing
();
CloseWindow
();
CloseWindow
();
return
0
;
return
EXIT_SUCCESS
;
}
}
\ No newline at end of file
tetris-main.exe
View file @
24e6e5ff
No preview for this file type
tetris-struct.c
View file @
24e6e5ff
...
@@ -2,6 +2,9 @@
...
@@ -2,6 +2,9 @@
Grid
gridInit
(){
Grid
gridInit
(){
Grid
g
;
Grid
g
;
g
.
width
=
GRID_WIDTH
;
g
.
height
=
GRID_HEIGHT
;
g
.
grid
=
calloc
(
g
.
width
,
sizeof
(
Square
*
));
g
.
grid
=
calloc
(
g
.
width
,
sizeof
(
Square
*
));
for
(
int
i
=
0
;
i
<
g
.
width
;
i
++
){
for
(
int
i
=
0
;
i
<
g
.
width
;
i
++
){
...
@@ -13,19 +16,35 @@ Grid gridInit(){
...
@@ -13,19 +16,35 @@ Grid gridInit(){
Piece
pieceInit
(){
Piece
pieceInit
(){
Piece
p
;
Piece
p
;
p
.
squares
=
calloc
(
p
.
size
,
sizeof
(
Square
));
p
.
size
=
PIECE_SIZE
;
p
.
size
=
PIECE_SIZE
;
p
.
squares
=
calloc
(
p
.
size
,
sizeof
(
Square
));
return
p
;
return
p
;
}
}
Square
squareInit
(){
Square
squareInit
(){
Square
sq
=
malloc
(
sizeof
(
Square
));
Square
sq
=
malloc
(
sizeof
(
square_s
));
sq
->
color
=
RAYWHITE
;
sq
->
color
=
RAYWHITE
;
sq
->
posX
=
0
;
sq
->
posY
=
0
;
return
sq
;
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
){
bool
gameOver
(
Grid
grid
){
return
height
(
grid
)
==
grid
.
height
;
return
height
(
grid
)
==
grid
.
height
;
}
}
...
@@ -41,28 +60,19 @@ int height(Grid g){
...
@@ -41,28 +60,19 @@ int height(Grid g){
height
=
height_p
;
height
=
height_p
;
}
}
}
}
return
height
;
}
}
int
heightColumn
(
Square
*
column
,
int
size
){
int
heightColumn
(
Square
*
column
,
int
size
){
int
height
=
0
;
int
height
=
size
;
int
y
;
for
(
int
i
=
0
;
i
<
size
;
i
++
){
while
(
height
>
0
&&
column
[
size
-
height
]
==
NULL
){
if
(
column
[
i
]
!=
NULL
){
height
--
;
y
=
column
[
i
]
->
posY
;
if
(
y
+
1
>
height
){
height
=
y
+
1
;
}
}
}
}
return
height
;
return
height
;
}
}
bool
canMove
(
Piece
piece
,
Grid
grid
){
bool
canMove
(
Piece
piece
,
Grid
grid
){
int
x
,
y
;
for
(
int
i
=
0
;
i
<
piece
.
size
;
i
++
){
for
(
int
i
=
0
;
i
<
piece
.
size
;
i
++
){
if
(
!
canMoveSquare
(
piece
.
squares
[
i
],
grid
)){
if
(
!
canMoveSquare
(
piece
.
squares
[
i
],
grid
)){
...
@@ -75,18 +85,49 @@ bool canMove(Piece piece, Grid grid){
...
@@ -75,18 +85,49 @@ bool canMove(Piece piece, Grid grid){
bool
canMoveSquare
(
Square
sq
,
Grid
g
){
bool
canMoveSquare
(
Square
sq
,
Grid
g
){
int
x
=
sq
->
posX
;
int
x
=
sq
->
posX
;
int
y
=
sq
->
posY
;
int
y
=
sq
->
posY
;
return
(
y
!=
0
||
y
==
heightColumn
(
g
.
grid
[
x
],
g
.
height
));
char
text3
[
5
];
itoa
(
y
,
text3
,
50
);
DrawText
(
text3
,
400
,
20
,
20
,
RED
);
if
(
y
<
g
.
height
-
1
){
if
(
y
<
g
.
height
-
heightColumn
(
g
.
grid
[
x
],
g
.
height
)
-
1
){
return
true
;
}
return
false
;
}
return
false
;
}
}
void
move
(
Piece
piece
){
void
rotateSquare
(
Square
sq
,
Square
ref
,
bool
rotation
){
for
(
int
i
=
0
;
i
<
piece
.
size
;
i
++
){
int
refX
=
ref
->
posX
;
moveSquare
(
piece
.
squares
[
i
]);
int
refY
=
ref
->
posY
;
// Keep memory
int
posX
=
sq
->
posX
;
int
posY
=
sq
->
posY
;
if
(
rotation
){
sq
->
posX
=
refX
-
posY
;
sq
->
posY
=
refY
+
posX
;
}
else
{
sq
->
posX
=
refX
+
posY
;
sq
->
posY
=
refY
-
posX
;
}
}
}
}
void
moveSquare
(
Square
sq
){
void
rotatePiece
(
Piece
p
,
bool
rotation
){
sq
->
posY
--
;
//const int width = GRID_WIDTH;
//const int height = GRID_HEIGHT;
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
){
void
addPieceToGrid
(
Piece
piece
,
Grid
grid
){
...
@@ -96,10 +137,9 @@ void addPieceToGrid(Piece piece, Grid grid){
...
@@ -96,10 +137,9 @@ void addPieceToGrid(Piece piece, Grid grid){
}
}
void
addSquareToGrid
(
Square
sq
,
Grid
g
){
void
addSquareToGrid
(
Square
sq
,
Grid
g
){
int
x
=
sq
->
posX
;
Square
newSq
=
copySquare
(
sq
);
int
y
=
sq
->
posY
;
g
.
grid
[
x
][
y
]
=
s
q
;
g
.
grid
[
newSq
->
posX
][
newSq
->
posY
]
=
newS
q
;
}
}
int
randInt
(
int
lower
,
int
upper
){
int
randInt
(
int
lower
,
int
upper
){
...
@@ -152,46 +192,58 @@ Piece getPieceFromLine(char* line){
...
@@ -152,46 +192,58 @@ Piece getPieceFromLine(char* line){
return
p
;
return
p
;
}
}
Piece
*
getPiecesData
(){
Piece
*
getPiecesData
(
char
*
fileName
){
Piece
*
pieceList
;
FILE
*
dataFile
=
NULL
;
unsigned
int
numPieces
;
char
*
fileName
=
"tetris.data"
;
FILE
*
dataFile
;
dataFile
=
fopen
(
fileName
,
"r"
);
dataFile
=
fopen
(
fileName
,
"r"
);
if
(
dataFile
==
NULL
){
exit
(
EXIT_FAILURE
);
}
unsigned
int
pieceNumber
;
// Initialize number of pieces
fscanf
(
dataFile
,
"%u
\n
"
,
&
pieceNumber
);
fscanf
(
dataFile
,
"%u
\n
"
,
&
numPieces
);
char
*
line
=
calloc
(
22
,
sizeof
(
char
));
// Initialize piecesList
pieceList
=
calloc
(
pieceNumber
,
sizeof
(
Piece
));
char
*
line
=
calloc
(
30
,
sizeof
(
char
));
for
(
int
i
=
0
;
i
<
pieceNumber
;
i
++
){
Piece
*
pieceList
=
calloc
(
numPieces
,
sizeof
(
Piece
));
for
(
int
i
=
0
;
i
<
numPieces
;
i
++
){
pieceList
[
i
]
=
pieceInit
();
pieceList
[
i
]
=
pieceInit
();
fgets
(
line
,
22
,
dataFile
);
}
for
(
int
i
=
0
;
i
<
numPieces
;
i
++
){
fgets
(
line
,
30
,
dataFile
);
pieceList
[
i
]
=
getPieceFromLine
(
line
);
pieceList
[
i
]
=
getPieceFromLine
(
line
);
setColor
(
pieceList
[
i
],
i
);
}
}
free
(
line
);
free
(
line
);
return
pieceList
;
return
pieceList
;
}
}
Piece
generePiece
(
Piece
*
pieceList
){
Square
copySquare
(
Square
sq
){
int
numPiece
=
randInt
(
0
,
6
);
Square
newSq
=
squareInit
();
DrawText
(
"test"
,
20
,
20
,
20
,
RED
);
Piece
p
=
pieceList
[
numPiece
];
return
p
;
}
void
gameInstructions
(
Grid
grid
,
Piece
*
pList
){
newSq
->
color
=
sq
->
color
;
Piece
piece
=
generePiece
(
pList
);
newSq
->
posX
=
sq
->
posX
;
newSq
->
posY
=
sq
->
posY
;
drawPiece
(
piece
);
return
newSq
;
}
while
(
canMove
(
piece
,
grid
)){
Piece
generePiece
(
Piece
*
pieceList
){
move
(
piece
);
int
numPiece
=
randInt
(
0
,
6
);
}
addPieceToGrid
(
piece
,
grid
);
Piece
pieceFromList
=
pieceList
[
numPiece
];
Piece
newPiece
=
pieceInit
();
for
(
int
i
=
0
;
i
<
newPiece
.
size
;
i
++
){
newPiece
.
squares
[
i
]
=
copySquare
(
pieceFromList
.
squares
[
i
]);
}
return
newPiece
;
}
}
void
displaySquare
(
Square
sq
,
int
pIndex
,
int
pListIndex
){
void
displaySquare
(
Square
sq
,
int
pIndex
,
int
pListIndex
){
...
@@ -215,4 +267,27 @@ void displayPieceList(Piece* pList, int size){
...
@@ -215,4 +267,27 @@ void displayPieceList(Piece* pList, int size){
for
(
int
i
=
0
;
i
<
size
;
i
++
){
for
(
int
i
=
0
;
i
<
size
;
i
++
){
displayPiece
(
pList
[
i
],
i
);
displayPiece
(
pList
[
i
],
i
);
}
}
}
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
);
}
void
moveSquareHztl
(
Square
sq
,
int
shift
){
sq
->
posX
+=
shift
;
}
void
movePieceHztl
(
Piece
p
,
int
shift
){
for
(
int
i
=
0
;
i
<
p
.
size
;
i
++
){
moveSquareHztl
(
p
.
squares
[
i
],
shift
);
}
}
}
\ No newline at end of file
tetris-struct.h
View file @
24e6e5ff
#include <stdbool.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdlib.h>
#include <time.h>
#include "raylib.h"
#include "raylib.h"
#define PIECE_SIZE 4;
#define PIECE_SIZE 4;
#define GRID_WIDTH 10;
#define GRID_HEIGHT 20;
typedef
struct
square_s
{
typedef
struct
square_s
{
int
posX
;
int
posX
;
...
@@ -31,6 +34,8 @@ Grid gridInit();
...
@@ -31,6 +34,8 @@ Grid gridInit();
Grid
initSquares
(
Grid
g
);
Grid
initSquares
(
Grid
g
);
Piece
pieceInit
();
Piece
pieceInit
();
Square
copySquare
(
Square
sq
);
bool
gameOver
(
Grid
grid
);
bool
gameOver
(
Grid
grid
);
int
height
(
Grid
g
);
int
height
(
Grid
g
);
int
heightColumn
(
Square
*
column
,
int
size
);
int
heightColumn
(
Square
*
column
,
int
size
);
...
@@ -38,8 +43,8 @@ int heightColumn(Square* column, int size);
...
@@ -38,8 +43,8 @@ int heightColumn(Square* column, int size);
bool
canMove
(
Piece
piece
,
Grid
grid
);
bool
canMove
(
Piece
piece
,
Grid
grid
);
bool
canMoveSquare
(
Square
sq
,
Grid
g
);
bool
canMoveSquare
(
Square
sq
,
Grid
g
);
void
move
(
Piece
piece
);
void
rotateSquare
(
Square
sq
,
Square
ref
,
bool
rotation
);
void
moveSquare
(
Square
sq
);
void
rotatePiece
(
Piece
p
,
bool
rotation
);
// False makes a counterclockwise rotation and true makes a clockwise rotation
void
addPieceToGrid
(
Piece
piece
,
Grid
grid
);
void
addPieceToGrid
(
Piece
piece
,
Grid
grid
);
void
addSquareToGrid
(
Square
sq
,
Grid
g
);
void
addSquareToGrid
(
Square
sq
,
Grid
g
);
...
@@ -47,11 +52,14 @@ void addSquareToGrid(Square sq, Grid g);
...
@@ -47,11 +52,14 @@ void addSquareToGrid(Square sq, Grid g);
int
randInt
(
int
lower
,
int
upper
);
int
randInt
(
int
lower
,
int
upper
);
Square
getSquare
(
char
*
line
,
int
index
);
Square
getSquare
(
char
*
line
,
int
index
);
Piece
getPieceFromLine
(
char
*
line
);
Piece
getPieceFromLine
(
char
*
line
);
Piece
*
getPiecesData
();
Piece
*
getPiecesData
(
char
*
fileName
);
Piece
generePiece
(
Piece
*
pieceList
);
Piece
generePiece
(
Piece
*
pieceList
);
void
displaySquare
(
Square
sq
,
int
pIndex
,
int
pListIndex
);
void
displaySquare
(
Square
sq
,
int
pIndex
,
int
pListIndex
);
void
displayPiece
(
Piece
p
,
int
pListIndex
);
void
displayPiece
(
Piece
p
,
int
pListIndex
);
void
displayPieceList
(
Piece
*
pList
,
int
size
);
void
displayPieceList
(
Piece
*
pList
,
int
size
);
void
gameInstructions
(
Grid
grid
,
Piece
*
pList
);
void
moveSquareVert
(
Square
sq
,
int
shift
);
\ No newline at end of file
void
movePieceVert
(
Piece
piece
,
int
shift
);
void
moveSquareHztl
(
Square
sq
,
int
shift
);
void
movePieceHztl
(
Piece
p
,
int
shift
);
\ No newline at end of file
tetris.data
View file @
24e6e5ff
7
7
(0,0)(1,0)(2,0)(3,0)
(0,0)(1,0)(2,0)(3,0)
(
0,0)(1,0)(0,1)(1
,1)
(
1,0)(2,0)(1,1)(2
,1)
(0,0)(1,0)(2,0)(1,1)
(0,0)(1,0)(2,0)(1,1)
(0,0)(1,0)(2,0)(0,1)
(0,0)(1,0)(2,0)(0,1)
(0,0)(1,0)(2,0)(2,1)
(0,0)(1,0)(2,0)(2,1)
...
...
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