Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Universidade de Brasilia
- Instituto de Ciencias Exatas
- Departamento de Ciencia da Computacao
- Algoritmos e Programacao de Computadores 2/2018
- Aluno(a): Murilo Ferreira Pires
- Matricula: 180128361
- Turma: A
- Versao do compilador: gcc version 7.3.1 20180323
- Descricao: Um programa que tem como objetivo implementar um jogo em um grid 2D representado por uma matriz similar ao jogo Puyo Puyo,
- porem em versao DIY (Do it Yourself, ou seja, Faca voce mesmo). O jogo possui diversos tijolos aleatorios, que cairao de posicoes
- randomicas a partir do topo do tabuleiro. A partir do momento em que o tijolo esta caindo, o jogador pode controla-lo horizontalmente,
- inverter a ordem das pecas, vira-los em posicao vertical e faze-los descer rapido. O jogador tem como objetivo conseguir o maior numero
- de pontos antes do fim do jogo, que acaba no momento em que os tijolos alcancam o topo do grid.
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- #include <unistd.h>
- #include <fcntl.h>
- #define GREEN "\x1b[32m"
- #define YELLOW "\x1b[33m"
- #define BLUE "\x1b[34m"
- #define MAGENTA "\x1b[35m"
- #define CYAN "\x1b[36m"
- #define RESET "\x1b[0m"
- #define RED "\x1b[31m"
- /* o codigo abaixo inclui as funcoes kbhit e getch se o sistema operacional nao for windows e define um macro para limpar a tela independente do sistema operacional */
- #ifdef _WIN32
- #define CLEAR "cls"
- #else
- #define CLEAR "clear"
- #endif
- #ifndef _WIN32
- #include <termios.h>
- int kbhit(){
- struct termios oldt, newt;
- int ch, oldf;
- tcgetattr(STDIN_FILENO,&oldt);
- newt = oldt;
- newt.c_lflag &= ~(ICANON | ECHO);
- tcsetattr(STDIN_FILENO, TCSANOW, &newt);
- oldf = fcntl(STDIN_FILENO, F_GETFL, 0);
- fcntl(STDIN_FILENO, F_SETFL, oldf | O_NONBLOCK);
- ch = getchar();
- tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
- fcntl(STDIN_FILENO, F_SETFL, oldf);
- if(ch != EOF){
- ungetc(ch,stdin);
- return 1;
- }
- return 0;
- }
- int getch(void) {
- int ch;
- struct termios oldt;
- struct termios newt;
- tcgetattr(STDIN_FILENO,&oldt);
- newt = oldt;
- newt.c_lflag &= ~(ICANON | ECHO);
- tcsetattr(STDIN_FILENO, TCSANOW, &newt);
- ch = getchar();
- tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
- return ch;
- }
- #else
- #include <conio.h>
- #endif
- /* Variaveis globais */
- int al = 13; /* altura */
- int la = 8; /* largura */
- int x1 = 1, y1 = 0, x2 = 2, y2 = 0; /* cordenadas das pecas 1 e 2 */
- char tabu[100][100]; /* tabuleiro */
- int state = 1; /* tipo da peca (muda quando gira) */
- int pg = 1; /* variavel pra indicar se pode girar */
- char p1, p2; /* pecas 1 e 2 */
- int velocidade = 720000;
- int pontos;
- int combo;
- int qtdp = 5;
- int pontuacao = 4;
- int replay = 0; /* define se o replay esta ativado */
- char nomereplay[100]; /* nome do replay a ser lido/escrito */
- char preplay[2001]; /* string com todas as pecas do replay (maximo de 2000) */
- int creplay = 0; /* contador auxiliar para identificar a peca a ser lida na string do replay */
- int tamreplay = 0; /* numero de pecas salvas no replay */
- int gravar = 0; /* grava ou nao o jogo */
- /* funcao para a tela inicial */
- int inicial(){
- printf (CYAN ".########..##.....##.##....##..#######.....########..##.....##.##....##..#######.\n.##.....##.##.....##..##..##..##.....##....##.....##.##.....##..##..##..##.....##\n.##.....##.##.....##...####...##.....##....##.....##.##.....##...####...##.....##\n.########..##.....##....##....##.....##....########..##.....##....##....##.....##\n.##........##.....##....##....##.....##....##........##.....##....##....##.....##\n.##........##.....##....##....##.....##....##........##.....##....##....##.....##\n.##.........#######.....##.....#######.....##.........#######.....##.....#######." RESET "\n\n\n\n\n Pressione <enter> para jogar!");
- getch();
- return 1;
- }
- /*funcao para criar as pecas compostas */
- void criachar(){
- if (replay == 1 && creplay < tamreplay){
- p1 = preplay[creplay];
- creplay++;
- p2 = preplay[creplay];
- creplay++;
- }
- else{
- p1 = 'A' + rand() % qtdp;
- p2 = 'A' + rand() % qtdp;
- if (gravar == 1){
- FILE*df;
- df = fopen(nomereplay, "a");
- fprintf(df,"%c%c",p1,p2);
- fclose(df);
- }
- }
- }
- /* zerar o tabuleiro com # e ' ' */
- void zeratabu(void){
- int x;
- int y;
- for (y=0; y < al; y++){
- for(x=0; x < la; x++){
- if ((y == al-1) || (x == la-1) || (x == 0)){
- tabu[x][y] = '#';
- }
- else{
- tabu[x][y] = ' ';
- }
- }
- }
- }
- /* atualiza a tela */
- void attframe(void){
- system(CLEAR);
- int x, y;
- for (y = 0; y < al; y++){
- for (x=0; x < la; x++){
- if (tabu[x][y] == 'A'){
- printf(GREEN "%c" RESET, tabu[x][y]);
- }
- else if (tabu[x][y] == 'B'){
- printf(YELLOW "%c" RESET, tabu[x][y]);
- }
- else if (tabu[x][y] == 'C'){
- printf(BLUE "%c" RESET, tabu[x][y]);
- }
- else if (tabu[x][y] == 'D'){
- printf(MAGENTA "%c" RESET, tabu[x][y]);
- }
- else if (tabu[x][y] == 'E'){
- printf(CYAN "%c" RESET, tabu[x][y]);
- }
- else if (tabu[x][y] == '*'){
- printf(RED "%c" RESET, tabu[x][y]);
- }
- else {printf("%c", tabu[x][y]);
- }
- }
- printf("\n");
- }
- printf("\nPontos: %d \n", pontos);
- }
- /* salva as pecas no tabuleiro */
- void saveframe(int x){
- if (x == 1 && p1 != '0'){
- tabu[x1][y1] = p1;
- }
- if (x == 2 && p2 != '0'){
- tabu[x2][y2] = p2;
- }
- if (x == 3 && p1 != '0' && p2 != '0'){
- tabu[x1][y1] = p1;
- tabu[x2][y2] = p2;
- }
- }
- /* funcao que gira as pecas */
- void girar(){
- if (pg == 0){
- return;
- }
- if (state == 1 && tabu[x2][y2+1] == ' '){
- tabu[x1][y1] = ' ';
- tabu[x2][y2] = p1;
- x1 = x2;
- y2 = y1 + 1;
- saveframe(3);
- attframe();
- state++;
- return;
- }
- if (state == 2 && tabu[x1-1][y1] == ' ' && tabu[x2+1][y2] == ' '){
- tabu[x2][y2] = ' ';
- x2--;
- y2--;
- saveframe(2);
- attframe();
- state++;
- return;
- }
- if (state == 3 && tabu[x1][y1+1] == ' '){
- tabu[x2][y2] = ' ';
- x2++;
- y1++;
- saveframe(3);
- attframe();
- state++;
- return;
- }
- if(state == 4 && tabu[x2-1][y2] == ' '){
- tabu[x1][y1] = ' ';
- x1--;
- y1--;
- saveframe(1);
- attframe();
- state = 1;
- return;
- }
- return;
- }
- /*movimento para baixo automatico */
- void smove(void){
- if (p1 == '0'){
- tabu[x2][y2] = ' ';
- y2++;
- saveframe(2);
- attframe();
- return;
- }
- if (p2 == '0'){
- tabu[x1][y1] = ' ';
- y1++;
- saveframe(1);
- attframe();
- return;
- }
- tabu[x1][y1] = ' ';
- tabu[x2][y2] = ' ';
- y2++;
- y1++;
- saveframe(3);
- attframe();
- }
- /* deleta as pecas marcadas com * e desce as que tao em cima */
- void score(){
- int x, y;
- for (y=0; y < al; y++){
- for(x=0; x < la; x++){
- if(tabu[x][y] == '*'){
- if (combo > 1){
- pontos = pontos + combo;
- }
- else{
- pontos++;
- }
- tabu[x][y] = tabu[x][y-1];
- tabu[x][y-1] = ' ';
- x = 0;
- y = 0;
- }
- while (tabu[x][y+1] == ' ' && tabu[x][y] != ' ' && tabu[x][y] != '#'){
- tabu[x][y+1] = tabu[x][y];
- tabu[x][y] = ' ';
- }
- }
- }
- }
- /* checa se da pra fazer ponto e substitui as pecas que fazem por * */
- int check(){
- /*essa funcao utiliza uma funcao auxiliar para identificar quais pecas ja foram testadas*/
- char aux[la][al];
- int x, y, c = 1, i = 0, k = 0, j = 0, e = 0, a = 0, b = 0;
- /*dentro do for, eh construida uma cadeia de celulas que estao ligadas, utilizando um vetor de struct para salvar as coordenadas das celulas
- se a cadeia for maior que 4, as celulas sao substituidas por * */
- typedef struct {
- int x;
- int y;
- } coord;
- coord pecas[1000];
- for (a=0; a < al; a++){
- for(b=0; b < la; b++){
- aux[b][a] = '0';
- }
- }
- for (x = 1; x < la - 1; x++){
- for (y=0; y < al - 1; y++){
- if(tabu[x][y] != ' ' && tabu[x][y] != '*' && tabu[x][y] != '#'){
- if ( (tabu[x][y] == tabu[x+1][y] && aux[x+1][y] == '0') || (tabu[x][y] == tabu[x-1][y] && aux[x-1][y] == '0')
- || (tabu[x][y] == tabu[x][y+1] && aux[x][y+1] == '0') || (y != 0 && tabu[x][y] == tabu[x][y-1] && aux[x][y-1] == '0') ){
- pecas[i].x = x;
- pecas[i].y = y;
- for(j = 0; j <= i && aux[pecas[j].x][pecas[j].y] != '1';j++){
- if(tabu[pecas[j].x][pecas[j].y] == tabu[pecas[j].x+1][pecas[j].y] && aux[pecas[j].x+1][pecas[j].y] != '1'){
- aux[pecas[j].x][pecas[j].y] = '1';
- i++;
- pecas[i].x = pecas[j].x + 1;
- pecas[i].y = pecas[j].y;
- c++;
- }
- if(tabu[pecas[j].x][pecas[j].y] == tabu[pecas[j].x-1][pecas[j].y] && aux[pecas[j].x-1][pecas[j].y] != '1'){
- aux[pecas[j].x][pecas[j].y] = '1';
- i++;
- pecas[i].x = pecas[j].x - 1;
- pecas[i].y = pecas[j].y;
- c++;
- }
- if(tabu[pecas[j].x][pecas[j].y] == tabu[pecas[j].x][pecas[j].y+1] && aux[pecas[j].x][pecas[j].y+1] != '1'){
- aux[pecas[j].x][pecas[j].y] = '1';
- i++;
- pecas[i].x = pecas[j].x;
- pecas[i].y = pecas[j].y + 1;
- c++;
- }
- if(y != 0 && tabu[pecas[j].x][pecas[j].y] == tabu[pecas[j].x][pecas[j].y-1] && aux[pecas[j].x][pecas[j].y-1] != '1'){
- aux[pecas[j].x][pecas[j].y] = '1';
- i++;
- pecas[i].x = pecas[j].x;
- pecas[i].y = pecas[j].y - 1;
- c++;
- }
- aux[pecas[j].x][pecas[j].y] = '1';
- }
- }
- if (c >= pontuacao){
- combo++;
- for (k=0; k <= i; k++){
- tabu[pecas[k].x][pecas[k].y] = '*';
- }
- attframe();
- score();
- e++;
- }
- i = 0;
- c = 1;
- }
- }
- }
- if (e == 0){
- return 0;
- }
- return 1;
- }
- /* ve se alguma das pecas colidiu com alguma coisa */
- int colisao(){
- if (p2 != '0' && p1 != '0'){
- if (state == 1 || state == 3){
- if (tabu[x1][y1+1] != ' ' && tabu[x2][y2+1] != ' '){
- return 3;
- }
- }
- if (state == 2){
- if (tabu[x2][y2+1] != ' '){
- return 3;
- }
- return 0;
- }
- if (state == 4){
- if (tabu[x1][y1+1] != ' '){
- return 3;
- }
- return 0;
- }
- }
- if (tabu[x2][y2+1] != ' ' && p2 != '0'){
- return 2;
- }
- if (tabu[x1][y1+1] != ' ' && p1 != '0'){
- return 1;
- }
- return 0;
- }
- /* move a(s) peca(s) para a esquerda */
- void mleft(void){
- if (x2 < x1 && p2 != '0' && p1 != '0'){
- if(tabu[x2-1][y2] == ' '){
- tabu[x2][y2] = ' ';
- x2--;
- saveframe(2);
- if (p1 != '0' && tabu[x1+1][y1] == ' '){
- tabu[x1][y1] = ' ';
- x1--;
- saveframe(1);
- }
- }
- }
- if (x1 < x2 && p1 != '0' && p2 != '0'){
- if(tabu[x1-1][y1] == ' '){
- tabu[x1][y1] = ' ';
- x1--;
- saveframe(1);
- if (p2 != '0' && tabu[x2-1][y2] == ' '){
- tabu[x2][y2] = ' ';
- x2--;
- saveframe(2);
- }
- }
- }
- if (x2 == x1 && p1 != '0' && p2 != '0'){
- if(tabu[x2-1][y2] == ' ' && p2 != '0'){
- tabu[x2][y2] = ' ';
- x2--;
- saveframe(2);
- }
- if (p1 != '0' && tabu[x1-1][y1] == ' '){
- tabu[x1][y1] = ' ';
- x1--;
- saveframe(1);
- }
- }
- }
- /* move a(s) peca(s) para a direita */
- void mright(void){
- if (x2 > x1 && p2 != '0' && p1 != '0'){
- if(tabu[x2+1][y2] == ' '){
- tabu[x2][y2] = ' ';
- x2++;
- saveframe(2);
- if (p1 != '0' && tabu[x1+1][y1] == ' '){
- tabu[x1][y1] = ' ';
- x1++;
- saveframe(1);
- }
- }
- }
- if (x1 > x2 && p2 != '0' && p1 != '0'){
- if(tabu[x1+1][y1] == ' '){
- tabu[x1][y1] = ' ';
- x1 = x1 + 1;
- saveframe(1);
- if (p2 != '0' && tabu[x2+1][y2] == ' '){
- tabu[x2][y2] = ' ';
- x2 = x2 + 1;
- saveframe(2);
- }
- }
- }
- if (x2 == x1 && p2 != '0' && p1 != '0'){
- if(tabu[x2+1][y2] == ' ' && p2 != '0'){
- tabu[x2][y2] = ' ';
- x2 = x2 + 1;
- saveframe(2);
- }
- if (p1 != '0' && tabu[x1+1][y1] == ' '){
- tabu[x1][y1] = ' ';
- x1 = x1 + 1;
- saveframe(1);
- }
- }
- }
- /*inverte as 2 pecas */
- void inverter(void){
- int auxx, auxy;
- if (state == 1 || state == 2){
- state = state +2;
- }
- else if (state == 3){
- state = 1;
- }
- else if (state == 4){
- state = 2;
- }
- if(p1 != '0' && p2 != '0'){
- auxx = x1;
- auxy = y1;
- x1 = x2;
- y1 = y2;
- x2 = auxx;
- y2 = auxy;
- saveframe(3);
- attframe();
- }
- }
- /*controla as inputs do jogador */
- void acao(){
- char c;
- c = '0';
- while ((c != ' ') && (c != 'a') && (c != 's') && (c != 'd') && (c != 'w')){
- c = getch();
- }
- if(c == ' '){
- if (pg == 1){
- girar();
- attframe();
- }
- }
- if (c == 'a'){
- mleft();
- attframe();
- }
- if (c == 'd'){
- mright();
- attframe();
- }
- if (c == 's'){
- while (colisao() == 0){
- smove();
- attframe();
- }
- }
- if (c == 'w'){
- inverter();
- attframe();
- }
- }
- /*essa funcao ve se alguma peca chegou ao topo da grid */
- int gameon(){
- int x;
- for (x = 1; x < la - 1; x++){
- if (tabu[x][0] != ' ' && tabu[x][1] != ' '){
- return 0;
- }
- }
- return 1;
- }
- /* funcao que repete toda vez que uma peca nova eh liberada */
- int game(){
- attframe();
- criachar();
- x1 = 1; y1 = 0; x2 = x1 + 1; y2 = y1;
- pg = 1;
- state = 1;
- saveframe(3);
- attframe();
- while (gameon()){
- while(colisao() == 0){
- while(kbhit()){
- acao();
- saveframe(3);
- attframe();
- }
- if(colisao() == 0){
- usleep(velocidade);
- smove();
- }
- }
- if (colisao() == 3){ /* as 2 pecas colidiram */
- saveframe(3);
- p1 = '0';
- p2 = '0';
- }
- if (colisao() == 2){ /* p2 colidiu */
- pg = 0;
- saveframe(2);
- p2 = '0';
- }
- if (colisao() == 1){ /* p1 colidiu */
- pg = 0;
- saveframe(1);
- p1 = '0';
- }
- saveframe(3);
- attframe();
- while(p1 == '0' && p2 == '0' && check()){
- attframe();
- }
- combo = 0;
- if(p1 == '0' && p2 == '0' && gameon != 0){
- return 1;
- }
- }
- return 0;
- }
- /*tela de fim de jogo */
- int gameover(){
- attframe();
- usleep(720000);
- system(CLEAR);
- printf("\n\nGAME OVER! OH NAO, VOCE PERDEU!");
- printf("\n\n\n\n Aperte qualquer tecla para voltar para o menu");
- getch();
- return 1;
- }
- /* A funcao do jogo em si */
- int jogar(){
- x1 = 1;
- y1 = 1;
- x2 = x1 + 1;
- y2 = y1;
- zeratabu();
- pontos = 0;
- while(game()){}
- if (gameover()){
- return 1;
- }
- return 0; /*retornar 0 sai do jogo, retornar 1 volta pro menu */
- }
- /* funcao para instrucoes com a opcao de voltar para o menu ou sair do jogo */
- int instrucoes(){
- char c;
- c = '0';
- system(CLEAR);
- printf(" +-+-+-+-+-+-+-+-+-+-+\n |I|n|s|t|r|u|c|o|e|s|\n +-+-+-+-+-+-+-+-+-+-+\n\n\n");
- printf("Como fazer pontos?\nBasta conectar 4 ou mais pecas iguais.\n\nCOMANDOS:\nA - Movimento para a esquerda\nD - Movimento para a direita\nS - Cai a peca mais rapido\nSPACE - Gira no sentido horario\nW - Inverte as pecas.\n\nAperte 1 para voltar para o menu e 2 para sair do jogo.");
- while ((c != 49) && (c != 50)){
- c = getch();
- }
- if (c == 49){
- return 1;
- }
- return 0;
- }
- /* funcao para controlar o replay */
- int modoreplay(){
- system(CLEAR);
- FILE* fd;
- char u;
- printf("1 - Gravar o proximo jogo\n2 - Carregar o jogo a partir de um arquivo ja salvo\n3 - Voltar\n\nDigite o numero correspondente a opcao desejada: ");
- scanf("\n%c", &u);
- while ((u != '1') && (u != '2') && (u != '3')){
- printf("\nOpcao %c invalida!\n\nDigite o numero correspondente a opcao desejada: ", u);
- scanf("\n%c", &u);
- }
- if (u == '1'){
- printf("\nDigite o nome do arquivo em que o jogo sera salvo (com .txt): ");
- scanf("%s", nomereplay);
- printf("\nAs pecas do proximo jogo serao gravadas, pressione qualquer tecla para continuar.");
- gravar = 1;
- getch();
- return 0;
- }
- if (u == '2'){
- printf("\n Digite o nome do arquivo (com .txt): ");
- scanf("%s", nomereplay);
- fd = fopen(nomereplay, "r+");
- if(fd == NULL){
- printf("\nO arquivo informado nao existe. Pressione qualquer tecla para voltar.");
- getch();
- return modoreplay();
- }
- else{
- replay = 1;
- fscanf(fd, "%s", preplay);
- for (tamreplay=0; preplay[tamreplay] != '\0'; tamreplay++){
- }
- return 0;
- }
- }
- if (u == 3){
- return jogar();
- }
- }
- /* funcao para o menu utilizando switch para incializar a funcao correspondente */
- int menu(){
- system(CLEAR);
- char c;
- char w;
- c = '0';
- printf(RED ".##.....##.########.##....##.##.....##\n.###...###.##.......###...##.##.....##\n.####.####.##.......####..##.##.....##\n.##.###.##.######...##.##.##.##.....##\n.##.....##.##.......##..####.##.....##\n.##.....##.##.......##...###.##.....##\n.##.....##.########.##....##..#######.\n\n" RESET);
- printf("1 - Jogar\n2 - Instrucoes\n3 - Configuracoes\n4 - Ranking (Indisponivel)\n5 - Sair\n Digite o numero correspondente a opcao desejada: ");
- scanf("\n%c", &c);
- while ((c != '1') && (c != '2') && (c != '3') /*&& (c != '4')*/ && (c != '5')){
- printf("\nOpcao %c invalida!\n\nDigite o numero correspondente a opcao desejada: ", c);
- scanf("\n%c", &c);
- }
- switch (c){
- case '1':
- system(CLEAR);
- printf("\nDigite 1 para jogar normalmente e 2 para entrar no Modo Replay: ");
- scanf("\n%c", &w);
- while ((w != '1') && (w != '2')){
- printf("\nOpcao %c invalida!\n\nDigite o numero correspondente a opcao desejada: ", w);
- scanf("\n%c", &w);
- }
- if (w == '2'){
- system(CLEAR);
- modoreplay();
- }
- system(CLEAR);
- if (jogar()){ /*se jogar retorna 0, menu retorna 0 e sai do jogo */
- return 1;
- }
- return 0;
- case '2':
- system(CLEAR);
- if (instrucoes()){ /* se instrucoes retorna 1, menu retorna 1 e a main faz voltar para o menu */
- return 1;
- }
- system(CLEAR);
- return 0;
- case '3':
- configuracoes();
- /*
- case '4':
- ranking(); */
- case '5':
- system(CLEAR);
- return 0;
- }
- return 0;
- }
- /* funcao para o menu de configuracoes utilizando switch para incializar a funcao correspondente */
- int configuracoes(){
- system(CLEAR);
- char c;
- c = '0';
- printf("1 - Tabuleiro\n2 - Pecas\n3 - Ativar Modo Rankeado\n4 - Voltar\n Digite o numero correspondente a opcao desejada: ");
- scanf("\n%c", &c);
- while ((c != '1') && (c != '2') && (c != '3') && (c != '4')){
- printf("\nOpcao %c invalida!\n\nDigite o numero correspondente a opcao desejada: ", c);
- scanf("\n%c", &c);
- }
- switch (c){
- case '1':
- configtabu();
- return 0;
- case '2':
- configpecas();
- return 0;
- case '3':
- printf("O modo ranqueado (e o ranking) nao foram implementados :( \nPressione qualquer tecla para voltar ao menu.");
- getch();
- return menu();
- case '4':
- return menu();
- }
- }
- /* funcao para o menu de configuracoes de tabuleiro */
- int configtabu(){
- system(CLEAR);
- char c;
- c = '0';
- printf("Configuracoes de Tabuleiro\n\n1 - Altura (%d)\n2 - Largura (%d)\n3 - Velocidade (%d)\n4 - Voltar\n Digite o numero para alterar a opcao desejada: ", al, la, velocidade);
- scanf("\n%c", &c);
- while ((c != '1') && (c != '2') && (c != '3') && (c != '4')){
- printf("\nOpcao %c invalida!\n\nDigite o numero correspondente a opcao desejada: ", c);
- scanf("\n%c", &c);
- }
- int o;
- FILE *arquivo;
- arquivo = fopen("config.txt", "w");
- switch (c){
- case '1':
- printf("\nDigite um novo valor para altura entre 5 e 80: ");
- scanf("%d", &o);
- while(o < 5 || o > 80){
- printf("\nValor fora dos limites! Digite novamente: ");
- scanf("%d", &o);
- }
- al = o;
- fprintf(arquivo,"%d %d %d %d %d", al, la, qtdp, pontuacao, velocidade);
- return configtabu();
- case '2':
- printf("\nDigite um novo valor para largura entre 5 e 80: ");
- scanf("%d", &o);
- while(o < 5 || o > 80){
- printf("\nValor fora dos limites! Digite novamente: ");
- scanf("%d", &o);
- }
- la = o;
- fprintf(arquivo,"%d %d %d %d %d", al, la, qtdp, pontuacao, velocidade);
- return configtabu();
- case '3':
- printf("\nDigite um novo valor para velocidade (em microssegundos) entre 100000 e 5000000: ");
- scanf("%d", &o);
- while(o < 100000 || o > 5000000){
- printf("\nValor fora dos limites! Digite novamente: ");
- scanf("%d", &o);
- }
- velocidade = o;
- fprintf(arquivo,"%d %d %d %d %d", al, la, qtdp, pontuacao, velocidade);
- return configtabu();
- case '4':
- return configuracoes();
- }
- }
- /* funcao para o menu de configuracoes de pecas */
- int configpecas(){
- system(CLEAR);
- char c;
- c = '0';
- printf("Configuracoes de Pecas\n\n1 - Tipos de pecas diferentes (%d)\n2 - Quantidade de pecas ligadas para fazer ponto (%d)\n3 - Voltar\n Digite o numero para alterar a opcao desejada: ", qtdp, pontuacao);
- scanf("\n%c", &c);
- while ((c != '1') && (c != '2') && (c != '3')){
- printf("\nOpcao %c invalida!\n\nDigite o numero correspondente a opcao desejada: ", c);
- scanf("\n%c", &c);
- }
- int o;
- FILE *arquivo;
- arquivo = fopen("config.txt", "w");
- switch (c){
- case '1':
- printf("\nDigite um novo valor para a variedade de pecas entre 2 e 10: ");
- scanf("%d", &o);
- while(o < 2 || o > 10){
- printf("\nValor fora dos limites! Digite novamente: ");
- scanf("%d", &o);
- }
- qtdp = o;
- fprintf(arquivo,"%d %d %d %d %d", al, la, qtdp, pontuacao, velocidade);
- return configpecas();
- case '2':
- printf("\nDigite um novo valor para o combo de pecas entre 3 e 10: ");
- scanf("%d", &o);
- while(o < 3 || o > 10){
- printf("\nValor fora dos limites! Digite novamente: ");
- scanf("%d", &o);
- }
- pontuacao = o;
- fprintf(arquivo,"%d %d %d %d %d", al, la, qtdp, pontuacao, velocidade);
- return configpecas();
- case '3':
- return configuracoes();
- }
- }
- int main () {
- FILE *arquivo;
- arquivo = fopen("config.txt","r");
- if(arquivo == NULL){
- arquivo = fopen("config.txt", "w");
- fprintf(arquivo,"%d %d %d %d %d", al, la, qtdp, pontuacao, velocidade);
- }
- arquivo = fopen("config.txt","r");
- fscanf(arquivo, "%d %d %d %d %d", &al, &la, &qtdp, &pontuacao, &velocidade);
- system(CLEAR);
- if (inicial()){
- system(CLEAR);
- }
- srand(time(0));
- while(menu()){}
- fclose(arquivo);
- if (replay == 1 || gravar == 1){
- FILE *fd;
- fd = fopen(nomereplay,"r");
- fclose(fd);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement