Tags:
create new tag
view all tags

Non OO Tic Tac Toe

#include <iostream>
using namespace std;
void displayBoard(char board[3][3]);
void nextMove(char player, char board[][3]);
bool notValid(int r, int c);
bool inUse(int r, int c, char board[3][3]);
bool Win(char board[3][3]);
bool checkRowWin(char board[3][3], char &p);
bool checkColWin(char board[3][3], char &p);
bool checkDiaWin(char board[3][3], char &p);


int main() {
    char player = 'X';
    char aBoard[3][3] = {' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '};
    while (!Win(aBoard)) {
        nextMove(player, aBoard);
        displayBoard(aBoard);
        if (player == 'X') {
            player = 'O';
        } else {
            player = 'X';
        }
    }
    return 0;
}

void displayBoard(char board[3][3]) {
    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;
        }
    }
}

void nextMove(char player, char board[][3]) {
    int row, col;
    do {
        cout << "Player " << player << " what is your move (row column):";
        cin >> row >> col;
    } while (notValid(row, col)||inUse(row,col,board));
    board[row - 1][col - 1] = player;
}

bool 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 inUse(int r, int c, char board[3][3]){
    bool result = (board[r-1][c-1]!=' ');
    if (result) {
        cout << "Error, space in use!" << endl;
    }
    return result;
}

bool Win(char board[3][3]) {
    char p;
    if (checkRowWin(board, p) || checkColWin(board, p) || checkDiaWin(board, p)) {
        cout << p << " Wins!!!" << endl;
        return true;
    }
    return false;
}

bool checkRowWin(char board[3][3], 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 checkColWin(char board[3][3], 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 checkDiaWin(char board[3][3], 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;
}
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