Commit 24e6e5ff authored by Yoann Bordin's avatar Yoann Bordin

Added basic rules

parent 60a52c28
......@@ -7,15 +7,24 @@ void InitGame(){
void InitDisplay(){
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);
DrawLine(BORDER_GAP + GRID_WIDTH, BORDER_GAP + GRID_HEIGHT, BORDER_GAP + GRID_WIDTH, BORDER_GAP, RAYWHITE);
const int posY = BORDER_GAP+GRID_PHEIGHT;
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){
DrawRectangle(sq->posX*SQUARE_SIZE, sq->posY*SQUARE_SIZE,
SQUARE_SIZE, SQUARE_SIZE,
if(sq != NULL){
DrawRectangle(BORDER_GAP + sq->posX*SQUARE_SIZE,
BORDER_GAP + sq->posY*SQUARE_SIZE,
SQUARE_SIZE,
SQUARE_SIZE,
sq->color);
}
}
void drawPiece(Piece p){
......@@ -23,3 +32,23 @@ void drawPiece(Piece p){
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
......@@ -5,11 +5,15 @@
#define BORDER_GAP 20
#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 InitGame();
void drawSquare(Square sq);
void drawPiece(Piece p);
void displayGrid(Grid g);
void gameInstructions(Grid grid, Piece* pList);
\ No newline at end of file
#include "tetris-graph.h"
#include <stdio.h>
int main(void){
InitGame();
Piece* pieceList = getPiecesData();
Piece* pieceList = getPiecesData("tetris.data");
Grid grid = gridInit();
Piece p = pieceInit();
bool isFixed = true;
while(!WindowShouldClose()){
BeginDrawing();
int frameCounter = 0;
while(!WindowShouldClose() && !gameOver(grid)){
BeginDrawing();
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);
}
// Nouvelles pièces
if(isFixed){
p = generePiece(pieceList);
movePieceHztl(p, 3);
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;
}
}
/* Grid grid = gridInit();
while(!gameOver(grid)){
gameInstructions(grid);
} */
Piece p = generePiece(pieceList);
drawPiece(p);
displayGrid(grid);
frameCounter++;
EndDrawing();
}
EndDrawing();
CloseWindow();
return 0;
return EXIT_SUCCESS;
}
\ No newline at end of file
No preview for this file type
......@@ -2,6 +2,9 @@
Grid gridInit(){
Grid g;
g.width = GRID_WIDTH;
g.height = GRID_HEIGHT;
g.grid = calloc(g.width, sizeof(Square*));
for(int i = 0; i < g.width; i++){
......@@ -13,19 +16,35 @@ Grid gridInit(){
Piece pieceInit(){
Piece p;
p.squares = calloc(p.size, sizeof(Square));
p.size = PIECE_SIZE;
p.squares = calloc(p.size, sizeof(Square));
return p;
}
Square squareInit(){
Square sq = malloc(sizeof(Square));
Square sq = malloc(sizeof(square_s));
sq->color = RAYWHITE;
sq->posX = 0;
sq->posY = 0;
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;
}
......@@ -41,28 +60,19 @@ int height(Grid g){
height = height_p;
}
}
return height;
}
int heightColumn(Square* column, int size){
int height = 0;
int y;
for(int i = 0; i < size; i++){
if(column[i] != NULL){
y = column[i]->posY;
int height = size;
if(y+1 > height){
height = y+1;
while(height > 0 && column[size-height] == NULL){
height--;
}
}
}
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)){
......@@ -76,17 +86,48 @@ bool canMoveSquare(Square sq, Grid g){
int x = sq->posX;
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){
for(int i = 0; i < piece.size; i++){
moveSquare(piece.squares[i]);
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;
sq->posY = refY + posX;
}
else{
sq->posX = refX + posY;
sq->posY = refY - posX;
}
}
void moveSquare(Square sq){
sq->posY --;
void rotatePiece(Piece p, bool rotation){
//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){
......@@ -96,10 +137,9 @@ void addPieceToGrid(Piece piece, Grid grid){
}
void addSquareToGrid(Square sq, Grid g){
int x = sq->posX;
int y = sq->posY;
Square newSq = copySquare(sq);
g.grid[x][y] = sq;
g.grid[newSq->posX][newSq->posY] = newSq;
}
int randInt(int lower, int upper){
......@@ -152,46 +192,58 @@ Piece getPieceFromLine(char* line){
return p;
}
Piece* getPiecesData(){
Piece* pieceList;
Piece* getPiecesData(char* fileName){
FILE* dataFile = NULL;
unsigned int numPieces;
char* fileName = "tetris.data";
FILE* dataFile;
dataFile = fopen(fileName, "r");
if(dataFile == NULL){
exit(EXIT_FAILURE);
}
unsigned int pieceNumber;
fscanf(dataFile, "%u\n", &pieceNumber);
// Initialize number of pieces
fscanf(dataFile, "%u\n", &numPieces);
char* line = calloc(22, sizeof(char));
pieceList = calloc(pieceNumber, sizeof(Piece));
for(int i = 0; i < pieceNumber; i++){
// Initialize piecesList
char* line = calloc(30, sizeof(char));
Piece* pieceList = calloc(numPieces, sizeof(Piece));
for(int i = 0; i < numPieces; i++){
pieceList[i] = pieceInit();
fgets(line, 22, dataFile);
}
for(int i = 0; i < numPieces; i++){
fgets(line, 30, dataFile);
pieceList[i] = getPieceFromLine(line);
setColor(pieceList[i], i);
}
free(line);
return pieceList;
}
Piece generePiece(Piece* pieceList){
int numPiece = randInt(0, 6);
DrawText("test", 20, 20, 20, RED);
Piece p = pieceList[numPiece];
Square copySquare(Square sq){
Square newSq = squareInit();
return p;
newSq->color = sq->color;
newSq->posX = sq->posX;
newSq->posY = sq->posY;
return newSq;
}
void gameInstructions(Grid grid, Piece* pList){
Piece piece = generePiece(pList);
Piece generePiece(Piece* pieceList){
int numPiece = randInt(0, 6);
drawPiece(piece);
Piece pieceFromList = pieceList[numPiece];
Piece newPiece = pieceInit();
while(canMove(piece, grid)){
move(piece);
for(int i = 0; i < newPiece.size; i++){
newPiece.squares[i] = copySquare(pieceFromList.squares[i]);
}
addPieceToGrid(piece, grid);
return newPiece;
}
void displaySquare(Square sq, int pIndex, int pListIndex){
......@@ -216,3 +268,26 @@ void displayPieceList(Piece* pList, int size){
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
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "raylib.h"
#define PIECE_SIZE 4;
#define GRID_WIDTH 10;
#define GRID_HEIGHT 20;
typedef struct square_s{
int posX;
......@@ -31,6 +34,8 @@ Grid gridInit();
Grid initSquares(Grid g);
Piece pieceInit();
Square copySquare(Square sq);
bool gameOver(Grid grid);
int height(Grid g);
int heightColumn(Square* column, int size);
......@@ -38,8 +43,8 @@ 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 rotateSquare(Square sq, Square ref, bool rotation);
void rotatePiece(Piece p, bool rotation); // False makes a counterclockwise rotation and true makes a clockwise rotation
void addPieceToGrid(Piece piece, Grid grid);
void addSquareToGrid(Square sq, Grid g);
......@@ -47,11 +52,14 @@ void addSquareToGrid(Square sq, Grid g);
int randInt(int lower, int upper);
Square getSquare(char* line, int index);
Piece getPieceFromLine(char* line);
Piece* getPiecesData();
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);
void gameInstructions(Grid grid, Piece* pList);
\ No newline at end of file
void moveSquareVert(Square sq, int shift);
void movePieceVert(Piece piece, int shift);
void moveSquareHztl(Square sq, int shift);
void movePieceHztl(Piece p, int shift);
\ No newline at end of file
7
(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)(0,1)
(0,0)(1,0)(2,0)(2,1)
......
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