< Back

Lecture 2 - C++ Introduction Part Deux


As usual, create a directory for today's lab:

$ cd ~/cs170/labs
$ mkdir lab2 
$ cd lab2 

Arrays

Dealing with a lot of data at once is a very common concern of programs. In Python, it's easy to deal with a lot of data at once: Lists can store many pieces of data under the same variable name. In C++, we don't have a perfect analoge, but arrays can do similar things. The only drawback is that arrays cannot grow to fit the size of the data. However, we can leverage other aspects of the language to make up for these drawbacks.


Lab Assignment 1
Tic Tac Toe - Print Board

Tic Tac Toe is a very classical example of how arrays work in a particular language. It is not a very complicated game, but it's also not entirely straight forward to complete. Today, we are only going to work on two pieces of the tic tac toe game. The first part is printing the board, which will allow you to visualize the board via the command line.

Details

In a file called TicTacToe.cc, create a program which creates a 2-dimensional array to represent a Tic Tac Toe board. Your program should print the board to the terminal in a 2-dimensional fashion. While you are not required to write the part of the program to play the game, you should be able to place "tokens" in the board and have them printed at the end.

You should create an additional function called printBoard to accomplish this goal. Your function should take the board as a parameter to the function, and it should not return anything.

Example

  $ ./tictactoe
     |   |
  -----------
     |   |
  -----------
     |   |
  $ ./tictactoe
   X | O | X
  -----------
   O | X | O
  -----------
   X | O | X

Hint

  • In your main function, you need to create the 2-dimensional array to represent the board. This will be passed as a parameter to your other functions in order to complete the program.

    When declaring the array, you need to specify the size of ALL dimensions of the array. Since we are doing 2-dimensions, we need 2 sets of square brackets.

    	  int a2DArray[5][5];
    	
  • You can store whatever you want in the array, but an integer might make the most sense. You can translate these values into strings when you print them out.


Lab Assignment 2
Tic Tac Toe - Victory Condition

The other piece of the tic tac toe puzzle we are going to work on today is the victory condition: How do you tell if someone has won? In order to win a game of tic tac toe, you must determine if the player has 3 tokens in any row, column, or diagonal.

Details

In your TicTacToe.cc file, add functions to check winning conditions for rows, columns, and the two diagonals. You should have at least 4 functions to accomplish this, one for each check.

Make sure you test each one of your functions! You should be able to demonstrate that each one of them is working correctly to get checked off.

Example

  $ ./tictactoe
   X | O | X
  -----------
   O | X | O
  -----------
   X | O | X
  Row Victory: False
  Column Victory: False
  Major Diagonal Victory: True
  Minor Diagonal Victory: False
  $ ./tictactoe
   O | X | X
  -----------
   O | O | O
  -----------
   O | X | X
  Row Victory: True
  Column Victory: True
  Major Diagonal Victory: False
  Minor Diagonal Victory: False

Hint

  • Each one of your functions will have roughly the same function declaration:
    	  bool checkRowVictory(int board[3][3]);
    	

 

Challenge

Finish the program! Your program should be a 2-player game: X goes first, O goes second. Players should take turns entering locations in the board. The program should stop as soon as one of the players wins, or if the game ends in a tie.