CSci 1901: Lab 14, April 26

Lab 14: Graphical objects

In this lab you will extend the tic-tac-toe program you wrote last week by adding a graphical interface using the gdraw.py tool. For extra credit you can give the program some intelligence so it can play against a human player or against another bot.

Related files:

What to do for the graphical interface

  1. You need to create graphical objects (i.e. rectangles) corresponding to each tile of the board and draw them. This is done using draw_rectangle or fill_rectangle (see gdraw.py for details on the arguments). In gdraw when you create a graphical object, what is returned is the handle to the object, which is a number identifying the object. This number is what you will pass as an argument to some of gdraw procedures.
    Hint: create a list of lists to represent the graphical board. Store in each element the handle of the corresponding graphical object. Make this list to have the same number of elements as the board used the play the game, so when the player clicks in a legal square the program can update the game board accordingly.
    Hint: Write your procedures to work for any number of tiles in the game, so you'll be able to use the same graphics for different size games and earn extra credit.
  2. Once you have drawn the rectangles you need to get the coordinates of the point where the mouse is clicked. This is done by attaching a procedure to the canvas that is activated when the mouse is clicked on the canvas.

    Look for a simple example in rectangles.py The procedure get_mouse_coords() returns a tuple with the coordinates. You'll need to check that the click is on one of the rectangles and not outside and that the rectangle clicked on is empty. If both conditions are true you should display the symbol of the player ('x' or 'o') in the rectangle and update the corresponding element in the game board.

  3. Display something cool on the canvas when a player wins or when the game ends in a draw.
  4. Modify the play_game procedure to just initialize the board, set the bindings for the rectangles, and start the game. The game will then run from the graphical interface, so the work will be done by the procedure that is activated when the mouse is clicked.

    You may want to make new_board and current_player from play_game() global variables so the procedure which is called when the mouse is clicked can access and modify them.

  5. Play the game.

Bonus Question 1 -- 2 points

Play the game on a 4x4 board, instead of a 3x3. If you wrote the graphical procedures to work with any size board this will not take any time.

Bonus Question 2 -- 4 points

Write a procedure to chose which move to play, so you can have the program playing against a human opponent. There are different strategies you can use to play, ranging from simply hoosing a random move to computing what moves gives the best chance of winning. There is an algorithm called min-max that will compute the move that maximizes your chances of winning while minimizing your opponent chances. It migth be a bit too complex to implement. You can find it in many algorithms and in AI books. Congratulations on completing Lab 14!
Lab 14 total: 10 pts

Purpose of this Lab
What you should learn from this lab:

Copyright: © 2006 by the Regents of the University of Minnesota
Department of Computer Science and Engineering. All rights reserved.
Comments to: Maria Gini
Changes and corrections are in red.