Multidimensional Arrays and string

TickTackToe

#include <iostream>
using namespace std;

void DisplayBoard(char board[][3]) {
    cout << endl;
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            cout << " " << board[i][j];
            if (j < 2) {
                cout << " |";
            }
        }
        cout << endl;
        if (i < 2) {
            cout << "-----------" << endl;
        }
    }
    cout << endl;
}

void nextMove(char player, char board[][3]) {
    int row, col;
    bool ok = true;
    do {
        ok = true;
        cout << "Player " << player << " What row (1,2,3) and column (1,2,3)? ";
        cin >> row >> col;
        if (row < 1 || row > 3) {
            cout << "Bad row, must be between 1 and 3." << endl;
            ok = false;
        }
        if (col < 1 || col > 3) {
            cout << "Bad column, must be between 1 and 3." << endl;
            ok = false;
        }
        if (ok && board[row - 1][col - 1] != ' ') {
            cout << "Sorry, there is already an " << board[row - 1][col - 1] << " in that position!" << endl;
            ok = false;
        }
    } while (!ok);
    board[row - 1][col - 1] = player;

}

bool match3(char a, char b, char c) {
    if (a == b && b == c) {
        return true;
    } else {
        return false;
    }
}

bool checkDone(char board[][3], char& winner) {
    // Checks rows
    for (int i = 0; i < 3; i++) {
        if (board[i][0] != ' ') {
            if (match3(board[i][0],board[i][1],board[i][2])) {
                winner = board[i][0];
                return true;
            }
        }
    }
    // Checks columns
    for (int i = 0; i < 3; i++) {
        if (board[0][i] != ' ') {
            if (match3(board[0][i],board[1][i],board[2][i])) {
                winner = board[0][i];
                return true;
            }
        }
    }

    // Check diagonals
    if (board[0][0] != ' ') {
        if (match3(board[0][0],board[1][1],board[2][2])) {
            winner = board[0][0];
            return true;
        }
    }
    if (board[2][0] != ' ') {
        if (match3(board[2][0],board[1][1],board[0][2])) {
            winner = board[2][0];
            return true;
        }
    }
    return false;
}

/*
 * 
 */
int main() {
    char myBoard[3][3] = {' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '};
    char winner;
    do {
        DisplayBoard(myBoard);
        nextMove('X', myBoard);
        if (checkDone(myBoard, winner)) {
            break;
        }
        DisplayBoard(myBoard);
        nextMove('O', myBoard);
        if (checkDone(myBoard, winner)) {
            break;
        }
    } while (1);
    DisplayBoard(myBoard);
    cout << "You win " << winner << "!!!!" << endl;
    
    return 0;
}

Hello world


Hello world


Hello world


Hello world


Hello world


Edit | Attach | Watch | Print version | History: r2 < r1 | Backlinks | Raw View | Raw edit | More topic actions...
Topic revision: r1 - 2016-10-23 - JimSkon
 
  • Edit
  • Attach
This site is powered by the TWiki collaboration platform Powered by PerlCopyright © 2008-2019 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback