Tags:
create new tag
view all tags

Extra Credit Lab - Tick Tack Toe with computer opponent

Due: May 1, 11:55pm

Moodle Link

3526260-Hand-drawn-tic-tac-toe-game-isolated-on-white-Stock-Photo.jpg

Instructions

This optional lab is an opportunity to get points back on a previous lab lost due to incorrectness or lateness. You may complete this lab and add points earned to one previous lab (Labs 1-7 only), up to 100%. Alternatively, you may apply these points to the Code Lab grade.

How it works. Complete the work by the due date (no late wok will be accepted for this without prior approval). As part of the assign, in the text part of the Moodle assignment, clearly explain which assignment you would like these points to apply toward. You can ONLY pick one.

Your Goal: Your goal is to take the Tick Tack Toe program created in class, and a give it an automatic computer opponent. The game will then allow the user to play as many games as they want, with the program keeping score of wins, loses, and draws for each player (the human and the computer. The game will start and ask the user if they want to be X or O. It will then randomly determine who goes first for first game. From then on who goes first will alternate.

Grading Table

Requirement or option Grading Comments Points Score
Easy to use user interface   5  
C++ code includes comments, with project information at top, pre and post conditions for each functions and other comments as needed.   5  
The C++ code has good formatting, indentation, and organization.   5  
Good variable and function names, appropriate use of constants rather then literal numbers.   5  
Functions: Logic divided up into cohesive functions with a single purpose   10  
Runs: Run examples showing complete operation of system features. You must demonstrate that the systems has the features added.   40  
Required features: Program must have a computer opponent. Program starts up and the user picks X or O, a random user starts, and from then on starters alternate. After each game the win/loss/draw tally is displayed, and the user can choose to continue.   20  
You may only pick ONE of the options below. Clearly explain which option you pick in the text area of Moodle when you turn it in. You must supply evidence in the form of runs and explanations for the option you choose.      
Option 1: Program fully plays as a "dumb" opponent. Use random numbers to generate only LEGAL computer moves.   10  
Option 2: Computer plays with some well defined strategy making it harder to beat. Explain your strategy in the the text area of the Moodle assignment for credit here. The most obvious strategy is to watch for, and block opponent when they have two out of three in a row.   20  
Option 3 (bonus points): Your program never loses, winning if it can, only tieing when it cannot lose. For this you can get points above 100, which can be directed to another project of your choosing. This can be applied to any lab (1-9).   50  
Total   100  
Super Duper Bonus Extra Credit - These are points above 100 you may direct to one additional assignment.   40  

How to submit

Turn in:

  1. All source code
  2. A write up explaining which option which choose
  3. Runs that demonstrate all functionality
  4. A selected project (from 1-7) to receive points for this extra credit (0-100 points)
  5. If you did option 3, a second selected project (from 1-9) to recieve this extra credit (0-40 points)

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;
}

Edit | Attach | Watch | Print version | History: r7 < r6 < r5 < r4 < r3 | Backlinks | Raw View | Raw edit | More topic actions
Topic revision: r7 - 2018-04-26 - 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