Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
labirintoo.exe
nbproject/
95 changes: 95 additions & 0 deletions Labirintilt.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
#ifndef LABIRINTILT_H
#define LABIRINTILT_H

#include <stdlib.h>

#include "Player.h"
#include "Scenario.h"

#define STAGES 9

typedef struct {
int level;
Player player;
Traffic traffics[4];
} Game;

void new_Game(Game * game)
{
game->level = 1;
new_Player(&(game->player));
int i;

for (i = 0; i < STAGES / 2; i ++) {
new_Traffic(&(game->traffics[i]));
}
}

void describe_scenario(Game game)
{
int i;
for (i = 3; i >=0; i--) {
printf("stage: %d ", i);
describe_traffic(game.traffics[i]);
printf("\n\n");
}
}

void move_scenario(Game * game, int speed_min)
{
int i,j;
Space * space;
Traffic traffic;
for (i = 0; i < STAGES / 2; i ++) {
if (game->traffics[i].speed < speed_min) {
continue;
}
traffic = game->traffics[i];
for (space = traffic.first_space, j =0; j < traffic.num_spaces; j ++ , space = space->next) {
space->x_start++;
space->x_final++;
if (space->x_start > 120) {
srand(time(NULL));
space->x_final = 0;
space->x_start = -15;
rotateTraffic(&(game->traffics[i]), space);
}
}
}
speed_min = (speed_min++) % 5;
}

int colision(Game game) {
int stage = (game.player.y) / 5;
// printf("\nstage: %d\n",stage);
if (stage % 2 == 0) {
return 0;
}
Traffic traffic = game.traffics[stage / 2];
Space * space;
int count;
for (space = traffic.first_space, count = 0;count < traffic.num_spaces; count++, space = space->next) {
if ((game.player.x >= space->x_start) && (game.player.x + 5 <= space->x_final)) {
return 0;
}
}
return 1;
}

int survive(Game * game) {
return die(&game->player);
}

void next_level(Game * game) {
game->level ++;
game->player.lives ++;
describe_player(game->player);
reset_position_player(&game->player);
}

int win(Game game) {
int stage = game.player.y / 5 + 1;
return stage == STAGES;
}

#endif /* LABIRINTILT_H */
89 changes: 89 additions & 0 deletions Player.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
#ifndef PLAYER_H
#define PLAYER_H


#define PLAYER_STEP 5

typedef struct {
int x; // posição horizontal do jogador
int y; // posição vertical do jogador
int lives; // quantidade de vidas restantes
} Player;


typedef int (*FunctionMove) (Player * player);

// Inicia
void new_Player(Player *);

// Coloca as coordenadas iniciais do jogador
int reset_position_player(Player *);

// Diminui a quantidade de vidas e retorna a quantidade restante
int die(Player * player);

void new_Player(Player * player)
{
player->lives = 3;
reset_position_player(player);
}

int reset_position_player(Player * player)
{
player->x = 60;
player->y = 0;
}

int die(Player * player)
{
reset_position_player(player);
return --player->lives;
}



int move_player_up(Player * player) {
if (player->y > 35) {
return 0;
}
player->y = player->y + PLAYER_STEP;
}

int move_player_down(Player * player) {
if (player->y <= 0) {
return 0;
}
player->y = player->y - PLAYER_STEP;
}

int move_player_left(Player * player) {
if (player->x <= 0) {
return 0;
}
player->x = player->x - PLAYER_STEP;
}

int move_player_right(Player * player) {
if (player->x >= 115) {
return 0;
}
player->x = player->x + PLAYER_STEP;
}

void move_player(int key, Player * player) {
FunctionMove function_move[4] = {
move_player_left,
move_player_up,
move_player_right,
move_player_down
};

(*(function_move[key]))(player);
}

void describe_player(Player player)
{
printf("---PLAYER----\nlives: %d\nx: %d\ny: %d\n\n", player.lives, player.x, player.y);
}

#endif /* PLAYER_H */
Binary file added Player.h.gch
Binary file not shown.
89 changes: 89 additions & 0 deletions Scenario.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
#ifndef SCENARIO_H
#define SCENARIO_H

typedef struct S_space{
int x_start;
int x_final;
struct S_space * next;
} Space;

void new_Space(Space * space, int x1);

void new_Space(Space * space, int x1)
{
space->x_start = x1;
space->x_final = x1 +15;
space->next = NULL;
}

typedef struct {
int num_spaces;
int speed;
Space * first_space;
} Traffic;

void add_Space(Traffic * traffic, Space * space)
{

if (!traffic->first_space) {
traffic->first_space = space;
space->next = space;
return;
}
Space * last = traffic->first_space;
while(last->next != traffic->first_space) {
last = last->next;
}
last->next = space;
space->next = traffic->first_space;
}

void rotateTraffic(Traffic * traffic, Space * space)
{
traffic->first_space = space;
}


void new_Traffic(Traffic * traffic)
{
int num_spaces = 1 + rand() % 3;
int band = 120/num_spaces;
int last_x = 0;
int i;
Space * space;

traffic->num_spaces = num_spaces;
traffic->speed = 1 + rand() % 5;
traffic->first_space = NULL;

for (i = 0; i < traffic->num_spaces; i++) {
last_x += last_x + 15 + rand() % band;
space = malloc(sizeof(Space));
new_Space(space, last_x);
add_Space(traffic, space);
}
}


void describe_space(Space space) {
printf("%d <--> %d", space.x_start, space.x_final);
}


void describe_traffic(Traffic traffic)
{
printf("speed: %d\n", traffic.speed);
Space * space;
int i;

for (i =0, space = traffic.first_space; i < traffic.num_spaces; i++, space= space->next){
describe_space(*space);
printf(" ");
}
printf("\n");
}



#endif /* SCENARIO_H */

45 changes: 45 additions & 0 deletions assertions.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#ifndef ASSERTIONS_H
#define ASSERTIONS_H

void assertTrue(int var) {
if (!var) {
printf("esperado: true, encontrado: %d", var);
exit(1);
}
printf(".");
}

void assertFalse(int var) {
if (var) {
printf("esperado: false, encontrado: %d", var);
exit(1);
}
printf(".");
}

void assertEquals(int expected, int value) {
if (value != expected) {
printf("esperado: %d, encontrado: %d", expected, value);
exit(1);
}
printf(".");
}

void assertGreaterThan(int expected, int value) {
if (value <= expected) {
printf(", esperado maior que %d, encontrado: %d", expected, value);
exit(1);
}
printf(".");
}

void assertLessThan(int expected, int value) {
if (value >= expected) {
printf(", esperado menor que %d, encontrado: %d", expected, value);
exit(1);
}
printf(".");
}

#endif /* ASSERTIONS_H */

Loading