Topical Information

This lab will help you practice with 2D vectors.

Program Information

Write a program to play Tic-Tac-Toe with the user. Well, sort-of.

Let's first get rid of that graphical interface mess. Let's just have a simple console interface like this:

    1  |  2  |  3
  -----+-----+-----
    4  |  5  |  6
  -----+-----+-----
    7  |  8  |  9

Your move?  3

    1  |  2  |  X
  -----+-----+-----
    4  |  5  |  6
  -----+-----+-----
    7  |  8  |  9

Your move?  4

    1  |  2  |  X
  -----+-----+-----
    O  |  5  |  6
  -----+-----+-----
    7  |  8  |  9

Your move?  _

But don't forget to check for invalid input (taken squares, squares that aren't there, etc.).

Thought Provoking Questions

  1. What kind of vector can store both digits and X or O?

  2. How can you translate a numeric position (1-9) into a [0-based] 2D coordinate? (Hint: Search might work; or, even better, integer arithmetic tricks?)

  3. How do you determine a winner? (Rule: 3 of a single player's pieces in a row either horizontally, vertically, or diagonally.) Will loops be of use here? Perhaps one that starts at the placed piece location and looks out from there...relatively to the extent of the 'in-a-row' count? (Hint: How can you accumulate a boolean value like true or false? After all, they'll only win if this one and this one and that one are theirs, right?)

This assignment is (Level 3.5).

Options