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

Edited struct, added functions

parent 76f29de8
#include "tetris-struct.h"
#include "raylib.h"
#include <stdio.h>
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 @@
Grid gridInit(){
Grid g;
g.matrix = calloc(g.width, sizeof(Square*));
g.grid = calloc(g.width, sizeof(Square*));
for(int i = 0; i < g.width; i++){
g.matrix[i] = calloc(g.height, sizeof(Square));
g.grid[i] = calloc(g.height, sizeof(Square));
}
return g;
}
g.matrix = squareMatrixInit(g.matrix, g.width, g.height);
Piece pieceInit(){
Piece p;
p.squares = calloc(p.size, sizeof(Square));
return g;
return p;
}
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;
}
}
}
Square squareInit(){
Square sq;
sq.isFilled = false;
int heightColumn(Square* column, int size){
int height = 0;
int y;
return sq;
for(int i = 0; i < size; i++){
if(column[i] != NULL){
y = column[i]->position[1];
if(y+1 > height){
height = y+1;
}
}
}
return height;
}
Square** squareMatrixInit(Square** matrix, int width, int height){
for(int i = 0; i < width; i++){
for(int j = 0; j < height; j++){
matrix[i][j] = squareInit();
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 matrix;
return true;
}
Piece pieceInit(){
Piece p;
p.matrix = calloc(p.size, sizeof(Square*));
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));
}
for(int i = 0; i < p.size; i++){
p.matrix[i] = calloc(p.size, sizeof(Square));
void move(Piece piece){
for(int i = 0; i < piece.size; i++){
moveSquare(piece.squares[i]);
}
}
p.matrix = squareMatrixInit(p.matrix, p.size, p.size);
void moveSquare(Square sq){
sq->position[1] --;
}
return p;
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>
typedef struct Square{
int size;
bool isFilled;
typedef struct square_s{
int* position;
int coordSize;
} Square;
int color;
typedef struct Grid
{
int height = 20;
int width = 10;
} square_s;
typedef square_s* Square;
Square** matrix;
} Grid;
typedef struct Grid{
Square** grid;
typedef struct Piece
{
int size = 4;
int width;
int height;
} Grid;
Square** matrix;
typedef struct Piece{
Square* squares;
int size;
} Piece;
Grid gridInit();
Square squareInit();
Square** squareMatrixInit(Square** matrix, int width, int height);
Grid initSquares(Grid g);
Piece pieceInit();
void down();
void move();
void rotate();
\ No newline at end of file
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 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