Thread: Help!4 in a row game, error in source code.

  1. #1
    Registered User
    Join Date
    Jun 2013
    Posts
    1

    Question Help!4 in a row game, error in source code.

    Hello

    Me and a collegue are doing our c programming project for a 4 in a row game.
    In the line marked bold and red we get this message:
    error: expected declaration sprecifiers declaration before ´/´ token,and we are able to find were is the error.
    can anybody help us ?

    Thanks
    Code:
    #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("**************************4EMLINH..........************************\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_jogo(int n) //objectivo: avaliar se o jogo acabou (4emlinha empate) ou nao
    {
        int i; // iterador de linha
        int j; // iterador de coluna
    
    
        // ciclo de procura nas colunas
        for(i=0; i<6; i++){
            for(j=0; j< (7 - 4)/* nao e preciso procurar nas ultimas posicoes */; j++){
                 if(tabuleiro[i][j]!=' ' &&
                    tabuleiro[i][j]==tabuleiro[i][j+1] &&
                    tabuleiro[i][j]==tabuleiro[i][j+2] &&
                    tabuleiro[i][j]==tabuleiro[i][j+3])
                     return tabuleiro[i][j];
            }
        }
    
    
        // ciclo de procura nas linhas
        for(i=0; i<(6 - 4)/* nao e preciso procurar nas ultimas posicoes */; i++){
            for(j=0; j< 7; j++){
                 if(tabuleiro[i][j]!=' ' &&
                    tabuleiro[i][j]==tabuleiro[i+1][j] &&
                    tabuleiro[i][j]==tabuleiro[i+2][j] &&
                    tabuleiro[i][j]==tabuleiro[i+3][j])
                     return tabuleiro[i][j];
            }
        }
    
    
        // ciclo de procura na diagonal esquerda-direita
        for(i=0; i<(6 - 4)/* nao e preciso procurar nas ultimas posicoes */; i++){
            for(j=0; j<(7 - 4)/* nao e preciso procurar nas ultimas posicoes */; j++){
                 if(tabuleiro[i][j]!=' ' &&
                    tabuleiro[i][j]==tabuleiro[i+1][j+1] &&
                    tabuleiro[i][j]==tabuleiro[i+2][j+2] &&
                    tabuleiro[i][j]==tabuleiro[i+3][j+3])
                     return tabuleiro[i][j];
            }
        }
    
    
        // ciclo de procura na diagonal direita-esquerda
        for(i=0; i<(6 - 4)/* nao e preciso procurar nas ultimas posicoes */; i++){
            for(j=0; j<(7 - 4)/* nao e preciso procurar nas ultimas posicoes */; j++){
                 if(tabuleiro[i][j+3]!=' ' &&
                    tabuleiro[i][j+3]==tabuleiro[i+1][j+2] &&
                    tabuleiro[i][j+3]==tabuleiro[i+2][j+1] &&
                    tabuleiro[i][j+3]==tabuleiro[i+3][j])
                     return tabuleiro[i][j];
            }
        }
    
    
        if (n==42) return 'E';
    
    
        return ' ';
    }
    
    
    
    
    
    
         void finaliza(int ti, char venc)
    
    
    {
    
    
    }

  2. #2
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Code:
    gcc 4.c -ansi
    4.c: In function ‘fim_jogo’:
    4.c:238:22: error: expected declaration specifiers before ‘/’ token
     char fim_jogo(int n) //objectivo: avaliar se o jogo acabou (4emlinha empate) ou nao
                          ^
    Guess your compiler doesnt support single line comments ( // )
    Kurt

  3. #3
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Presumably your compiler doesn't like double-slash comments. Use the /* */ form of comment instead.

    There are other problems. The worst is that you call jogada_Player recursively when a move is invalid. This will not work. You need to use a loop instead.

    And jogada_Player should return something at the very end of the function. Presumably 0.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well it's one of the problems you're always going to face when you simply copy/paste code without getting clued up yourself.
    Ajuda no jogo 4 em linha - Liguagem C
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Error message when compiling source code
    By vangodo in forum C Programming
    Replies: 19
    Last Post: 09-24-2011, 04:07 PM
  2. 'Type' Error on Build of Officially Released Source Code
    By Jedi_Mediator in forum C++ Programming
    Replies: 5
    Last Post: 07-07-2008, 05:28 PM
  3. Game of Craps Source Code
    By Miles in forum C Programming
    Replies: 5
    Last Post: 02-17-2002, 01:46 PM
  4. 2 error in source code
    By xlordt in forum C Programming
    Replies: 1
    Last Post: 01-06-2002, 02:55 AM