Building Tic Tac Toe and Other Board Games

From Al, 6/21/16: From Mary, 10.20.16:
Work with a Partner

In this lab, you'll code a general draw gameboard for Tic Tac Toe and other games, and build a simple Tic Tac Toe game.
Tic Tac Toe video Rani's game sudoku board

The game rules vary, but the game boards are alike except for the number of tiles. So, it makes sense to write one program for all three boards. For example, it would be nice if draw gameboard with (3) tiles and draw gameboard with (5) tiles would draw these boards, fitting neatly on the stage:

3x3array-of-squares-on-stage     5x5array-of-squares-on-stage

  1. Take turns speaking First, load this file. It has two costumes and one brief script.
    • Experiment by dragging the sprite around the stage to see what the script does.
    • Then clear the stage and create a new sprite that will draw the gameboard.
  2. You won't create the draw gameboard block that accepts any number of tiles as input until the end of the next page. The task is complex, so start by building blocks that specialize in each part of the task. One approach is described below. Be sure to test each block by itself before building a block that uses it.
    1. Build tile of size (size) to make a single square tile. You can use the same code from your draw square block.
      Why abstraction? Why build draw tile when you could use draw square or just the script inside it? Making a specific block to hide the details does two useful things:
      1. Less code (if written well) makes the code easier to read and debug.
      2. If you later decide to create the tiles differently (maybe with thicker borders, filled with random colors, or by using stamp), it makes sense to edit draw tile (changing draw square would change all the squares).
    2. Build row of using your draw tile block to draw one row of tiles, like this:
      row of 5 tiles
    3. Save Your Work Pair Programming Swap
    4. Build draw square board with () rows of tiles of size () using draw row to draw as many rows as needed, with the same number of tiles in each row.
  3. Clear the screen and draw a 3-by-3 gameboard. You can now drag and drop the first spite (the one with the "X" and "O" costumes) to play a game of Tic Tac Toe. Play a few games, and discuss some changes you could make to improve the game.
  4. Experiment (or calculate) to find a good starting place that draws a size 100, 3-by-3 game board centered on the stage.
    • Then, build go to starting place for board (using go to x y) to make it easy to move the sprite to that starting place.
    • Add go to starting place to your draw square board block definition.

You will improve this project on the next page. In Unit 3, you will teach the computer to check for wins and illegal moves; and, still later, you will teach the computer how to be an intelligent player! Now Is a Good Time to Save

  1. You can use your draw square board block to create a "1-by-1 array" (just one large square) of the right size to "erase" the outside border of the 3-by-3 board to make something that looks more like a Tic-Tac-Toe board. (To "erase," use a white pencolor and a thicker pensize.)