Lab 6: 2D Arrays: Tic Tac Toe!
Objectives
The main goals for this lab are:
-
To get practice using multidimensional arrays.
-
To develop the ability to break a larger problem down into smaller steps.
Start by creating a /lab6 folder for yourself. You will write your code
in a file you create called TicTacToe.java.
Complete all tasks that are labeled as TODO.
1. Tic Tac Toe
Lets begin by making sure you are familiar with tic tac toe. If you aren’t, briefly look at wikipedia.
2. Top Down Approach
When designing a program, its a good idea to take a top-down approach. In a top down approach, we use the following steps:
-
Identify features of the program
-
Identify the verbs and nouns in the feature list
-
Sketch out major steps
-
Write the program skeleton
-
Implement and test functions one at a time
2.1 Identify Features of the Program
What features are necessary for our tic tac toe program?
Some ideas to consider:
-
What do we need to keep track of and how will we do it? (think: the game grid, the current state of the game, whose turn it is, etc)
-
What does each player need to do? (think: make a move based on the current state of the board)
-
What is the flow of the program? (think: player 1 goes, game grid updates, player 2 goes, game grid updates…)
-
What do we need to check on each turn? (think: is this move legal? does this move cause the game to end?)
-
How will the players see the game grid?
-
When should the players see the game grid?
TODO: Write your answers in the space below.
2.2 Categorize Our Features
After listing our features, we need to figure out which of our features should be variables and which of the features should be methods.
-
What variables do we need?
-
What methods do we need?
TODO: Write your answers in the space below.
2.3 Algorithm Time
Now that we outlined what our methods and variables should be, we can go ahead and sketch out the major steps of the program. In esence, we are outlining how the features should fit together.
List out the steps for your program. The first three steps are provided for you:
-
Ask the players for their names.
-
Create an empty board.
-
Display an empty board.
-
Ask player 1 to make their move.
-
Get their input, and update the board accordingly.
-
Display the board to the players.
-
…
-
…
-
…
Note: You will probably have more than 9 steps.
TODO: Write your algorithm in the space below.
When you have the first draft of your algorithm, pause and play a game of Tic-tac-toe with yourself, and follow every step of of your algorithm. This will help you find errors, mistakes, and missing steps in your algorithm.
TODO: How did your algorithm change when you played through the Tic-tac-toe game on your own?
TODO: Show Prof. Shiptoski your work before continuing.
2.4 Designing Your Variables
Before we write out our methods in our code, its important think about what type of variables should we use. The most complicated variable we will need to keep track of is the board.
TODO: What type of variable should we use to represent our board?
2.5 Skeleton Time
Now that we have convinced ourselves that we have a sufficient algorithm for implementing tic-tac-toe, its time to start programming! But, we dont want to program everything at once, instead we will implement one method at a time.
Lets begin by writing out the skeleton of the program. A skeleton of a program is a list of method stubs (these are placeholders for our functions).
TODO: Write out the skeleton of your program in TicTacToe.java. Make sure to
have a method stub for each of the methods you identified in 2.2.
TODO: Show Prof. Shiptoski your work before continuing.
2.6 Implementation
Now that we’ve written out our skeleton, we can implement each method.
TODO: In your java program, now implement each method. Be sure to test each method in the main function before implementing the next one.
2.7 Gametime
Congrats! You have implemented TicTacToe! Try out your program and see how it does.
TODO: Show Prof. Shiptoski your work and then you are all done with lab 6.