Lab 4

Solution has been posted

Lab instruction:

Please follow this instruction step by step to finish today's lab.

Today's lab is about modifying a Tic-Tac-Toe game sample code to get familiar with our new programming knowledge:

(1) use array to store Tic-Tac-Toe board; initialization of array

(2) read information from file to initialize Tic-Tac-Toe board

FYI, 

Tic-Tac-Toe wiki: https://en.wikipedia.org/wiki/Tic-tac-toe

Tic-Tac-Toe online game: http://www.calculatorcat.com/games/tic_tac_toe.phtml


Stage1 (1 point):

You should create a Qt C++ project, rather than C project, otherwise there will be some compile error. C++ library name is a little different from C library. 

1. Download main.cpp, read and play with this sample code to know the code structure.

 if you play the game, the output should be like this:

--------------------------------screen shot---------------------------------------

=====================================

Welcome to TicTacToe

Board positions are numbers:

        1  2  3

        4  5  6

        7  8  9

=====================================

-------

 . X .

 O . O

 X X .

-------

Step count: 1

Player to move is (X/O): X

Enter position for placement (1-9): 5

-------

 . X .

 O X O

 X X .

-------

Player X has won the game.

------------------------------screen shot-----------------------------------------

2. You could see that the sample code use 9 global variable to store board information,

    the board is initialized in initializeBoard(). 

    

Please modify the code to use a global one dimensional array to store board information. 

Importantly, the board should be initialized in the same way as original sample code.

   

After you finis this stage, please call your TA to give you partial score, since in next stage, 

you need to modify this function.

Stage2 (1 point):

 There is a board.txt file provided. 

Please refer to the sample code readFile.cpp to read board information from file and initialize 

the board using information in the file. 

Please try to make readFile.cpp work first, then try to modify initializeBoard() function. 

Note:

If you cannot make readFile.cpp work and stuck there, please call TA to help you. 

board.txt should not be in the same folder of .cpp file, but should be in the folder, which automatically 

created by Qt when you build the project. 

All you need to do is to modify and write a new initializeBoard() function.

Stage3 (1 point):

This game has a while loop without checking if one of the player X or O win the game.

Search "TODO: stage3" in this file. 

Please finish checkWin() function and call it in main(), so that when one of the player

win the game, immediately terminate the loop and finish the game.

Note: if bool checkWin() does not work, the reason might be you did  not use c++ compiler. Just change

         it to int checkWin();


The output should be exactly like:

"Player X has won the game." or "Player O has won the game."