Ir para conteúdo
Fórum Script Brasil
  • 0

BUG.....programa com erro...


LuKaum

Pergunta

toh com serios problemas num jogo da cobrinha q eu fiz akew....

a logica dele é simples...ele adiciona um ponto a frent da atual cabeça e depois o atribui para uma variavel cauda(tail) e segue todos os pontos proximos por if....

o foda é que quando ela axa mais de um caminho possivel dá bug...entaum eu usei algo parecido com um bool para poder tirar a

probabilidade de ir nos dois caminhos... o problema é q se a cobra ir para o ponto errado (ela sempre faz

isso....aff danada!!!)babaus... entaum eu usei uma função ow coisa parecida para determinar um nivel

de prioridade para q ela faça sempre a escolha certa.....

mas quando passa uma parte do corpo da cobra ao lado do resto do corpo dela e clika para esquerda ou direita (se ela tiver subindo ou descendo) ou para cima ou baixo (se ela tiver para direita ou esquerda) gera um conflito com a prioridade gerando um loop-in infinito dentro do laço for.....dai eu adicionei um contador para impedir o loop-in infinito e concertou em duas posiçoes só q nas outras duas acontecem erros muito feios.....quem puder dá uma olhada e compilar o codigo pra fragar direito o problema....

#include <iostream>
#include <conio.h>
#include <windows.h>
using namespace std;

void MAP();
void CONTROL();
void FOOD();
void PRIORITY();

int option, i, j, tail_x, tail_y, food_x, food_y, priority, contador;
const int com = 20, lar = 50;
int block [com][lar];
int snake_x = 10, snake_y = 25, head_x = -1, head_y = 0, size = 3, off = 0, points = 0;
int speed = 2, pr_u = 1, pr_d = 2, pr_l = 3, pr_r = 4;
char key2, key = 'w', key_up = 'w', key_down = 's', key_left = 'a', key_right = 'd';
bool end = false, upgrade = false, start_food = false, r = false, l = false, u = true, d = false, end_turn = false;

int main()
{
    do
    {
        system("color 1f");
        cout<<"\n\n\t\t\t    SNAKE --- NEW VERSION\n\n";
        cout<<"\t\t    This Game is a product by Lukaum\n\n\n\n";
        if (key == 'x')
        {
            cout<<"0_ Continue\n";
        }
        cout<<"1_ Play\n";
        cout<<"2_ Instructions\n";
        cout<<"3_ Option\n";
        cout<<"4_ Exit\n";
        cin>>option;
        if (option == 1 || (option == 0 && key == 'x'))
        {
            system("cls");
            if (option == 0 && key == 'x')
            {
                key = key_up;
            }
            else
            {
                snake_x = 10, snake_y = 25, head_x = -1, head_y = 0, size = 3, off = 0, points = 0;
                key = key_up;
                start_food = false;
                for (i = 0; i < com; i++)
                {
                    for (j = 0; j < lar; j++)
                    {
                        block [i][j] = ' ';
                    }
                }
                for (i = 0; i < com; i++)
                {
                    block [i][0] = char(186);
                    block [i][lar - 1] = char(186);
                }
                for (j = 0; j < lar; j++)
                {
                    block [0][j] = char(205);
                    block [com - 1][j] = char(205);
                }
                block [0][0] = char(201);
                block [com - 1][0] = char(200);
                block [0][lar - 1] = char(187);
                block [com - 1][lar - 1] = char(188);
                block [snake_x][snake_y] = char(254);
                block [snake_x + 1][snake_y] = char(254);
                block [snake_x + 2][snake_y] = char(254);
            }
            do
            {
                MAP();
                FOOD();
                CONTROL();
                snake_x += head_x;
                snake_y += head_y;
                if (block [snake_x][snake_y] == char(186) || block [snake_x][snake_y] == char(205) || block [snake_x][snake_y] == char(254))
                {
                    end = true;
                }
                else
                {
                    if (block [snake_x][snake_y] == '*')
                    {
                        size++;
                        upgrade = true;
                        start_food = false;
                        points += (5 * speed);
                    }
                    block [snake_x][snake_y] = char(254);
                    PRIORITY();
                    tail_x = snake_x;
                    tail_y = snake_y;
                    contador = 4 * size;
                    if (upgrade != true)
                    {
                        for (i = 0; i < size; i++)
                        {
                                if (block [tail_x + 1][tail_y] == char(254) && off != 2 && end_turn != true && pr_u == priority)
                                {
                                    tail_x += 1;
                                    off = 1;
                                    end_turn = true;
                                }
                                if (block [tail_x - 1][tail_y] == char(254) && off != 1 && end_turn != true && pr_d == priority)
                                {
                                    tail_x -= 1;
                                    off = 2;
                                    end_turn = true;
                                }
                                if (block [tail_x][tail_y - 1] == char(254) && off != 4 && end_turn != true && pr_r == priority)
                                {
                                    tail_y -= 1;
                                    off = 3;
                                    end_turn = true;
                                }
                                if (block [tail_x][tail_y + 1] == char(254) && off != 3 && end_turn != true && pr_l == priority)
                                {
                                    tail_y += 1;
                                    off = 4;
                                    end_turn = true;
                                }
                                if (end_turn != true)
                                {
                                    if (priority > 3)
                                    {
                                        priority = 1;
                                    }
                                    else
                                    {
                                        priority++;
                                    }
                                    i--;
                                    if (contador == 0)
                                    {
                                        i = size;
                                    }
                                }
                                contador--;
                                end_turn = false;
                        }
                        off = 0;
                        block [tail_x][tail_y] = ' ';
                    }
                    cout<<"Points: "<<points;
                    upgrade = false;
                    Sleep(200 / speed);
                    system("cls");
                }
            }while (key != 'x' && end != true);
            if (end == true)
            {
                system("cls");
                cout<<"\n\n\n\n\t\t\t\tYOU LOSE!!!!\n\n\n\n\n";
                cout<<"\n\n\n\n\t\t\t\tyou did "<<points;
                cout<<" points \n\n\n";
                cout<<"press ENTER to continue...";
                system("pause > nul");
                system("cls");
            }
            if (key != 'x')
            {
                snake_x = 10, snake_y = 25, head_x = -1, head_y = 0, size = 3, off = 0, points = 0;
                end = false, upgrade = false, start_food = false, r = false, l = false, u = true, d = false;
            }
        }
        if (option == 2)
        {
            system("cls");
            cout<<"\n\n\n\nUse the buttons: 'w', 's', 'd', 'a', for up, down, right and left, respective.\n\n";
            cout<<"Click in 'x' for pause the game.\n\n";
            cout<<"Change the keys and the snake's speed in the menu, options.\n\n\n\n";
            cout<<"press ENTER to continue...";
            system("pause > nul");
            system("cls");
        }
        if (option == 3)
        {
            do
            {
                system("cls");
                cout<<"\n\n\n\nUse the keys '+' or '-' for change the snake's speed\n\n";
                cout<<"Use 'e' for edit the control. (just use characters)\n\n";
                cout<<"Press 'x' for exit of this options.\n\n";
                cout<<"Speed of Snake: "<<speed;
                if (kbhit())
                {
                    key2 = getch();
                    if (key2 == '+' && speed != 5)
                    {
                        speed++;
                    }
                    if (key2 == '-' && speed != 1)
                    {
                        speed--;
                    }
                    if (key2 == 'e')
                    {
                        cout<<"\nType the up key: ";
                        cin>>key_up;
                        cout<<"\nType the down key: ";
                        cin>>key_down;
                        cout<<"\nType the left key: ";
                        cin>>key_left;
                        cout<<"\nType the right key: ";
                        cin>>key_right;
                    }
                }
            }while (key2 != 'x');
            system("cls");
            key2 = 'o';
        }
    }while (option != 4);
    return 0;
}

void MAP()
{
    for (i = 0; i < com; i++)
    {
        printf("\t    ");
        for (j = 0; j < lar; j++)
        {
            printf("%c", block [i][j]);
        }
        printf("\n");
    }
}

void CONTROL()
{
    if (kbhit())
    {
        key = getch();
        if (key == key_up && d != true)
        {
            head_x = -1;
            head_y = 0;
            u = true;
            d = false;
            l = false;
            r = false;
        }
        if (key == key_down && u != true)
        {
            head_x = 1;
            head_y = 0;
            u = false;
            d = true;
            l = false;
            r = false;
        }
        if (key == key_left && r != true)
        {
            head_x = 0;
            head_y = -1;
            u = false;
            d = false;
            l = true;
            r = false;
        }
        if (key == key_right && l != true)
        {
            head_x = 0;
            head_y = 1;
            u = false;
            d = false;
            l = false;
            r = true;
        }
    }
}

void FOOD()
{
    int x;
    bool y = true;
    srand(time(NULL));
    food_x = rand() % (com - 1);
    food_y = rand() % (lar - 1);
    while (y == true)
    {
        for (x = 0; x < 255; x++)
        {
            if (block [food_x][food_y] == char(x))
            {
                if (x == 254 || x == 186 || x == 187 || x == 188 || x == 200 || x == 201 || x == 205)
                {
                    food_x = rand() % (com - 1);
                    food_y = rand() % (lar - 1);
                }
                else
                {
                    y = false;
                }
            }
        }
    }
    if (start_food != true)
    {
        block [food_x][food_y] = '*';
        start_food = true;
    }
}

void PRIORITY()
{
    if (u == true)
    {
        priority = 1;
    }
    if (d == true)
    {
        priority = 2;
    }
    if (l == true)
    {
        priority = 3;
    }
    if (r == true)
    {
        priority = 4;
    }
}

quem conseguir arrumar posta aeww!!

Editado por LuKaum
Link para o comentário
Compartilhar em outros sites

0 respostass a esta questão

Posts Recomendados

Até agora não há respostas para essa pergunta

Participe da discussão

Você pode postar agora e se registrar depois. Se você já tem uma conta, acesse agora para postar com sua conta.

Visitante
Responder esta pergunta...

×   Você colou conteúdo com formatação.   Remover formatação

  Apenas 75 emoticons são permitidos.

×   Seu link foi incorporado automaticamente.   Exibir como um link em vez disso

×   Seu conteúdo anterior foi restaurado.   Limpar Editor

×   Você não pode colar imagens diretamente. Carregar ou inserir imagens do URL.



  • Estatísticas dos Fóruns

    • Tópicos
      152,2k
    • Posts
      652k
×
×
  • Criar Novo...