CS 1502: JAVA! Programming Assignment #2 - Spring 1999. Program Title: Making That Special Connection Assigned: April 2, 1999 P2 Program Due: April 16, 1999 Files Provided: o p2.nfo - this file o TestIC.class - a test interface for your program o TestIC.html - Documentation for TestIC o stylesheet.css - Style sheet for TestIC documentation Learning Objectives: o Array instantiation/initialization o 'for' loops for array iteration o Model-View-Controller (MVC) GAME OVERVIEW: ======================================================================== Most people are probably familiar with the game of Connect-4, but we'll go through it anyways just to make sure. The game bears a slight resemblance to tic-tac-toe, but adds many fun twists. The board is an 8 by 8 grid of spaces where the pieces will be played. Two players alternatingly drop their colored pieces into each of the columns, where they fall all the way to the bottom, landing on the bottom row of the board or on top of another piece. The objective of the game is to get 4 of your pieces in a row before your opponent. "In a row" can mean vertically, horizontally, or diagonally. For instance: | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | X | O | | | | | | | X | X | O | | | | | | X | O | O | X | | | 0 1 2 3 4 5 6 7 If the board looked like this, and it was O's turn, it would be a smart move to drop into column 5 to block the diagonal Connect-4 that X is about to get.... | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | O | | | | | | | | X | O | | | | | | | X | X | O | | | | | | X | O | O | X | | | 0 1 2 3 4 5 6 7 OK, so now O has successfully blocked X's attempt to win the game. But now X must deal with the same situation. O is about to get a vertical Connect-4 in column 5. If X is to keep O from winning the game, he should also go into column 5. And so on. This action-packed intensity lasts the entire length of the game! Situations can arise where the entire board is filled and nobody has won yet. This is a tie, something tic-tac-toe players should be familiar with. Now that you're familiar with the game, let's get you familiar with the data structure you'll need for the board. ARRAYS OVERVIEW: ======================================================================== Arrays are simply matrices. They are static data structures which can have any number of dimensions and any size in any direction, but after the array is created, it cannot be resized. (That's why they are called a "static" data structure.) You will need to declare your array in the instance variable section of your class so all of the class's methods can access it. An array declaration looks like: private Donut[] boxOfDonuts = new Donut [ MAXDONUTS ]; It's a poor idea to hard code the size into the array declaration. Use a constant to specify the maximum elements. The initial size of our array will be 10. (Note: One may also see the above line of code written as: private Donut boxOfDonuts[] = new Donut [ MAXDONUTS ]; Both lines are equally legal.) A two dimensional array is similar in notation, except the brackets [] are replaced by double brackets [][]: private Soda[][] TwelvePack = new Soda[3][4]; private Soda TwelvePack[][] = new Soda[3][4]; What you are actually creating here is not a 2-D array, but an array of arrays. You may wonder which of these brackets refers to the X direction and which the Y direction ( have we created 3 rows and 4 columns or 3 columns and 4 rows? ). Well, actually it doesn't matter as long as you are consistent when you are referring to the array. Also emphasized in this program is array initialization. This is very different from array instantiation. Instantiating the array, which are what the above lines of code do, simply declares the array. Every element in the array is initially null, so any attempt to access anything in there will result in an error (NullPointerException to be specific). This is why we also initialize arrays, which puts a default object in each spot in the array, so attempts to access the array will not cause the program to die. Let's also make you familiar with MVC... MVC OVERVIEW: ======================================================================== A primary concept of Computer Science is an acronym, MVC, which stands for Model-View-Controller. What this refers to is dividing your program into the functional part (model), the interface (view), and the input (controller). We will be partially implementing this idea in our program. Why, you ask? Well, P6 this quarter will be giving your Connect-4 game a graphical interface, and we want to completely eliminate duplication of code. In this case, though, we will be combining the Viewer and the Controller into one class, the AsciiInterface class. We'll get into the specifics in a second, but it is important to realize that the driver will have no references to println (besides debug statements) or util.readXXXX(). The game will have no knowledge of the interface type (ASCII or graphical), and the interface will have no idea what game we're playing! Since you'll be writing the driver first, and not worrying about the interface until later in the program, we have provided the TestIC class to test out your program with. Use this as your interface until you get the part of the program where you write your AsciiInterface. The method calls to TestIC are _identical_ to the calls in AsciiInterface. To help you use the TestIC class, we have provided some Javadoc documentation for the class. If you open the TestIC.html file in a browser, you will get a list of all of the method headers in this class in the same format as the Sun API on their website. The documentation is fairly straight-forward. One hint is to remember that java.lang.String -means- String PROGRAM WALKTHROUGH: ======================================================================== You'll need to create a P2.java file and create a class P2 inside it: public class P2 { (Don't forget your header.) We'll first need some instance variables to hold stuff: private char turn; private TestIC Bob; private char[][] Board = ??? You should be able to fill in the ???'s by yourself. To have your program work with the supplied code, you'll need your board to use what is called "column-major" order. That is, the board is done like this: Board[x][y] rather than: Board[row][column] which is dramatically different. If your program acts in strange ways, check that you are using x/y, not row/column format. We will have five methods and a constructor in the driver to set up the board and play the game. Let's go through them one by one. main ==== public static void main ( String argv[] ) { P2 myP2 = new P2(); } We'll give you this one cause we're nice guys! All it does is call the constructor of P2. constructor =========== public P2 () This is a pretty simple module (five lines to be exact). It will first set the initial turn to 'X' or 'O' (your choice). Next it will instantiate the TestIC, Bob, and pass it a reference to the Board: Bob = new TestIC( Board ); For initially testing your program, you will use the TestIC provided with the project. After you have completed this class, you will be writing an AsciiInterface yourself. After the turn is set, a call to initBoard() should be made to initialize the board array. Next a call to playGame() is in order to actually run the game. Finally, a call to System.exit(0) will end the program after playGame() finishes. initBoard ========= private void initBoard () This method should loop through the array, probably using nested 'for' loops like this: for ( int j=0 ; j