CSC 2100, Fall 2008

Programming Assignment 4

Assign date: November 12, 2008, Due: November 19, 2008 at class

Write a program that allows two players to play a game of tic-tac-toe. Use a two dimensional char array with 3 rows and 3 columns as game board. Each element of the array should be initialized with null (‘\0’). The program should first ask the players to enter their names. The program should then run in a loop that

Display the content of the board.

Allow player 1 to select a location on the board for an X, The program should ask the user to enter the row and column number.

Allow player 2 to select a location on the board for an O, The program should ask the user to enter the row and column number.

Determine whether a payer has won, or a tie has occurred. If a player has won, the program should declare that player the winner and end. If a tie has occurred, the program should say so and end.

Player 1 wins when there are three Xs in a row, or in a column, or diagonally across the board. Player 2 wins when there are three Os in a row, or in a column, or diagonally across the board. A tie occurs when all of the locations on the board are full, but there is no winner.

You must implement a function to check the status of the game after every move made by a player. The header of the function is as follows:

int gameStatus( int board[][3], int row)

The function should return 1 if player 1 wins, return 2 if player 2 wins, 0 if game is tied, and 3 other wise.

Similarly you should implement another function to display the game board.

Sample Execution I:

Welcome to Tic-Tac-Toe

First player, enter your name: John

First player, enter your name: David

__|__|__

__|__|__

| |

John, make your move: 1 1

X | |

------

| |

------

| |

David, make your move: 2 1

X | |

------

O | |

------

| |

John, make your move: 3 3

X | |

------

O | |

------

| | X

David, make your move: 1 1

Illegal move, make another move: 3 4

Illegal move, make another move: 2 3

X | |

------

O | | O

------

| | X

John, make your move: 2 2

X | |

------

O | X | O

------

| | X

John has won.

Sample Execution II:

Welcome to Tic-Tac-Toe

First player, enter your name: John

First player, enter your name: David

__|__|__

__|__|__

| |

John, make your move: 1 1

X | |

------

| |

------

| |

David, make your move: 2 1

X | |

------

O | |

------

| |

John, make your move: 3 3

X | |

------

O | |

------

| | X

David, make your move: 2 2

X | |

------

O | O |

------

| | X

John, make your move: 2 3

X | |

------

O | O | X

------

| | X

David, make your move: 1 3

X | | O

------

O |O | X

------

| | X

John, make your move: 3 3

X | | O

------

O | O | X

------

X| | X

David, make your move: 3 2

X | | O

------

O |O | X

------

X | O| X

John, make your move: 1 2

X | X | O

------

O | O | X

------

X | O | X

Game tied

Submission:

Send your source code by email (as attachment) to me and TA (). Also submit a printout of your code (Don’t forget to type your full name on the printout) to me in class. The name of your source file should be lastname_prog4_2100.cpp. For example name of my program will be ghafoor_prog4_2100.cpp. Also the subject line of your email must be “CSC2100 Fall 08 Prog4 Submission”. Your program will not be graded if

1.  You don’t send the code to both TA and me

2.  Subject line of your email is not “CSC2100 Fall 08 Prog4 Submission”

3.  You don’t submit the printout of your code

**** Many of you are still not following the submission guide line. Please follow the submission guideline.

Grading :

Programming Style 10%

(Program header and comment 5%, appropriate variable name and proper indentation 5%)

Correct implementation of gameStatus() 40%

Correct implementation of display function 10%

Correct implementatioyn of the main loop that drives the game 40%

Late submission:

There will be a 5% penalty/day for up to 5 days. For delays more than five days an score of zero will be assigned.

*** Start working on your assignment early; and don’t hesitate to come to me if you need help. And the TAs are there to help you.

5