CS 151 Fall 2007 Lab 9

Working with Arrays and ArrayLists

 

The goal for this lab is to get more practice with arrays, in particular two dimensional ones, and to gain more experience in user interface development.  We will do so using the source code for Tic Tac Toe from section 7.6 of your book.  Basically, we'll be doing Programming Exercises P7.10 and P7.11 from your book with a few extra things I thought we should change. If you didn't bring your book, you should still be able to complete the lab.

 

  1. Open BlueJ either by selecting it from the Dock or navigating to it in the Applications folder and double clicking it.

  2. Mount your network directory/folder so that you can access the lab after you leave Olin 323.  Go to the Finder and from the Go menu select Connect to Server....  Type afp://fileserver1 or choose it from the list of selections and select the Personal volume when prompted.

  3. Download lab9.zip and copy/drag it into your personal network folder.  Double click it to uncompress the lab9 project that it contains.  Start BlueJ if you haven't already and open the lab9 project.

  4. Compile all the classes by clicking the rectangular compile button in the project window.  Run the main method of the TicTacToeRunner class to see what it does.  Take a look at the code inside the two TicTacToe classes.  If you don't know how to play the game or are unsure of the rules, ask your neighbor, the TA, or me.

 

  1. In the TicTacToeRunner class, feel free to change the marks to uppercase X and O if you prefer (I did).  Be sure to change the instruction text too!

 

  1. [P7.10] Add a method called getWinner() to your TicTacToe class.  The method should return "X" or "O" if that player has won, and " " (a blank space) otherwise.  Take some time to think about this before rushing in and writing the code.  I recommend starting with some comments in your method first so you're clear about the logic you want to implement.  Note that you can check whether either player is a winner and then return the winner if there is one.  This will make your code easier to read.  Also, see if you can use for loops to minimize the number of hard coded values or "magic numbers" as your book calls them.

 

  1. After you have your getWinner() method working, modify the TicTacToeRunner class so that the user can enter positions starting with 1 instead of 0.  There's no reason the user should have to think like a computer.  Your job as the developer is to hide such things from the user as much as possible.  Don't forget to update the instructions.

 

  1. Another thing I found annoying about the Runner class was the need to ask and answer to position questions.  Try to modify the class so that the user can enter the row and column positions at the same time.  If the user separates the two entries with a space, then you can use the first nextInt() method call to get the first entry, and a second nextInt() call to get the second.  You may want to comment out some of the old lines at least until you get the single question thing working.  Don't make the user enter two numbers when they want to quit.  They should still just enter -1 (now 0?) and press return to quit.

    Add some breakpoints and step through your code using BlueJ's debugger if you have problems.

  2. [P7.11] Write an application that plays tic-tac-toe.  Your program should draw the game board , change players after every successful move, and pronounce the winner.  I recommend you start by adding a method to the TicTacToe class that lets you check whether or not a particular position on the board has already been taken (has an X or O in it).  How many and what type parameters should the method take?  What should it return?  I called mine isSet.

    Use the Runner class as a starting point.  You probably will want to create a new class so that you can keep TicTacToeRunner in its pristine state for reference.  Again, before you jump in think out what you want to accomplish.  Sketch it out with comments.  Does it make sense to add some "helper" methods to your class to improve readability and minimize code duplication?

 

  1. When you think you have a working version, ask the TA or me to play your game.  You'll be surprised by the many ways we can break it so try to anticipate our dumb/malicious behavior.  Feel free to ask us for design suggestions after you have thought about things for a few minutes yourself or in discussion with your neighbor.

 

  1. Time permitting, create two classes called TicTacToeBoardComponent and TicTacToeBoardViewer.