Jump to content

Recommended Posts

Posted

Olá pessoal tudo bem...

saquei o jogo do três em linha, e funciona bem, no entanto, tenho um problema..

quis alterar o código para ter funções e procedimentos..

ou melhor procedimentos...

o problema é que quero fazer um procedimento para criar o tabuleiro que é facil:

tabuleiro[1]='1';
tabuleiro[2]='2';
tabuleiro[3]='3';
tabuleiro[4]='4';
tabuleiro[5]='5';
tabuleiro[6]='6';
tabuleiro[7]='7';
tabuleiro[8]='8';
tabuleiro[9]='9';

printf ("\n\n\n\t\t\t\tJOGO DO GALO\n\n");
printf ("\t\t\t\t%c %c %c %c %c \n",linha [1][1],linha[2] [1],linha[3][1],linha[4] [1], linha [5] [1]);
printf ("\t\t\t\t%c %c %c %c %c \n",linha[1][2],tabuleiro[1],tabuleiro[2],tabuleiro[3], linha [5][2]);
printf ("\t\t\t\t%c %c %c %c %c \n",linha [1][3],tabuleiro[4],tabuleiro[5],tabuleiro[6], linha[5][3]);
printf ("\t\t\t\t%c %c %c %c %c \n",linha[1][4],tabuleiro[7],tabuleiro[8],tabuleiro[9], linha [5] [4]);
  printf ("\t\t\t\t%c %c %c %c %c \n",linha [1][5],linha[2] [5],linha[3][5],linha[4] [5], linha [5] [5]);

e depois um procedimento para o jogo:

aqui é que está o problema, ele no while entra num ciclo infinito, mas não percebo porque??

alguem me ajuda??

outro problema é conforme jogo actualizar o tabuleiro, basta na função jojo chamar tabuleiro()? deixo o codigo ja a seguir:

void jogar ()
{
nj=0;
jogada =0;
       while (sair == 0)
      

printf ("jogador 1, introduza a jogada\n");

scanf ("%d",&jogada);
printf ("entrei");
system ("pause");


tabuleiro[jogada]='x';nj++;
if ((tabuleiro[1]=='x' && tabuleiro[2]=='x' && tabuleiro[3]=='x') || (tabuleiro[4]=='x' && tabuleiro[5]=='x' && tabuleiro[6]=='x') || (tabuleiro[7]=='x' && tabuleiro[8]=='x' && tabuleiro[9]=='x'))
{
printf ("O jogador 1 venceu!\n");
sair=1;
}
else if ((tabuleiro[1]=='x' && tabuleiro[4]=='x' && tabuleiro[7]=='x') || (tabuleiro[2]=='x' && tabuleiro[5]=='x' && tabuleiro[8]=='x') || (tabuleiro[3]=='x' && tabuleiro[6]=='x' && tabuleiro[9]=='x'))
{
printf ("o jogador 1 venceu!\n");
sair=1;
}
else if ((tabuleiro[1]=='x' && tabuleiro[5]=='x' && tabuleiro[9]=='x') || (tabuleiro[2]=='x' && tabuleiro[5]=='x' && tabuleiro[8]=='x') || (tabuleiro[3]=='x' && tabuleiro[5]=='x' && tabuleiro[7]=='x'))
{
printf ("o jogador 1 venceu!\n");
sair=1;
}

if(nj==9)
{
printf("empate");
printf("\n");
system ("pause");
sair=1;}

//jogador 2

else
{
printf ("jogador 2, introduza a jogada\n");
scanf ("%d",&jogada);nj++;
tabuleiro();
tabuleiro[jogada]='o';
if ((tabuleiro[1]=='o' && tabuleiro[2]=='o' && tabuleiro[3]=='o') || (tabuleiro[4]=='o' && tabuleiro[5]=='o' && tabuleiro[6]=='o') || (tabuleiro[7]=='o' && tabuleiro[8]=='o' && tabuleiro[9]=='o'))
{
printf ("O jogador 2 venceu!\n");
sair=1;
}
else if ((tabuleiro[1]=='o' && tabuleiro[4]=='o' && tabuleiro[7]=='o') || (tabuleiro[2]=='o' && tabuleiro[5]=='o' && tabuleiro[8]=='o') || (tabuleiro[3]=='o' && tabuleiro[6]=='o' && tabuleiro[9]=='o'))
{
printf ("o jogador 2 venceu!\n");
sair=1;
}
else if ((tabuleiro[1]=='o' && tabuleiro[5]=='o' && tabuleiro[9]=='o') || (tabuleiro[2]=='o' && tabuleiro[5]=='o' && tabuleiro[8]=='o') || (tabuleiro[3]=='o' && tabuleiro[5]=='o'&& tabuleiro[7]=='o'))
{
printf ("o jogador 2 venceu!\n");
sair=1;

}
}
}

Eu sei que se colocar tudo dentro da função jogar, isto é fazer os prints do tabulareido para jogador um e outro para jogador 2 funciona mas eu queria assim...

Alguem me ajuda??

cumprimentos!!

Posted

1º - não estás a usar o GeShi correctamente

2º - não identaste uma única linha de código

3º - não existe uma única linha de comentário no teu código

4º - não encontro o while que referes do ciclo infinito do código que apresentaste

como achas que uma se pode resolver uma questão desta maneira ??

IRC : sim, é algo que ainda existe >> #p@p
Posted

ok tens razão em tudo:-))

desculpa o while está logo a começar o procedimento jogar()

no enatnto, resolvi de outra forma, criei o tabuleiro no proprio procedimento jogar que assim atualiza sempre e não da erro:-))

mais uma vez desculpa!!

tenho um tópico que tem um erro e precisava mesmo de conseguir tirar!!

o topico chama-se o jogo da forca..

não me podes dar uma ajuda??

muito obrigado por tudo...e mais uma vez desculpa.

Posted

isso só te desculpa o primeiro ponto ... e os outros 3 ?

LOOOOOOOL  :wallbash:

poe as chavetas no while:

void jogar ()
{
nj=0;
jogada =0;

while (sair == 0)
{
    printf ("jogador 1, introduza a jogada\n");

     scanf ("%d",&jogada);
     printf ("entrei");
     system ("pause");
     
     tabuleiro[jogada]='x';nj++;
     if ((tabuleiro[1]=='x' && tabuleiro[2]=='x' && tabuleiro[3]=='x') || (tabuleiro[4]=='x' && tabuleiro[5]=='x' && tabuleiro[6]=='x') || (tabuleiro[7]=='x' && tabuleiro[8]=='x' && tabuleiro[9]=='x'))
     {
        printf ("O jogador 1 venceu!\n");
        sair=1;
     }
     else if ((tabuleiro[1]=='x' && tabuleiro[4]=='x' && tabuleiro[7]=='x') || (tabuleiro[2]=='x' && tabuleiro[5]=='x' && tabuleiro[8]=='x') || (tabuleiro[3]=='x' && tabuleiro[6]=='x' && tabuleiro[9]=='x'))
     {
        printf ("o jogador 1 venceu!\n");
        sair=1;
     }
     else if ((tabuleiro[1]=='x' && tabuleiro[5]=='x' && tabuleiro[9]=='x') || (tabuleiro[2]=='x' && tabuleiro[5]=='x' && tabuleiro[8]=='x') || (tabuleiro[3]=='x' && tabuleiro[5]=='x' && tabuleiro[7]=='x'))
     {
        printf ("o jogador 1 venceu!\n");
        sair=1;
     }
     if(nj==9)
     {
         printf("empate");
         printf("\n");
         system ("pause");
         sair=1;
     }

     //jogador 2
    
     else
     {
         printf ("jogador 2, introduza a jogada\n");
         scanf ("%d",&jogada);nj++;
         tabuleiro();
         tabuleiro[jogada]='o';
         if ((tabuleiro[1]=='o' && tabuleiro[2]=='o' && tabuleiro[3]=='o') || (tabuleiro[4]=='o' && tabuleiro[5]=='o' && tabuleiro[6]=='o') || (tabuleiro[7]=='o' && tabuleiro[8]=='o' && tabuleiro[9]=='o'))
         {
         printf ("O jogador 2 venceu!\n");
         sair=1;
         }
         else if ((tabuleiro[1]=='o' && tabuleiro[4]=='o' && tabuleiro[7]=='o') || (tabuleiro[2]=='o' && tabuleiro[5]=='o' && tabuleiro[8]=='o') || (tabuleiro[3]=='o' && tabuleiro[6]=='o' && tabuleiro[9]=='o'))
            {
                printf ("o jogador 2 venceu!\n");
                sair=1;
             }
             else if ((tabuleiro[1]=='o' && tabuleiro[5]=='o' && tabuleiro[9]=='o') || (tabuleiro[2]=='o' && tabuleiro[5]=='o' && tabuleiro[8]=='o') || (tabuleiro[3]=='o' && tabuleiro[5]=='o'&& tabuleiro[7]=='o'))
             {
                printf ("o jogador 2 venceu!\n");
                sair=1;
            }
      }
}   

Aprende a indentar codigo e a comenta-lo

Por muito mais que que estude só aprendo uma coisa, que ainda tenho muita coisa para aprender.

A beleza de um código está em decompor problemas complexos em pequenos blocos simples.

"learn how to do it manually first, then use the wizzy tool to save time."

"Kill the baby, don't be afraid of starting all over again. Fail soon, learn fast."

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.