Visualizzazione dei risultati da 1 a 7 su 7
  1. #1

    [c++] Othello - implementare classi

    Salve a tutti, ho questo programma:
    codice:
    #include <iostream>
    #include <cstdlib>
    using namespace std;
    class Othello
    {
    
    
    public:
           void DisplayBoard;
           int PlaceDisk;
           int MoveIsPossible;
           void GetP1Move;
           void GetP2Move;
           void SaveBoard;
           
    private:      
    // Board define.
    const int ROWS = 8;  
    const int COLS = 8;}
    
    
    
    // Discs define.
    const char PLAYER1    = 'X';
    const char PLAYER2    = 'O';
    
    // Display the board to the screen.
    // [in] board - Game board
    void Othello::DisplayBoard(const char board[][COLS])
    {
        cout << " ";
        for (int c = 0; c < COLS; c++) 
            cout << c;
        cout << endl;
    
        for (int r = 0; r < ROWS; r++)
        {
            cout << r;
            for (int c = 0; c < COLS; c++)
                cout << board[r][c];
            cout << endl;
        }
    }
         
    /****************************************************************************/
    
    // Place the disc on the board, flip the discs affected, and
    // return the number of discs flipped.  Note if 0 is returned,
    // the position moved is illegal, and the board is not changed.
    // [in/out] board - Game board
    // [in]     row   - Row in board to move to
    // [in]     col   - Col in board to move to
    // [in]     disc  - Player's disc to be placed (X or O)
    int Othello::PlaceDisc(char board[][COLS], int row, int col, char disc)
    {
        int num_flipped = 0;
    
        if (board[row][col] != ' ')
            return 0;
    
        board[row][col] = disc;
    
        char opposing_disc;
        if (disc == PLAYER1)
            opposing_disc = PLAYER2;
        else
            opposing_disc = PLAYER1;
    
    // Do we have discs to the right to flip?
        int disc_pos = -1;
        for (int c = col + 1; c < COLS && board[row][c] != ' ' && disc_pos == -1; c++)
        {
            if (board[row][c] == disc)
                disc_pos = c;
        }
    
        // Make sure we found a disc and that it is at least 2 spots away
        if (disc_pos != -1 && disc_pos > col + 1)
        {
            // Flip discs to the right
            for (int c = col + 1; c < disc_pos; c++)
            {
                board[row][c] = disc;
                num_flipped++;
            }
        }
    
        // Do we have discs to the left to flip?
        disc_pos = -1;
        for (int c = col - 1; c >= 0 && board[row][c] != ' ' && disc_pos == -1; c--)
        {
            if (board[row][c] == disc)
                disc_pos = c;
        }
    
        //if (disc_pos > -1) cout << "left disc_pos = " << disc_pos << endl;
    
        // Make sure we found a disc and that it is at least 2 spots away
        if (disc_pos != -1 && disc_pos < col - 1)
        {
            // Flip discs to the left
            for (int c = col - 1; c > disc_pos; c--)
            {
                board[row][c] = disc;
                num_flipped++;
            }
        }
    
        // Do we have discs above to flip?
        disc_pos = -1;
        for (int r = row - 1; r >= 0 && board[r][col] != ' ' && disc_pos == -1; r--)
        {
            if (board[r][col] == disc)
                disc_pos = r;
        }
    
        // Make sure we found a disc and that it is at least 2 spots away
        if (disc_pos != -1 && disc_pos < row - 1)
        {
            // Flip discs above
            for (int r = row - 1; r > disc_pos; r--)
            {
                board[r][col] = disc;
                num_flipped++;
            }
        }
    
        // Do we have discs below to flip?
        disc_pos = -1;
        for (int r = row + 1; r < ROWS && board[r][col] != ' ' && disc_pos == -1; r++)
        {
            if (board[r][col] == disc)
                disc_pos = r;
        }
    
        // Make sure we found a disc and that it is at least 2 spots away
        if (disc_pos != -1 && disc_pos > row + 1)
        {
            // Flip discs below
            for (int r = row + 1; r < disc_pos; r++)
            {
                board[r][col] = disc;
                num_flipped++;
            }
        }
    
        // Do we have discs diagnally up-left to flip?
        disc_pos = -1;
        int c = col - 1;
        for (int r = row - 1; c >= 0 && r >= 0 && board[r][c] != ' ' && disc_pos == -1; r--)
        {
            if (board[r][c] == disc)
                disc_pos = r;
            c--;
        }
    
        // Make sure we found a disc and that it is at least 2 spots away
        if (disc_pos != -1 && disc_pos < row - 1)
        {
            c = col - 1;
            for (int r = row - 1; r > disc_pos; r--)
            {
                board[r][c] = disc;
                num_flipped++;
                c--;
            }
        }
    
        // Do we have discs diagnally up-right to flip?
        disc_pos = -1;
        c = col + 1;
        for (int r = row - 1; c < COLS && r >= 0 && board[r][c] != ' ' && disc_pos == -1; r--)
        {
            if (board[r][c] == disc)
                disc_pos = r;
            c++;
        }
    
        // Make sure we found a disc and that it is at least 2 spots away
        if (disc_pos != -1 && disc_pos < row - 1)
        {
            c = col + 1;
            for (int r = row - 1; r > disc_pos; r--)
            {
                board[r][c] = disc;
                num_flipped++;
                c++;
            }
        }
    
        // Do we have discs diagnally down-left to flip?
        disc_pos = -1;
        c = col - 1;
        for (int r = row + 1; c >= 0 && r < ROWS && board[r][c] != ' ' && disc_pos == -1; r++)
        {
            if (board[r][c] == disc)
                disc_pos = r;
            c--;
        }
    
        // Make sure we found a disc and that it is at least 2 spots away
        if (disc_pos != -1 && disc_pos > row + 1)
        {
            c = col - 1;
            for (int r = row + 1; r < disc_pos; r++)
            {
                board[r][c] = disc;
                num_flipped++;
                c--;
            }
        }
    
        // Do we have discs diagnally down-right to flip?
        disc_pos = -1;
        c = col + 1;
        for (int r = row + 1; c < COLS && r < ROWS && board[r][c] != ' ' && disc_pos == -1; r++)
        {
            if (board[r][c] == disc)
                disc_pos = r;
            c++;
        }
    
        // Make sure we found a disc and that it is at least 2 spots away
        if (disc_pos != -1 && disc_pos > row + 1)
        {
            c = col + 1;
            for (int r = row + 1; r < disc_pos; r++)
            {
                board[r][c] = disc;
                num_flipped++;
                c++;
            }
        }
    
        // Reset board if nothing was flipped... illegal move
        if (num_flipped == 0)
            board[row][col] = ' ';
    
        return num_flipped;
    }
    
    /****************************************************************************/
    
    // Check to see if the given player can make a valid move on the game board.
    // Return 1 if a move can be made, 0 otherwise.
    // [in] board  - Game board
    // [in] player - Character representing the player (X or O)
    int Othello::MoveIsPossible(const char board[][COLS], char player)
    {
        return 1;
    }
    
    /****************************************************************************/
    
    // Get the human's move (if a legal move is possible) and place it on the board.  
    // [in/out] board - Game board
    void Othello::GetP1Move(char board[][COLS])
    {
        int row, col;
        bool illegal_move;
    
        // Make sure there is a legal move that can be made
        if (MoveIsPossible(board, PLAYER1))
        {
            do
            {
                illegal_move = false;
    
                cout << "Tocca al Giocatore 1" << endl;
                cout << "Row? ";
                cin >> row;
                while (row < 0 || row >= ROWS)
                {
                    cout << "Please select a row between 0 and " << (ROWS - 1) << ".\n";
                    cout << "Row? ";
                    cin >> row;
                }
    
                cout << "Col? ";
                cin >> col;
                while (col < 0 || col >= COLS)
                {
                    cout << "Please select a column between 0 and " << (COLS - 1) << ".\n";
                    cout << "Col? ";
                    cin >> col;
                }
    
                if (board[row][col] != ' ')
                {
                    cout << "Please select an empty row and column.\n";
                    illegal_move = 1;
                }
                else
                {
                    int discs_flipped = PlaceDisc(board, row, col, PLAYER1);    
                    if (discs_flipped == 0)
                    {
                        cout << "Illegal move.\n";
                        illegal_move = true;
                    }
                    else if (discs_flipped == 1)
                        cout << "Flipped 1 disc.\n\n";
                    else
                        cout << "Flipped " << discs_flipped << " discs.\n\n";
                }
            } while (illegal_move);
        }
        else
        {
            cout << "X cannot move.\n";
        }
    }
    
    // Get the human's move (if a legal move is possible) and place it on the board.  
    // [in/out] board - Game board
    void Othello::GetP2Move(char board[][COLS])
    {
        int row, col;
        bool illegal_move;
    
        // Make sure there is a legal move that can be made
        if (MoveIsPossible(board, PLAYER2))
        {
            do
            {
                illegal_move = false;
    
                cout << "Tocca al Giocatore 2" << endl;
                cout << "Row? ";
                cin >> row;
                while (row < 0 || row >= ROWS)
                {
                    cout << "Please select a row between 0 and " << (ROWS - 1) << ".\n";
                    cout << "Row? ";
                    cin >> row;
                }
    
                cout << "Col? ";
                cin >> col;
                while (col < 0 || col >= COLS)
                {
                    cout << "Please select a column between 0 and " << (COLS - 1) << ".\n";
                    cout << "Col? ";
                    cin >> col;
                }
    
                if (board[row][col] != ' ')
                {
                    cout << "Please select an empty row and column.\n";
                    illegal_move = 1;
                }
                else
                {
                    int discs_flipped = PlaceDisc(board, row, col, PLAYER2);    
                    if (discs_flipped == 0)
                    {
                        cout << "Illegal move.\n";
                        illegal_move = true;
                    }
                    else if (discs_flipped == 1)
                        cout << "Flipped 1 disc.\n\n";
                    else
                        cout << "Flipped " << discs_flipped << " discs.\n\n";
                }
            } while (illegal_move);
        }
        else
        {
            cout << "O cannot move.\n";
        }
    }
    
    void Othello::SaveBoard(char board[][COLS], char temp_board[][COLS])
    {
    
        for( int i = 0; i < ROWS; i++)
        {
            for(int j =0; j < COLS; j++)
                temp_board[i][j]= board[i][j];
        }
    }
    
    int main()
    {
    
        
        char board[ROWS][COLS];
    
    
        // Create a test configuration
        strncpy(board[0],"        ", COLS);
        strncpy(board[1],"        ", COLS);
        strncpy(board[2],"        ", COLS);
        strncpy(board[3],"   OX   ", COLS);
        strncpy(board[4],"   XO   ", COLS);
        strncpy(board[5],"        ", COLS);
        strncpy(board[6],"        ", COLS);
        strncpy(board[7],"        ", COLS);
    
    
           DisplayBoard(board);
    
           cout << endl;
    do {
           GetP1Move(board);
        
           DisplayBoard(board);
        
           GetP2Move(board);
        
           DisplayBoard(board);
                               }
    while (MoveIsPossible );
            
    
    system("PAUSE");
    }
    Se tolgo la classe, e lo faccio funzionare con le sole funzioni, non ha problemi a funzionare. Però non appena inserisco la classe Othello, tutto va a rotoli.. come mai? Sbaglio qualcosa nella sintassi?
    Grazie mille..

  2. #2
    1. Manca un ; alla fine della dichiarazione della classe.
    2. Se non crei nemmeno una istanza della classe Othello e ti riferisci ai suoi membri come se fossero globali come puoi pretendere che funzioni?
    Amaro C++, il gusto pieno dell'undefined behavior.

  3. #3
    Utente di HTML.it L'avatar di oregon
    Registrato dal
    Jul 2005
    residenza
    Roma
    Messaggi
    36,466

    Re: [c++] Othello - implementare classi

    Originariamente inviato da lordsabbath
    ... appena inserisco la classe Othello, tutto va a rotoli .. come mai?
    Probabilmente dovresti studiare un po' di più (e meglio) come si programma con le classi, prima di usarle ...

    Cioè, non pensare di poter passare dalla programmazione "funzionale" a quella "OOP" aggiungendo delle linee di codice ... bisogna anche impiegare un po' di tempo a studiare la teoria ...
    No MP tecnici (non rispondo nemmeno!), usa il forum.

  4. #4
    Ti ringrazio per la risposta.. che cosa intendi per "creare un istanza per la classe"?
    Io fino ad ora ho programmato senza utilizzare le classi e per me sono una cosa nuova purtroppo come immagino si veda non ci prendo molto

    @oregon: lo so, però purtroppo il mio professore di programmazione ad oggetti spiega davvero da cani, e il libro di testo è fatto pure peggio! Lui fondamentalmente ci ha detto che questo programma è a se stante anche senza l'utilizzo della classe.. e che vista la sua struttura piuttosto semplice poi potevamo trasformarlo inserendo una classe subito dopo..

    Non pretendo che mi rifacciate il programma.. ma se voleste darmi qualche aiutino mi sarebbe di grande aiuto
    Grazie comunque!

  5. #5
    Partiamo dalle basi... sai cos'è una classe? E cosa vuol dire istanziarla?

    Per il professore di informatica... hai la mia comprensione ; ora che hanno inflitto anche a me qualche lezione universitaria di C++ comprendo un sacco di domande qui sul forum.
    Amaro C++, il gusto pieno dell'undefined behavior.

  6. #6
    Utente di HTML.it L'avatar di oregon
    Registrato dal
    Jul 2005
    residenza
    Roma
    Messaggi
    36,466
    Originariamente inviato da MItaly
    Per il professore di informatica... hai la mia comprensione ; ora che hanno inflitto anche a me qualche lezione universitaria di C++ comprendo un sacco di domande qui sul forum.
    Mah ... sarò stato fortunato ma, sia per Informatica I sia per Informatica III (e specialmente per quest'ultima) ho avuto professori veramente in gamba ...
    No MP tecnici (non rispondo nemmeno!), usa il forum.

  7. #7
    Non saprei... sarà che il mio informatica 1 è all'interno del corso di laurea in fisica (e non in informatica); in ogni caso se non sbaglio anche XWolverineX (che sappiamo tutti che il C++ lo conosce) si lamenta spesso del suo corso di C++, e mi pare proprio che lui faccia informatica.
    Amaro C++, il gusto pieno dell'undefined behavior.

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2024 vBulletin Solutions, Inc. All rights reserved.