## PIC 10B

(Sec. 1a-1b)

C++ Programming and Problem Solving

## Instructor: Ivo Dinov, Asst. Prof.Neurology, Mathematics, Statistics

 http://www.math.ucla.edu/~dinov/10b.1.011/

Due Date:

# Monday, 8:00 PM, July 09, 2001

Please, submit your homework electronically using the "submit" system. Please try to follow the "good programming" conventions we discussed in class. Write modular code and include all of your functions in the same CPP file as your main.

• (HW_2_1.cpp/exe) Create a class TicTacToe that will enable you to write a complete program to play the game of tic-tac-toe. The class contains as private data a 2D 3-by-3 array of integers. The constructor should initialize the empty board to all zeros. Allow two human players. Wherever the first player moves, place a 1 in the specified square; place a 2 wherever the second player moves. Each move must be to an empty square. After each move, determine if the game has been won or if the game is a draw. You do not need to draw the table, just print out the current 2D array containing the board entries.

Sample Run:
%> The game of tic-tac-toe. To exit enter (Q/X). You need 2 players:
%> Current status-quo:
 0 0 0 0 0 0 0 0 0

%> Player 1: Enter a row-number and a column-number separated by a space:
%> 1 1
%> Current status-quo:
 1 0 0 0 0 0 0 0 0

%> Player 2: Enter a row-number and a column-number separated by a space:
%> 1 1
%> Position 1, 1 is already occupied, Please select another position on the board:
%> 2 2
%> Current status-quo:
 1 0 0 0 2 0 0 0 0

%> Player 1: Enter a row-number and a column-number separated by a space:
%> 1 2
%> %> Current status-quo:
 1 1 0 0 2 0 0 0 0

%> Player 2: Enter a row-number and a column-number separated by a space:
%> 1 3
%> Current status-quo:
 1 1 2 0 2 0 0 0 0

%> Player 1: Enter a row-number and a column-number separated by a space:
%> 3 1
%> Current status-quo:
 1 1 2 0 2 0 1 0 0

%> Player 2: Enter a row-number and a column-number separated by a space:
%> 2 1
%> Current status-quo:
 1 1 2 2 2 0 1 0 0

%> Player 1: Enter a row-number and a column-number separated by a space:
%> 2 3
%> Current status-quo:
 1 1 2 2 2 1 1 0 0

%> Player 2: Enter a row-number and a column-number separated by a space:
%> 3 2
%> Current status-quo:
 1 1 2 2 2 1 1 2 0

%> After 4 turns for each player this game is a tie!
%> Would you two want to play another game (Y/N)?
%> N
%> Good game. Bye!

\Ivo D. Dinov, Ph.D., Department of Neurology and Program in Computing, UCLA School of Medicine/