Developing a Board Game

From BH 10/27/15 observations:

In Unit 1, you started building a tic-tac-toe game by writing the code to draw the game board. Now you'll take another step by sensing which cell a player wants to move into and centering the "X" or "O" in that cell.

  1. Load your project U1L4-TicTacToe. Save it as U2L4-TicTacToe (be sure to choose "Save as..." from the File menu, not just "Save").
  2. Build a predicate Is () between () and () ? that tests whether its first input is bigger than the second input and less than the third. You'll use this to test where the sprite is.
  3. Build a predicate on the board? that uses your between? block to report true if the sprite is on the board and report false if the sprite is dragged completely off the board.
  4. Build a reporter which cell? that tells which cell the sprite has been dragged into. One way to do that is to build blocks that reports which column? and which row? the sprite is in and then figure out how to use them to report the exact cell.
    One way to do this is to think of specific examples: What if the sprite is at the position (90, 110)? What if the sprite is at the position (-100, 80)?
    If you need more help: download this file and import it into your project. It contains completed which row? and which column? blocks, but try to build them yourself first.
    partial script for 'which-cell?' reporter

    Remember: Your board isn't only for tic-tac-toe. You can draw nine-by-nine Sudoku boards, seven-by-seven Connect Four boards, and so on. So, these blocks will need the input n to specify how many rows and columns are in the grid.

    There are many ways to write which row? and which column? You might think of using lots of if statements, but since the number of rows can change, you don't know how many to use. One convenient way is to divide the total size of the game board by the size of each cell. To use that result, you need to round down. The round a number block can do it, but it rounds to the nearest integer, up or down. The function floor function always rounds down, making it easier to use. Likewise, the function ceiling function always rounds up. The floor and ceiling functions are in the same block as sqrt.
  5. Here's a script that you can use to test how well your block works. Use it with the second sprite, which draws the board. Try it out for boards with different numbers of tiles. (The video shows it only on a 3×3 board.)

    animation of play
  6. To create move to tile, you may want mod. You saw mod in Unit 1 Lesson 4, Expressions and Operations.
  7. Create a move to tile () on borad with () tiles per row block that takes a tile number as input and moves your sprite to the center of that tile. Because the location of the tile (for example, tile 7) depends on how many tiles there are in a row, this block will also need an input that says how many tiles are in each row.
  8. Modify the when I am dropped script for the XO sprite so that when the sprite is dropped, it moves to the center of its tile before it is stamped.
  9. Save Your Work Save your work. In Unit 3, you will continue with this project and write code to keep track of which tiles have been taken by which player.