Results 1 to 5 of 5

Thread: Help with 2d arrays, code almost complete*

  1. #1
    Join Date
    Sep 2006
    Location
    Texas
    Posts
    1,349
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default Help with 2d arrays, code almost complete*

    Code:
    /////////////////////////////////////////////////////////////////////
    // 
    // Name: <Put your name here>
    // Date: <Today's date>
    // Class: CSCI/CMPE 1170.03
    // Semester: Fall 2011
    // CSCI/CMPE 1370 Instructor: Christine Reilly
    //
    // This program simulates a tic-tac-toe game. is uses a 2d array to 
    // store the values of 'X' or 'O' and displays them on a board in
    // the console screen. every turn the program checks for a winner. if 
    // there is no winner, the game is tied. the program terminates after 
    // one match whether there is a winner or the game is tied.
    //
    /////////////////////////////////////////////////////////////////////
    
    #include<iostream>
    #include<iomanip>
    
    using namespace std;
    
    void drawBoard(char board[][3]);
    
    char checkWinner3by3(char board[][3]);
    
    //
    // The main funciton is provided for you.
    //
    // DO NOT MODIFY THE MAIN FUNCTION
    //
    int main()
    {
        // This array of chars represents the game board, and it holds the content
        // of each space. By default all spaces are set to a blank space.
        char board[3][3] = {{' ',' ',' '},{' ',' ',' '},{' ',' ',' '}};
        
        // The current player. Because X plays first, initialize to X
        char player = 'X';
    	
        // The winner. either 'X', 'O', or 't' if it's a tie.
        // Or a blank space if the game has not finished.
        char winner = ' ';
    	
        // These variables will hold the number of the row and column selected
        // by the players.
        int row;
        int column;
    	
        // boolean variables used to verify if the move is valid.
        bool is_move;
        bool is_row;
        bool is_column;
    
        cout<<"************ TIC TAC TOE ************\n";
    
        // The game loops again and again until the game is over
        while(winner == ' ')
        {
    		//set the boolean variables back to false for a new turn.
            is_move = false; 
            is_row = false;
            is_column = false;
    		
            //draw the board.
            drawBoard(board);
    				
            // If the game is not yet over show who`s the next player to move
            cout << "Player ";
            if(player == 'X')
            {
                cout << 1;
            }
            else
            {
                cout << 2;
            }
            cout << "'s turn:" << endl;
    
            // Loop until the player selects a valid space for their move
            is_move = false;
            while(!is_move)
            {  
                // Loop until the player selects a valid row
                // Assume the user inputs a valid integer
                is_row = false;
                while(!is_row)
                {
                    cout << "Row: ";
                    cin >> row;
                    if(row == 1 || row == 2 || row == 3)
                    {
                        is_row = true;
                    }
                    else
                    {
                        cout << endl << "Invalid row!" << endl;
                    }
                } // end of input row loop
                
                // Loop until the player selects a valid column
                // Assume the user inputs a valid integer
                is_column = false;
                while(!is_column)
                {
                    cout << "Column: ";
                    cin >> column;
                    if(column == 1 || column == 2 || column == 3)
                    {
                        is_column = true;
                    }
                    else
                    {
                        cout << endl << "Invalid column!" << endl;
                    }
                } // end of input column loop
    			
                // If the space is empty, mark the chosen space and swich players
                if(board[row-1][column-1] == ' ')
                {
                    // Mark the space and end the player's turn
                    board[row-1][column-1] = player;
                    is_move = true;
                    
                    // Switch to the other player
                    if(player == 'X')
                    {
                        player = 'O';
                    }
                    else
                    {
                        player = 'X';
                    }
                } // end of marking space and changing players
                
                // If the space is occupied
                else
                {
                    cout<<"The selected space is occupied!" << endl;
                    cout << "Select a different space:" << endl << endl;
                    drawBoard(board);
                }
            } // end of player's move loop
            
            cout << endl; // for nice output formatting
            
            //check if the player won.
            winner = checkWinner3by3(board);
    		
            //If there's a winner
            if(winner == 'X' || winner == 'O')
            {
                drawBoard(board);
                
                //Display congratulations message
                cout<<"Congratulations! Player ";
                if(winner == 'X')
                {
                    cout << 1;
                }
                else
                {
                    cout << 2;
                }
                cout<<" is the winner!"<<endl;
            }
            else if(winner == 'T')
            {
                drawBoard(board);
                
                //Display a message if it`s tie
                cout << "It's a tie!" << endl;
            }
            
        } // End of player's turn loop
    
        system("pause");
        return 0;
    }
    
    
    //
    // Prints the game board
    // We know the board is 3 by 3 so we don't need to have the number of rows as
    // a parameter.
    //
    // WRITE THIS FUNCTION
    //
    void drawBoard(char board[][3])
    {
        //Draw the top line of the board
    cout << "     1   2   3" << endl << "   +---+---+---+" << endl;     
    for(int i = 1; i<4; i++)
    {         
    cout << " " << i << " | " << board[i-1][0] << " | " << board[i-1][1] << " | " << board[i-1][2] << " |" << endl << "   +---+---+---+" << endl;
        
        
    
    
    
    
    }
    
    //
    // Checks the whole board if there is a winner.
    // We know the board is 3 by 3 so we don't need to have the number of rows as
    // a parameter.
    //
    // WRITE THIS FUNCTION
    //
    char checkWinner3by3(char board[][3])
    {
    int winner;
    
         for(int i=0;i<3;i++)
    	 {
    		if(board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][0] != ' ')
    		{
    		winner = board[i][0];}      
    		}
    
    	for(int i=0;i<3;i++)
    	{
    		if(board[0][i] == board[1][i] && board[1][i] == board[2][i] && board[0][i] != ' ')
    	{
    		winner = board[0][i]; 
    	}     
       }  
    
        if(board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[0][0] != ' ')
    	{
    	winner = board[0][0];      
    	}   
    
    	if(board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[0][2] != ' ')
    	{ 
    	winner = board[0][2];      
    	}  
    
    	if(board[0][0] == board[0][1] && board[0][1] == board[0][2] && board[0][2] == board[1][0] && board[1][0] == board[1][1] && board[1][1] == board[1][2] && board[1][2] == board[2][0] && board[2][0] == board[2][1] && board[2][1] == board[2][2] && board[0][0] != ' ')
    	{
    	winner = 't';      
    	}
    }
    Help :x

    Trying to make it check for a winner but it wont run. Coming up with 2 errors.

    This is a tic-tac-toe game.

    Error 1 error C2601: 'checkWinner3by3' : local function definitions are illegal c:\users\jtluna\desktop\tictactoe\tictactoe\tictac toe.cpp 211 ticTacToe
    Error 2 fatal error C1075: end of file found before the left brace '{' at 'c:\users\jtluna\desktop\tictactoe\tictactoe\ticta ctoe.cpp(189)' was matched c:\users\jtluna\desktop\tictactoe\tictactoe\tictac toe.cpp 243 ticTacToe

  2. #2
    Join Date
    Feb 2007
    Location
    PA, USA
    Posts
    5,240
    Mentioned
    36 Post(s)
    Quoted
    496 Post(s)

    Default

    second error looks like it'd be simple to solve.
    did you look to make sure you used closure squiglys?

  3. #3
    Join Date
    Jan 2008
    Location
    Ontario, Canada
    Posts
    7,805
    Mentioned
    5 Post(s)
    Quoted
    3 Post(s)

    Default

    You really need to work on your standards..

    It would show you why you're getting both of those errors.
    Writing an SRL Member Application | [Updated] Pascal Scripting Statements
    My GitHub

    Progress Report:
    13:46 <@BenLand100> <SourceCode> @BenLand100: what you have just said shows you 
                        have serious physchological problems
    13:46 <@BenLand100> HE GETS IT!
    13:46 <@BenLand100> HE FINALLY GETS IT!!!!1

  4. #4
    Join Date
    Jan 2010
    Posts
    5,227
    Mentioned
    6 Post(s)
    Quoted
    60 Post(s)

    Default

    Code:
    /////////////////////////////////////////////////////////////////////
    //
    // Name: <Put your name here>
    // Date: <Today's date>
    // Class: CSCI/CMPE 1170.03
    // Semester: Fall 2011
    // CSCI/CMPE 1370 Instructor: Christine Reilly
    //
    // This program simulates a tic-tac-toe game. is uses a 2d array to
    // store the values of 'X' or 'O' and displays them on a board in
    // the console screen. every turn the program checks for a winner. if
    // there is no winner, the game is tied. the program terminates after
    // one match whether there is a winner or the game is tied.
    //
    /////////////////////////////////////////////////////////////////////
    
    #include <iostream>
    #include <iomanip>
    
    using namespace std;
    
    void drawBoard(char board[][3]);
    
    char checkWinner3by3(char board[][3]);
    
    //
    // The main funciton is provided for you.
    //
    // DO NOT MODIFY THE MAIN FUNCTION
    //
    int main()
    {
        // This array of chars represents the game board, and it holds the content
        // of each space. By default all spaces are set to a blank space.
        char board[3][3] = {{' ', ' ', ' '}, {' ', ' ', ' '}, {' ', ' ', ' '}};
    
        // The current player. Because X plays first, initialize to X
        char player = 'X';
    
        // The winner. either 'X', 'O', or 't' if it's a tie.
        // Or a blank space if the game has not finished.
        char winner = ' ';
    
        // These variables will hold the number of the row and column selected
        // by the players.
        int row;
        int column;
    
        // boolean variables used to verify if the move is valid.
        bool is_move;
        bool is_row;
        bool is_column;
    
        cout << "************ TIC TAC TOE ************\n";
    
        // The game loops again and again until the game is over
        while(winner == ' ')
        {
    		//set the boolean variables back to false for a new turn.
            is_move = false;
            is_row = false;
            is_column = false;
    
            //draw the board.
            drawBoard(board);
    
            // If the game is not yet over show who`s the next player to move
            cout << "Player ";
            if(player == 'X')
                cout << 1;
            else
                cout << 2;
            cout << "\'s turn:" << endl;
    
            // Loop until the player selects a valid space for their move
            is_move = false;
            while(!is_move)
            {
                // Loop until the player selects a valid row
                // Assume the user inputs a valid integer
                is_row = false;
                while(!is_row)
                {
                    cout << "Row: ";
                    cin >> row;
                    if(row == 1 || row == 2 || row == 3)
                        is_row = true;
                    else
                        cout << "\nInvalid row!" << endl;
                } // end of input row loop
    
                // Loop until the player selects a valid column
                // Assume the user inputs a valid integer
                is_column = false;
                while(!is_column)
                {
                    cout << "Column: ";
                    cin >> column;
                    if(column == 1 || column == 2 || column == 3)
                        is_column = true;
                    else
                        cout << "\nInvalid column!" << endl;
                } // end of input column loop
    
                // If the space is empty, mark the chosen space and swich players
                if(board[row-1][column-1] == ' ')
                {
                    // Mark the space and end the player's turn
                    board[row-1][column-1] = player;
                    is_move = true;
    
                    // Switch to the other player
                    if(player == 'X')
                        player = 'O';
                    else
                        player = 'X';
                } // end of marking space and changing players
    
                // If the space is occupied
                else
                {
                    cout << "The selected space is occupied!\n";
                    cout << "Select a different space:\n" << endl;
                    drawBoard(board);
                }
            } // end of player's move loop
    
            cout << endl; // for nice output formatting
    
            //check if the player won.
            winner = checkWinner3by3(board);
    
            //If there's a winner
            if(winner == 'X' || winner == 'O')
            {
                drawBoard(board);
    
                //Display congratulations message
                cout << "Congratulations! Player ";
                //cout << winner == 'X' ? 1 : 2;
                if(winner == 'X')
                    cout << 1;
                else
                    cout << 2;
                cout << " is the winner!" << endl;
            }
            else if(winner == 'T')
            {
                drawBoard(board);
    
                //Display a message if it`s tie
                cout << "It's a tie!" << endl;
            }
    
        } // End of player's turn loop
    
        system("pause");
        return 0;
    }
    
    
    //
    // Prints the game board
    // We know the board is 3 by 3 so we don't need to have the number of rows as
    // a parameter.
    //
    // WRITE THIS FUNCTION
    //
    void drawBoard(char board[][3])
    {
        //Draw the top line of the board
        cout << "     1   2   3\n   +---+---+---+" << endl;
        for(int i = 1; i < 4; i++)
            cout << " " << i << " | " << board[i-1][0] << " | " << board[i-1][1] << " | " << board[i-1][2] << " |\n   +---+---+---+" << endl;
    }
    
    //
    // Checks the whole board if there is a winner.
    // We know the board is 3 by 3 so we don't need to have the number of rows as
    // a parameter.
    //
    // WRITE THIS FUNCTION
    //
    char checkWinner3by3(char board[][3])
    {
        int winner;
    
        for(int i = 0; i < 3; i++)
            if(board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][0] != ' ')
                winner = board[i][0];
    
    	for(int i=0;i<3;i++)
    		if(board[0][i] == board[1][i] && board[1][i] == board[2][i] && board[0][i] != ' ')
                winner = board[0][i];
    
        if(board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[0][0] != ' ')
            winner = board[0][0];
    
    	if(board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[0][2] != ' ')
            winner = board[0][2];
    
    	if(board[0][0] == board[0][1] && board[0][1] == board[0][2] && board[0][2] == board[1][0] &&
            board[1][0] == board[1][1] && board[1][1] == board[1][2] && board[1][2] == board[2][0] &&
                board[2][0] == board[2][1] && board[2][1] == board[2][2] && board[0][0] != ' ')
            winner = 'T';
    }
    Doesn't function exactly how it should, but at least it compiles.

  5. #5
    Join Date
    Nov 2012
    Posts
    1
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    inside the checkwinner3by3() initialize the winner variable to winner = ' '; and add 2 new ints, int i, j , c =0;
    and replace
    this
    if(board[0][0] == board[0][1] && board[0][1] == board[0][2] && board[0][2] == board[1][0] &&
    board[1][0] == board[1][1] && board[1][1] == board[1][2] && board[1][2] == board[2][0] &&
    board[2][0] == board[2][1] && board[2][1] == board[2][2] && board[0][0] != ' ')
    winner = 'T';
    with this

    for (i=0; i<3 ; i++){
    for (j=0; j<3 ; j++){
    if (board[i][j] != ' ')
    c++;
    }
    }

    if(c==9 && winner == ' ')
    winner ='T';
    finally return winner... and that's it...

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •