Difference: HandsOnTTT (1 vs. 2)

Revision 22017-03-28 - JimSkon

Line: 1 to 1
 
META TOPICPARENT name="Spring2017"
%CODE{"c++"}%
Line: 89 to 89
 }

%ENDCODE% \ No newline at end of file

Added:
>
>
<-- SyntaxHighlightingPlugin -->
#include<iostream>
using namespace std;

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

void nextMove(char p, char b[][3]) {
    int x, y;
    bool ok = true;
    do {
        ok = true;
        cout << p << " What row (1,2,3) and column (1,2,3)? ";
        cin >> x;
        cin >> y;
        if (x < 1 || x > 3) {
            cout << "Sorry, the row must be 1,2 or 3!" << endl;
            ok = false;
        }
        if (y < 1 || y > 3) {
            cout << "Sorry, the column must be 1,2 or 3!" << endl;
            ok = false;
        }
        if (ok && b[x - 1][y - 1] != ' ') {
            cout << "Location already in use!" << endl;
            ok = false;
        }
    } while (!ok);
    b[x - 1][y - 1] = p;

}

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

bool checkDone(char b[][3], char& winner){
    // Check rows
    for (int r = 0; r < 3; r++) {
        if (match3(b[r][0],b[r][1],b[r][2])) {
            winner = b[r][0];
            return true;
        }
    }
    // Check columns
    for (int c = 0; c < 3; c++) {
        if (match3(b[0][c],b[1][c],b[2][c])) {
            winner = b[0][c];
            return true;
        }
    }
    // Check Diagonals
    if (match3(b[0][0],b[1][1],b[2][2]) ||
        match3(b[0][2],b[1][1],b[2][0])) {
        winner = b[1][1];
        return true;
    }

    return false;
}

char nextPlayer(char p) {
    if (p == 'X') {
        return 'O';
    } else {
        return 'X';
    }
}

int main() {
    bool done = false;
    char winner = ' ';
    char board[3][3] = {' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '};
    displayBoard(board);
    char player = 'X';
    int moves = 0;
    do {
        nextMove(player, bnextMoveoard);
        moves++;
        player = nextPlayer(player);
        displayBoard(board);
        done = checkDone(board,winner);
    } while (!done && moves < 9);
    if (winner != ' ') {
        cout << winner << " wins!" << endl;
    } else {
        cout << "Cat's game!" << endl;
    }
    return 0;
}
 
<-- end SyntaxHighlightingPlugin -->
 \ No newline at end of file

Revision 12017-03-28 - JimSkon

Line: 1 to 1
Added:
>
>
META TOPICPARENT name="Spring2017"
<-- SyntaxHighlightingPlugin -->
#include<iostream>
using namespace std;

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

void nextMove(char p, char b[][3]) {
    int x, y;
    bool ok = true;
    do {
        ok = true;
        cout << p << " What row (1,2,3) and column (1,2,3)? ";
        cin >> x;
        cin >> y;
        if (x < 1 || x > 3) {
            cout << "Sorry, the row must be 1,2 or 3!" << endl;
            ok = false;
        }
        if (y < 1 || y > 3) {
            cout << "Sorry, the column must be 1,2 or 3!" << endl;
            ok = false;
        }
        if (b[x - 1][y - 1] != ' ') {
            cout << "Location already in use!" << endl;
            ok = false;
        }
    } while (!ok);
    b[x - 1][y - 1] = p;

}

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

bool checkDone(char b[][3], char& winner){
    // Check rows
    for (int r = 0; r < 3; r++) {
        if (match3(b[r][0],b[r][1],b[r][2])) {
            winner = b[r][0];
            return true;
        }
    }
    // Check columns
    for (int c = 0; c < 3; c++) {
        if (match3(b[0][c],b[1][c],b[2][c])) {
            winner = b[0][c];
            return true;
        }
    }

    return false;
}

int main() {
    bool done = false;
    char winner;
    char board[3][3] = {' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '};
    displayBoard(board);
    do {
        nextMove('X', board);
        displayBoard(board);
        done = checkDone(board,winner);
        if (!done) {
            nextMove('O', board);
            displayBoard(board);
        }
        done = checkDone(board,winner);
    } while (!done);
    cout << winner << " wins!" << endl;
    return 0;
}

 
<-- end SyntaxHighlightingPlugin -->
 
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