CS50 - Section 4

Section Information

The Game of Hi-Q

Once you get into it, this assignment shouldn't be too hard, but there are few things you should study before you dive in:

  1. Start by carefully reading the assignment, if you have not already done so. Before you start to plan out your program, however, continue to the next step:

  2. Start by looking at hi-q.c. Note that you should not change anything in the main function of hi-q.c.

  3. At the start of hi-q.c, you'll see the declarations for the functions you need to write, and later in the file you'll find the skeletons of their definitions.

The backbone of the program has been written for you. Your task is to implement some of the functions that are necessary to complete the program.

A look at main() reveals the algorithm we are going to use to play the game of Hi-Q.

Note that there are no implementation details here. From the main function we can't tell how the board is implemented or how any of the functions that main calls actually work. In fact, we can't even tell exactly what game we are playing; one could imagine the same main (or one very similar) working well for a variety of different games. This is the beauty of hierarchical decomposition- not only is it possible to reuse general functions like printf, it is sometimes possible to generalize and reuse functions like main!

Implementation notes:

The board

Think about how you'll choose to represent the flat board and the pins that go in the various holes.

Note that the fact that the board is not passed as an argument to any of the functions that need to have it implies that your board must be a global variable (or at least, a global variable is the only way that we've told you thus far to accomplish this). While this would ordinarily be considered suboptimal style, we intend that you use a global variable (or variables) to represent the board. Don't change the function declarations and definitions to add, delete or change any parameters!




Read the rules for what constitutes a legal move in the supplementary information.



Count the pins, see if there's only one left.


There's more than one way to do this. For each peg on the board, you can check if you can move it (using is_legal_move). Alternatively, for each empty spot on the board, you can check if you can move something to it.

C (continued)


typedef is used to create a new type, which can then be used by your program as the type of variables.

Once you've created a new type with typedef, you can use this type anywhere you would use an ordinary type.


One of the things that you can typedef are enumerated types. An enumerated type is closely related to #define'd integer constants, but are a more sophisticated construct and useful.

vi Tips and Tricks

Just a small dose this week:

Please bring any errors or inconsistencies in this document to the attention of the author.

Dan Ellard <ellard@deas.harvard.edu>