Skip to content
School of Physics
Home Our Teaching Resources C programming exb.html
Back to top

PHY3134 Exercise 2

This is an assessed exercise and should be handed in next week for quick feedback. It will be returned the following week.

The primary aim of this exercise is for you to use a structure in a familiar context. The second is to reinforce the idea of using functions for self-contained tasks and that such functions may call other functions. As always we want you to build up your program a bit at a time, moving on to the next piece only when you are sure that what you have already written is OK.

The task is to put a user-friendly wrapper around a game of Connect Four.

What you should have from last week

The reason for not requiring or assessing a "win" function is to avoid giving an unfair advantage to anybody who wrote a Connect Four program for their PHY2004 project.

From last week we expect you to have a working game that allows two people to play Connect Four. You do not need to have the "win" function. We will ask in class if anybody does not have a win function and will provide one for anybody who needs one.

This means that your "win" function will not count as part of the assessment. All of the rest of your program will.

However your game should do the basics of allowing people to play moves, and checking for invalid input, full columns etc.

This week

  1. Create a structure to represent each player. (You may of course base it on the example in the lecture notes.) You should have a global array of two of these structures as in the notes. Ask for the players' names and their preferred character to use to mark their pieces. You may restrict yourselves to names without spaces if you prefer.

    You should do some sensible checks on the token character. Look up isprint() and isspace() which are both defined inside ctype.h. This could be a candidate for a self-contained function.

  2. Use these names whilst the game is playing ("Sam, please can I have your next move", "Congratulations Sally you have won", "I'm sorry, Dave. I'm afraid I can't do that", etc.) and use the character "token" when printing.
  3. Move all of the process of playing a game into a separate function, i.e. after reading in the players' details have main() call a function that then plays the entire game. The only thing left inside main() after that should be return 0;
  4. Now inside main() put a loop around your game function. At the end of each game ask if they want to play another one. Add members to your structure definition that record the number of wins, losses and draws each player has. Update this information at the end of each individual game and print out the scores so far.
  5. Finally, when the players do not want to play any more games record all these details in a text file.

Hand in

  • The program.
  • The terminal "conversation" (printed two-per page if possible).
  • The final text file

I propose that work should be handed in in hard-copy, paper, form rather than WebCT.

When things go wrong

If you've gone 15 minutes without making progress (outside of lab time) ask me for help, either in person or by email. Always show me the complete, latest code.
                                                                                                                                                                                                                                                                       

Validate   Link-check © Copyright & disclaimer Share
Back to top