From 9a1ac1837749009ee12c112ba6b45aeee55356d0 Mon Sep 17 00:00:00 2001 From: Vytor Calixto <vytorcalixto@gmail.com> Date: Sat, 22 Apr 2017 17:52:19 -0300 Subject: [PATCH] Criado tabuleiro do jogo e feita a leitura --- Makefile | 13 +++++++++++-- board.c | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ board.h | 15 +++++++++++++++ main.c | 11 ++++++++++- 4 files changed, 91 insertions(+), 3 deletions(-) create mode 100644 board.c create mode 100644 board.h diff --git a/Makefile b/Makefile index a630cdc..1cf6aca 100644 --- a/Makefile +++ b/Makefile @@ -1,2 +1,11 @@ -all: - gcc main.c -o main +CFLAGS = -std=c99 -O2 -W -Wall -g + +.PHONY: all clean + +all: main + +main: main.c board.o + $(CC) $(CFLAGS) -o $@ $^ + +clean: + $(RM) *.o diff --git a/board.c b/board.c new file mode 100644 index 0000000..0dfb7d0 --- /dev/null +++ b/board.c @@ -0,0 +1,55 @@ +#include <stdio.h> +#include <stdlib.h> +#include <stdbool.h> +#include "board.h" + +struct Cell { + // Cor da célula + int color; + // Se a célula já foi visitada + bool visited; +}; + +struct Board { + int x, y, colors; + Cell *cells; +}; + +Board createBoard() { + Board b = malloc(sizeof(struct Board)); + if(!b) return NULL; + b->x = b->y = b->colors = 0; + return b; +} + +int readBoard(Board b) { + int x, y, colors; + scanf("%d %d %d", &x, &y, &colors); + b->x = x; + b->y = y; + b->colors = colors; + // Aloca um único array em vez de um "array de arrays" + b->cells = malloc(sizeof(struct Cell) * x * y); + if(b->cells == NULL) return 0; + for(int i=0; i < x; ++i) + for(int j=0; j < y; ++j) { + // Para acessar uma matrix [X][Y] em um vetor fazemos i*Y + j + Cell c = malloc(sizeof(struct Cell)); + c->visited = false; + scanf("%d", &(c->color)); + b->cells[i * y + j] = c; + } + return 1; +} + +void printBoard(Board b) { + printf("%d ", b->x); + printf("%d ", b->y); + printf("%d\n", b->colors); + for(int i=0; i < b->x; ++i) { + for(int j=0; j < b->y; ++j) + printf("%d ", b->cells[i * b->y + j]->color); + puts(""); + } + return; +} diff --git a/board.h b/board.h new file mode 100644 index 0000000..e8556cb --- /dev/null +++ b/board.h @@ -0,0 +1,15 @@ +#ifndef _BOARD_ +#define _BOARD_ +#include <stdbool.h> + +typedef struct Cell *Cell; + +typedef struct Board *Board; + +Board createBoard(); + +int readBoard(Board b); + +void printBoard(Board b); + +#endif diff --git a/main.c b/main.c index fbfae3d..085bac5 100644 --- a/main.c +++ b/main.c @@ -1,8 +1,17 @@ +#include <stdio.h> #include <stdlib.h> #include <stdio.h> #include <time.h> +#include "board.h" + +int main() { + Board b = createBoard(); + if(!readBoard(b)) { + puts("Erro na leitura do tabuleiro"); + return -1; + } + printBoard(b); -int main(int argc, char const *argv[]) { printf("Ler o tabuleiro (em matriz?)\n"); printf("Enquanto pilha não vazia:\n"); printf("\tPega os adjacentes do grupo (retorna os pesos dos filhos = montar o grafo)\n"); -- GitLab