Unconfigured Ad Widget

Collapse

Anúncio

Collapse
No announcement yet.

AJUDA jogo da forca em pascal

Collapse
X
 
  • Filter
  • Tempo
  • Show
Clear All
new posts

  • Font Size
    #1

    Duvida AJUDA jogo da forca em pascal

    Fala galera!

    sou novo em programação, comecei a estudar a 1 semana .
    tava treinando PASCAL ontem, e tentei fazer um joguinho da forca...

    o jogo funcionou tranquilo, mas quando inclui umas funções nele, entrou num loop infinito, e nao estou conseguindo identificar...
    toda a parte do jogo em si estava funcionando perfeitamente.
    o que eu inseri depois (e começou o problema) foi uma tela inicial de apresentação do jogo, e uma flag pra quando terminar o programa, ele perguntar se quer iniciar novamente...
    pronto..depois disso o programa ficou maluco!

    queria a ajuda de vocês pra achar o erro na programação!
    fiz o codigo em FPC (nao sei se isso faz alguma diferença)

    valeu galera! obrigado!

    abaixo está o codigo do programa...



    Código:
    
    PROGRAM FORCA;
    uses crt;
    var
    n, i, POSICAO, ERROS,ACERTOS,spc,inicio,nivel: integer;
    senha,letra,FLAG: string;
    
    
    begin
    
     clrscr;
    
    
    
        writeln('');
        writeln('-----------------------');
        writeln('|                     |');
        writeln('|                     |');
        writeln('|');
        writeln('|');
        writeln('|');
        writeln('|');
        writeln('|');
        writeln('|');
        writeln('|');
        writeln('|');
        writeln('|');
        writeln('|');
        writeln('|');
        writeln('|');
        writeln('|');
        writeln('|');
        writeln('----|');
    
    
        gotoxy(20,5);
        write('=======');
        gotoxy(20,6);
        write('[      ]');
        gotoxy(20,7);
        write('[      ]');
        gotoxy(20,8);
        write('[      ]');
        gotoxy(20,9);
        write('[      ]');
        gotoxy(20,10);
        write('========');
        gotoxy(23,11);
        write('||');
        gotoxy(23,12);
        write('||');
        gotoxy(23,13);
        write('||');
        gotoxy(23,14);
        write('||');
        gotoxy(23,15);
        write('||');
        gotoxy(23,16);
        write('||');
        gotoxy(24,16);
        write('||');
        gotoxy(25,17);
        write('||');
        gotoxy(26,18);
        write('||');
        gotoxy(22,16);
        write('||');
        gotoxy(21,17);
        write('||');
        gotoxy(20,18);
        write('||');
        gotoxy(22,13);
        write('||');
        gotoxy(21,13);
        write('||');
        gotoxy(20,13);
        write('||');
        gotoxy(24,13);
        write('||');
        gotoxy(25,13);
        write('||');
        gotoxy(26,13);
        write('||');
        gotoxy(25,7);
        write('+');
        gotoxy(22,7);
        write('+');
        gotoxy(22,9);
        write('----');
    
        gotoxy(35,15);
    writeln('BEM VINDO AO JOGO DA FORCA!');
    gotoxy(35,16);
    writeln('CRIADO EM DESENVOLVIDO POR TULIO GUIMARÇES');
    gotoxy(35,18);
    
        write('ESCOLHA O NIVEL: ');
        gotoxy(35,19);
        write('1 - FACIL (10 CHANCES)');
       gotoxy(35,20);
       write('2 - MEDIO (7 CHANCES)');
       gotoxy(35,21);
       write('3 - DIFICIL (5 CHANCES)');
        read(nivel);
    
    
          case nivel of
          1: nivel:= 10;
          2: nivel:= 7;
          3: nivel:= 5;
          end;
    
    
    
    
        clrscr;
    
    
        writeln('');
        writeln('-----------------------');
        writeln('|                     |');
        writeln('|                     |');
        writeln('|');
        writeln('|');
        writeln('|');
        writeln('|');
        writeln('|');
        writeln('|');
        writeln('|');
        writeln('|');
        writeln('|');
        writeln('|');
        writeln('|');
        writeln('|');
        writeln('|');
        writeln('|');
        writeln('----|');
    
    
       readkey;
    
    
        writeln('digite a palavra : ');
        read(senha);
    
        n:= length(senha);
        i:=0;
        clrscr;
    
    
    
    
    
        gotoxy(20,20);
    
            repeat
            write('_ ');
            i:= i + 1;
            until i = n;
    
        i:=1;
    
        writeln(' ');
        writeln(' ');
    
        ACERTOS:=0;
        ERROS:=-1;
    
    
    
    
    
    
        gotoxy(20,22);
        write('digite uma letra : ');
    
        spc:= 1;
    
        while (ACERTOS <> n) and (ACERTOS <>'999') do
         begin
    
               gotoxy(50,15);
        writeln('VOCE AINDA TEM ',nivel-ERROS, ' CHANCES');
               gotoxy(50,20);
        writeln('LETRAS ERRADAS ( ',ERROS, ' )');
    
    
    
        gotoxy(20+ spc,23);
         readln(letra);
         spc:= spc+2;
    
         inicio:= 1;
    
            if pos(letra,senha)=0 then
            begin
            ERROS:=ERROS + 1;
            gotoxy(45+spc,21);
            write(letra);
            end;
    
        if nivel = 5 then
        begin
    
            if ERROS = 1 then
            begin
            gotoxy(20,5);
            write('=======');
            gotoxy(20,6);
            write('[      ]');
            gotoxy(20,7);
            write('[      ]');
            gotoxy(20,8);
            write('[      ]');
            gotoxy(20,9);
            write('[      ]');
            gotoxy(20,10);
            write('========');
            end;
    
            if ERROS = 2 then
            begin
            gotoxy(23,11);
            write('||');
            gotoxy(23,12);
            write('||');
            gotoxy(23,13);
            write('||');
            gotoxy(23,14);
            write('||');
            gotoxy(23,15);
            write('||');
            gotoxy(23,16);
            write('||');
            end;
    
    
            if ERROS = 3 then
            begin
            gotoxy(24,16);
            write('||');
            gotoxy(25,17);
            write('||');
            gotoxy(26,18);
            write('||');
            gotoxy(22,16);
            write('||');
            gotoxy(21,17);
            write('||');
            gotoxy(20,18);
            write('||');
            end;
    
    
            if ERROS = 4 then
            begin
            gotoxy(22,13);
            write('||');
            gotoxy(21,13);
            write('||');
            gotoxy(20,13);
            write('||');
            gotoxy(24,13);
            write('||');
            gotoxy(25,13);
            write('||');
            gotoxy(26,13);
            write('||');
            end;
    
            if ERROS = 5 then
            begin
            gotoxy(25,7);
            write('+');
            gotoxy(22,7);
            write('+');
            gotoxy(22,9);
            write('----');
            gotoxy(45,10);
            write('VOCÒ PERDEU!!!!');
            readkey;
            ACERTOS:= 999;
            end;
    
        end;
    
    
        if nivel = 7 then
        begin
            if ERROS = 1 then
            begin
            gotoxy(20,5);
            write('=======');
            gotoxy(20,6);
            write('[      ]');
            gotoxy(20,7);
            write('[      ]');
            gotoxy(20,8);
            write('[      ]');
            gotoxy(20,9);
            write('[      ]');
            gotoxy(20,10);
            write('========');
            end;
    
            if ERROS = 2 then
            begin
            gotoxy(23,11);
            write('||');
            gotoxy(23,12);
            write('||');
            gotoxy(23,13);
            write('||');
            gotoxy(23,14);
            write('||');
            gotoxy(23,15);
            write('||');
            gotoxy(23,16);
            write('||');
            end;
    
    
            if ERROS = 3 then
            begin
            gotoxy(24,16);
            write('||');
            gotoxy(25,17);
            write('||');
            gotoxy(26,18);
            write('||');
            end;
    
            if ERROS = 4 then
            gotoxy(22,16);
            write('||');
            gotoxy(21,17);
            write('||');
            gotoxy(20,18);
            write('||');
            end;
    
    
            if ERROS = 5 then
            begin
            gotoxy(22,13);
            write('||');
            gotoxy(21,13);
            write('||');
            gotoxy(20,13);
            write('||');
            end;
    
            if ERROS = 6 then
            begin
            gotoxy(24,13);
            write('||');
            gotoxy(25,13);
            write('||');
            gotoxy(26,13);
            write('||');
            end;
    
            if ERROS = 7 then
            begin
            gotoxy(25,7);
            write('+');
            gotoxy(22,7);
            write('+');
            gotoxy(22,9);
            write('----');
            gotoxy(45,10);
            write('VOCÒ PERDEU!!!!');
            readkey;
            ACERTOS:= 999;
            end;
    
         end;
    
    
         if nivel = 10 then
         begin
              if ERROS = 1 then
              begin
              gotoxy(20,5);
              write('=======');
              gotoxy(20,6);
              write('[      ]');
              gotoxy(20,7);
              write('[      ]');
              gotoxy(20,8);
              write('[      ]');
              gotoxy(20,9);
              write('[      ]');
              gotoxy(20,10);
              write('========');
              end;
    
              if ERROS = 2 then
              begin
              gotoxy(23,11);
              write('||');
              gotoxy(23,12);
              write('||');
              gotoxy(23,13);
              end;
    
    
              if ERROS = 3 then
              begin
              gotoxy(22,13);
              write('||');
              gotoxy(21,13);
              write('||');
              gotoxy(20,13);
              write('||');
              end;
    
              if ERROS = 4 then
              begin
              gotoxy(24,13);
              write('||');
              gotoxy(25,13);
              write('||');
              gotoxy(26,13);
              write('||');
              end;
    
    
    
              if ERROS = 5 then
              begin
              write('||');
              gotoxy(23,14);
              write('||');
              gotoxy(23,15);
              write('||');
              gotoxy(23,16);
              write('||');
              end;
    
    
              if ERROS = 6 then
              begin
              gotoxy(24,16);
              write('||');
              gotoxy(25,17);
              write('||');
              gotoxy(26,18);
              write('||');
              end;
    
              if ERROS = 7 then
              begin
              gotoxy(22,16);
              write('||');
              gotoxy(21,17);
              write('||');
              gotoxy(20,18);
              write('||');
              end;
    
    
    
    
              if ERROS = 8 then
              begin
              gotoxy(25,7);
              write('+');
              end;
    
              if ERROS = 9 then
              begin
              gotoxy(22,7);
              write('+');
              end;
    
              if ERROS = 10 then
              begin
              gotoxy(22,9);
              write('----');
              gotoxy(45,10);
              write('VOCÒ PERDEU!!!!');
              readkey;
              ACERTOS:= 999;
              end;
    
         end;
    
    
    
    
    
    
    
    
    
    
            if pos(letra,senha) > 0 then
            begin
                 for i:= inicio to n do
                    if letra = senha[i] then
                    begin
                    POSICAO:= i;
                    gotoxy(19+posicao*2-1,20);
                    write(letra);
                    ACERTOS:= ACERTOS + 1;
                    inicio:= inicio + 1;
                    end;
                 end;
    
    
    
    
    
    
         if ACERTOS = n then
         begin
         clrscr;
         gotoxy(15,20);
         writeln('PARABNS, VOCÒ VENCEU!!!!');
         writeln('JOGAR DE NOVO? S - Sim / N - NÆo');
         read(flag);
         end;
    
    
    
         if ACERTOS = 999 then
         begin
         clrscr;
         write('TENTAR DE NOVO? S - Sim / N - NÆo');
         read(flag);
         ACERTOS:=0;
         ERROS:=-1;
         I:=0;
         end;
    
    
    
      readkey;
    
      end.

  • Font Size
    #2
    ahhh..so uma observação...
    no codigo à cima está sem o WHILE flag <> N no inicio, que tinha no codigo original...
    eu tirei ele pra ver se parava o erro, mas continuou...

    Comment


    • Font Size
      #3
      Cara eu só num ajuda porque eu tenho nojo de pascal!!

      Espero que alguém ajude!
      Se você é fã! Use!
      _ - _ _ - _ _ - _ _ - _
      .

      Comment


      • Font Size
        #4
        bom galera...reescrevi todo o codigo ontem (aproveitei pra treinar os precedures, que aprendi na sexta feira

        funcionou trankilo....o problema era que na hora de ler as variaveis, eu estava usando o read, ao invés do readln...e isso causou algum bug...

        se alguem quizer conferir o resultado, ta ai o codigo final...


        ahh...e H4S_KJ27, nao sei se pascal presta pra alguma coisa ou nao, mas todo mundo tem que começar por alguma linguagem tosca pra um dia chegar onde quer neh!


        Código:
        
        program forca4;
        uses crt;
        var
        n, i, POSICAO, ERROS, ACERTOS, spc, inicio, nivel : integer;
        senha, letra, FLAG: string;
        
        
        
        
        
        
        
        procedure imprime_forca;
        begin
        
            writeln('');
            writeln('-----------------------');
            writeln('|                     |');
            writeln('|                     |');
            writeln('|');
            writeln('|');
            writeln('|');
            writeln('|');
            writeln('|');
            writeln('|');
            writeln('|');
            writeln('|');
            writeln('|');
            writeln('|');
            writeln('|');
            writeln('|');
            writeln('|');
            writeln('|');
            writeln('----|');
        
        end;  //fim da procedure
        
        
        procedure imprime_cabeca;
        begin;
                  gotoxy(20,5);
                  write('=======');
                  gotoxy(20,6);
                  write('[      ]');
                  gotoxy(20,7);
                  write('[      ]');
                  gotoxy(20,8);
                  write('[      ]');
                  gotoxy(20,9);
                  write('[      ]');
                  gotoxy(20,10);
                  write('========');
        
        end;  //fim da procedure
        
        
        procedure imprime_corpo1;
        begin
        
                  gotoxy(23,11);
                  write('||');
                  gotoxy(23,12);
                  write('||');
                  gotoxy(23,13);
        
        
        end;  //fim da procedure
        
        
        procedure imprime_braco1;
        begin
        
                  gotoxy(22,13);
                  write('||');
                  gotoxy(21,13);
                  write('||');
                  gotoxy(20,13);
                  write('||');
        
        end;  //fim da procedure
        
        procedure imprime_braco2;
        begin
        
                  gotoxy(24,13);
                  write('||');
                  gotoxy(25,13);
                  write('||');
                  gotoxy(26,13);
                  write('||');
        
        end;  //fim da procedure
        
        
        procedure imprime_corpo2;
        begin
        
                  write('||');
                  gotoxy(23,14);
                  write('||');
                  gotoxy(23,15);
                  write('||');
                  gotoxy(23,16);
                  write('||');
        
        end;  //fim da procedure
        
        procedure imprime_perna1;
        begin
        
                  gotoxy(24,16);
                  write('||');
                  gotoxy(25,17);
                  write('||');
                  gotoxy(26,18);
                  write('||');
        
        end;  //fim da procedure
        
        procedure imprime_perna2;
        begin
        
                  gotoxy(22,16);
                  write('||');
                  gotoxy(21,17);
                  write('||');
                  gotoxy(20,18);
                  write('||');
        
        end;  //fim da procedure
        
        procedure imprime_olho1;
        begin
        
                  gotoxy(25,7);
                  write('+');
        
        end;  //fim da procedure
        
        
        procedure imprime_olho2;
        begin
        
                  gotoxy(22,7);
                  write('+');
        
        end;  //fim da procedure
        
        procedure imprime_boca;
        begin
        
                  gotoxy(22,9);
                  write('----');
        
        end; //fim da procedure
        
        
        
        procedure tela_inicial;
        begin
        
                imprime_forca;
                imprime_cabeca;
                imprime_corpo1;
                imprime_corpo2;
                imprime_braco1;
                imprime_braco2;
                imprime_perna1;
                imprime_perna2;
                imprime_olho1;
                imprime_olho2;
                imprime_boca;
        
                gotoxy(35,15);
                writeln('BEM VINDO AO JOGO DA FORCA!');
                gotoxy(35,16);
                writeln('CRIADO E DESENVOLVIDO POR TULIO GUIMARÇES');
                readkey;
        
        end;  // fim da procedure
        
        
        procedure escolher_nivel;
        begin
                gotoxy(35,18);
                write('ESCOLHA O NIVEL: ');
                gotoxy(35,19);
                write('1 - FACIL (10 CHANCES)');
                gotoxy(35,20);
                write('2 - MEDIO (7 CHANCES)');
                gotoxy(35,21);
                write('3 - DIFICIL (5 CHANCES)');
                readln(nivel);
        
        
        end;  //fim da procedure
        
        
        procedure recebe_senha;
        begin
        
                writeln('Digite a palavra secreta : ');
                readln(senha);
        
        
        end;  // fim da procedure
        
        
        procedure tela_jogo;
        begin
        
                n:= length(senha);
                i:=0;
                gotoxy(20,20);
        
                repeat
                write('_ ');  // imprime os espa‡os das letras
                i:= i + 1;
                until i = n;
        
                i:=1;
        
                writeln(' ');
                writeln(' ');
        
                ACERTOS:=0;
                ERROS:=0;
        
        
                gotoxy(20,22);
                write('digite uma letra : ');
        
         end;  // fim da procedure
        
        
         procedure teste_letra;
         begin
        
        
        
                gotoxy(20+ spc,23);
                readln(letra);
                spc:= spc+2;
        
                inicio:= 1;
        
        
                // letra nao encontrada
                if pos(letra,senha)=0 then
                begin
                ERROS:=ERROS + 1;
                gotoxy(45+spc,21);
                write(letra);
                end;
        
        
                // letra encontrada
                if pos(letra,senha) > 0 then
                begin
                     for i:= inicio to n do
                        if letra = senha[i] then
                        begin
                        POSICAO:= i;
                        gotoxy(19+posicao*2-1,20);
                        write(letra);
                        ACERTOS:= ACERTOS + 1;
                        inicio:= inicio + 1;
                        end; //fim do if
                     end;   //fim do for
        
        
        end;  // fim da procedure
        
        
        
        procedure teste_erros;
        begin
        
                  case nivel of
                1: nivel:= 10;   // nivel recebe 10 chances
                2: nivel:= 7;    // nivel recebe 7 chances
                3: nivel:= 5;    // nivel recebe 5 chances
                end;
        
        
        
                  if nivel = 5 then
                  begin
                     case ERROS of
                     1: imprime_cabeca;
                     2: begin
                     imprime_corpo1;
                     imprime_corpo2;
                     end;
                     3: begin
                     imprime_perna1;
                     imprime_perna2;
                     end;
                     4: begin
                     imprime_braco1;
                     imprime_braco2;
                     end;
                     5: begin
                     imprime_olho1;
                     imprime_olho2;
                     imprime_boca;
                     gotoxy(45,10);
                     write('VOCÒ PERDEU!!!!');
                     gotoxy(50,15);
                     writeln('VOCE AINDA TEM ',nivel-ERROS, ' CHANCES');
                     gotoxy(50,20);
                     writeln('LETRAS ERRADAS ( ',ERROS, ' )');
                     readkey;
                     ACERTOS:= 999;
                     clrscr;
                   ERROS:=0;
                   i:=0;
                   write('TENTAR DE NOVO? S - Sim / N - NÆo');
                   readln(flag);
                     end;
                  end; // fim do if nivel 5
                  end;
        
                  if nivel = 7 then
                  begin
                     case ERROS of
                     1: imprime_cabeca;
                     2: begin
                     imprime_corpo1;
                     imprime_corpo2;
                     end;
                     3: imprime_perna1;
                     4: imprime_perna2;
                     5: imprime_braco1;
                     6: imprime_braco2;
                     7: begin
                     imprime_olho1;
                     imprime_olho2;
                     imprime_boca;
                     gotoxy(45,10);
                     write('VOCÒ PERDEU!!!!');
                     gotoxy(50,15);
                     writeln('VOCE AINDA TEM ',nivel-ERROS, ' CHANCES');
                     gotoxy(50,20);
                     writeln('LETRAS ERRADAS ( ',ERROS, ' )');
                     readkey;
                     ACERTOS:= 999;
                     clrscr;
                   ERROS:=0;
                   i:=0;
                   write('TENTAR DE NOVO? S - Sim / N - NÆo');
                   readln(flag);
                     end;
                  end; // fim do if nivel 7
                  end;
        
                  if nivel = 10 then
                  begin
                     case ERROS of
                     1: imprime_cabeca;
                     2: imprime_corpo1;
                     3: imprime_braco1;
                     4: imprime_braco2;
                     5: imprime_corpo2;
                     6: imprime_perna1;
                     7: imprime_perna2;
                     8: imprime_olho1;
                     9: imprime_olho2;
                     10:begin
                     imprime_boca;
                     gotoxy(45,10);
                     write('VOCÒ PERDEU!!!!');
                     gotoxy(50,15);
                     writeln('VOCE AINDA TEM ',nivel-ERROS, ' CHANCES');
                     gotoxy(50,20);
                     writeln('LETRAS ERRADAS ( ',ERROS, ' )');
                     readkey;
                     ACERTOS:= 999;
                     clrscr;
                   ERROS:=0;
                   i:=0;
                   write('TENTAR DE NOVO? S - Sim / N - NÆo');
                   readln(flag);
                     end;
                  end; // fim do if nivel 10
                  end;
        
        
        
        
        end; // fim da procedure
        
        
        procedure teste_acertos;
        begin
                   // acertou todas as letras
                   if ACERTOS = n then
                   begin
                   clrscr;
                   gotoxy(15,20);
                   writeln('PARABNS, VOCÒ VENCEU!!!!');
                   writeln('JOGAR DE NOVO? S - Sim / N - NÆo');
                   readln(flag);
                   end;
        
              {
                   // acertos recebeu 999 se as chances terminaram
                   if ACERTOS = 999 then
                   begin
                   clrscr;
                   ACERTOS:=0;
                   ERROS:=-1;
                   i:=0;
                   write('TENTAR DE NOVO? S - Sim / N - NÆo');
                   read(flag);
                   end;
                                 }
        end;  // fim da procedure
        
        
        
        
        
        
        
        
        
        
        
        
        BEGIN  // INICIO DO PROGRAMA
        
                  repeat
        
        senha:='a';
        
        clrscr;
        tela_inicial;
        
         flag:='a';
        
        
        
        clrscr;
        
        
        recebe_senha;
        
        escolher_nivel;
        
        clrscr;
        
        
        imprime_forca;
        
        tela_jogo;
        
        spc:=1;
        
        
        case nivel of
        1: nivel:= 10;
        2: nivel:=7;
        3: nivel:=5;
        end;
        
        
           while (ACERTOS <> n) and (ACERTOS <> 999) do
           begin
        
        
        
           gotoxy(50,15);
           writeln('VOCE AINDA TEM ',nivel-ERROS, ' CHANCES');
           gotoxy(50,20);
           writeln('LETRAS ERRADAS ( ',ERROS, ' )');
        
        
        
           teste_letra;
           teste_erros;
           teste_acertos;
        
        
        
        
        
           end;  // fim do while acertos <> n ou 999
        
        
        
                      UNTIL upcase(FLAG) = 'N' // fim do while flag!!
        
        
        
        end.  // FIM DO PROGRAMA

        Comment

        X
        Working...
        X