CSC108H Assignment 2, Summer 2011

Due Monday, June 27, 11:55 pm

Introduction

The purpose of this assignment is to give you more practice writing functions and programs in Python, and in particular writing code with strings, lists, and loops.

You should spend at least one hour reading through this handout to make sure you understand what is required, so that you don't waste your time going in circles, or worse yet, hand in something you think is correct and find out later that you misunderstood the specifications. Highlight the handout, scribble on it, and find all the things you're not sure about as soon as possible.

It is probably worth spending at least a day thinking about the assignment before writing code of any sort. Try to think through potential problems to catch them before they appear. This will save hoers of frustrating debugging time.

Portable game notation

Chess games are often written down in what is known as algebraic game notation. This has been extended to a file type known as .pgn for portable game notation. These are files that chess programs can read in order to replay games or look at commentary.

We will be building a basic pgn parser that will be working with a limited version of pgn files, that will be able to output game summaries, game descriptions, and that will be able to keep track of what the board looks like after each move. This will involve lots of string and list manipulation.

Our version of .pgn

We will have a slightly simpler version of .pgn notation. A description of .pgn can be found here. We will have the following changes:

Things that you do have to worry about:

A summary of .pgn notation

Your Task

You are going to write a program that takes a string obtained from a .pgn file, and extracts the tag and move information from it, as well as providing a function that will return board information as specified points in the game. For tag information, the information must be returned as a string, with each of the tags matching the following format:

Each of the preceding tags must be on it's own line. The move information must similarly be written in a restrictive format, with each move being it's own line:

The final major function that you need to write will return a two-dimensional list that represents the chess board. The first dimension corresponds to the letters in algebraic notation, with 0 corresponding to 'a', 1 to 'b' and so on. The second corresponds to number with 0 corresponding to '1', 1 corresponding to '2' and so on. The list elements should be of the form 'cA', where c is 'w' or 'b' depending on the colour of the piece, and A is the type of piece, so 'P' for pawn, 'B' for bishop, 'N' for knight, 'R' for rook, 'K' for King and 'Q' for queen. Empty squares should be be denoted by the empty string.

To be explicit, you are to fill in game_summary, move_summary, board_state, from_alg_notn and to_alg_notn in assignment2.py. You should not modify load_game_file. You may use the if __name__ == '__main__' block for testing if you which, but you are not obliged to.

Additional requirements

How to tackle this assignment

This program is much larger than what you wrote for Assignment 1, so you'll need a good strategy for how to tackle it. Here is our suggestion. First download the code from assignment2.py.

Principles:

Advice:

This program can be broken down into several separate aspects. Some of these follow:
  1. Read this handout thoroughly and carefully, making sure you understand everything in it, particularly the the subset of .pgn you are supposed to cover.
  2. To parse this, one needs to do several things.
  3. One needs to extract the tags.
  4. Given a tag, one needs to extract the pertinent information.
  5. One needs to separate the tags from the move text.
  6. Given the movetext one needs to extract the individual moves from it.
  7. Given an individual move, one needs to extract all the information from it.
  8. Once you're able to do this, one needs to then take that information and keep track of it.
  9. If you can extract a move, take out the information from it, and keep track of it, then you should be able to loop through the movetext.
  10. Both the game summary and the board will probably rely on a bunch of the same code, so make sure to intelligently reuse code.
  11. Make sure to test the code as you're writing each small bit, so that you can ferret out problems early.
  12. The shell can be useful for testing half-written functions.
  13. A set of .pgn files can be found here. Make sure that the files themselves are in the same directory as assignment2.py.

Marking

These are the aspects of your work that we will focus on in the marking:

Submitting your assignment

You must hand in your work electronically, using the MarkUs system. Log in to it here, using your cdf login and password.

To declare your partnership, one of you needs to invite the other to be a partner, and then they need to accept the invitation. To invite a partner, navigate to the Assignment 2 page, find "Group Information", and click on "Invite". You will be prompted for the other student's cdf user name; enter it. To accept an invitation, find "Group Information" on the Assignment 2 page, find the invitation listed there, and click on "Join". Note that, when working in a pair, only one person should submit the assignment.

To submit your work, again navigate to the Assignment 2 page, then click on the "Submissions" tab near the top. Click "Add a New File" and either type a file name or use the "Browse" button to choose one. Then click "Submit".

For this assignment, hand in just one file:

Once you have submitted, be sure to check that you have submitted the correct version; new or missing files will not be accepted after the due date. Remember that spelling of filenames, including case, counts. If your file is not named exactly as above, your code will receive zero for correctness.