CSCI 131 Techniques of Programming--Spring 2014

    Home | | Syllabus | | Assignments | | Lectures | | Resources

    Solution to Project 4 proj4.cc

    //********************************************************
    // proj4.cc
    //     Author: A good solution writer
    //     Date: April 11, 2014
    //     Class: CSCI 131 project4, Professor Royden
    //     Purpose: Plays the game of Tic Tac Toe
    //     Input: (from keyboard) Inputs users' desire to play
    //                 the game (again). Inputs users' moves (row and column)
    //     Output: (to monitor) Outputs board with X's and O's in
    //              played positions; declaration of winner; total
    //              wins for each player and total draws.
    //********************************************************
    
    #include <iostream>
    #include <iomanip>
    #include "board.h"
    using namespace std;
    
    int main (void)
    {
        board playingBoard;  //3x3 tic-tac-toe board
        char answerX;          //X's answer to 'want to play?' question
        char answerO;          //O's answer to 'want to play?' question
        int  Xwins = 0;        //number of times X won
        int  Owins = 0;        //number of times O won
        int  draws = 0;        //number of times the game ended in a draw
        char which;             //current player ('X' or 'O')
        int done;                //true (1) if game done, otherwise 0
    
        //Ask players if they want to play
        cout << "\nPlayer 1 - would you like to play tic-tac-toe? ";
        cin  >> answerX;
        cout << answerX << endl;
        answerX = toupper( answerX );
    
        cout << "\nPlayer 2 - would you like to play tic-tac-toe? ";
        cin  >> answerO;
        cout << answerO << endl;
        answerO = toupper( answerO );
    
        //Play games as long as players want to continue
        while (answerX == 'Y' && answerO == 'Y') {
    
            cout << "\nMoves are made by entering a row and column "
                << "number each\nin the range 1 to 3."
                << "For example, the upper left corner would be 1 1 and"
                << "\nthe upper right corner would be 1 3.\n\n";
            //clear the board, give instructions and handle
            //the playing of one game (i.e. you must use function calls
            //to accomplish this) here, including determing if there is
            // a winner, incrementing any win/draw counters.
            ClearBoard(playingBoard );
            done = 0;
            which = 'X';
            while (!done) {
                GetSpot(playingBoard, which);
                PrintBoard(playingBoard);
                DetermineWinner(playingBoard, done, Xwins, Owins, draws);
                if (which == 'X') {  //Switch players
                    which = 'O';
                } // end if (player is 'X')
                else {
                    which = 'X';
                } //end else (player is 'O')
            } //while game not done
    
            //Ask if players want to play again
            cout << "\nPlayer 1 - would you like to play tic-tac-toe again? ";
            cin  >> answerX;
            cout << answerX << endl;
            answerX = toupper( answerX );
    
            cout << "\nPlayer 2 - would you like to play tic-tac-toe? ";
            cin  >> answerO;
            cout << answerO << endl;
            answerO = toupper( answerO );
    
        } //while both players want to play
    
        //code to print statistics 
        cout.setf(ios::left);
        cout << endl << endl;
        cout << setw(30) << "Number of games won by X: " << Xwins << endl;
        cout << setw(30) << "Number of games won by O: " << Owins << endl;
        cout << setw(30) << "Number of draws: " << draws << endl;
        cout << endl;
        cout << "Thank you for using this program." << endl << endl;
    
        return 0;
    }//end of main()
    
    
    
    

    Home | | Syllabus | | Assignments | | Lectures | | Resources


    Constance Royden--croyden@cs.holycross.edu
    CSCI 131: Techniques of Programming
    Last Modified: April 11, 2014
    Page Expires: August 15, 2014