Commit 6e78304a authored by Yoann Bordin's avatar Yoann Bordin

Edited struct, added functions

parent 76f29de8
#include "tetris-struct.h" #include "tetris-struct.h"
#include "raylib.h"
#include <stdio.h>
int main(){ int main(){
const int screenWidth = 1280;
const int screenHeight = 720;
InitWindow(screenWidth, screenHeight, "Tetris");
SetTargetFPS(60);
while(!WindowShouldClose()){
BeginDrawing();
ClearBackground(RAYWHITE);
EndDrawing();
}
CloseWindow();
Grid grid = gridInit();
while(!gameOver(grid)){
Piece piece = pieceInit();
while(canMove(piece, grid)){
move(piece);
}
addPieceToGrid(piece, grid);
}
} }
\ No newline at end of file
...@@ -4,43 +4,94 @@ ...@@ -4,43 +4,94 @@
Grid gridInit(){ Grid gridInit(){
Grid g; Grid g;
g.matrix = 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++){
g.matrix[i] = calloc(g.height, sizeof(Square)); g.grid[i] = calloc(g.height, sizeof(Square));
} }
g.matrix = squareMatrixInit(g.matrix, g.width, g.height);
return g; return g;
} }
Square squareInit(){ Piece pieceInit(){
Square sq; Piece p;
sq.isFilled = false; p.squares = calloc(p.size, sizeof(Square));
return sq; return p;
} }
Square** squareMatrixInit(Square** matrix, int width, int height){ bool gameOver(Grid grid){
for(int i = 0; i < width; i++){ return height(grid) == grid.height;
for(int j = 0; j < height; j++){ }
matrix[i][j] = squareInit();
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 matrix;
} }
Piece pieceInit(){ int heightColumn(Square* column, int size){
Piece p; int height = 0;
p.matrix = calloc(p.size, sizeof(Square*)); int y;
for(int i = 0; i < p.size; i++){ for(int i = 0; i < size; i++){
p.matrix[i] = calloc(p.size, sizeof(Square)); if(column[i] != NULL){
y = column[i]->position[1];
if(y+1 > height){
height = y+1;
}
} }
p.matrix = squareMatrixInit(p.matrix, p.size, p.size); }
return p; return height;
} }
bool canMove(Piece piece, Grid grid){
int x, y;
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->position[0];
int y = sq->position[1];
return (y != 0 || y == heightColumn(g.grid[x], g.height));
}
void move(Piece piece){
for(int i = 0; i < piece.size; i++){
moveSquare(piece.squares[i]);
}
}
void moveSquare(Square sq){
sq->position[1] --;
}
void addPieceToGrid(Piece piece, Grid grid){
for(int i = 0; i < piece.size; i++){
addSquareToGrid(piece.squares[i], grid);
}
}
void addSquareToGrid(Square sq, Grid g){
int x = sq->position[0];
int y = sq->position[1];
g.grid[x][y] = sq;
}
\ No newline at end of file
#include <stdbool.h> #include <stdbool.h>
typedef struct Square{ typedef struct square_s{
int size; int* position;
bool isFilled; int coordSize;
} Square; int color;
typedef struct Grid } square_s;
{ typedef square_s* Square;
int height = 20;
int width = 10;
Square** matrix; typedef struct Grid{
} Grid; Square** grid;
typedef struct Piece int width;
{ int height;
int size = 4; } Grid;
Square** matrix; typedef struct Piece{
Square* squares;
int size;
} Piece; } Piece;
Grid gridInit(); Grid gridInit();
Square squareInit(); Grid initSquares(Grid g);
Square** squareMatrixInit(Square** matrix, int width, int height);
Piece pieceInit(); Piece pieceInit();
void down(); bool gameOver(Grid grid);
void move(); int height(Grid g);
void rotate(); int heightColumn(Square* column, int size);
\ No newline at end of file
bool canMove(Piece piece, Grid grid);
bool canMoveSquare(Square sq, Grid g);
void move(Piece piece);
void moveSquare(Square sq);
void addPieceToGrid(Piece piece, Grid grid);
void addSquareToGrid(Square sq, Grid g);
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment