CS35 Lab 2: Chicken Chicken

Due by 11:59pm Wednesday, September 19, 2012

The goal of this lab is to practice using classes and inheritance in C++ by implementing Chicken, a simple strategy game. You will also gain experience with Makefiles, generating random numbers, and utilizing polymorphism.

To get started, run update35 from a computer science lab computer. The program update35 will create a cs35/labs/02 directory in your CS home directory, and the program handin35 will subsequently submit files from that directory.



Introduction

The game of Chicken models two drivers heading toward each other at high speed. Each driver faces the same choice: to either swerve or continue driving straight. If both drivers drive straight then they collide and both drivers badly lose. If a driver Alice drives straight and her competitor Bob swerves, then Alice wins and Bob loses. The outcome is neutral if both drivers swerve.

Chicken can be formalized mathematically as a two-person non-zero-sum game. Specifically, on a given turn each player must commit to swerving or driving straight without knowing her competitor's decision. Points can then be awarded based on both players' actions:

Chicken is easy if players may collude to determine their individual strategies, but players may not collude: each player must choose her move without knowing what her competitor will choose. If the game is repeated then strategies become complicated because a pure strategy (either always drive straight or always swerve) could result in a large negative score depending on your opponents strategy.

In this assignment you will implement the Chicken game and use C++ inheritance to implement player classes to play the game. That is, you will implement the Chicken game such that different types of players can join the game. One of your player classes (the HumanPlayer) will allow a human player to type input to play the game, and another player class (the AwesomePlayer) will enable a computer player to automatically play the game. You could theoretically have many different types of Players join in the game, each with their own set of strategies.



Implementation details
This lab is more complicated than the previous, but we have provided much of the implementation already. An overview of the different components:

An implementation strategy
We recommend the following implementation strategy which allows you to continually test each part of your program as you implement it:
  1. After you've run update35, compile and run the initial code using the make program and the resulting ./chicken executable program. The program should compile and allow you to create default players, but result in a Segmentation fault because the Chicken game is not implemented.
  2. Implement the Chicken game (i.e., the Chicken class) and test your implementation using the default players. When you are done the default players should run without error but play poorly against each other. To implement the Chicken game, first analyze the Chicken class interface as defined in Chicken.h, paying attention to the data it stores. Then implement the Chicken constructor in Chicken.cpp, which initializes the two Player (pointers) as well as their scores and the current round number of the game. Finally implement the .accessor methods as well as the method playOneRound. This last function simulates one round of chicken by obtaining a move from each player, determining the outcome of those moves, updating the players' scores and round number, and finally sending the results back to each player.
  3. Extend main.cpp to allow the creation and use of AwesomePlayers, extend the Makefile to compile the AwesomePlayer class, and then implement and test a simple AwesomePlayer strategy. Your initial AwesomePlayer implementation need not be so awesome; just make sure you understand C++ inheritance and that your program behaves as expected.
  4. Extend main.cpp to allow the creation and use of HumanPlayers, extend the Makefile to compile the HumanPlayer class, and then implement and test a HumanPlayer class. At this point you should be able to play (as a human user) against either the default player or your initial AwesomePlayer strategy.
  5. Once everything is working, make your AwesomePlayer live up to its name. I will be pitting your AwesomePlayer against your classmates' AwesomePlayers to see who's AwesomePlayer is...awesomest! There may be more than pride on the line.


Advice and hints


Submit

Once you are satisfied with your program, hand it in by typing handin35 at the unix prompt.

You may run handin35 as many times as you like, and only the most recent submission will be recorded. This is useful if you realize after handing in some programs that you'd like to make a few more changes to them.