CS 202 - Computer Science II - Fall 2005
Project 2


Loyola College > Department of Computer Science > Dr. James Glenn > CS 202 > Projects > Project 2

Due

Friday, November 11th at 11:59pm. Projects submitted after the due date will be assessed a 20% penalty per day. Projects will not be accepted more than four days late.

Objectives

Introduction

For this project you will enhance the Stratego project to allow players to determine the setup of their pieces (in project 1 the setup was random), and allow play against an opponent over a network instead of playing a computer making random moves.

Assignment

There are three parts to this project: 1) making the GUI that allows the user to enter the network information, choose which color to play, and specify the setup filename; 2) reading the setup information from the file; and 3) sending and reading the moves over the network connection.

For the first part, complete the inner class ConfigurationWindow in the GameWindow class. ConfigurationWindow needs code in its constructor to create the components for the user interface. You will have to create panels and use layouts to create an interface that looks like the one shown below. Note that the "IP Address" field defaults to "127.0.0.1" and the "Port" field defaults to "57890".

You will also have to create an event handler and connect it to the button. This event handler should read the information from the text fields and check which item is selected in the combo box. Once that information has been read, it should be sent sent back to the game window via its setupGameWindow method. This method takes the IP address as a String, the port as an int (so the text from the JTextField will have to be converted to an integer), the side (using the constant NetStrategoBoardModel.RED or NetStrategoBoardModel.BLUE), and the setup file name (as a String). The event handler should also close the configuration window by invoking its dispose method.

Check this code by running GameWindow. You should see your interface displayed, and when you click the button the configuration window should close and you should see a message showing the information that was sent to the GameWindow via its setupGameWindow method.

Next, write the code in setupBoard(String) in the NetStrategoBoardModel class (beware that there are two versions of setupBoard; your code goes in the version that takes a String argument and the other version remains empty). First, this method should open the file whose name is given in the argument, and read 4 lines of 10 characters each. Each character represents a different kind of piece: '*' represents a bomb, '!' a flag, and digits represent pieces of the corresponding rank (with '0' for rank 10 Marshals). You can use the makePiece method, which is simlar to the code in StrategoBoardModel's setupBoard, to decode the characters. Once each piece is created, put it in the appropriate location in the pieces array. You can use the human field to determine which rows the pieces go in: if human is RED then the pieces should be red and will go in rows 6 through 9; if human == BLUE then the pieces are blue and go in rows 0 through 3.

Remember to use try and catch to handle exceptions. Your catch blocks may simply output an error message (and perhaps show the exception using e.printStackTrace(System.err) so you can see exactly what the problem is) and terminate the program by invoking System.exit(1).

To test this part you will need two windows -- one for the red player and one for the blue player. If everything works correctly so far, you should see the windows appear with the pieces for only one side.

To get the pieces to appear for the other side, you need to write the setup for one side over the network connection and then read the setup for the other side from the network connection. To write the setup, you can insert code where you read the setup file that sends everything you read from the file to the toOpponent object (which is a PrintWriter that wraps the network connection). Note that toOpponent has been opened for you and should not be closed because it will be used later to send moves to the opponent's computer. To read the opponent's setup, you should do the same thing as for reading from the setup file, except read from the fromOpponent reader instead of the file. Like toOpponent, fromOpponent has already been opened and should not be closed. Remember to create pieces of the opposite color and to place them at the opposite end of the board, so that if human is RED, you will be creating blue pieces in rows 0 through 3, otherwise you will be creating red pieces in rows 6 through 9. The opponent's pieces should not have their ranks visible at the start of the game.

When this is working correctly, you should see the game window open with both sides' pieces arranged as in the files. Only one side's ranks will be visible in each window.

Finally, you need to create code that sends and reads the moves over the network. To send your moves over the network, you will have to override the makeMove method so that it prints the move to toOpponent. For example, for a move from (0, 0) to (0, 1) you might print "0 0 0 1" to toOpponent. To prevent makeMove from sending the opponent's moves back over the network, the statement that prints to toOpponent will have to be inside an if statement that checks the turn field is set to HUMAN_TURN. makeMove should also do all of the work that is done in StrategoBoardModel's version of the method.

To read moves from the network, you will have to override the (misnamed) findComputerMove method. This method should read one move and set the computerFromR, computerFromC, computerToR, and computerToC fields according to the data read. For example, if you read "3 4 3 5", those fields should be set to 3, 4, 3, and 5 respectively. findComputerMove should end with the following code so that attacking pieces' ranks are made visible.

	// show opponent's piece's rank if it is attacking

	if (getPieceAt(computerToR, computerToC) != null)
	    ((StrategoPiece)(getPieceAt(computerFromR, computerFromC))).makeVisible();

When checking this final code, it may be helpful to see that you are sending over the network. You can do this by sending exactly the same things to System.out as you send to toOpponent. When reading from fromOpponent it may be helpful to send whatever you read to System.out as well.

Files

All the code for the GUI and board are in a Java archive file. Save this file to a new directory and unpack the archive. The archive includes a setup file called "setup.txt" and compiled versions of all the piece classes you can use instead of your own. If you wish to use your own classes, you can copy those files from your project 1 directory. You may have to edit some code to reflect differences between your classes and the classes in the archive.

You should not change any of the given classes except for the GameWindow and NetStrategoBoardModel classes.

To test your configuration window you should run GameWindow. To test everything else, you will have to have two copies of GameWindow running at the same time. Since the firewall setup at Loyola will not allow connections between computers (you will get a warning from Windows Firewall to this effect) , you will have to run both copies on the same machine. Unfortunately, there is no obvious way to get jGRASP to run the program twice simultaneously. You can start one copy in jGRASP but you will have to start the second copy manually. To do this, you will need to start the Java interpreter from a command prompt. Under Windows, you means you must

Update: to make it easier to run two copies of Stratego on the same computer in order to test the game completely, I've updated GameWindow and created a new class TwoWindows. The update for GameWindow is in GameWindowUpdate.java. Download that file and replace the setupGameWindow method in GameWindow with all the methods and inner classes that are in GameWindowUpdate. To run two Stratego windows on the same computer, download TwoWindows.java and use that class as the main class instead of GameWindow. Leave the default port and IP address unchanged, select "Blue" in one window and "Red" in the other, select a setup file, and click "Start Game" in both windows (within about 5 seconds of each other).

Extra Credit

Do something interesting. Some possibilities are given below. Check with the instructor if you think something else would be interesting.

Submissions

Submit through e-mail the source code (.java files) for the two classes you edited.