Problem with a game program (Rooster game in c++)

Asked

Viewed 27 times

-2

I’m trying to develop the game of the rooster in c++, but when I try to compile the program I get the same mistake constantly (I’m still new in this world of programming so I’m not sure where I’m going wrong) My goal was that when button 1 was pressed, a 1-to-1 game would start in the game of the rooster with a scoring system and the ability to reset, but I cannot properly apply this command

  #include <iostream>
    #include <stdlib.h>
    #include <locale>
    
    #define Max_Tentativas 3
    
    using namespace std;
    void init(int board[] [3]);
    char printBlock(int block);
    void show (int board[] [3]);
    void playMove (int board[] [3], int);
    int checkContinue (int *board[3]);
    int checkWIn (int *board[3]);
    int game (int board[] [3]);
    void scoreboard (int, int &, int &);
    
    
    int main()
    {
        L1:
        setlocale (LC_ALL, "");
        system ("CLS");
        cout << "      _                                 _              ____           _\n";
        cout << "     | |   ___     __ _    ___       __| |   ___      / ___|   __ _  | |   ___  \n";
        cout << "  _  | |  / _ \\   / _` |  / _ \\     / _` |  / _ \\    | |  _   / _` | | |  / _ \\\n";
        std::cout << " | |_| | | (_) | | (_| | | (_) |   | (_| | | (_) |   | |_| | | (_| | | | | (_) | \n";
        std::cout << "  \\___/   \\___/   \\__, |  \\___/     \\__,_|  \\___/     \\____|  \\__,_| |_|  \\___/ \n";
        std::cout << "                  |___/                                                          \n";
        
        
        std::cout << "\nBem vindo ao jogo do galo \n";
        std::cout << "\t1- Jogador 1 vs Jogador 2\n";
        std::cout << "\t2- Jogador 1 vs CPU\n";
        std::cout << "\t3- Regras\n";
        std::cout << "\t4- Sair\n\n\n";
        std::cout << "(É recomendado jogar este jogo em fullscreen)\n";
        char num1;
        char cont;
        std::cout << "Opção: ";
        std::cin >> num1;
       
        
        switch (num1)
        {
        case '1':
             goto L2;
             break;
        case '2':
            
        case '3':
            system ("CLS");
            std::cout << "+----------------------------------------------------------------------------------------------------------------------------------------------+\n";
            std::cout << "|Regra nº1 - Os dois jogadores colocam, alternadamente, as suas peças de forma a construirem uma linha com 3 peças iguais em tabuleiros 3 × 3  |\n";
            std::cout << "|Regra nº2 - A linha de peças iguais pode ser construida na vertical, na horizontal ou na oblíqua                                              |\n";
            std::cout << "|Regra nº3 - Após três tentativas erradas o jogador perde automaticamente                                                                      |\n";
            std::cout << "+----------------------------------------------------------------------------------------------------------------------------------------------+\n\n";
            std::cout << "1- Voltar\n";
            cout << "Opção: ";
            cin >> num1;
            
            switch (num1)
            {
                case '1':
                goto L1;
                break;  
            }
        case '4':
            system ("CLS");
            std::cout << "Volte sempre :D";
            exit (0);   
            break;
        
        
        return 0;   
        }
    
        
    L2:
    string charName1 = "Player1";   
    string charName2 = "Player2"; 
        int jogo()
        {
            int board [3][3];
            int cont=0, player1=0, player2=0, result;
    
            do{
                init(board);
                result=game(board);
                show(board);
                scoreboard(result, player1, player2);
                std::cout<<"\n Outra partida?\n";
                std::cout<<"0. Sair\n";
                std::cout<<"1. Jogar de novo.\n";
                std::cin>>cont;
                system ("CLS");
            }while(cont);
            return 0;
        }
            void init(int board[][3])
            {
            for(int i=0;i<3;i++)
            for(int j=0;j<3;j++)
             board[i][j]=0;
            }
            char printBlock(int block)
            {
                if(block==0)
                 return ' ';
                else if(block==1)
                return 'X';
                else 
                return 'O';
            }
            void show(int board[][3])
            {
                cout<<endl;
                for(int row=0; row<3; row++){
                cout<<" "<<printBlock(board[row][0])<<" |";
                cout<<" "<<printBlock(board[row][1])<<" |";
                cout<<" "<<printBlock(board[row][2])<<endl;
                if(row!=2){
                    cout<<"___ ___ ___\n"<<endl;
                          }
                                           }
            }
            void playMove(int board[][3], int player)
            {
                int row, col, check;
                do{
                    cout<<"Linha: ";
                    cin>>row;
                    cout<<"Coluna: ";
                    cin>>col;
                    row--;col--;
                    check=board[row][col] || row <0 || row>2 || col<0 || col>2;
                    if(check)
                    cout<<"Essa casa não está vazia ou está fora do intervalo 3x3"<<endl;
                }while(check);
                if(player==0)
                board[row][col]=1;
                else 
                 board[row][col]=-1;
            }
                int checkContinue(int board[][3])
    {
        for(int i=0; i<3; i++)
         for(int j=0; j<3; j++)
          if(board[i][j]==0)
           return 1;
        return 0;
    }
    int checkWin(int board[][3])
    {
        int row, col, sum;
        for(row=0; row<3; row++){
            sum=0;
            for(col=0; col<3; col++)
             sum+=board[row][col];
            if(sum==3)
             return 1;
             system ("CLS");
            if(sum==-3)
             return -1;
             system ("CLS");
        }
        for(col=0; col<3; col++){
            sum=0;
            for(row=0; row<3; row++)
             sum+= board[row][col];
            if(sum==3)
             return 1;
             system ("CLS");
            if(sum==-3)
             return -1;
             system ("CLS");
        }
        sum=0;
        for(row=0; row <3; row++)
         sum += board[row][row];
        if(sum==3)
         return 1;
        if(sum==-3)
         return -1;
         system ("CLS");
         
        sum=board[0][2]+board[1][1]+board[2][0];
            if(sum==3)
             return 1;
            if(sum==-3)
             return -1;
             system ("CLS");
            return 0;
    }
    int game(int board[][3])
    {
    cout << "Player 1's name : ";
    cin >> charName1;
    cout << "Player 2's name : ";
    cin >> charName2;
        int turn=0, cont, win;
        do{
            show(board);
            cout<<"Jogador"<<1+turn%2<<endl;
            playMove(board, turn%2);
            turn++;
            cont=checkContinue(board);
            win=checkWin(board);
        }while(cont && !win);
        if(win==1){
            system ("CLS");
            cout<<"Jogador 1 - " << charName1 << " ganhou!\n"<<endl;
            return 1;
        }else
        if(win==-1){
            system ("CLS");
            cout<<"Jogador 2 - " << charName2 <<" ganhou!\n"<<endl;
            return 2;
        }else
            cout<<"Empate\n"<<endl;
            return 0;
    }
    void scoreboard(int result, int &player1, int &player2)
    {
        if(result==1)
         player1++;
        if(result==2)
         player2++;
        cout<<"\nPlacar: "<<endl;
        cout<<player1<<"x"<<player2<<endl;
    }   
    
        return 0;
    }

1 answer

-1

The problem is that you are setting functions within the function main. In C++ it is not possible to do this. To fix, just put your functions at the top level of the program, this way:

 #include <iostream>
 #include <stdlib.h>
 #include <locale>

 #define Max_Tentativas 3

 using namespace std;
 void init(int board[][3]);
 char printBlock(int block);
 void show(int board[][3]);
 void playMove(int board[][3], int);
 int checkContinue(int * board[3]);
 int checkWIn(int * board[3]);
 int game(int board[][3]);
 void scoreboard(int, int & , int & );

 int jogo() {
     // ...
 }
 void init(int board[][3]) {
     // ...
 }
 char printBlock(int block) {
     // ...
 }
 void show(int board[][3]) {
     // ...
 }
 void playMove(int board[][3], int player) {
     // ...
 }
 int checkContinue(int board[][3]) {
     // ...
 }
 int checkWin(int board[][3]) {
     // ...
 }
 int game(int board[][3]) {
     // ...
 }
 void scoreboard(int result, int & player1, int & player2) {
     // ...
 }

 int main() {
     // ...
 }

Another problem is that you are declaring charName1 and charName2 out of function game:

 int game(int board[][3]) {
     string charName1 = "Player1";
     string charName2 = "Player2";

     cout << "Player 1's name : ";
     cin >> charName1;
     cout << "Player 2's name : ";
     cin >> charName2;

     // ...

Finally, you’re not calling jogo in function main:

 int main() {
     // ...

     L2: jogo();
     return 0;
 }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.