CSCI 131 Techniques of Programming--Spring 2014

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

    Solution to Project 4 board.cc

    //********************************************************
    // board.cc
    //     Author: A good solution writer
    //     Date: April 11, 2014
    //     Class: CSCI 131 project4, Professor Royden
    //     Purpose: Defines board functions to play tic tac toe.
    //                Functions to clear the board, print the board
    //                Place a marker on a position and determine the winner
    //     Input: (from keyboard) Location to place a marker on the board.
    //     Output: (to monitor) Playing board and who has wone (if game over)
    //********************************************************
    
    #include <iostream>
    #include "board.h"
    
    using namespace std;
    
    //*************************************************************************
    // void ClearBoard(board playingBoard)
    //      Purpose: Places a space character in each board location
    //      Input: None
    //      Output: None
    //      Pre: None
    //      Post: Every position in the playing board has a space value
    //************************************************************************ 
    void ClearBoard( /*in out*/ board playingBoard){
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++){
                playingBoard[i][j] = ' ';
            } //end for each column
        } //end for each row
    } //end ClearBoard( )
    
    //*************************************************************************
    // void GetSpot(board playingBoard, char which)
    //      Purpose: Gets row and column for move from current player (repeatedly
    //                 if move not legal) and places the current player's marker there.
    //      Input: (from keyboard) row and column for move
    //      Output:(to monitor) prompts to user for row and column
    //      Pre: Game is in progress and is not done
    //      Post: Player's mark has been placed for a legal move
    //************************************************************************ 
    void GetSpot( /*in out*/ board playingBoard, /*in*/ char which) {
        int good = 0;        //0 if move not good, 1 otherwise
        int row;              //row position for move
        int col;              //column position for move
    
        while(!good) {
            good = 1;
    
            //Get row and column from player
            cout << "Player " << which 
                << " - please enter a row and column number for your move ==> ";
            cin >> row >> col;
            cout << row << " " << col << endl;
    
            //Check for illegal move
            if ((row < 1) || (row > 3)){
                cout << "Illegal row number. Please enter again." << endl< 3)){
                cout << "Illegal column number. Please enter again." << endl << endl;
                good = 0;
            } //end if column out of range
            else if (playingBoard[row-1][col-1] != ' ') {
                cout << "This spot is already taken. " << endl;
                cout << "Please enter a different spot." << endl << endl;
                good = 0;
            } //end if already a marker in position
            else {
                playingBoard[row-1][col-1] = which;
                good = 1;
            } //end else (move is legal)
        } //end while move not good
            
    } //end GetSpot( )
    
    //*************************************************************************
    // void PrintBoard(board playingBoard)
    //      Purpose: Prints the playing board in the current state
    //      Input: None
    //      Output: (to Monitor) playing board with all values in each position
    //      Pre: None
    //      Post: The playing board has been printed to the monitor
    //************************************************************************ 
    void PrintBoard( /*in*/ board playingBoard) {
        cout << endl;
        for (int i = 0; i < 3; i++) {
            cout << "             -------------" << endl;
            cout << "             ";
            for (int j = 0; j < 3; j++ ) {
                cout << "| " << playingBoard[i][j] << " ";
            } //end for each column
            cout << "|" << endl;
        } //end for each row
        cout << "             -------------" << endl << endl;
    
    } //end PrintBoard( )
    
    //*************************************************************************
    //  void DetermineWinner( board playingBoard, int& done, int& Xwins, 
    //                int& Owins, int& draws)
    //      Purpose: Determines whether a player has won the game or whether it
    //                  is a draw.  Increments statistics variables appropriately
    //      Input: None
    //      Output: (to monitor) Player who has one or statement that the game is
    //                a draw, if applicable.
    //      Pre: Game is in progress
    //      Post: Winner has been determined, statistics variables updated
    //              appropriately and done set to 1 if game is done.
    //************************************************************************ 
    void DetermineWinner( /*in*/ board playingBoard, /*out*/ int& done, 
                                 /*in out*/ int& Xwins, 
                    /*in out*/ int& Owins, /*in out */ int& draws) {
        char which = 'N';                 //Player who wins
        done = 0;                            //0 if game not done, 1 otherwise
    
        for (int i = 0; i < 3; i++) {
            if ((playingBoard[i][0] != ' ') &&
                (playingBoard[i][0] == playingBoard[i][1])&&
                (playingBoard[i][0] == playingBoard[i][2])){
                which = playingBoard[i][0];
                done = 1;
            } //end if horizontal three-in-a-row
            else if ((playingBoard[0][i] != ' ') &&
                (playingBoard[0][i] == playingBoard[1][i] ) &&
                (playingBoard[0][i] == playingBoard[2][i])) {
                which = playingBoard[0][i];
                done = 1;
            }//end if vertical three-in-a-row
        } // end for each row and column
    
        if (!done) {
            if ((playingBoard[0][0] != ' ') &&
                (playingBoard[0][0] == playingBoard[1][1]) && 
                (playingBoard[0][0] == playingBoard[2][2])) {
                which = playingBoard[0][0];
                done = 1;
            } //end if diagonal three-in-a-row
            else if ((playingBoard[0][2] != ' ') &&
                (playingBoard[0][2] == playingBoard[1][1]) &&
                (playingBoard[0][2] == playingBoard[2][0])) {
                which = playingBoard[0][2];
                done = 1;
            } //end if other diagonal three-in-a-row
        } //end if game not done
    
        if (done) {
            cout << "Player " << which << " is the winner!" << endl;
            if (which == 'X') {
                Xwins++;
            } //end if player 'X' wins 
            else {
                Owins++;
            } //end else (player 'O' wins)
        } //end if game done
        else {
            //check if any spaces left
            done = 1;
            for (int i = 0; i < 3; i++) {
                for (int j = 0; j < 3; j++) {
                    if (playingBoard[i][j] == ' ')
                        done = 0;
                } //end for each column
            } //end for each row
            if (done) {
                cout << "The game is a draw. " << endl;
                draws++;
            } //end if game done (its a draw)
         
        } //end else (no three-in-a-row)
    
    } //end DetermineWinner( )
    
    
    

    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