Forum Pplware

Versão Completa: Ajuda no jogo 4 em linha - Liguagem C
Está de momento a ver uma versão reduzida do nosso conteúdo. Ver versão completa com o formato adequado.
Não consigo desenvolver o algoritmo que termine o jogo.
Será que me podem ajudar?
Já agora se tiverem alguma sugestão para tornar mais simples o código, agradecia.

Abraço

Código:
#include <stdio.h>  
#include <stdlib.h>

char tabuleiro[6][7];  

int inicio(void);
void inicia_tabuleiro(void);
void mostra_tabuleiro(void);
void jogada(int n, int jog, int tipo);
int jogada_Player(int jog);
void jogada_COM(int jog);
char fim_jogo(int n);
void finaliza(int ti, char venc);

int main(void)
{
  char fim;
  int n, jog, tipo;

  printf("                        4 em linha Eng.Electronica\n\n\n");
  tipo = inicio();
  fim = ' '; n = 0; jog = 0;
  inicia_tabuleiro();
  
  do {
    n++; if (jog==1) jog = 2; else jog = 1;
    printf("Jogada %d \n \n", n);
    mostra_tabuleiro();
    jogada(n, jog, tipo);
    fim = fim_jogo(n);
    } while(fim == ' ');
  
   finaliza(tipo,fim);
  
    system("PAUSE");
  return 0;
}

int inicio(void)
{
    int ti;
    printf("Escolha um modo de jogo: \n");
    printf("1-Player/Player 2-Player/COM 3-COM/Player 4-COM/COM\n");
    scanf("%d",&ti);
    if(ti<1 || ti>4){
            printf("Opcao invalida, tente novamente.\n");
            inicio();
            }
    return ti;
}

void inicia_tabuleiro(void)
{
  int x, y;

  for(x=1; x<=6; x++)
    for(y=1; y<=7; y++)
             tabuleiro[x][y] =  ' ';                  
}

void mostra_tabuleiro(void)
{
  int x;  
    printf("    | 1 | 2 | 3 | 4 | 5 | 6 | 7 |\n");
    printf("    |---|---|---|---|---|---|---|\n");
  for(x=1; x<=6; x++) {
    printf("    | %c | %c | %c | %c | %c | %c | %c |\n",tabuleiro[x][1],tabuleiro[x][2],tabuleiro[x][3],tabuleiro[x][4],tabuleiro[x][5],tabuleiro[x][6],tabuleiro[x][7]);
   if (x!=0)
    printf("    |---|---|---|---|---|---|---|\n");
  }
  printf("\n");
}

void jogada(int n, int jog, int tipo)
{
  
     if (n % 2 == 1) {
           if (tipo<=2) jogada_Player(jog); else jogada_COM(jog);
     }
     else {
          if (tipo==1 || tipo==3) jogada_Player(jog); else jogada_COM(jog);
     }
}

int jogada_Player(int jog)
{
  int x, y, z, w, a, b, c, d, e;
  
  z=6;
  w=6;
  a=6;
  b=6;
  c=6;
  d=6;
  e=6;
  
  
   for(x=1; x<=6; x++){
           if(tabuleiro[x][1]!= ' ') z--;
           }
  for(x=1; x<=6; x++){
           if(tabuleiro[x][2]!= ' ') w--;
           }  
  for(x=1; x<=6; x++){
           if(tabuleiro[x][3]!= ' ') a--;
           }
  for(x=1; x<=6; x++){
           if(tabuleiro[x][4]!= ' ') b--;
           }          
  for(x=1; x<=6; x++){
           if(tabuleiro[x][5]!= ' ') c--;
           }
  for(x=1; x<=6; x++){
           if(tabuleiro[x][6]!= ' ') d--;
           }  
  for(x=1; x<=6; x++){
           if(tabuleiro[x][7]!= ' ') e--;
           }                
  
  if(z<1 && y==1){
         printf("Jogada invalida!\n");
         jogada_Player(jog);
                 }
  if(w<1 && y==2){
         printf("Jogada invalida!\n");
         jogada_Player(jog);
                 }
  if(a<1 && y==3){
         printf("Jogada invalida!\n");
         jogada_Player(jog);
                 }
  if(b<1 && y==4){
         printf("Jogada invalida!\n");
         jogada_Player(jog);
                 }
  if(c<1 && y==5){
         printf("Jogada invalida!\n");
         jogada_Player(jog);
                 }
  if(d<1 && y==6){
         printf("Jogada invalida!\n");
         jogada_Player(jog);
                 }
  if(e<1 && y==7){
         printf("Jogada invalida!\n");
         jogada_Player(jog);
                 }
  
  printf("Selecione a coluna pretendida: ");
  scanf("%d",&y);
  

  if(y<1 || y>7){
    printf("Jogada Invalida, tente novamente.\n");
    jogada_Player(jog);
  }
  
if(y==1){
           if (jog==1) tabuleiro[z][1] = 'X'; else tabuleiro[z][1] = 'O';
           return 1;
           }
  if(y==2){
           if (jog==1) tabuleiro[w][2] = 'X'; else tabuleiro[w][2] = 'O';
           return 1;
           }
  if(y==3){
           if (jog==1) tabuleiro[a][3] = 'X'; else tabuleiro[a][3] = 'O';
           return 1;
           }
  if(y==4){
           if (jog==1) tabuleiro[b][4] = 'X'; else tabuleiro[b][4] = 'O';
           return 1;
           }
  if(y==5){
           if (jog==1) tabuleiro[c][5] = 'X'; else tabuleiro[c][5] = 'O';
           return 1;
           }
  if(y==6){
           if (jog==1) tabuleiro[d][6] = 'X'; else tabuleiro[d][6] = 'O';
           return 1;
           }
  if(y==7){
           if (jog==1) tabuleiro[e][7] = 'X'; else tabuleiro[e][7] = 'O';
           return 1;
           }
  
  
  }



void jogada_COM(int jog)
{
  int x, y;
    
  for(x=1; x<=6; x++)
    for(y=1; y<=7; y++)
       if(tabuleiro[x][y]==' ') {
          if (jog==1) tabuleiro[x][y] ='X'; else tabuleiro[x][y] ='O';
          return ;
       }
  return;
}

char fim( int n)                                //objectivo: avaliar se o jogo acabou (4emlinha empate) ou nao
{
int i;
int j;

  for(i=0; i<6; i++){
    for(j=0; j<4; j++){                                   //testar 4 em linha linhas
             if(tabuleiro[i][j]==tabuleiro[i][j+1] && tabuleiro[i][j+2]==tabuleiro[i][j+3] &&tabuleiro[i][j]==tabuleiro[i][j+3] && tabuleiro[i][j]!=' ')
                 return tabuleiro[i][j];}}

  for(i=0; i<6; i++){
    for(j=0; j<4; j++){                                   //testar 4 em linha nas colunas
             if(tabuleiro[j][i]==tabuleiro[j+1][i] && tabuleiro[j+2][i]==tabuleiro[j+3][i] && tabuleiro[j][i]==tabuleiro[j+2][i] && tabuleiro[j][i]!=' ')
                 return tabuleiro[j][i];}}

  for(i=0; i<4; i++){
    for(j=0; j<3; j++){                                   //testar 4 em linha diagonais1 começar em cima
             if(tabuleiro[j][i]==tabuleiro[j+1][i+1] && tabuleiro[j+2][i+2]==tabuleiro[j+3][i+3] && tabuleiro[j][i]==tabuleiro[j+2][i+2] && tabuleiro[j][i]!=' ')
                 return tabuleiro[j][i];}}
                
   for(i=0; i<4; i++){
    for(j=5; j>2; j--){                                   //testar 4 em linha diagonais2 começar em baixo
             if(tabuleiro[j][i]==tabuleiro[j-1][i+1] && tabuleiro[j-2][i+2]==tabuleiro[j-3][i+3] && tabuleiro[j][i]==tabuleiro[j-2][i+2] && tabuleiro[j][i]!=' ')
                 return tabuleiro[j][i];}}
                
  
  if (n == 42) return 'E';
  
  return ' ';

}


     void finaliza(int ti, char venc)
{
     /* esta função não a consigo definir de maneira simples */
}
se tu criares um vector assim:
Código:
char tabuleiro[5];


vai ser criado um vector assim:
0 1 2 3 4
tabuleiro = [] [] [] [] []


em que tu se quiseres aceder à primeira posição tens de fazer

Código:
tabuleiro[0]

e se quiseres aceder à última

Código:
tabuleiro[4]


por isso nao podes percorrer o teu vector da pos 1 até à 6...


Podes começar por corrigir isso
URL's de Referência