Tags:
create new tag
view all tags

Object Oriented Tic Tac Toe

TTTBoard.h

#ifndef TTTBOARD_H
#define TTTBOARD_H

class TTTBoard {
public:
    TTTBoard();   // Board constructor
    void clear(); // Clear the board
    bool move(int row, int col, char player); // Try to do a move, true if success
    bool win(char &p); // check for win
    void display(); // output the board

private:
    char board[3][3];

    bool notValid(int r, int c);    // Check if move valid
    bool inUse(int r, int c);       // Check if position in use
    bool checkRowWin(char &p);      // Check row win
    bool checkColWin(char &p);      // Check column win
    bool checkDiaWin(char &p);      // Check diagonal win

};

#endif /* TTTBOARD_H */

TTTBoard.cpp

/* A Tic Tac Toe Board Object */

#include <iostream>
#include "TTTBoard.h"
using namespace std;




// Member Functions
TTTBoard::TTTBoard() {
    this->clear();
}

void TTTBoard::clear() {
    for (int r=0; r<3; r++) {
        for (int c=0; c<3; c++) {
            board[r][c]=' ';
        }
    }
}

bool TTTBoard::move(int row, int col, char player) {
    if (notValid(row,col) || inUse(row,col)) {
        return false;
    }
    board[row - 1][col - 1] = player;
    return true;

}

bool TTTBoard::win(char &p) {
    if (checkRowWin(p) || checkColWin(p) || checkDiaWin(p)) {
        return true;
    }
    return false;
}

void TTTBoard::display() {
    for (int row = 0; row < 3; row++) {
        for (int col = 0; col < 3; col++) {
            cout << board[row][col];
            if (col < 2) {
                cout << "|";
            }
        }
        cout << endl;
        if (row < 2) {
            cout << "-----" << endl;
        }
    }
}

// Helper Functions
bool TTTBoard::notValid(int r, int c){
    bool result = r>3 || r<1 || c>3 || c<1;
    if (result) {
        cout << "Error, row and colum must be between 1 and 3!" << endl;
    }
    return result;
}

bool TTTBoard::inUse(int r, int c){
    if (board[r-1][c-1]!=' ') {
        cout << "Error, space in use!" << endl;
        return true;
    }
    return false;
}

bool TTTBoard::checkRowWin(char &p) {
    for(int row=0; row<3; row++) {
        if (board[row][0]!=' ') {
            if (board[row][0]==board[row][1] && 
                    board[row][1]==board[row][2]) {
                p = board[row][0];
                return true;
            }
        }
    }
    return false;
}

bool TTTBoard::checkColWin(char &p) {
    for(int col=0; col<3; col++) {
        if (board[0][col]!=' ') {
            if (board[0][col]==board[1][col] && 
                    board[1][col]==board[2][col]) {
                p = board[0][col];
                return true;
            }
        }
    }
    return false;
}

bool TTTBoard::checkDiaWin(char &p) {
        if (board[1][1]!=' ') {
            if (board[0][0]==board[1][1] && 
                    board[1][1]==board[2][2]) {
                p=board[0][0];
                return true;
            }
            if (board[0][2]==board[1][1] && 
                    board[1][1]==board[2][0]) {
                p = board[0][0];
                return true;
            }
        }

    return false;
}

TTTGames.cpp

#include <iostream>
#include "TTTBoard.h"

using namespace std;

int main() {
    TTTBoard myBoard;
    char again;
    int row, col;
    do {
        char player = 'X';
        char winner;
        while (!myBoard.win(winner)) {
            do {
                cout << "Player " << player << " what is your move (row column):";
                cin >> row >> col;
            } while (!myBoard.move(row, col, player));
            myBoard.display();
            if (player == 'X') {
                player = 'O';
            } else {
                player = 'X';
            }
        }
        cout << winner << " wins!" << endl;
        cout << "Play again? ('y' if yes)";
        cin >> again;
        myBoard.clear();
    } while (again == 'y');


    return 0;
}

Topic revision: r1 - 2018-03-22 - JimSkon
 
This site is powered by the TWiki collaboration platform Powered by PerlCopyright © 2008-2018 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback