Lab 12: Classes

Quiz:

The quiz will be available during the first 5 minutes of lab: Your lab TA will tell you the quiz password for your section. You may use either one of the lab machines or your own laptop. For the lab computers your account name and password are the same as your UIC NetID and password.

Select Your Lab Time: 9am, 10am, 11am, 12pm, 1pm, 2pm, 3pm, 4pm, 5pm

Complete the lab using ZyBooks 9.44 as follows:

Consider the implementation of Tic-Tac-Toe shown below that does not use classes. Convert this code into code that does utilize classes. The lab is separated into two parts, each worth 1 point, along with an extra credit point, as follows:

    1. (1 Point) Create class Board, using a string to store the board characters. Implement a default constructor to initialize the board to a string of all underscores "_________". Make sure class Board member function displayBoard() works.

    2. (1 Point) Implement making a move using class Board member function makeMove( int movePosition, char pieceToPlace) Parameter movePosition will be a value in the range 0..8 and parameter pieceToPlace will be 'X' or 'O'.

    3. Extra Credit (1 point) As play progresses, store each board on a vector of boards. At the end of the game when play has finished, display the initial board followed by the 9 boards reflecting each move, in order.


#include <iostream>

using namespace std;



//-----------------------------------------------------------

// Display the 9 board index positions, and below that display

// the current board.

void displayBoard( string boardCharacters)

{

cout << "Board is: \n";

// Display board position index values

for( int i=0; i<boardCharacters.size(); i++) {

cout << i << " ";

// Display a newline character at the end of each row

if( (i+1)%3 == 0) {

cout << endl;

}

}

cout << endl;


// Display board characters

for( int i=0; i<boardCharacters.size(); i++) {

cout << boardCharacters.at( i) << " ";

// Display a newline character at the end of each row

if( (i+1)%3 == 0) {

cout << endl;

}

}

cout << endl;

} //end displayBoard()



//-----------------------------------------------------------

int main() {

cout << "Lab 13: TacTacToe using Classes \n"

<< endl;

int moveNumber = 1; // Move number starts at 1

int theSquare = 0; // Square into which to play, in range 1..9

char pieceToPlay = ' '; // Should be 'X' or 'O' for each move

string boardCharacters = "_________";

// Game play loop

while( true) {

displayBoard( boardCharacters);

// Prompt for user input and make move

cout << moveNumber << ". Enter square to play: ";

cin >> theSquare;

pieceToPlay = 'X';

// Set pieceToPlay to 'O' for even moves

if( moveNumber % 2 == 0) {

pieceToPlay = 'O';

}

boardCharacters.at( theSquare) = pieceToPlay;

// Increment the move number

moveNumber++;

// Exit game if board is full

if( moveNumber == 10) {

// Display the final board

displayBoard( boardCharacters);

break; // Break out of gameplay loop

}

} //end while( true)

cout << "Game over." << endl;

return 0;

} //end main()