CS 102 (Fall '09)
[Schedule] [Programs] [Notes & Reference] [Examples][Syllabus] [Lab & TA] [Tests] [Grades]

Program #4: Kaos

Prof. Reed, CS 102, Spring '09
Due Tuesday 11/17 at 2:00 p.m.

Description

Write a program in C to play the game of Kaos (similar to the Mindware game of Chaos). For a video introduction of how the game is played, see the following video:

Our situation programming in C is a bit different, as we will be using a text interface and will not be designing a graphical interface. See what our interface will look like:

As illustrated above, our user interface is text-based, with an additional trace shown below. See this trace for an even longer example of the program running. As mentioned in the video, you can run the program yourself from your CS account, by typing in:
     ~i102/Kaos
which will start an executable copy of my program. Note that since we can't graphically represent truly stacking pieces, we are showing them in a list.


Author: Dale Reed 
Date: 11/3/2009 
Class: CS 102, program #4 
 
Welcome to the game of Kaos!  
 
Each player starts with 12 pieces and take turns placing the pieces 
on a 3x3 board. Pieces can be stacked on top of each other. Once a  
4th piece is placed on a stack, the pieces are redistributed in a  
clockwise direction to the immediately (but not diagonal) squares,  
starting with the square furthest from the player. Pieces that go  
"off" the board are returned to their respective original owner,  
to be replayed. The winner is the player that gets rid of her   
pieces first.
--------------------------------------------- 
Pieces for X: 12
 _    _    _    
 _    _    _    
 _    _    _    1  2  3
 _    _    _    

 _    _    _    
 _    _    _    
 _    _    _    4  5  6
 _    _    _    

 _    _    _    
 _    _    _    
 _    _    _    7  8  9
 _    _    _    

Pieces for O: 12

1. Enter the square for 'X' to move: 5

--------------------------------------------- 
Pieces for X: 11
 _    _    _    
 _    _    _    
 _    _    _    1  2  3
 _    _    _    

 _    _    _    
 _    _    _    
 _    _    _    4  5  6
 _    X    _    

 _    _    _    
 _    _    _    
 _    _    _    7  8  9
 _    _    _    

Pieces for O: 12

2. Enter the square for 'O' to move: 5

--------------------------------------------- 
Pieces for X: 11
 _    _    _    
 _    _    _    
 _    _    _    1  2  3
 _    _    _    

 _    _    _    
 _    _    _    
 _    O    _    4  5  6
 _    X    _    

 _    _    _    
 _    _    _    
 _    _    _    7  8  9
 _    _    _    

Pieces for O: 11

3. Enter the square for 'X' to move: 5

--------------------------------------------- 
Pieces for X: 10
 _    _    _    
 _    _    _    
 _    _    _    1  2  3
 _    _    _    

 _    _    _    
 _    X    _    
 _    O    _    4  5  6
 _    X    _    

 _    _    _    
 _    _    _    
 _    _    _    7  8  9
 _    _    _    

Pieces for O: 11

4. Enter the square for 'O' to move: 5

Full stack at position 5 

--------------------------------------------- 
Pieces for X: 10
 _    _    _    
 _    _    _    
 _    _    _    1  2  3
 _    _    _    

 _    O    _    
 _    X    _    
 _    O    _    4  5  6
 _    X    _    

 _    _    _    
 _    _    _    
 _    _    _    7  8  9
 _    _    _    

Pieces for O: 10

Redistributing pieces for 'O' at position 5

--------------------------------------------- 
Pieces for X: 10
 _    _    _    
 _    _    _    
 _    _    _    1  2  3
 _    O    _    

 _    _    _    
 _    _    _    
 _    _    _    4  5  6
 X    _    X    

 _    _    _    
 _    _    _    
 _    _    _    7  8  9
 _    O    _    

Pieces for O: 10

5. Enter the square for 'X' to move: 4

--------------------------------------------- 
Pieces for X: 9
 _    _    _    
 _    _    _    
 _    _    _    1  2  3
 _    O    _    

 _    _    _    
 _    _    _    
 X    _    _    4  5  6
 X    _    X    

 _    _    _    
 _    _    _    
 _    _    _    7  8  9
 _    O    _    

Pieces for O: 10

6. Enter the square for 'O' to move:

You need to know the following concepts in order to write this program:

How to write a basic program in C, with the addition of C++ cin and cout, and C++ reference parameters. You may not use C++ classes (or Strings or template libraries).

Notes:

  1. If you want to "move" pieces within a Microsoft Word document, you can retrieve this board (doc), which is the somewhat imperfect tool I used in creating the videos. If you decide to use this to help you think about the program, "play" the pieces starting from the right of the list, so that they stack correctly.
  2. Your program needs to use parameters for functions to communicate with each other. The only global variables you may have are the Board array, and the number of pieces for X and for O. All other variables must be declared in main (or locally in other functions) and passed as parameters. A 15 point deduction will be taken if you have more global variables than this.
  3. In both the video and longer textual traces of this program I have included trace information showing the board with the stack of 4 pieces, as well as the board after each complete redistribution when there is a chain of redistributions. Though this is not necessary, I highly recommend that you include this in your program as well. This will help us grade your program and will very much help you write and debug your code.
  4. I recommend you write your program using the following steps shown below.. If you do not complete all these steps PLEASE clearly document this fact in your program header, displayed both in your program documentation as well as on the screen when your program begins to run. Steps to take, along with points (out of 55 for run-time points):
    1. (0 points) Get your C++ environment working. You may start with this program outline. Note you must make a design decision of how to represent the board. There is no "right" or "wrong" way here. Some options are using a 1 or 2-dimensional array of characters, or a 1 or 2-dimensional array of arrays of 4 characters, for each square stack.
    2. (5 points) Write the function to display the board, using your Board array.
    3. (10 points) Write the function to read in user input and place the appropriate piece ('X' or 'O') on the board in the appropriate spot. Moves onto positions where there are already pieces should results in the pieces being "stacked."
    4. (10 points) Write the function to redistribute pieces when a move is the 4th one on a stack. For now only test this using the middle position on the board.
    5. (10 points) Update your code so that the redistribute function now also works for the squares on the edges of the board. You will need to make sure you do not go past the end of an array. Remember, Java protected us from this, but C will let you go off the end of an array, usually without complaining.
    6. (20 points) Finally add the code to handle a chain of redistributions, such as when 4 adjacent squares all have 3 pieces on them already and a 4th piece is placed on one of them.

  5. Turnin your program electronically using the "turnin" command from your CS account as follows:
    turnin -c cs102 -p prog4 Kaos.cpp

    where the file containing your solution is called Kaos.cpp.  

 

[CS Dept] [UIC] [Prof. Reed]