Stacks, recursion, and games
Due: 26 Sep 2012 (The deadline for part 2 alone is extended to 1 Oct)
Note: This project has two parts. The first part is an individual assignment. In the second part, you may work in a group if you wish to.
Part 1 (80 %)
Educational objectives:
- Primary objectives: Experience implementing stacks, using recursive function calls, analyzing the runtime of your code.
Statement of work: (i) Implement a generic
Vector
class and adapt it to create a stack class, and (ii) evaluate a recursive function given below.Deliverables:
- Turn in a
makefile
and all header (*.h) and cpp (*.cpp) files that are needed to build your software, as described in www.cs.fsu.edu/~asriniva/courses/DS12/HWinstructions.html. Turn in your development log too, which should be a plain ASCII text file calledLOG.txt
in your project directory.Requirements:
- Create a subdirectory called
proj2
.- You will need to have a
makefile
in this directory. In addition, all the header and cpp files needed to build your software must be present here, as well as theLOG.txt
file.- You should create the following additional files.
- Vector.h: This should implement a generic
Vector
class with at least the following features: (i) a default constructor, (ii) a destructor, (iii)void push_back(const T &e)
, (iv) the operator[]
, and (v)int size()
.- Stack.h: This should implement a generic
Stack
class with at least the following features: (i)void push(T &)
, (ii)void pop()
, (iii)T &top()
, and (iv)bool empty()
.- Other files: You may use more files.
- main.cpp: This is the main program. The command:
./recurse N1 C1 A1 M1 M2 M3 M4 D1 D2 S1 S2 Arg Op
will cause the function given below to be evaluated recursively, and the answer output. The function is defined as follows.
f(N) = 0
, ifN < N1
f(N1) = C1
f(N)= A1 + M1*f(M2*N/D1 - S1) Op M3*f(M4*N/D2 - S2)
, ifN > N1
Here,
f(Arg)
needs to be evaluated. N1 C1 A1 M1 M2 M3 M4 D1 D2 S1 S2, Arg are integers and Op is either+
or-
. The division performed is the usual integer division with truncation.Example:
Prompt> ./recurse 2 3 2 1 2 0 1 3 6 0 0 18 +
13
(program output)- Analysis.pdf: This file should contain the following. (i) The asymptotic time complexity for evaluation of the recursive function as a function of all arguments to the executable
recurse
which influence the running time of that program. For the analysis alone, assume thatS1 = S2 = 0
,M2 = M4 = 1
, andD1 = D2 > 1
. Briefly justify the results of your analysis. (ii) Amortized time complexity forN
push
operations on the stack. Briefly justify your analysis.Sample executable: A sample executable is available at ~cop4530/fall12/solutions/proj2/recurse on
linprog
. The first person to find errors in our program will get a bonus point!Notes:
- You should not use STL containers, other than the
string
class. Please get my written permission before using any other STL feature.- You will need to pass certain components of this assignment to pass this course.
Part 2 (20 %)
Educational objectives:
- Primary objectives: Using recursion to evaluate a game tree, implementing a tic-tac-toe code that uses a game tree.
- Secondary objectives: Developing a good user interface, developing a strategy to play tic-tac-toe well.
Statement of work: Write a program to play tic-tac-toe. Your program should accept moves from a human, and should make good moves in its turn, in trying to win the game.
Background: Tic-tac-toe is normally played with a 3x3 array. This can be generalized to bigger arrays. All cells in the array are initially unmarked. Two players take turns marking previously unmarked cells, with the first player making an X mark and the second player making a 0 mark. The game ends if (i) either player wins, or (ii) the game is tied, because neither player wins. A player wins if he has a specified number of consecutive marks along a line. These consecutive marks can either be along a horizontal line, a vertical line, or a diagonal. A diagonal is as explained below. Let the cells be numbered so that the bottom left cell is denoted by (1,1) and the top right by (n,n) in an n x n array. A diagonal going North-East will have cell (i+1,j+1) adjacent to cell (i,j), while a cell going North-West will has cell (i-1,j+1) adjacent to cell (i,j). http://boulter.com/ttt/ has an example of a tic-tac-toe game. They permit an array that has a height different from its width. But we will require the height and the width to be equal. You should evaluate game trees with recursion to try to win the game.
Deliverables:
- Create a subdirectory called
TicTacToe
.- You will need to have a
makefile
in this directory. In addition, all the header and cpp files needed to build your software must be present here, as well as theLOG.txt
file.- You should create the following additional file.
- main.cpp: This is the main program. It will accept three command line arguments. The command:
./proj2 <n> <m> <h/c>
will cause a tic-tac-toe game to be played using an n x n array, with m marks in a line needed to win. The argument h will cause the human to play first, while a c will cause the computer to play first. For example,./proj2 4 3 h
will play a tic-tac-toe game on a 4x4 array, with 3 consecutive marks required to win, and the human playing first. Whenever it is the human's turn to play, the program should present the current state of the game, and specify how the human should let the computer know about his/her move. The human has unlimited time to make a move. However, the computer should complete its move within 5 seconds. The program should also give the correct final result of the game.Notes:
- You can get up to 10 bonus points if your code wins a tic-tac-toe tournament that we will organize, competing against other tic-tac-toe programs.
- You will be graded on your user interface too.
- You may use any STL feature for this part of the assignment.
- You must use a game tree for at least a few levels of recursion, after which you may use some intelligent strategy to evaluate a position.
- You may form groups of up to 4 students each if you wish to. In this case, please email me the names of all group members by Sep 15, with a cc to each group member, and also include the names of the group members in the log file.
- The size of the problem will be at most
10 x 10
.
Last modified: 14 Sep 2012