Java Programming Project; Political Rumble, A Client/Server Game

130
Running head: POLITICAL RUMBLE, A JAVA CLIENT/SERVER APPLICATION 1 Political Rumble, A Java Client/Server Application Daniel J. LeBlanc, James Hamidou, Mark Brazinski, John Hanna National University Software Architecture Application SEN632 Walter Wesley, PhD November 23, 2014

Transcript of Java Programming Project; Political Rumble, A Client/Server Game

Running head: POLITICAL RUMBLE, A JAVA CLIENT/SERVER APPLICATION

1

Political Rumble, A Java Client/Server Application

Daniel J. LeBlanc, James Hamidou, Mark Brazinski, John Hanna

National University

Software Architecture Application

SEN632

Walter Wesley, PhD

November 23, 2014

POLITICAL RUMBLE, A JAVA CLIENT/SERVER APPLICATION 2

Table of Contents

Table of ContentsProject Requirements....................................................................4Deliverables:...........................................................................6readme.txt..............................................................................7About Game Play.........................................................................8Extra Credit Features...................................................................9To Start the Game......................................................................10Importing a Project To NetBeans........................................................11How we met the Operation Requirements..................................................13Java Server Code.......................................................................18Java Client Code.......................................................................56Source Files...........................................................................74UML Diagrams...........................................................................77Application Screenshots................................................................85

POLITICAL RUMBLE, A JAVA CLIENT/SERVER APPLICATION 3

POLITICAL RUMBLE, A JAVA CLIENT/SERVER APPLICATION 4

Political Rumble, A Java Client/Server Application

Project RequirementsThis assignment involved the development and production of a Java-based,

Client/Server combat-style game for multiple players in a threaded environment. The

virtual server acts as a moderator, connecting a minimum of two players in a combat-style

environment. All of the I/O functions are carried out by the server and the data is

passed to the clients as needed for game play. The project deliverables are as follows;

Operational Requirements: 1 Role of the Combat Moderator server application: The Combat Moderator shall

wait and listen for Warriors to request to register and engage in combat. The Combat

Moderator subsequently manages the combat operation exchanges between the various Warriors

currently within the battlefield.

2 Role of the Warrior client: The Warrior client application registers itself

with the Combat Moderator (which it identifies via an IP address provided by the user).

POLITICAL RUMBLE, A JAVA CLIENT/SERVER APPLICATION 5

The Warrior client displays the current status of its attributes to the user. The

Warrior client enables the user to specify a target Warrior and combat operation to be

directed at that target Warrior.

3 Warrior Object Creation: A Warrior shall be created and initialized from an

associated data file within the current working directory. Warrior files shall be

designated by an extension of “.wdat”. The filename (text before the extension) shall

used as the name of the Warrior. The file itself shall contain a set of data fields. At

a minimum, the data fields supplied shall be the health-level, place of origin, and

personal description of the Warrior. the health-level, which is an integer value from 0

to 100, inclusive (a value of 100 denotes a fully healthy Warrior, and a value of 0

denotes a dead Warrior). When a Warrior object is created, the user shall be given the

opportunity to select the file he/she wishes to open. The selected file shall be opened

and read via an input stream. The data shall be parsed and used to initialize the data

fields of the Warrior object.

POLITICAL RUMBLE, A JAVA CLIENT/SERVER APPLICATION 6

4 Registration Protocol: Upon establishing a socket connection with a Warrior

Combatant, the Warrior Combatant shall provide name and health-level information to the

Combat Moderator. The Combat Moderator shall then welcome the Warrior by name to the

field of battle, and shall provide the Warrior with a series of names and associated

health-levels, places of origin, descriptions, etc. of Warriors who are currently present

in the field of battle.

5 Battle Protocol:

5.1 The user who has created a Warrior shall be able to select and thereby

challenge another Warrior. The user shall also be able to select one of at least three

possible combat operations, i.e. attacks or defenses. You may decide what sorts of

attacks or defenses you will support. Examples of attacks and defenses are: slash, stab,

parry, block, sidestep, laser, particle beam, rail-gun, shield, temporary de-

materialization, etc.. You may create attacks and defenses of your own.

POLITICAL RUMBLE, A JAVA CLIENT/SERVER APPLICATION 7

5.2 Upon completion of a combat operation by an attacking Warrior, the

designated attack or defense and the name of the target Warrior shall be passed along to

the Combat Moderator. The Combat Moderator shall then notify the target Warrior that

he/she is being engaged in conflict. The user in control of the target Warrior shall be

able to select one of the possible combat operations (as stipulated above, c.f. 4.1).

5.3 Upon completion of a combat operation by a Warrior that is being attacked,

the designated attack or defense and the name of the target Warrior shall be passed along

to the Combat Moderator.

5.4 Upon receipt of the combat operation response from a Warrior that is being

attacked, the Combat Moderator shall perform the necessary logic to determine whether and

by how much the health-levels of the battling Warriors is to be changed. The Combat

Moderator shall then enforce the determined changes upon the battling Warriors.

5.5 If the Warrior's health-level changes such that it is less than or equal to

zero, then the Warrior will notify the Combat Moderator that it is at the point of death,

POLITICAL RUMBLE, A JAVA CLIENT/SERVER APPLICATION 8

whereupon the Warrior and the Combat Moderator will close the socket, and the Warrior

client will exit. Note that the responsibility for declaring point of death is with the

Warrior, not the Combat Moderator. This allows the software to be extended, by

subclassing and polymorphic, method overriding such that Zombie Warriors can be allowed to

participate. Zombie Warriors are not required, however.

5.6 When a Warrior object is destroyed, either because it has reached a point

of death or because the Warrior client application has exited, the Warrior state

information (i.e. field values) will be written out to its associated file.

Deliverables:

1. A UML diagram of the system.

2. A sequence diagram of the Battle Protocol.

3. All source code files (.class files), and compiled files (.class files).

POLITICAL RUMBLE, A JAVA CLIENT/SERVER APPLICATION 9

4. At least two sample “.wdat” files.

5. Screenshots of the client and server applications in operation.

6. Since there may be significant differences from one implementation to another,

please provide a readme.txt file that lists the other member(s) of your team, and explains

how to launch the server and client applications.

7. If you add extra features, please explain those as well. Note that any extra

feature shall add, not detract from the other requirements or features.

readme.txtThe Envisage Team consists of 4 members; Daniel LeBlanc, James Hamidou, Mark

Brazinski and John Hanna.

POLITICAL RUMBLE, A JAVA CLIENT/SERVER APPLICATION 10

About the resource fi les: There are 3 .wdat resource files that must be installed with

the server.java file in order for the application to run. The .wdat resource files are as

follows;

1. characters.wdat - Contains the data for the possible game characters

2. smear.wdat - Contains the data for the possible smear campaigns

3. tvads.wdat - Contains the data for the possible television ad campaigns

The server, during game play, creates it’s own .wdat files called; player1.wdat and

player2.wdat where the chosen character data is stored. These files will be created

automatically by the server so there is no need to install these.

The client, during game play, creates it’s own .wdat files called; player1.wdat and

player2.wdat. Because the server .wdat files are different than the client version it is

important that the client is installed in a separate directory structure than the server.

This necessitates creating two separate packages; one for the server and one for the

client.

POLITICAL RUMBLE, A JAVA CLIENT/SERVER APPLICATION 11

About Game PlayWhen both players are connected and have chosen a character, player 1 is prompted to

make the first combat attack and is presented with the “Combat Menu”. A player can select

one of the combat approaches from the list of 4 possible combat functions. A submenu

under each of the combat functions presents the player with a list of possible attacks to

send to player 2. These submenu attack possibilities are tailored to whichever character

a players opponent has chosen. If a player enters one of these submenus and decides they

want to try a different type of attack then they can simply enter BACK or back at the

‘Enter Selection’ field and return to the initial Combat Menu. Similarly, if a player

wishes to end the game before the energy is depleted for either players then they can

simply enter END or end in the ‘Enter Selection’ field to end the game. After an attack is

POLITICAL RUMBLE, A JAVA CLIENT/SERVER APPLICATION 12

sent, both players are alerted to the end results of the campaign and control is switch to

the defending player; the attacking player is told to wait while it is the other players

turn. Game play continues back and forth with each player getting a turn in order until

one or both of the players runs out of energy. The game can end with a winner and a loser

or in a tie if both players run out of energy in the same play.

Extra Credit Features1. There are 4 combat functions in the ‘Combat Menu’ as opposed to the required 3.

2. Warriors can taunt one another.

3. A ‘BACK’ function was implemented to allow a player to return to an upper menu.

4. An ‘END’ function was implemented to allow a player to end the game early.

5. The application implements 3 resource files and creates resource files on it’s

own.

POLITICAL RUMBLE, A JAVA CLIENT/SERVER APPLICATION 13

6. Attacks are tailored to whatever character the opponent has chosen for game

play.

How to Load the Files in Eclipse.

1. Create a new java project called “PoliticianRumble”.

2. Create a new package under the PoliticianRumble project called

“politicianrumbletestclient”.

3. Create a second package called “politicianrumbletestserver”.

4. Right click on the politicianrumbletestclient and select “import”.

5. Select “File System” and browse to the location of the

PoliticianRumbleTestClient.java file and select to import the file.

6. Right click on the politicianrumbletestserver and select “import”

POLITICAL RUMBLE, A JAVA CLIENT/SERVER APPLICATION 14

7. Select “File System” and browse to the location of the

PoliticianRumbleTestServer.java file and select to import the file.

8. Right click on the PoliticianRumble project folder and select “import”.

9. Select “File System” and browse to the location of the “Characters.wdat;

smear.wdat; and tvads.wdat” files and select to import the files.

10. Note: If you are using Windows you will need to go to open the

PoliticianRumbleTestClient and either delete or comment out (Placing // before) the import

command:

“import static sun.lwawt.macosx.CWrapper.NSView.frame;”

To Start the Game1. Right click on the PoliticianRumbleTestServer.java file and select “Run-as”

a. In the console screen you should see:

i. “Politician Rumble Server is Running”

POLITICAL RUMBLE, A JAVA CLIENT/SERVER APPLICATION 15

b. At this point the Server is running and waiting for clients.

2. Right click on the PoliticianRumbleTestClient.java file and select “Run-as).

3. The client GUI will appear, you may need to accept the security warning

depending on your security. Select a character for player 1 before launching the second

client.

4. Repeat step two to create a second player.

NOTE: It is important that the first player chooses a character before the second

player connects. If the second player connects before the first player has chosen a

character then they will be presented with a blank GUI until the first player has

completed character selection. A fully multi-threaded version would eliminate this issue

but at this time, although the application is threaded, the first player must connect and

chose a character before the second player connects.

POLITICAL RUMBLE, A JAVA CLIENT/SERVER APPLICATION 16

Importing a Project To NetBeansThis section shows you how to handle the initial importing of projects into the

IDE.

Importing an Eclipse Workspace Into NetBeans

For Eclipse projects, you can use the Import Eclipse Project wizard to help you

create NetBeans projects from projects in an Eclipse workspace and import the project

classpaths and other settings. When you use the Import Eclipse Project wizard, you do not

need to use create and configure the NetBeans project manually. Open the wizard by

choosing File > Import Project > Eclipse Project. See Importing Eclipse Projects into

NetBeans IDE for information on how the wizard works and how you can work simultaneously

in Eclipse and NetBeans projects.

Setting Up a Java Project Based on Existing Sources in NetBeans

For other Java projects developed outside of NetBeans, you use an "Existing Sources"

template in the New Project wizard to make a NetBeans project. In the wizard, you identify

POLITICAL RUMBLE, A JAVA CLIENT/SERVER APPLICATION 17

the location of the sources and specify a location for the NetBeans project metadata. You

then use the Project Properties dialog box to configure the project.

To set up a NetBeans project for an existing Java application:

Choose File > New Project (Ctrl-Shift-N on Windows/Cmd-Shift-N on OS X).

Choose Java > Java Project with Existing Sources. Click Next.

In the Name and Location page of the wizard, follow these steps:

Type a project name.

(Optional) Change the location of the project folder.

(Optional) Change the name of the build script used by the IDE. This might be

desirable if there is already a build script called build.xml that is used to build the

sources.

(Optional) Select the Use Dedicated Folder for Storing Libraries checkbox and specify

the location for the libraries folder. See Sharing Project Libraries for more information

on this option.

POLITICAL RUMBLE, A JAVA CLIENT/SERVER APPLICATION 18

(Optional) Select the Set as Main Project checkbox. When you select this option,

keyboard shortcuts for commands such as Clean and Build Main Project (Shift-F11) apply to

this project.

Click Next to advance to the Existing Sources page of the wizard.

In the Source Packages Folder pane, click Add Folder. Then navigate to your sources

and select the source roots, click Open.

When you add a folder containing source code, you must add the folder that contains

the highest folder in your package tree. For example, for the com.mycompany.myapp.ui

package, you add the folder that contains the com folder.

(Optional) In the Test Package Folders pane, click Add Folder to select the folder

containing the JUnit package folders.

Click Next to advance to the Includes & Excludes page of the wizard.

POLITICAL RUMBLE, A JAVA CLIENT/SERVER APPLICATION 19

(Optional) In the Includes & Excludes page of the wizard, enter file name patterns

for any files that should be included or excluded from the project. By default, all files

in your source roots are included.

Click Finish.

How we met the Operation RequirementsOperational Requirements:

1 Role of the Combat Moderator server application: The Combat Moderator shall

wait and listen for Warriors to request to register and engage in combat. The Combat

Moderator subsequently manages the combat operation exchanges between the various Warriors

currently within the battlefield.

*** A separate server and client application were implemented;

PoliticianRumbleTestServer

POLITICAL RUMBLE, A JAVA CLIENT/SERVER APPLICATION 20

PoliticianRumbleTestClient

2 Role of the Warrior client: The Warrior client application registers itself

with the Combat Moderator (which it identifies via an IP address provided by the user).

The Warrior client displays the current status of its attributes to the user. The

Warrior client enables the user to specify a target Warrior and combat operation to be

directed at that target Warrior.

*** As soon as the client is launched it connects to the server and is presented

with a list of possible characters from the server. After the player selects

a character they are

prompted to wait for an opponent to connect to the server. Once an

opponent

connects to the server and selects a character for game play, player 1 is

notified of

POLITICAL RUMBLE, A JAVA CLIENT/SERVER APPLICATION 21

their own identity as well as the opponents character identity.

3 Warrior Object Creation: A Warrior shall be created and initialized from an

associated data file within the current working directory. Warrior files shall be

designated by an extension of “.wdat”. The filename (text before the extension) shall

used as the name of the Warrior. The file itself shall contain a set of data fields. At

a minimum, the data fields supplied shall be the health-level, place of origin, and

personal description of the Warrior. the health-level, which is an integer value from 0

to 100, inclusive (a value of 100 denotes a fully healthy Warrior, and a value of 0

denotes a dead Warrior). When a Warrior object is created, the user shall be given the

opportunity to select the file he/she wishes to open. The selected file shall be opened

and read via an input stream. The data shall be parsed and used to initialize the data

fields of the Warrior object.

POLITICAL RUMBLE, A JAVA CLIENT/SERVER APPLICATION 22

*** Once connected to the server a client is presented with the list of possible

characters

from the servers .wdat file. Future enhancements can include additional

characters for game play which would be included in this file.

4 Registration Protocol: Upon establishing a socket connection with a Warrior

Combatant, the Warrior Combatant shall provide name and health-level information to the

Combat Moderator. The Combat Moderator shall then welcome the Warrior by name to the

field of battle, and shall provide the Warrior with a series of names and associated

health-levels, places of origin, descriptions, etc. of Warriors who are currently present

in the field of battle.

*** As per the operational requirements, our project is for two players. With

time constraints and the skill level of our team members, it was not

POLITICAL RUMBLE, A JAVA CLIENT/SERVER APPLICATION 23

possible to implement a fully multi-threaded design, although our design

does implement threading for two players.

5 Battle Protocol:

5.1 The user who has created a Warrior shall be able to select and thereby

challenge another Warrior. The user shall also be able to select one of at least three

possible combat operations, i.e. attacks or defenses. You may decide what sorts of

attacks or defenses you will support. Examples of attacks and defenses are: slash, stab,

parry, block, sidestep, laser, particle beam, rail-gun, shield, temporary de-

materialization, etc.. You may create attacks and defenses of your own.

*** Our server sends a ‘Combat Menu’ to each player when it is their turn at game

play.

The combat functions are as follows;

1. Taunt your opponent

2. Launch a smear campaign against your opponent

POLITICAL RUMBLE, A JAVA CLIENT/SERVER APPLICATION 24

3. Run a television ad berating your opponent

4. Host a political fund raising event

5.2 Upon completion of a combat operation by an attacking Warrior, the

designated attack or defense and the name of the target Warrior shall be passed along to

the Combat Moderator. The Combat Moderator shall then notify the target Warrior that

he/she is being engaged in conflict. The user in control of the target Warrior shall be

able to select one of the possible combat operations (as stipulated above, c.f. 4.1).

*** A player is notified of an attack.

5.3 Upon completion of a combat operation by a Warrior that is being attacked,

the designated attack or defense and the name of the target Warrior shall be passed along

to the Combat Moderator.

*** A player notifies the server of an attack.

POLITICAL RUMBLE, A JAVA CLIENT/SERVER APPLICATION 25

5.4 Upon receipt of the combat operation response from a Warrior that is being

attacked, the Combat Moderator shall perform the necessary logic to determine whether and

by how much the health-levels of the battling Warriors is to be changed. The Combat

Moderator shall then enforce the determined changes upon the battling Warriors.

*** The server performs the logic to determine the results of the attack.

5.5 If the Warrior's health-level changes such that it is less than or equal to

zero, then the Warrior will notify the Combat Moderator that it is at the point of death,

whereupon the Warrior and the Combat Moderator will close the socket, and the Warrior

client will exit. Note that the responsibility for declaring point of death is with the

Warrior, not the Combat Moderator. This allows the software to be extended, by

subclassing and polymorphic, method overriding such that Zombie Warriors can be allowed to

participate. Zombie Warriors are not required, however.

POLITICAL RUMBLE, A JAVA CLIENT/SERVER APPLICATION 26

*** Both the player and the server monitor the energy level and depending on the

point at which a players energy drops to or below 0, the players are notified that the

game has ended.

5.6 When a Warrior object is destroyed, either because it has reached a point

of death or because the Warrior client application has exited, the Warrior state

information (i.e. field values) will be written out to its associated file.

*** At each stage of the game, including the ending, warrior state is written out

to .wdat files at both the server and the client end. These warrior state files are

called player1.wdat and player2.wdat, respectively. The server maintains both of these

files at it’s end.

Java Server Code/********************************************************************************** ********************************************************************************** **** Programmer: Daniel LeBlanc, James Hamidou, Mark Brazinski, John Hanna *****

POLITICAL RUMBLE, A JAVA CLIENT/SERVER APPLICATION 27

**** Assignment: SEN632 Final Project SERVER Test ***** **** Company Name: Envisage ***** **** Political Rumble Server Test v015 ***** **** Created: November 02, 2014 ***** **** Compiler Used: Eclipse ***** ********************************************************************************** **********************************************************************************/package politicianrumbletestserver;

import java.io.BufferedReader;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.IOException;import java.io.InputStreamReader;import java.io.PrintWriter;import java.net.ServerSocket;import java.net.Socket;import java.util.ArrayList;import java.util.Formatter;import java.util.Random;import java.util.Scanner;import java.util.logging.Level;import java.util.logging.Logger;

/************************************************************************** ************************************************************************** ***** PROJECT DESCRIPTION ***** ***** ***** ***** Implement a multiplayer, network-based, Warrior-Combat ***** ***** game. The architecture shall make use of a client-server ***** ***** model, with the server being the combat moderator, and ***** ***** the various clients being the warrior combatants. *****

POLITICAL RUMBLE, A JAVA CLIENT/SERVER APPLICATION 28

************************************************************************** **************************************************************************/public class PoliticianRumbleTestServer {//Start class PoliticianRumbleTestServer public static int gameState = 0; public static String player1CharacterNum; public static String player2CharacterNum; public static String player1CharacterName; public static String player2CharacterName; public static int player1Energy = 100; public static int player2Energy = 100; public static String taunt1 = ""; public static String taunt2 = ""; public static String smear1 = ""; public static String smear2 = ""; public static String smear3 = ""; public static String tvad1 = ""; public static String tvad2 = ""; public static String tvad3 = ""; public static String tvad4 = ""; public static String tvad5 = ""; public static String tvad6 = ""; public static Random randomNumbers = new Random(); public static boolean messageQue = false; public static String messageHeader = ""; public static String message = ""; public static String messageCost = ""; /** * Runs the application. Pairs up clients that connect. */

POLITICAL RUMBLE, A JAVA CLIENT/SERVER APPLICATION 29

/** * @param args the command line arguments * @throws java.lang.Exception */ public static void main(String[] args) throws Exception { // Begin PoliticianRumbleTestServer Main ServerSocket listener = new ServerSocket(8901); System.out.println("Politician Rumble Server is Running"); try { while (true) { Game game = new Game(); Game.Player player1 = game.new Player(listener.accept(), '1'); Game.Player player2 = game.new Player(listener.accept(), '2'); player1.setOpponent(player2); player2.setOpponent(player1); game.currentPlayer = player1; player1.start(); System.out.println("Player1 Started"); player2.start(); System.out.println("Player2 Started"); } } finally { listener.close(); }

} // End PoliticianRumbleTestServer Main} /** * A two-player game. */ class Game {

POLITICAL RUMBLE, A JAVA CLIENT/SERVER APPLICATION 30

/** ** Each Player begins the game with 100 points of energy. ** The object of the game is to force the opponent to go broke. **/

/** * The current player. */ Player currentPlayer;

/** * Returns whether the current state of the game is such that one * of the players is a winner. */ public boolean hasWinner() { return (PoliticianRumbleTestServer.player1Energy <=0 || PoliticianRumbleTestServer.player2Energy <=0); }

/** * Returns whether both players are out of energy. */ public boolean bothPlayersBroke() { if (PoliticianRumbleTestServer.player1Energy >= 0 && PoliticianRumbleTestServer.player2Energy >= 0) { return false; } return true; }

POLITICAL RUMBLE, A JAVA CLIENT/SERVER APPLICATION 31

/** * Called by the player threads when a player tries to make a * move. This method checks to see if the move is legal. If the move is legal the game state is updated * (the square is set and the next player becomes current) and * the other player is notified of the move so it can update its * client. */ public synchronized boolean legalMove(int location, Player player) { if (player == currentPlayer) { //board[location] = currentPlayer; currentPlayer = currentPlayer.opponent; currentPlayer.otherPlayerMoved(location); return true; } return false; }

/** * The class for the helper threads in this multithreaded server * application. A Player is identified by a character mark * which is either '1' or '2'. For communication with the * client the player has a socket with its input and output * streams. Since only text is being communicated we use a * reader and a writer. */ class Player extends Thread { public ArrayList<String> characterList = new ArrayList<>(30); public ArrayList<String> smearList = new ArrayList<>(30); public ArrayList<String> tvadsList = new ArrayList<>(30); char mark;

POLITICAL RUMBLE, A JAVA CLIENT/SERVER APPLICATION 32

Player opponent; Socket socket; BufferedReader input; PrintWriter output; public Formatter fileOut; public Scanner fileIn; /** * Constructs a handler thread for a given socket and mark * initializes the stream fields, displays the first two * welcoming messages. */ public Player(Socket socket, char mark) { this.socket = socket; this.mark = mark; try {//Start Try Block for Welcome and Character Selection Routine input = new BufferedReader( new InputStreamReader(socket.getInputStream())); output = new PrintWriter(socket.getOutputStream(), true); output.println("WELCOME " + mark); System.out.println("SENT WELCOME: " + mark); output.println("MESSAGE Waiting for opponent to connect"); System.out.println("SENT MESSAGE: Waiting for opponent to connect"); System.out.println("gameState = " + PoliticianRumbleTestServer.gameState); //Character Selection Routine FileInputStream input2 = new FileInputStream("characters.wdat"); BufferedReader br = new BufferedReader(new InputStreamReader(input2)); String strLine; while((strLine = br.readLine()) !=null) {

POLITICAL RUMBLE, A JAVA CLIENT/SERVER APPLICATION 33

characterList.add(strLine); }input2.close(); System.out.println(characterList); output.println("CHARACTERS " + characterList);

//Wait for character selection String command = input.readLine(); if (command.startsWith("CHARACTER")){// Start if (command.startsWith("CHARACTER")) System.out.println("Character selected: " + command.substring(9)); //Set Player1 Chosen Character Number and Character Name if (PoliticianRumbleTestServer.gameState == 0) {// Start if (PoliticianRumbleTestServer.gameState == 0) PoliticianRumbleTestServer.player1CharacterNum = command.substring(9); System.out.println("player1Character = " + PoliticianRumbleTestServer.player1CharacterNum); if ("1".equals(PoliticianRumbleTestServer.player1CharacterNum)) {//Start if ("1".equals(PoliticianRumbleTestServer.player1CharacterNum)) PoliticianRumbleTestServer.player1CharacterName = characterList.get(0); System.out.println("player1CharacterName = " + PoliticianRumbleTestServer.player1CharacterName);

}//End if ("1".equals(PoliticianRumbleTestServer.player1CharacterNum)) else if ("2".equals(PoliticianRumbleTestServer.player1CharacterNum)) {//Start if ("2".equals(PoliticianRumbleTestServer.player1CharacterNum)) PoliticianRumbleTestServer.player1CharacterName = characterList.get(3);

POLITICAL RUMBLE, A JAVA CLIENT/SERVER APPLICATION 34

System.out.println("player1CharacterName = " + PoliticianRumbleTestServer.player1CharacterName);

}//End if ("2".equals(PoliticianRumbleTestServer.player1CharacterNum)) else if ("3".equals(PoliticianRumbleTestServer.player1CharacterNum)) {//Start if ("3".equals(PoliticianRumbleTestServer.player1CharacterNum)) PoliticianRumbleTestServer.player1CharacterName = characterList.get(6); System.out.println("player1CharacterName = " + PoliticianRumbleTestServer.player1CharacterName);

}//End if ("3".equals(PoliticianRumbleTestServer.player1CharacterNum)) else if ("4".equals(PoliticianRumbleTestServer.player1CharacterNum)) {//Start if ("4".equals(PoliticianRumbleTestServer.player1CharacterNum)) PoliticianRumbleTestServer.player1CharacterName = characterList.get(9); System.out.println("player1CharacterName = " + PoliticianRumbleTestServer.player1CharacterName);

}//End if ("4".equals(PoliticianRumbleTestServer.player1CharacterNum)) sendCharacter(); sendEnergy(PoliticianRumbleTestServer.player1Energy); }// End if (PoliticianRumbleTestServer.gameState == 0) //Set Player2 Chosen Character Number and Character Name if (PoliticianRumbleTestServer.gameState == 1) {// Start if (PoliticianRumbleTestServer.gameState == 1) PoliticianRumbleTestServer.player2CharacterNum = command.substring(9);

POLITICAL RUMBLE, A JAVA CLIENT/SERVER APPLICATION 35

System.out.println("player2Character = " + PoliticianRumbleTestServer.player2CharacterNum); if ("1".equals(PoliticianRumbleTestServer.player2CharacterNum)) {//Start if ("1".equals(PoliticianRumbleTestServer.player2CharacterNum)) PoliticianRumbleTestServer.player2CharacterName = characterList.get(0); System.out.println("player2CharacterName = " + PoliticianRumbleTestServer.player2CharacterName);

}//End if ("1".equals(PoliticianRumbleTestServer.player2CharacterNum)) else if ("2".equals(PoliticianRumbleTestServer.player2CharacterNum)) {//Start if ("2".equals(PoliticianRumbleTestServer.player2CharacterNum)) PoliticianRumbleTestServer.player2CharacterName = characterList.get(3); System.out.println("player2CharacterName = " + PoliticianRumbleTestServer.player2CharacterName);

}//End if ("2".equals(PoliticianRumbleTestServer.player2CharacterNum)) else if ("3".equals(PoliticianRumbleTestServer.player2CharacterNum)) {//Start if ("3".equals(PoliticianRumbleTestServer.player2CharacterNum)) PoliticianRumbleTestServer.player2CharacterName = characterList.get(6); System.out.println("player2CharacterName = " + PoliticianRumbleTestServer.player2CharacterName);

}//End if ("3".equals(PoliticianRumbleTestServer.player2CharacterNum)) else if ("4".equals(PoliticianRumbleTestServer.player2CharacterNum))

POLITICAL RUMBLE, A JAVA CLIENT/SERVER APPLICATION 36

{//Start if ("4".equals(PoliticianRumbleTestServer.player2CharacterNum)) PoliticianRumbleTestServer.player2CharacterName = characterList.get(9); System.out.println("player2CharacterName = " + PoliticianRumbleTestServer.player2CharacterName);

}//End if ("4".equals(PoliticianRumbleTestServer.player2CharacterNum)) sendCharacter(); sendEnergy(PoliticianRumbleTestServer.player2Energy); }// End if (PoliticianRumbleTestServer.gameState == 1) PoliticianRumbleTestServer.gameState = PoliticianRumbleTestServer.gameState + 1; System.out.println("gameState = " + PoliticianRumbleTestServer.gameState); }// End if (command.startsWith("CHARACTER")) } catch (IOException e) { System.out.println("Player died: " + e); }//End Try Block for Welcome and Character Selection Routine }

/** * Accepts notification of who the opponent is. */ public void setOpponent(Player opponent) { this.opponent = opponent; }

/** * Handles the otherPlayerMoved message. */ public void otherPlayerMoved(int location) {

POLITICAL RUMBLE, A JAVA CLIENT/SERVER APPLICATION 37

output.println("OPPONENT_MOVED " + location); if (!(hasWinner())) { } else { if (1 == mark) { System.out.println("1: Game Completed (line 296) player1Energy = " + PoliticianRumbleTestServer.player1Energy); System.out.println("1: Game Completed (line 297) player2Energy = " + PoliticianRumbleTestServer.player2Energy); sendEnergy(PoliticianRumbleTestServer.player1Energy); if (PoliticianRumbleTestServer.player1Energy <= 0 && PoliticianRumbleTestServer.player2Energy <= 0) { System.out.println("1: Game TIED (line 301)"); sendWdatFile(); output.println("TIE"); } else if (PoliticianRumbleTestServer.player1Energy < PoliticianRumbleTestServer.player2Energy) { System.out.println("1: Player 1 Defeated (line 305)"); sendWdatFile(); output.println("DEFEAT"); } else if (PoliticianRumbleTestServer.player1Energy > PoliticianRumbleTestServer.player2Energy) { System.out.println("1: Player 1 Victory (line 309)"); sendWdatFile(); output.println("VICTORY"); } else output.println(""); } else {

POLITICAL RUMBLE, A JAVA CLIENT/SERVER APPLICATION 38

System.out.println("2: Game Completed (line 313) player1Energy = " + PoliticianRumbleTestServer.player1Energy); System.out.println("2: Game Completed (line 314) player2Energy = " + PoliticianRumbleTestServer.player2Energy); sendEnergy(PoliticianRumbleTestServer.player2Energy); if (PoliticianRumbleTestServer.player1Energy <= 0 && PoliticianRumbleTestServer.player2Energy <= 0) { System.out.println("2: Game TIED (line 318)"); sendWdatFile(); output.println("TIE"); } else if (PoliticianRumbleTestServer.player1Energy < PoliticianRumbleTestServer.player2Energy) { System.out.println("2: Player 2 Victory (line 322)"); sendWdatFile(); output.println("VICTORY"); } else if (PoliticianRumbleTestServer.player1Energy > PoliticianRumbleTestServer.player2Energy) { System.out.println("2: Player 2 Defeated (line 326)"); sendWdatFile(); output.println("DEFEAT"); } else output.println("");

} } } // Sends the chosen character information public void sendCharacter() {//Start public void sendCharacter()

POLITICAL RUMBLE, A JAVA CLIENT/SERVER APPLICATION 39

if (mark == '1') { output.println("CHOICE Your chosen game character is " + PoliticianRumbleTestServer.player1CharacterName); } else output.println("CHOICE Your chosen game character is " + PoliticianRumbleTestServer.player2CharacterName); //output.println("CHOICE Your chosen game character is " + character); }//End public void sendCharacter()

//Sends the challenger identity to each player public void sendChallenger() {//Start public void sendChallenger() if (mark == '1') { output.println("OPONNENT Your opponent character is " + PoliticianRumbleTestServer.player2CharacterName); } else output.println("OPONNENT Your opponent character is " + PoliticianRumbleTestServer.player1CharacterName); }//End public void sendChallenger() //Sends the energy level to the player public void sendEnergy(int energyLevel) {//Start public void sendEnergy(int energyLevel) output.println("ENERGY " + energyLevel); try { fileOut = new Formatter("player1.wdat"); fileOut.format(PoliticianRumbleTestServer.player1CharacterNum + "\n" + PoliticianRumbleTestServer.player1CharacterName + "\n" + PoliticianRumbleTestServer.player1Energy + "\n" + PoliticianRumbleTestServer.player2CharacterNum +

POLITICAL RUMBLE, A JAVA CLIENT/SERVER APPLICATION 40

"\n" + PoliticianRumbleTestServer.player2CharacterName + "\n" + PoliticianRumbleTestServer.player2Energy); fileOut.close(); }catch(FileNotFoundException fileNotFoundException){}

try { fileOut = new Formatter("player2.wdat"); fileOut.format(PoliticianRumbleTestServer.player2CharacterNum + "\n" + PoliticianRumbleTestServer.player2CharacterName + "\n" + PoliticianRumbleTestServer.player2Energy + "\n" + PoliticianRumbleTestServer.player1CharacterNum + "\n" + PoliticianRumbleTestServer.player1CharacterName + "\n" + PoliticianRumbleTestServer.player1Energy); fileOut.close(); }catch(FileNotFoundException fileNotFoundException){} }//End public void sendEnergy(int energyLevel) /** * Sends the combat menu to the players */ public void sendCombatMenu() {//Start public void sendCombatMenu() //output.println("CLEAR"); output.println("MEN1 Combat Menu--------------------------" + "1. Taunt Opponent" + "2. Launch Smear Campaign" + "3. Launch Television Ad" + "4. Throw Fund Raising Dinner"); System.out.println("*** Combat Menu Sent to player ***"); }//End public void sendCombatMenu()

POLITICAL RUMBLE, A JAVA CLIENT/SERVER APPLICATION 41

//Sends the Taunt Menu and taunt options to the player public void sendTauntMenu() { output.println("CLEAR"); sendCharacter(); sendChallenger(); System.out.println("At sendTauntMenu mark = " + mark); if (mark == '1') { System.out.println("At sendTauntMenu; Inside mark == '1'; mark = " + mark); if ("1".equals(PoliticianRumbleTestServer.player2CharacterNum)) { System.out.println("At sendTauntMenu; player2CharacterNum == 1; player2CharacterNum = " + PoliticianRumbleTestServer.player2CharacterNum); PoliticianRumbleTestServer.taunt1 = (characterList.get(1)); PoliticianRumbleTestServer.taunt2 = (characterList.get(2)); } else if ("2".equals(PoliticianRumbleTestServer.player2CharacterNum)) { PoliticianRumbleTestServer.taunt1 = (characterList.get(4)); PoliticianRumbleTestServer.taunt2 = (characterList.get(5)); }else if ("3".equals(PoliticianRumbleTestServer.player2CharacterNum)) { PoliticianRumbleTestServer.taunt1 = (characterList.get(7)); PoliticianRumbleTestServer.taunt2 = (characterList.get(8)); }else if ("4".equals(PoliticianRumbleTestServer.player2CharacterNum)) { PoliticianRumbleTestServer.taunt1 = (characterList.get(10)); PoliticianRumbleTestServer.taunt2 = (characterList.get(11)); } } else { System.out.println("At sendTauntMenu; Inside mark == '2'; mark = " + mark);

POLITICAL RUMBLE, A JAVA CLIENT/SERVER APPLICATION 42

if ("1".equals(PoliticianRumbleTestServer.player1CharacterNum)) { PoliticianRumbleTestServer.taunt1 = (characterList.get(1)); PoliticianRumbleTestServer.taunt2 = (characterList.get(2)); } else if ("2".equals(PoliticianRumbleTestServer.player1CharacterNum)) { PoliticianRumbleTestServer.taunt1 = (characterList.get(4)); PoliticianRumbleTestServer.taunt2 = (characterList.get(5)); }else if ("3".equals(PoliticianRumbleTestServer.player1CharacterNum)) { PoliticianRumbleTestServer.taunt1 = (characterList.get(7)); PoliticianRumbleTestServer.taunt2 = (characterList.get(8)); }else if ("4".equals(PoliticianRumbleTestServer.player1CharacterNum)) { PoliticianRumbleTestServer.taunt1 = (characterList.get(10)); PoliticianRumbleTestServer.taunt2 = (characterList.get(11)); }} output.println("TAUNT Taunt Opponent------------------------------"); output.println("OPT" + "1. " + PoliticianRumbleTestServer.taunt1); output.println("OPT" + "2. " + PoliticianRumbleTestServer.taunt2); output.println("TINS (Select the taunt to send.)"); System.out.println("*** Taunt Options transmitted to player ***"); } // Send the chosen taunt to the opponent and decrement energy public void sendTaunt(String option) {//Start public void sendTaunt(String option) int ranNum = 0; ranNum = 1 + PoliticianRumbleTestServer.randomNumbers.nextInt(5); System.out.println("SendTaunt.ranNum = " + ranNum);

POLITICAL RUMBLE, A JAVA CLIENT/SERVER APPLICATION 43

//Decrement Energy Level and transmit to player if (mark == '1') { PoliticianRumbleTestServer.player1Energy = PoliticianRumbleTestServer.player1Energy - ranNum; sendEnergy(PoliticianRumbleTestServer.player1Energy); output.println("CLEAR"); sendCharacter(); sendChallenger(); output.println("TEND You sent a taunt that cost you: " + ranNum + " point(s)."); System.out.println("*** 1:Taunt Cost transmitted to sending player ***" + "- Player Mark: " + mark); int location = 1; if (legalMove(location, this)) { output.println("VALID_MOVE"); output.println(hasWinner() ? "VICTORY" : bothPlayersBroke() ? "TIE" : ""); } else { output.println("MESSAGE ?"); } System.out.println("1:currentPlayer = " + currentPlayer); if ("1".equals(option)) { PoliticianRumbleTestServer.messageQue = true; PoliticianRumbleTestServer.message = "TSEND You received a taunt from " + PoliticianRumbleTestServer.player1CharacterName + "; " + PoliticianRumbleTestServer.taunt1; System.out.println("** 1:Message Que Armed with taunt1");

POLITICAL RUMBLE, A JAVA CLIENT/SERVER APPLICATION 44

} else if ("2".equals(option)) { PoliticianRumbleTestServer.messageQue = true; PoliticianRumbleTestServer.message = "TSEND You received a taunt from " + PoliticianRumbleTestServer.player1CharacterName + "; " + PoliticianRumbleTestServer.taunt2; System.out.println("** 1:Message Que Armed with taunt2"); } } else { PoliticianRumbleTestServer.player2Energy = PoliticianRumbleTestServer.player2Energy - ranNum; sendEnergy(PoliticianRumbleTestServer.player2Energy); output.println("CLEAR"); sendCharacter(); sendChallenger(); output.println("TEND You sent a taunt that cost you: " + ranNum + " points."); System.out.println("*** 2:Taunt Cost transmitted to sending player ***" + "- Player Mark: " + mark); int location = 1; if (legalMove(location, this)) { output.println("VALID_MOVE"); output.println(hasWinner() ? "VICTORY" : bothPlayersBroke() ? "TIE" : ""); } else { output.println("MESSAGE ?"); }

System.out.println("2:currentPlayer = " + currentPlayer);

POLITICAL RUMBLE, A JAVA CLIENT/SERVER APPLICATION 45

if ("1".equals(option)) { PoliticianRumbleTestServer.messageQue = true; PoliticianRumbleTestServer.message = "TSEND You received a taunt from " + PoliticianRumbleTestServer.player2CharacterName + "; " + PoliticianRumbleTestServer.taunt1; System.out.println("** 2:Message Que Armed with taunt1"); } else if ("2".equals(option)) { PoliticianRumbleTestServer.messageQue = true; PoliticianRumbleTestServer.message = "TSEND You received a taunt from " + PoliticianRumbleTestServer.player2CharacterName + "; " + PoliticianRumbleTestServer.taunt2; System.out.println("** 2:Message Que Armed with taunt2"); } } }//End public void sendTaunt(String option) public void sendSmearMenu() throws IOException {//Start public void sendSmearMenu() //Smear Campaign Selection Routine FileInputStream input3 = null; try { input3 = new FileInputStream("smear.wdat"); } catch (FileNotFoundException ex) { Logger.getLogger(Game.class.getName()).log(Level.SEVERE, null, ex); } BufferedReader br = new BufferedReader(new InputStreamReader(input3));

POLITICAL RUMBLE, A JAVA CLIENT/SERVER APPLICATION 46

String strLine; while((strLine = br.readLine()) !=null) { smearList.add(" " + strLine); }input3.close(); System.out.println("Smear Campaigns Loaded: " + smearList); output.println("CLEAR"); sendCharacter(); sendChallenger(); System.out.println("At sendSmearMenu mark = " + mark); if (mark == '1') { System.out.println("At sendSmearMenu; Inside mark == '1'; mark = " + mark); if ("1".equals(PoliticianRumbleTestServer.player2CharacterNum)) { PoliticianRumbleTestServer.smear1 = (smearList.get(0)); PoliticianRumbleTestServer.smear2 = (smearList.get(1)); PoliticianRumbleTestServer.smear3 = (smearList.get(2)); } else if ("2".equals(PoliticianRumbleTestServer.player2CharacterNum)) { PoliticianRumbleTestServer.smear1 = (smearList.get(3)); PoliticianRumbleTestServer.smear2 = (smearList.get(4)); PoliticianRumbleTestServer.smear3 = (smearList.get(5)); }else if ("3".equals(PoliticianRumbleTestServer.player2CharacterNum)) { PoliticianRumbleTestServer.smear1 = (smearList.get(6)); PoliticianRumbleTestServer.smear2 = (smearList.get(7)); PoliticianRumbleTestServer.smear3 = (smearList.get(8)); }else if ("4".equals(PoliticianRumbleTestServer.player2CharacterNum)) { PoliticianRumbleTestServer.smear1 = (smearList.get(9));

POLITICAL RUMBLE, A JAVA CLIENT/SERVER APPLICATION 47

PoliticianRumbleTestServer.smear2 = (smearList.get(10)); PoliticianRumbleTestServer.smear3 = (smearList.get(11)); } } else { System.out.println("At sendSmearMenu; Inside mark == '2'; mark = " + mark); if ("1".equals(PoliticianRumbleTestServer.player1CharacterNum)) { PoliticianRumbleTestServer.smear1 = (smearList.get(0)); PoliticianRumbleTestServer.smear2 = (smearList.get(1)); PoliticianRumbleTestServer.smear3 = (smearList.get(2)); } else if ("2".equals(PoliticianRumbleTestServer.player1CharacterNum)) { PoliticianRumbleTestServer.smear1 = (smearList.get(3)); PoliticianRumbleTestServer.smear2 = (smearList.get(4)); PoliticianRumbleTestServer.smear3 = (smearList.get(5)); }else if ("3".equals(PoliticianRumbleTestServer.player1CharacterNum)) { PoliticianRumbleTestServer.smear1 = (smearList.get(6)); PoliticianRumbleTestServer.smear2 = (smearList.get(7)); PoliticianRumbleTestServer.smear3 = (smearList.get(8)); }else if ("4".equals(PoliticianRumbleTestServer.player1CharacterNum)) { PoliticianRumbleTestServer.smear1 = (smearList.get(9)); PoliticianRumbleTestServer.smear2 = (smearList.get(10)); PoliticianRumbleTestServer.smear3 = (smearList.get(11)); }} output.println("SMEAR Start Smear Campaign -----------------------------------------"); output.println("OPT" + "1. " + PoliticianRumbleTestServer.smear1); output.println("OPT" + "2. " + PoliticianRumbleTestServer.smear2); output.println("OPT" + "3. " + PoliticianRumbleTestServer.smear3); output.println("SINS (Select the smear campaign to start.)");

POLITICAL RUMBLE, A JAVA CLIENT/SERVER APPLICATION 48

System.out.println("*** Smear Campaign Options transmitted to player ***"); }//End public void sendSmearMenu() public void startSmear(String option) {//Start public void startSmear(String option) int ranNum = 0; ranNum = 1 + PoliticianRumbleTestServer.randomNumbers.nextInt(10); System.out.println("SendSmearCost.ranNum = " + ranNum); //Decrement Energy Level and transmit to player if (mark == '1') { output.println("CLEAR"); sendCharacter(); sendChallenger(); output.println("SSEND You started a smear campaign that cost you: " + ranNum + " point(s)."); PoliticianRumbleTestServer.player1Energy = PoliticianRumbleTestServer.player1Energy - ranNum; sendEnergy(PoliticianRumbleTestServer.player1Energy); ranNum = 5 + PoliticianRumbleTestServer.randomNumbers.nextInt(15); PoliticianRumbleTestServer.player2Energy = PoliticianRumbleTestServer.player2Energy - ranNum; output.println("SSEND Your smear campaign cost your opponent: " + ranNum + " point(s)."); output.println("SSEND Your opponents score is now: " + PoliticianRumbleTestServer.player2Energy + " point(s)."); System.out.println("*** 1:Smear Campaign Cost transmitted to sending player ***" +"- Player Mark: " + mark); int location = 1; if (legalMove(location, this)) { output.println("VALID_MOVE");

POLITICAL RUMBLE, A JAVA CLIENT/SERVER APPLICATION 49

output.println(hasWinner() ? "VICTORY" : bothPlayersBroke() ? "TIE" : ""); } else { output.println("MESSAGE ?"); } System.out.println("1:currentPlayer = " + currentPlayer); if ("1".equals(option)) { PoliticianRumbleTestServer.messageQue = true; PoliticianRumbleTestServer.messageHeader = "SSEND" + PoliticianRumbleTestServer.player1CharacterName + " started a smear campaign against you; "; PoliticianRumbleTestServer.message = "SSEND"+PoliticianRumbleTestServer.smear1; PoliticianRumbleTestServer.messageCost = "SSEND The smear campaign lost you " + ranNum + " point(s)"; System.out.println("** 1:SmearCampaignMessage Que Armed with smear1"); } else if ("2".equals(option)) { PoliticianRumbleTestServer.messageQue = true; PoliticianRumbleTestServer.messageHeader = "SSEND" + PoliticianRumbleTestServer.player1CharacterName + " started a smear campaign against you; "; PoliticianRumbleTestServer.message = "SSEND"+PoliticianRumbleTestServer.smear2; PoliticianRumbleTestServer.messageCost = "SSEND The smear campaign lost you " + ranNum + " point(s)"; System.out.println("** 1:SmearCampaignMessage Que Armed with smear2");

POLITICAL RUMBLE, A JAVA CLIENT/SERVER APPLICATION 50

} else if ("3".equals(option)) { PoliticianRumbleTestServer.messageQue = true; PoliticianRumbleTestServer.messageHeader = "SSEND" + PoliticianRumbleTestServer.player1CharacterName + " started a smear campaign against you; "; PoliticianRumbleTestServer.message = "SSEND"+PoliticianRumbleTestServer.smear3; PoliticianRumbleTestServer.messageCost = "SSEND The smear campaign lost you " + ranNum + " point(s)"; System.out.println("** 1:SmearCampaignMessage Que Armed with smear3"); } } else { output.println("CLEAR"); sendCharacter(); sendChallenger(); output.println("SSEND You started a smear campaign that cost you: " + ranNum + " point(s)."); PoliticianRumbleTestServer.player2Energy = PoliticianRumbleTestServer.player2Energy - ranNum; sendEnergy(PoliticianRumbleTestServer.player2Energy); ranNum = 5 + PoliticianRumbleTestServer.randomNumbers.nextInt(15); PoliticianRumbleTestServer.player1Energy = PoliticianRumbleTestServer.player1Energy - ranNum; output.println("SSEND Your smear campaign cost your opponent: " + ranNum +" point(s)."); output.println("SSEND Your opponents score is now: " + PoliticianRumbleTestServer.player1Energy + " point(s)."); System.out.println("*** 2:Smear Campaign Cost transmitted to sending player ***" + "- Player Mark: " + mark);

POLITICAL RUMBLE, A JAVA CLIENT/SERVER APPLICATION 51

int location = 1; if (legalMove(location, this)) { output.println("VALID_MOVE"); output.println(hasWinner() ? "VICTORY" : bothPlayersBroke() ? "TIE" : ""); } else { output.println("MESSAGE ?"); }

System.out.println("2:currentPlayer = " + currentPlayer);

if ("1".equals(option)) { PoliticianRumbleTestServer.messageQue = true; PoliticianRumbleTestServer.messageHeader = "SSEND" + PoliticianRumbleTestServer.player2CharacterName + " started a smear campaign against you; "; PoliticianRumbleTestServer.message = "SSEND"+PoliticianRumbleTestServer.smear1; PoliticianRumbleTestServer.messageCost = "SSEND The smear campaign lost you " + ranNum + " point(s)"; System.out.println("** 2:SmearCampaignMessage Que Armed with smear1"); } else if ("2".equals(option)) { PoliticianRumbleTestServer.messageQue = true; PoliticianRumbleTestServer.messageHeader = "SSEND" + PoliticianRumbleTestServer.player2CharacterName + " started a smear campaign against you; "; PoliticianRumbleTestServer.message = "SSEND"+PoliticianRumbleTestServer.smear2;

POLITICAL RUMBLE, A JAVA CLIENT/SERVER APPLICATION 52

PoliticianRumbleTestServer.messageCost = "SSEND The smear campaign lost you " + ranNum + " point(s)"; System.out.println("** 2:SmearCampaignMessage Que Armed with smear2"); } else if ("3".equals(option)) { PoliticianRumbleTestServer.messageQue = true; PoliticianRumbleTestServer.messageHeader = "SSEND" + PoliticianRumbleTestServer.player2CharacterName + " started a smear campaign against you; "; PoliticianRumbleTestServer.message = "SSEND"+PoliticianRumbleTestServer.smear3; PoliticianRumbleTestServer.messageCost = "SSEND The smear campaign lost you " + ranNum + " point(s)"; System.out.println("** 2:SmearCampaignMessage Que Armed with smear3"); } } }//End public void startSmear(String option) public void sendTvAdMenu() throws IOException {//Start public void sendTvAdMenu() //Smear Campaign Selection Routine FileInputStream input4 = null; try { input4 = new FileInputStream("tvads.wdat"); } catch (FileNotFoundException ex) { Logger.getLogger(Game.class.getName()).log(Level.SEVERE, null, ex); } BufferedReader br = new BufferedReader(new InputStreamReader(input4)); String strLine; while((strLine = br.readLine()) !=null)

POLITICAL RUMBLE, A JAVA CLIENT/SERVER APPLICATION 53

{ tvadsList.add(" " + strLine); }input4.close(); System.out.println("TV Ad Campaigns Loaded: " + tvadsList); output.println("CLEAR"); sendCharacter(); sendChallenger(); System.out.println("At sendTvAdMenu mark = " + mark); if (mark == '1') { System.out.println("At sendTvAdMenu; Inside mark == '1'; mark = " + mark); if ("1".equals(PoliticianRumbleTestServer.player2CharacterNum)) { PoliticianRumbleTestServer.tvad1 = (tvadsList.get(0)); PoliticianRumbleTestServer.tvad2 = (tvadsList.get(1)); PoliticianRumbleTestServer.tvad3 = (tvadsList.get(2)); PoliticianRumbleTestServer.tvad4 = (tvadsList.get(3)); PoliticianRumbleTestServer.tvad5 = (tvadsList.get(4)); PoliticianRumbleTestServer.tvad6 = (tvadsList.get(5)); } else if ("2".equals(PoliticianRumbleTestServer.player2CharacterNum)) { PoliticianRumbleTestServer.tvad1 = (tvadsList.get(6)); PoliticianRumbleTestServer.tvad2 = (tvadsList.get(7)); PoliticianRumbleTestServer.tvad3 = (tvadsList.get(8)); PoliticianRumbleTestServer.tvad4 = (tvadsList.get(9)); PoliticianRumbleTestServer.tvad5 = (tvadsList.get(10)); PoliticianRumbleTestServer.tvad6 = (tvadsList.get(11)); }else if ("3".equals(PoliticianRumbleTestServer.player2CharacterNum)) { PoliticianRumbleTestServer.tvad1 = (tvadsList.get(12)); PoliticianRumbleTestServer.tvad2 = (tvadsList.get(13));

POLITICAL RUMBLE, A JAVA CLIENT/SERVER APPLICATION 54

PoliticianRumbleTestServer.tvad3 = (tvadsList.get(14)); PoliticianRumbleTestServer.tvad4 = (tvadsList.get(15)); PoliticianRumbleTestServer.tvad5 = (tvadsList.get(16)); PoliticianRumbleTestServer.tvad6 = (tvadsList.get(17)); }else if ("4".equals(PoliticianRumbleTestServer.player2CharacterNum)) { PoliticianRumbleTestServer.tvad1 = (tvadsList.get(18)); PoliticianRumbleTestServer.tvad2 = (tvadsList.get(19)); PoliticianRumbleTestServer.tvad3 = (tvadsList.get(20)); PoliticianRumbleTestServer.tvad4 = (tvadsList.get(21)); PoliticianRumbleTestServer.tvad5 = (tvadsList.get(22)); PoliticianRumbleTestServer.tvad6 = (tvadsList.get(23)); } } else { System.out.println("At sendTvAdMenu; Inside mark == '2'; mark = " + mark); if ("1".equals(PoliticianRumbleTestServer.player1CharacterNum)) { PoliticianRumbleTestServer.tvad1 = (tvadsList.get(0)); PoliticianRumbleTestServer.tvad2 = (tvadsList.get(1)); PoliticianRumbleTestServer.tvad3 = (tvadsList.get(2)); PoliticianRumbleTestServer.tvad4 = (tvadsList.get(3)); PoliticianRumbleTestServer.tvad5 = (tvadsList.get(4)); PoliticianRumbleTestServer.tvad6 = (tvadsList.get(5)); } else if ("2".equals(PoliticianRumbleTestServer.player1CharacterNum)) { PoliticianRumbleTestServer.tvad1 = (tvadsList.get(6)); PoliticianRumbleTestServer.tvad2 = (tvadsList.get(7)); PoliticianRumbleTestServer.tvad3 = (tvadsList.get(8)); PoliticianRumbleTestServer.tvad4 = (tvadsList.get(9)); PoliticianRumbleTestServer.tvad5 = (tvadsList.get(10)); PoliticianRumbleTestServer.tvad6 = (tvadsList.get(11)); }else if ("3".equals(PoliticianRumbleTestServer.player1CharacterNum))

POLITICAL RUMBLE, A JAVA CLIENT/SERVER APPLICATION 55

{ PoliticianRumbleTestServer.tvad1 = (tvadsList.get(12)); PoliticianRumbleTestServer.tvad2 = (tvadsList.get(13)); PoliticianRumbleTestServer.tvad3 = (tvadsList.get(14)); PoliticianRumbleTestServer.tvad4 = (tvadsList.get(15)); PoliticianRumbleTestServer.tvad5 = (tvadsList.get(16)); PoliticianRumbleTestServer.tvad6 = (tvadsList.get(17)); }else if ("4".equals(PoliticianRumbleTestServer.player1CharacterNum)) { PoliticianRumbleTestServer.tvad1 = (tvadsList.get(18)); PoliticianRumbleTestServer.tvad2 = (tvadsList.get(19)); PoliticianRumbleTestServer.tvad3 = (tvadsList.get(20)); PoliticianRumbleTestServer.tvad4 = (tvadsList.get(21)); PoliticianRumbleTestServer.tvad5 = (tvadsList.get(22)); PoliticianRumbleTestServer.tvad6 = (tvadsList.get(23)); }} output.println("TVADS Start Televsion Ad Campaign -------------------------------------------------------"); output.println("OPT" + "1. " + PoliticianRumbleTestServer.tvad1); output.println("OPT" + "2. " + PoliticianRumbleTestServer.tvad2); output.println("OPT" + "3. " + PoliticianRumbleTestServer.tvad3); output.println("OPT" + "4. " + PoliticianRumbleTestServer.tvad4); output.println("OPT" + "5. " + PoliticianRumbleTestServer.tvad5); output.println("OPT" + "6. " + PoliticianRumbleTestServer.tvad6); output.println("TVINS (Select the televsion ad campaign to run.)"); System.out.println("*** Televsion Ad Campaign Options transmitted to player ***"); }//Start public void sendTvAdMenu() public void startTvAd(String option) {// Start public void startTvAd(String option) int ranNum = 0; ranNum = 3 + PoliticianRumbleTestServer.randomNumbers.nextInt(12);

POLITICAL RUMBLE, A JAVA CLIENT/SERVER APPLICATION 56

System.out.println("startTvAdCost.ranNum = " + ranNum); //Decrement Energy Level and transmit to player if (mark == '1') { output.println("CLEAR"); sendCharacter(); sendChallenger(); output.println("SSEND You started a television ad campaign that cost you: " + ranNum + " point(s)."); PoliticianRumbleTestServer.player1Energy = PoliticianRumbleTestServer.player1Energy - ranNum; sendEnergy(PoliticianRumbleTestServer.player1Energy); ranNum = 5 + PoliticianRumbleTestServer.randomNumbers.nextInt(18); PoliticianRumbleTestServer.player2Energy = PoliticianRumbleTestServer.player2Energy - ranNum; output.println("SSEND Your television ad campaign cost your opponent: " + ranNum +" point(s)."); output.println("SSEND Your opponents score is now: " + PoliticianRumbleTestServer.player2Energy + " point(s)."); System.out.println("*** 1:Television Ad Campaign Cost transmitted to sending player ***" + "- Player Mark: " + mark); int location = 1; if (legalMove(location, this)) { output.println("VALID_MOVE"); output.println(hasWinner() ? "VICTORY" : bothPlayersBroke() ? "TIE" : ""); } else { output.println("MESSAGE ?"); }

POLITICAL RUMBLE, A JAVA CLIENT/SERVER APPLICATION 57

System.out.println("1:currentPlayer = " + currentPlayer); if ("1".equals(option)) { PoliticianRumbleTestServer.messageQue = true; PoliticianRumbleTestServer.messageHeader = "SSEND" + PoliticianRumbleTestServer.player1CharacterName + " started a television ad campaign against you; "; PoliticianRumbleTestServer.message = "SSEND"+PoliticianRumbleTestServer.tvad1; PoliticianRumbleTestServer.messageCost = "SSEND The television ad campaign lost you " + ranNum + " point(s)"; System.out.println("** 1:TvAdCampaignMessage Que Armed with tvad1"); } else if ("2".equals(option)) { PoliticianRumbleTestServer.messageQue = true; PoliticianRumbleTestServer.messageHeader = "SSEND" + PoliticianRumbleTestServer.player1CharacterName + " started a television ad campaign against you; "; PoliticianRumbleTestServer.message = "SSEND"+PoliticianRumbleTestServer.tvad2; PoliticianRumbleTestServer.messageCost = "SSEND The televisionad campaign lost you " + ranNum + " point(s)"; System.out.println("** 1:TvAdCampaignMessage Que Armed with tvad2"); } else if ("3".equals(option)) { PoliticianRumbleTestServer.messageQue = true; PoliticianRumbleTestServer.messageHeader = "SSEND" + PoliticianRumbleTestServer.player1CharacterName + " started a television ad campaign against you; ";

POLITICAL RUMBLE, A JAVA CLIENT/SERVER APPLICATION 58

PoliticianRumbleTestServer.message = "SSEND"+PoliticianRumbleTestServer.tvad3; PoliticianRumbleTestServer.messageCost = "SSEND The televisionad campaign lost you " + ranNum + " point(s)"; System.out.println("** 1:TvAdCampaignMessage Que Armed with tvad3"); } else if ("4".equals(option)) { PoliticianRumbleTestServer.messageQue = true; PoliticianRumbleTestServer.messageHeader = "SSEND" + PoliticianRumbleTestServer.player1CharacterName + " started a television ad campaign against you; "; PoliticianRumbleTestServer.message = "SSEND"+PoliticianRumbleTestServer.tvad4; PoliticianRumbleTestServer.messageCost = "SSEND The televisionad campaign lost you " + ranNum + " point(s)"; System.out.println("** 1:TvAdCampaignMessage Que Armed with tvad4"); } else if ("5".equals(option)) { PoliticianRumbleTestServer.messageQue = true; PoliticianRumbleTestServer.messageHeader = "SSEND" + PoliticianRumbleTestServer.player1CharacterName + " started a television ad campaign against you; "; PoliticianRumbleTestServer.message = "SSEND"+PoliticianRumbleTestServer.tvad5; PoliticianRumbleTestServer.messageCost = "SSEND The televisionad campaign lost you " + ranNum + " point(s)"; System.out.println("** 1:TvAdCampaignMessage Que Armed with tvad5"); } else if ("6".equals(option)) {

POLITICAL RUMBLE, A JAVA CLIENT/SERVER APPLICATION 59

PoliticianRumbleTestServer.messageQue = true; PoliticianRumbleTestServer.messageHeader = "SSEND" + PoliticianRumbleTestServer.player1CharacterName + " started a television ad campaign against you; "; PoliticianRumbleTestServer.message = "SSEND"+PoliticianRumbleTestServer.tvad6; PoliticianRumbleTestServer.messageCost = "SSEND The televisionad campaign lost you " + ranNum + " point(s)"; System.out.println("** 1:TvAdCampaignMessage Que Armed with tvad6"); } } else { output.println("CLEAR"); sendCharacter(); sendChallenger(); output.println("SSEND You started a television ad campaign that cost you: " + ranNum + " point(s)."); PoliticianRumbleTestServer.player2Energy = PoliticianRumbleTestServer.player2Energy - ranNum; sendEnergy(PoliticianRumbleTestServer.player2Energy); ranNum = 5 + PoliticianRumbleTestServer.randomNumbers.nextInt(18); PoliticianRumbleTestServer.player1Energy = PoliticianRumbleTestServer.player1Energy - ranNum; output.println("SSEND Your television ad campaign cost your opponent: " + ranNum + " point(s)."); output.println("SSEND Your opponents score is now: " + PoliticianRumbleTestServer.player1Energy + " point(s)."); System.out.println("*** 2:Television Ad Campaign Cost transmitted to sending player ***" + "- Player Mark: " + mark); int location = 1; if (legalMove(location, this)) {

POLITICAL RUMBLE, A JAVA CLIENT/SERVER APPLICATION 60

output.println("VALID_MOVE"); output.println(hasWinner() ? "VICTORY" : bothPlayersBroke() ? "TIE" : ""); } else { output.println("MESSAGE ?"); }

System.out.println("2:currentPlayer = " + currentPlayer);

if ("1".equals(option)) { PoliticianRumbleTestServer.messageQue = true; PoliticianRumbleTestServer.messageHeader = "SSEND" + PoliticianRumbleTestServer.player2CharacterName + " started a television ad campaign against you; "; PoliticianRumbleTestServer.message = "SSEND"+PoliticianRumbleTestServer.tvad1; PoliticianRumbleTestServer.messageCost = "SSEND The televisionad campaign lost you " + ranNum + " point(s)"; System.out.println("** 2:TvAdCampaignMessage Que Armed with tvad1"); } else if ("2".equals(option)) { PoliticianRumbleTestServer.messageQue = true; PoliticianRumbleTestServer.messageHeader = "SSEND" + PoliticianRumbleTestServer.player2CharacterName + " started a television ad campaign against you; "; PoliticianRumbleTestServer.message = "SSEND"+PoliticianRumbleTestServer.tvad2; PoliticianRumbleTestServer.messageCost = "SSEND The television ad campaign lost you " + ranNum + " point(s)";

POLITICAL RUMBLE, A JAVA CLIENT/SERVER APPLICATION 61

System.out.println("** 2:TvAdCampaignMessage Que Armedwith tvad2"); } else if ("3".equals(option)) { PoliticianRumbleTestServer.messageQue = true; PoliticianRumbleTestServer.messageHeader = "SSEND" + PoliticianRumbleTestServer.player2CharacterName + " started a television ad campaign against you; "; PoliticianRumbleTestServer.message = "SSEND"+PoliticianRumbleTestServer.tvad3; PoliticianRumbleTestServer.messageCost = "SSEND The television ad campaign lost you " + ranNum + " point(s)"; System.out.println("** 2:TvAdCampaignMessage Que Armedwith tvad3"); } else if ("4".equals(option)) { PoliticianRumbleTestServer.messageQue = true; PoliticianRumbleTestServer.messageHeader = "SSEND" + PoliticianRumbleTestServer.player2CharacterName + " started a television ad campaign against you; "; PoliticianRumbleTestServer.message = "SSEND"+PoliticianRumbleTestServer.tvad4; PoliticianRumbleTestServer.messageCost = "SSEND The television ad campaign lost you " + ranNum + " point(s)"; System.out.println("** 2:TvAdCampaignMessage Que Armedwith tvad4"); } else if ("5".equals(option)) { PoliticianRumbleTestServer.messageQue = true; PoliticianRumbleTestServer.messageHeader = "SSEND" + PoliticianRumbleTestServer.player2CharacterName + " started a television ad campaign against you; ";

POLITICAL RUMBLE, A JAVA CLIENT/SERVER APPLICATION 62

PoliticianRumbleTestServer.message = "SSEND"+PoliticianRumbleTestServer.tvad5; PoliticianRumbleTestServer.messageCost = "SSEND The television ad campaign lost you " + ranNum + " point(s)"; System.out.println("** 2:TvAdCampaignMessage Que Armedwith tvad5"); } else if ("6".equals(option)) { PoliticianRumbleTestServer.messageQue = true; PoliticianRumbleTestServer.messageHeader = "SSEND" + PoliticianRumbleTestServer.player2CharacterName + " started a television ad campaign against you; "; PoliticianRumbleTestServer.message = "SSEND"+PoliticianRumbleTestServer.tvad6; PoliticianRumbleTestServer.messageCost = "SSEND The television ad campaign lost you " + ranNum + " point(s)"; System.out.println("** 2:TvAdCampaignMessage Que Armedwith tvad6"); } } }// End public void startTvAd(String option) public void sendPoliticalFundRaisingMenu() {//Start public void sendCharityFundRaiserMenu() output.println("CLEAR"); sendCharacter(); sendChallenger(); System.out.println("At sendPoliticalFundRaisingMenu mark = " + mark); output.println("PFRMPolitical Fund Raising Menu---------------------------------------------------" + "1. Host Charity Ball Cost: 2 pts. Potential Gain: 0-4 pts." + "2. Highfalutin Celebrity Ball Cost: 15 pts. Potential Gain: 12-40 pts."

POLITICAL RUMBLE, A JAVA CLIENT/SERVER APPLICATION 63

+ "3. Host Fireside Chat Cost: 3 pts. Potential Gain: 1-10 pts." + "4. Kiss A Baby Cost: 1 Pt. Potential Gain: 1-5 pts." + "5. Speak at a college Cost: 4 pts. Potential Gain: 1-8 pts." + "6. Donate Time for Charity Cost: 0 Pts. Potential Gain: 0-30 Pts." + "7. Grass Roots Fund Raising Cost: 10 Pts. Potential Gain: 1-20 Pts." + "8. Promise Political Support Cost: 8 Pts. Potential Gain: 3-25 Pts."); System.out.println("*** Political Fund Raising Menu Sent to player ***"); }//End public void sendCharityFundRaiserMenu() public void startPoliticalFundRaiser(String option) {//Start startPoliticalFundRaiser(String option) int ranNum = 0; int cost = 0; int gain = 0; if (null != option) //Cost and Gain Calculation Routine switch (option) { case "1": cost = 2; ranNum = 2 + PoliticianRumbleTestServer.randomNumbers.nextInt(2); gain = ranNum; break; case "2": cost = 15; ranNum = 12 + PoliticianRumbleTestServer.randomNumbers.nextInt(28); gain = ranNum; break; case "3": cost = 3; ranNum = 1 + PoliticianRumbleTestServer.randomNumbers.nextInt(9); gain = ranNum; break;

POLITICAL RUMBLE, A JAVA CLIENT/SERVER APPLICATION 64

case "4": cost = 1; ranNum = 1 + PoliticianRumbleTestServer.randomNumbers.nextInt(4); gain = ranNum; break; case "5": cost = 4; ranNum = 1 + PoliticianRumbleTestServer.randomNumbers.nextInt(7); gain = ranNum; break; case "6": cost = 0; ranNum = 0 + PoliticianRumbleTestServer.randomNumbers.nextInt(30); gain = ranNum; break; case "7": cost = 10; ranNum = 10 + PoliticianRumbleTestServer.randomNumbers.nextInt(10); gain = ranNum; break; case "8": cost = 8; ranNum = 3 + PoliticianRumbleTestServer.randomNumbers.nextInt(22); gain = ranNum; break; } System.out.println("startPoliticalFundRaiserCost.cost = " + cost); System.out.println("startPoliticalFundRaiserCost.gain = " + gain); //Increment or Decrement Energy Level and transmit to player if (mark == '1') {

POLITICAL RUMBLE, A JAVA CLIENT/SERVER APPLICATION 65

output.println("CLEAR"); sendCharacter(); sendChallenger(); output.println("SSEND You started a Political Fund Raiser that cost you: " + cost + " point(s)."); PoliticianRumbleTestServer.player1Energy = PoliticianRumbleTestServer.player1Energy - cost; PoliticianRumbleTestServer.player1Energy = PoliticianRumbleTestServer.player1Energy + gain; sendEnergy(PoliticianRumbleTestServer.player1Energy); output.println("SSEND Your Political Fund Raiser Earned: " + gain + " point(s)."); output.println("SSEND Your opponents score is presently: " + PoliticianRumbleTestServer.player2Energy + " point(s)."); System.out.println("*** 1:Political Fund Raising Costs transmitted to sending player ***" + "- Player Mark: " + mark); int location = 1; if (legalMove(location, this)) { output.println("VALID_MOVE"); output.println(hasWinner() ? "DEFEAT" : bothPlayersBroke() ? "TIE" : ""); } else { output.println("MESSAGE ?"); } System.out.println("1:currentPlayer = " + currentPlayer); PoliticianRumbleTestServer.messageQue = true; PoliticianRumbleTestServer.messageHeader = "SSEND" + PoliticianRumbleTestServer.player1CharacterName + " held a Poltical Fund Raiser; "; PoliticianRumbleTestServer.message = "SSEND The Fund Raising Even costyour opponent: " + cost +" Pts.";

POLITICAL RUMBLE, A JAVA CLIENT/SERVER APPLICATION 66

PoliticianRumbleTestServer.messageCost = "SSEND The Fund Raising Eventearned: " + gain + " point(s) for your opponent."; System.out.println("** 1:PoliticalFundRaiserMessage Que Armed with Results"); } else { output.println("CLEAR"); sendCharacter(); sendChallenger(); output.println("SSEND You started a Political Fund Raiser that cost you: " + cost + " point(s)."); PoliticianRumbleTestServer.player2Energy = PoliticianRumbleTestServer.player2Energy - cost; PoliticianRumbleTestServer.player2Energy = PoliticianRumbleTestServer.player2Energy + gain; sendEnergy(PoliticianRumbleTestServer.player2Energy); output.println("SSEND Your Political Fund Raiser Earned: " + gain + " point(s)."); output.println("SSEND Your opponents score is presently: " + PoliticianRumbleTestServer.player1Energy + " point(s)."); System.out.println("*** 2:Political Fund Raising Costs transmitted to sending player ***" + "- Player Mark: " + mark); int location = 1; if (legalMove(location, this)) { output.println("VALID_MOVE"); output.println(hasWinner() ? "VICTORY" : bothPlayersBroke() ? "TIE" : ""); } else { output.println("MESSAGE ?"); }

POLITICAL RUMBLE, A JAVA CLIENT/SERVER APPLICATION 67

System.out.println("2:currentPlayer = " + currentPlayer); PoliticianRumbleTestServer.messageQue = true; PoliticianRumbleTestServer.messageHeader = "SSEND" + PoliticianRumbleTestServer.player1CharacterName + " held a Poltical Fund Raiser; "; PoliticianRumbleTestServer.message = "SSEND The Fund Raising Even costyour opponent: " + cost +" Pts."; PoliticianRumbleTestServer.messageCost = "SSEND The Fund Raising Eventearned: " + gain + " point(s) for your opponent."; System.out.println("** 2:PoliticalFundRaiserMessage Que Armed with Results"); } }//End startPoliticalFundRaiser(String option) public void sendWdatFile() {//Start public void sendWdatFile() if (mark == '1') { output.println("CHNUM" + PoliticianRumbleTestServer.player1CharacterNum); output.println("CHNAM" + PoliticianRumbleTestServer.player1CharacterName); output.println("ENERGY " + PoliticianRumbleTestServer.player1Energy); output.println("OPNAM" + PoliticianRumbleTestServer.player2CharacterName); output.println("OPENERGY" + PoliticianRumbleTestServer.player2Energy); } else { output.println("CHNUM" + PoliticianRumbleTestServer.player2CharacterNum); output.println("CHNAM" + PoliticianRumbleTestServer.player2CharacterName); output.println("ENERGY " + PoliticianRumbleTestServer.player2Energy); output.println("OPNAM" + PoliticianRumbleTestServer.player1CharacterName); output.println("OPENERGY" + PoliticianRumbleTestServer.player1Energy); }

POLITICAL RUMBLE, A JAVA CLIENT/SERVER APPLICATION 68

}//End public void sendWdatFile() /** * The run method of this thread. */ public void run() { try { // The thread is only started after everyone connects. output.println("MESSAGE All players connected; Waiting for Player 1 to move."); sendChallenger(); sendWdatFile(); PoliticianRumbleTestServer.gameState = 2;

// Tell the first player that it is their turn. if (mark == '1') { output.println("MESSAGE Your turn!"); output.println("CLEAR"); sendCharacter(); sendChallenger(); sendCombatMenu(); }

// Repeatedly get commands from the client and process them. while (true) {

String command = input.readLine(); if (command.startsWith("MOVE")) { int location = Integer.parseInt(command.substring(5)); if (legalMove(location, this)) { if (!(hasWinner())) {

POLITICAL RUMBLE, A JAVA CLIENT/SERVER APPLICATION 69

} else { if (1 == mark) { System.out.println("1: Game Completed (line 1135) player1Energy = " + PoliticianRumbleTestServer.player1Energy); System.out.println("1: Game Completed (line 1135) player2Energy = " + PoliticianRumbleTestServer.player2Energy); sendEnergy(PoliticianRumbleTestServer.player1Energy); if (PoliticianRumbleTestServer.player1Energy <= 0 && PoliticianRumbleTestServer.player2Energy <= 0) { System.out.println("1: Game TIED (line 1140)"); output.println("TIE"); } else if (PoliticianRumbleTestServer.player1Energy < PoliticianRumbleTestServer.player2Energy) { System.out.println("1: Player 1 Defeated (line 1144)"); output.println("DEFEAT"); } else if (PoliticianRumbleTestServer.player1Energy > PoliticianRumbleTestServer.player2Energy) { System.out.println("1: Player 1 Victory (line 1148)"); output.println("VICTORY"); } else output.println(""); } else { System.out.println("2: Game Completed (line 1152) player1Energy = " + PoliticianRumbleTestServer.player1Energy); System.out.println("2: Game Completed (line 1153) player2Energy = " + PoliticianRumbleTestServer.player2Energy); sendEnergy(PoliticianRumbleTestServer.player2Energy); if (PoliticianRumbleTestServer.player1Energy <= 0 && PoliticianRumbleTestServer.player2Energy <= 0) {

POLITICAL RUMBLE, A JAVA CLIENT/SERVER APPLICATION 70

System.out.println("2: Game TIED (line 1157)"); output.println("TIE"); } else if (PoliticianRumbleTestServer.player1Energy < PoliticianRumbleTestServer.player2Energy) { System.out.println("2: Player 2 Victory (line 1162)"); output.println("VICTORY"); } else if (PoliticianRumbleTestServer.player1Energy > PoliticianRumbleTestServer.player2Energy) { System.out.println("2: Player 2 Defeated (line 1165)"); output.println("DEFEAT"); } else output.println("");

}} } else { output.println("MESSAGE ?"); } } else if (command.startsWith("MEN1")) { System.out.println("Combat Menu Option Received = " + command.substring(4)); if ("1".equals(command.substring(4))) //Send Taunt Menu { sendTauntMenu(); }else if ("2".equals(command.substring(4))) //Send Taunt Menu { sendSmearMenu(); }else if ("3".equals(command.substring(4))) //Send Taunt Menu { sendTvAdMenu(); }else if ("4".equals(command.substring(4))) //Send Taunt Menu {

POLITICAL RUMBLE, A JAVA CLIENT/SERVER APPLICATION 71

sendPoliticalFundRaisingMenu(); } } else if (command.startsWith("BACK")) { output.println("CLEAR"); sendCharacter(); sendChallenger(); sendCombatMenu(); } else if (command.startsWith("TSEL")) { sendTaunt(command.substring(4)); } else if (command.startsWith("SSEL")) { startSmear(command.substring(4)); } else if (command.startsWith("TVAD")) { startTvAd(command.substring(4)); } else if (command.startsWith("PFRM")) { startPoliticalFundRaiser(command.substring(4)); } else if (command.startsWith("QUE?")) { if(PoliticianRumbleTestServer.messageQue == true) { if (PoliticianRumbleTestServer.message.startsWith("TSEND")) { output.println("CLEAR"); sendCharacter(); sendChallenger(); output.println(PoliticianRumbleTestServer.message); PoliticianRumbleTestServer.messageQue = false; System.out.println("*** Taunt sent to Player ***"); sendCombatMenu(); }else if (PoliticianRumbleTestServer.message.startsWith("SSEND")) { output.println("CLEAR"); sendCharacter();

POLITICAL RUMBLE, A JAVA CLIENT/SERVER APPLICATION 72

sendChallenger(); output.println(PoliticianRumbleTestServer.messageHeader); output.println(PoliticianRumbleTestServer.message); output.println(PoliticianRumbleTestServer.messageCost); PoliticianRumbleTestServer.messageQue = false; System.out.println("*** Smear Campaign Data Transmitted to Player ***"); if (mark == '1') { sendEnergy(PoliticianRumbleTestServer.player1Energy); output.println("SSEND Your opponents score is now: " + PoliticianRumbleTestServer.player2Energy + " point(s)."); } else { sendEnergy(PoliticianRumbleTestServer.player2Energy); output.println("SSEND Your opponents score is now:" + PoliticianRumbleTestServer.player1Energy + " point(s)."); } sendCombatMenu(); } } } else if (command.startsWith("QUIT")) { System.exit(1); } }// End While true } catch (IOException e) {

POLITICAL RUMBLE, A JAVA CLIENT/SERVER APPLICATION 73

System.out.println("Player died: " + e); } finally { try {socket.close();} catch (IOException e) {} } } }

}//End class PoliticianRumbleTestServer

Java Client Code/********************************************************************************** ********************************************************************************** **** Programmer: Daniel LeBlanc, James Hamidou, Mark Brazinski, John Hanna ***** **** Assignment: SEN632 Final Project CLIENT Test ***** **** Company Name: Envisage ***** **** Political Rumble Client Test v017 ***** **** Created: November 02, 2014 ***** **** Compiler Used: Eclipse ***** ********************************************************************************** **********************************************************************************/package politicianrumbletestclient;

import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.io.BufferedReader;

POLITICAL RUMBLE, A JAVA CLIENT/SERVER APPLICATION 74

import java.io.File;import java.io.FileNotFoundException;import java.io.InputStreamReader;import java.io.PrintWriter;import static java.lang.Integer.parseInt;import java.net.Socket;import java.util.Formatter;import java.util.Scanner;import java.util.logging.Level;import java.util.logging.Logger;import javax.swing.ImageIcon;import javax.swing.JOptionPane;import static sun.lwawt.macosx.CWrapper.NSView.frame;

/************************************************************************** ************************************************************************** ***** PROJECT DESCRIPTION ***** ***** ***** ***** Implement a multiplayer, network-based, Warrior-Combat ***** ***** game. The architecture shall make use of a client-server ***** ***** model, with the server being the combat moderator, and ***** ***** the various clients being the warrior combatants. ***** ************************************************************************** **************************************************************************/

public class PoliticianRumbleTestClient extends javax.swing.JFrame implements ActionListener{//Start public class PoliticianRumbleTestClient // Variables declaration - GUI Form private javax.swing.JPanel clientJPanelPoliticalRumble;

POLITICAL RUMBLE, A JAVA CLIENT/SERVER APPLICATION 75

private javax.swing.JLabel enterEndJLabel; private javax.swing.JLabel enterSelectionJLabel; private javax.swing.JLabel gamePlayAreaLabel; private javax.swing.JTextArea gamePlayJTextArea; private javax.swing.JScrollPane jScrollPane1; private javax.swing.JScrollPane jScrollPane2; private javax.swing.JLabel messagesLabel; private javax.swing.JTextArea moderatorMessageJTextArea; private javax.swing.JTextField selectionJTextField; private javax.swing.JLabel welcomeLabel; private javax.swing.JLabel energyLevelJLabel; private javax.swing.JTextField energyLevelJTextField; // End of variables declaration - GUI Form //Variables declaration - networking private static final int PORT = 8901; private Socket socket; private BufferedReader in; private PrintWriter out; public static int gameState = 0; public static int menLevel = 0; public static int menVersion = 0; public static int characterNum = 0; public static int opponentCharacterNum = 0; public static String characterName = ""; public static String opponentCharacterName = ""; public static String energy = ""; public static String opponentEnergy = ""; public Scanner fileIn; public Formatter fileOut; public static char mark = '0';

POLITICAL RUMBLE, A JAVA CLIENT/SERVER APPLICATION 76

/** * Constructs the client by connecting to a server, laying out the * GUI and registering GUI listeners. * @param serverAddress * @throws java.lang.Exception */ public PoliticianRumbleTestClient(String serverAddress) throws Exception {// Start PoliticianRumbleTestv008Client Constructor initComponents(); // Setup networking socket = new Socket(serverAddress, PORT); in = new BufferedReader(new InputStreamReader( socket.getInputStream())); out = new PrintWriter(socket.getOutputStream(), true); }// end PoliticianRumbleTestClient Constructor /** * This method is called from within the constructor to initialize the form. * WARNING: Do NOT modify this code. The content of this method is always * regenerated by the Form Editor. */ @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code"> private void initComponents() {

clientJPanelPoliticalRumble = new javax.swing.JPanel(); welcomeLabel = new javax.swing.JLabel(); messagesLabel = new javax.swing.JLabel(); jScrollPane1 = new javax.swing.JScrollPane();

POLITICAL RUMBLE, A JAVA CLIENT/SERVER APPLICATION 77

moderatorMessageJTextArea = new javax.swing.JTextArea(); gamePlayAreaLabel = new javax.swing.JLabel(); jScrollPane2 = new javax.swing.JScrollPane(); gamePlayJTextArea = new javax.swing.JTextArea(); enterSelectionJLabel = new javax.swing.JLabel(); selectionJTextField = new javax.swing.JTextField(); enterEndJLabel = new javax.swing.JLabel(); energyLevelJLabel = new javax.swing.JLabel(); energyLevelJTextField = new javax.swing.JTextField(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

clientJPanelPoliticalRumble.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));

welcomeLabel.setFont(new java.awt.Font("Cooper Black", 0, 20)); welcomeLabel.setText("Welcome to Politician Rumble. Get Ready to Rumble!");

messagesLabel.setFont(new java.awt.Font("Lucida Grande", 3, 13)); messagesLabel.setText("Messages from the Moderator appear here:");

moderatorMessageJTextArea.setEditable(false); moderatorMessageJTextArea.setBackground(new java.awt.Color(204, 204, 204)); moderatorMessageJTextArea.setColumns(20); moderatorMessageJTextArea.setFont(new java.awt.Font("Cooper Black", 0, 16)); moderatorMessageJTextArea.setLineWrap(true); moderatorMessageJTextArea.setRows(5); moderatorMessageJTextArea.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0))); jScrollPane1.setViewportView(moderatorMessageJTextArea);

gamePlayAreaLabel.setFont(new java.awt.Font("Lucida Grande", 3, 13));

POLITICAL RUMBLE, A JAVA CLIENT/SERVER APPLICATION 78

gamePlayAreaLabel.setText("Game Play Message Area:");

gamePlayJTextArea.setEditable(false); gamePlayJTextArea.setBackground(new java.awt.Color(204, 204, 204)); gamePlayJTextArea.setColumns(20); gamePlayJTextArea.setFont(new java.awt.Font("Cooper Black", 0, 14)); gamePlayJTextArea.setRows(5); gamePlayJTextArea.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0))); jScrollPane2.setViewportView(gamePlayJTextArea);

enterSelectionJLabel.setFont(new java.awt.Font("Lucida Grande", 1, 14)); enterSelectionJLabel.setText("Enter Selection:");

selectionJTextField.setForeground(new java.awt.Color(0, 0, 204)); selectionJTextField.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));

enterEndJLabel.setFont(new java.awt.Font("Lucida Grande", 3, 14)); enterEndJLabel.setForeground(new java.awt.Color(51, 51, 255)); enterEndJLabel.setText("(Enter BACK for previous menu or END to exit the game)"); energyLevelJLabel.setFont(new java.awt.Font("Lucida Grande", 1, 13)); energyLevelJLabel.setText("Energy Level:");

energyLevelJTextField.setEditable(false); energyLevelJTextField.setFont(new java.awt.Font("Lucida Grande", 1, 13)); energyLevelJTextField.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));

POLITICAL RUMBLE, A JAVA CLIENT/SERVER APPLICATION 79

javax.swing.GroupLayout clientJPanelPoliticalRumbleLayout = new javax.swing.GroupLayout(clientJPanelPoliticalRumble); clientJPanelPoliticalRumble.setLayout(clientJPanelPoliticalRumbleLayout); clientJPanelPoliticalRumbleLayout.setHorizontalGroup( clientJPanelPoliticalRumbleLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(clientJPanelPoliticalRumbleLayout.createSequentialGroup() .addGroup(clientJPanelPoliticalRumbleLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(clientJPanelPoliticalRumbleLayout.createSequentialGroup() .addGap(46, 46, 46) .addGroup(clientJPanelPoliticalRumbleLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addComponent(gamePlayAreaLabel) .addComponent(messagesLabel) .addComponent(welcomeLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 646, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jScrollPane2) .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 816, Short.MAX_VALUE))) .addGroup(clientJPanelPoliticalRumbleLayout.createSequentialGroup() .addGap(168, 168, 168) .addGroup(clientJPanelPoliticalRumbleLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false) .addGroup(javax.swing.GroupLayout.Alignment.LEADING, clientJPanelPoliticalRumbleLayout.createSequentialGroup() .addComponent(enterSelectionJLabel) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(selectionJTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 62, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)

POLITICAL RUMBLE, A JAVA CLIENT/SERVER APPLICATION 80

.addComponent(energyLevelJLabel)) .addGroup(javax.swing.GroupLayout.Alignment.LEADING, clientJPanelPoliticalRumbleLayout.createSequentialGroup() .addGap(37, 37, 37) .addComponent(enterEndJLabel))) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(energyLevelJTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE))) .addContainerGap(62, Short.MAX_VALUE)) ); clientJPanelPoliticalRumbleLayout.setVerticalGroup( clientJPanelPoliticalRumbleLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(clientJPanelPoliticalRumbleLayout.createSequentialGroup() .addContainerGap() .addComponent(welcomeLabel) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(messagesLabel) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(18, 18, 18) .addComponent(gamePlayAreaLabel) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 287, Short.MAX_VALUE) .addGap(18, 18, 18) .addGroup(clientJPanelPoliticalRumbleLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(enterSelectionJLabel) .addComponent(selectionJTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)

POLITICAL RUMBLE, A JAVA CLIENT/SERVER APPLICATION 81

.addComponent(energyLevelJLabel) .addComponent(energyLevelJTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(enterEndJLabel) .addGap(35, 35, 35)) );

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap() .addComponent(clientJPanelPoliticalRumble, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addContainerGap()) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap() .addComponent(clientJPanelPoliticalRumble, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addContainerGap()) );

pack(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);//Closes program on windowclose selectionJTextField.requestFocusInWindow();//Sends cursor to data entry field setLocationRelativeTo(null); // Centers the frame on opening

POLITICAL RUMBLE, A JAVA CLIENT/SERVER APPLICATION 82

}// </editor-fold> /** * The main thread of the client will listen for messages * from the server. The first message will be a "WELCOME" * message in which we receive our mark. Then we go into a * loop listening for "VALID_MOVE", "OPPONENT_MOVED", "VICTORY", * "DEFEAT", "TIE", "OPPONENT_QUIT or "MESSAGE" messages, * and handling each message appropriately. The "VICTORY", * "DEFEAT" and "TIE" ask the user whether or not to play * another game. If the answer is no, the loop is exited and * the server is sent a "QUIT" message. If an OPPONENT_QUIT * message is received then the loop will exit and the server * will be sent a "QUIT" message also. * @throws java.lang.Exception */ public final void play() throws Exception { String response; try { response = in.readLine(); if (response.startsWith("WELCOME")) { mark = response.charAt(8);

} while (true) { response = in.readLine(); if (response.startsWith("VALID_MOVE")) { moderatorMessageJTextArea.setText("Play Processed, Opponents Play, Please Wait"); } else if (response.startsWith("OPPONENT_MOVED")) { moderatorMessageJTextArea.setText("Opponent played, your turn"); out.println("QUE?");

POLITICAL RUMBLE, A JAVA CLIENT/SERVER APPLICATION 83

} else if (response.startsWith("VICTORY")) { moderatorMessageJTextArea.setText("You win"); break; } else if (response.startsWith("DEFEAT")) { moderatorMessageJTextArea.setText("You lose"); break; } else if (response.startsWith("TIE")) { moderatorMessageJTextArea.setText("You tied"); break; } else if (response.startsWith("CHOICE")) { gamePlayJTextArea.setText(response.substring(7) + "\n");

} else if (response.startsWith("CLEAR")) { gamePlayJTextArea.setText(""); } else if (response.startsWith("CHNUM")) { characterNum = parseInt(response.substring(5)); } else if (response.startsWith("CHNAM")) { characterName = response.substring(5); } else if (response.startsWith("OPNAM")) { opponentCharacterName = response.substring(5); } else if (response.startsWith("OPENERGY")) { opponentEnergy = response.substring(8); setIdFile(); if (0 >= parseInt(opponentEnergy)) { if (0 < parseInt(energy)) { moderatorMessageJTextArea.setText("You win");

POLITICAL RUMBLE, A JAVA CLIENT/SERVER APPLICATION 84

out.println("WIN"); wantsToPlayAgain(); } else { if (0 >= parseInt(energy)) { moderatorMessageJTextArea.setText("You tied"); out.println("TIED"); wantsToPlayAgain(); } } } } else if (response.startsWith("OPONNENT")) { gamePlayJTextArea.append(response.substring(9) + "\n");

} else if (response.startsWith("MESSAGE")) { moderatorMessageJTextArea.setText(response.substring(8)); } else if (response.startsWith("ENERGY")) { energyLevelJTextField.setText(response.substring(7)); energy = response.substring(7); setIdFile(); if (0 >= parseInt(energy)) { if (0 < parseInt(opponentEnergy)) { moderatorMessageJTextArea.setText("You lose"); out.println("LOSE"); wantsToPlayAgain(); } else { if (0 >= parseInt(opponentEnergy)) { moderatorMessageJTextArea.setText("You tied"); out.println("TIED");

POLITICAL RUMBLE, A JAVA CLIENT/SERVER APPLICATION 85

wantsToPlayAgain(); } } } } else if (response.startsWith("TAUNT")) { gamePlayJTextArea.append("\n" + response.substring(6,20) + "\n" + response.substring(20,50)); } else if (response.startsWith("OPT")) { gamePlayJTextArea.append("\n" + response.substring(3)); }else if (response.startsWith("TEND")) { gamePlayJTextArea.append("\n" + response.substring(4)); } else if (response.startsWith("TSEND")) { gamePlayJTextArea.append("\n" + response.substring(5)); } else if (response.startsWith("SSEND")) { gamePlayJTextArea.append("\n" + response.substring(5)); } else if (response.startsWith("TINS")) { gamePlayJTextArea.append("\n\n" + response.substring(4)); menVersion = 2; menLevel = 3; selectionJTextField.setText(""); selectionJTextField.requestFocusInWindow();//Sends cursor to data entry field selectionJTextField.addActionListener(this); } else if (response.startsWith("MEN1")) { gamePlayJTextArea.append("\n " + response.substring(5,16) + "\n" + " " + response.substring(17,41) + "\n" + response.substring(42,59) + "\n" + response.substring(59,83) + "\n" + response.substring(83,106) + "\n" + response.substring(106,134)); gameState = 2;

POLITICAL RUMBLE, A JAVA CLIENT/SERVER APPLICATION 86

System.out.println("gameState = " + gameState); menVersion = 1; menLevel = 5; selectionJTextField.setText(""); selectionJTextField.requestFocusInWindow();//Sends cursor to data entry field selectionJTextField.addActionListener(this); } else if (response.startsWith("CHARACTERS")) { gamePlayJTextArea.setText ("Choose a character from the list below;\n\n" + "1. " + response.substring(12,24)+ "\n2. "+ response.substring(68,80) + "\n3. " + response.substring(162,177)+ "\n4. " + response.substring(267,283)); menVersion = 0; menLevel = 5; selectionJTextField.setText(""); selectionJTextField.requestFocusInWindow();//Sends cursor to data entry field selectionJTextField.addActionListener(this); }else if (response.startsWith("SMEAR")) { gamePlayJTextArea.append("\n" + response.substring(5,32) + "\n" + response.substring(32,79)); }else if (response.startsWith("SINS")) { gamePlayJTextArea.append("\n\n" + response.substring(4)); menVersion = 3; menLevel = 4; selectionJTextField.setText(""); selectionJTextField.requestFocusInWindow();//Sends cursor to data entry field selectionJTextField.addActionListener(this); }else if (response.startsWith("TVADS")) { gamePlayJTextArea.append("\n" + response.substring(5,39)

POLITICAL RUMBLE, A JAVA CLIENT/SERVER APPLICATION 87

+ "\n" + response.substring(39,98)); }else if (response.startsWith("TVINS")) { gamePlayJTextArea.append("\n\n" + response.substring(5)); menVersion = 4; menLevel = 7; selectionJTextField.setText(""); selectionJTextField.requestFocusInWindow();//Sends cursor to data entry field selectionJTextField.addActionListener(this); }else if (response.startsWith("PFRM")) { gamePlayJTextArea.append("\n " + response.substring(4,31) + "\n" + " " + response.substring(31,82) + "\n" + response.substring(82,158) + "\n" + response.substring(158,230) + "\n" + response.substring(230,306) + "\n" + response.substring(306,383) + "\n" + response.substring(383,458) + "\n" + response.substring(458,529) + "\n" + response.substring(529,599) + "\n" + response.substring(599,670) + "\n\n Enter the number of the Political Fund Raiser to run."); gameState = 2; System.out.println("gameState = " + gameState); menVersion = 5; menLevel = 9; selectionJTextField.setText(""); selectionJTextField.requestFocusInWindow();//Sends cursor to data entry field selectionJTextField.addActionListener(this); } } out.println("QUIT");

POLITICAL RUMBLE, A JAVA CLIENT/SERVER APPLICATION 88

} finally { socket.close(); } } public void getIdFile() {//Start public void getIdFile() if (mark == '1') { try { fileIn = new Scanner(new File("player1.wdat")); characterNum = fileIn.nextInt(); characterName = fileIn.next(); energy = fileIn.next(); opponentCharacterName = fileIn.next(); opponentEnergy = fileIn.next(); fileIn.close(); System.out.println("1:fileIn; characterNum = " + characterNum); System.out.println("1:fileIn; characterName = " + characterName); System.out.println("1:fileIn; energy = " + energy); System.out.println("1:fileIn; opponentCharacterName = " + opponentCharacterName); System.out.println("1:fileIn; opponentEnergy = " + opponentEnergy); }catch(FileNotFoundException fileNotFoundException){System.out.println("getIdFile: fileNotFoundException");} } else { try {

POLITICAL RUMBLE, A JAVA CLIENT/SERVER APPLICATION 89

fileIn = new Scanner(new File("player2.wdat")); characterNum = fileIn.nextInt(); characterName = fileIn.next(); energy = fileIn.next(); opponentCharacterName = fileIn.next(); opponentEnergy = fileIn.next(); fileIn.close(); System.out.println("1:fileIn; characterNum = " + characterNum); System.out.println("1:fileIn; characterName = " + characterName); System.out.println("1:fileIn; energy = " + energy); System.out.println("1:fileIn; opponentCharacterName = " + opponentCharacterName); System.out.println("1:fileIn; opponentEnergy = " + opponentEnergy); }catch(FileNotFoundException fileNotFoundException){System.out.println("getIdFile: fileNotFoundException");} } }//End public void getIdFile() public void setIdFile() {//Start public void setIdFile() if (mark == '1') { try { fileOut = new Formatter("player1.wdat"); fileOut.format(PoliticianRumbleTestClient.characterNum + "\n" + PoliticianRumbleTestClient.characterName + "\n" + PoliticianRumbleTestClient.energy + "\n" + PoliticianRumbleTestClient.opponentCharacterName + "\n" + PoliticianRumbleTestClient.opponentEnergy);

POLITICAL RUMBLE, A JAVA CLIENT/SERVER APPLICATION 90

fileOut.close(); }catch(FileNotFoundException fileNotFoundException){System.out.println("1:setIdFile: fileNotFoundException");} } else { try { fileOut = new Formatter("player2.wdat"); fileOut.format(PoliticianRumbleTestClient.characterNum + "\n" + PoliticianRumbleTestClient.characterName + "\n" + PoliticianRumbleTestClient.energy + "\n" + PoliticianRumbleTestClient.opponentCharacterName + "\n" + PoliticianRumbleTestClient.opponentEnergy); fileOut.close(); }catch(FileNotFoundException fileNotFoundException){System.out.println("1:setIdFile: fileNotFoundException");} } }//End public void setIdFile() private boolean wantsToPlayAgain() { int response = JOptionPane.showConfirmDialog(clientJPanelPoliticalRumble, "Ready to Exit?", "Political Rumble is Fun Fun Fun", JOptionPane.YES_NO_OPTION); if (response == JOptionPane.NO_OPTION) {} else { out.println("QUIT"); System.exit(1); }

POLITICAL RUMBLE, A JAVA CLIENT/SERVER APPLICATION 91

return response == JOptionPane.NO_OPTION; } public static void main(String[] args) throws Exception {//Start main PoliticianRumbleTestClient /* Set the Nimbus look and feel */ //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html */ try { for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { if ("Nimbus".equals(info.getName())) { javax.swing.UIManager.setLookAndFeel(info.getClassName()); break; } } } catch (ClassNotFoundException ex) { java.util.logging.Logger.getLogger(PoliticianRumbleTestClient.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(PoliticianRumbleTestClient.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(PoliticianRumbleTestClient.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) {

POLITICAL RUMBLE, A JAVA CLIENT/SERVER APPLICATION 92

java.util.logging.Logger.getLogger(PoliticianRumbleTestClient.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } //</editor-fold>

/* Create and display the form */ while (true) { String serverAddress = (args.length == 0) ? "localhost" : args[1]; PoliticianRumbleTestClient client = new PoliticianRumbleTestClient(serverAddress); client.setVisible(true); client.play(); if (!client.wantsToPlayAgain()) { break; } } }//End main PoliticianRumbleTestClient

public void actionPerformed(ActionEvent e) { if (e.getSource() == selectionJTextField) { String textFieldValue = selectionJTextField.getText(); //Get first entry System.out.println("CombatMenu.Player entered: " + textFieldValue); //Output the the console for trouble shooting if ("END".equals(textFieldValue) || "end".equals(textFieldValue)) { out.println("QUIT"); System.exit(2); } else if ("BACK".equals(textFieldValue) || "back".equals(textFieldValue)) {

POLITICAL RUMBLE, A JAVA CLIENT/SERVER APPLICATION 93

if (menVersion > 1) { out.println("BACK" + menVersion); selectionJTextField.removeActionListener(this); selectionJTextField.setText(""); } else { selectionJTextField.setText(""); } } else { //Exception/Error Correction routine - Be sure client enters a valid characternumber boolean goodInput = false; int n = 0; do { try{ int menlevel = PoliticianRumbleTestClient.menLevel; n = Integer.parseInt(textFieldValue); if (n > 0 && n < menlevel) { goodInput = true; selectionJTextField.setText(""); } else {textFieldValue = JOptionPane.showInputDialog("Please enter a number from 1 to " + (menlevel-1) +" only: "); System.out.println("Player entered: " + menlevel + " - " + textFieldValue); selectionJTextField.setText("");} } catch(NumberFormatException ne) {

POLITICAL RUMBLE, A JAVA CLIENT/SERVER APPLICATION 94

textFieldValue = JOptionPane.showInputDialog("Please enter a number from 1 to " + (PoliticianRumbleTestClient.menLevel-1) +" only: "); System.out.println("Player entered: " + PoliticianRumbleTestClient.menLevel + " - " + textFieldValue); selectionJTextField.setText(""); } } while(!goodInput); if (PoliticianRumbleTestClient.menVersion == 1){ System.out.println("Combat Menu Option selected: " + PoliticianRumbleTestClient.menLevel + " - " + textFieldValue); selectionJTextField.setText(""); gamePlayJTextArea.setText (""); out.println("MEN1" + textFieldValue);// Send character selection tothe server System.out.println("CombatMenu.Player selection transmitted: " + textFieldValue); } else if (PoliticianRumbleTestClient.menVersion == 2){ System.out.println("Taunt Menu Option selected: " + PoliticianRumbleTestClient.menLevel + " - " + textFieldValue); selectionJTextField.setText(""); gamePlayJTextArea.setText (""); out.println("TSEL" + textFieldValue);// Send character selection tothe server System.out.println("TauntMenu.Player selection transmitted: " + textFieldValue); } else if (PoliticianRumbleTestClient.menVersion == 3){ System.out.println("Smear Campaign Menu Option selected: " +

POLITICAL RUMBLE, A JAVA CLIENT/SERVER APPLICATION 95

PoliticianRumbleTestClient.menLevel + " - " + textFieldValue); selectionJTextField.setText(""); gamePlayJTextArea.setText (""); out.println("SSEL" + textFieldValue);// Send character selection tothe server System.out.println("SmearMenu.Player selection transmitted: " + textFieldValue); } else if (PoliticianRumbleTestClient.menVersion == 0){ System.out.println("Character selected: " + textFieldValue); selectionJTextField.setText(""); //moderatorMessageJTextArea.setText(""); gamePlayJTextArea.setText (""); out.println("CHARACTER" + textFieldValue);// Send character selection to the server characterNum = parseInt(textFieldValue); }else if (PoliticianRumbleTestClient.menVersion == 4){ System.out.println("Television Ad Campaign selected: " + textFieldValue); selectionJTextField.setText(""); //moderatorMessageJTextArea.setText(""); gamePlayJTextArea.setText (""); out.println("TVAD" + textFieldValue);// Send televsion campaign ad selection to the server }else if (PoliticianRumbleTestClient.menVersion == 5){ System.out.println("Political Fund Raising Event selected: " + textFieldValue); selectionJTextField.setText(""); //moderatorMessageJTextArea.setText("");

POLITICAL RUMBLE, A JAVA CLIENT/SERVER APPLICATION 96

gamePlayJTextArea.setText (""); out.println("PFRM" + textFieldValue);// Send televsion campaign ad selection to the server } selectionJTextField.removeActionListener(this); } } } }//End public class PoliticianRumbleTestClient

Source FilesThere are 3 source files required for installation;

1. characters.wdat

2. smear.wdat

3. tvads.wdat

These files must be installed with the Java server file during installation. The

delimiters in all 3 files is a simple carriage return.

POLITICAL RUMBLE, A JAVA CLIENT/SERVER APPLICATION 97

characters.wdat

John BoehnerGonna cry baby?Love that tan in a can!Barack ObamaWhat happened to all that change?I thought only citizens could be president?Michele BachmanJohn Wayne the actor was born in Winterset, Iowa!Carbon dioxide is a greenhouse gas!Elizabeth WarrenNative Americans have tribal names!Two years is not a career!

smear.wdat

John Boehner Against Raising Minimum WageJohn Boehner Files Lawsuit Against President ObamaJohn Boehner Wants To Repeal ObamacareVivek Murthy Is Unqualified To Serve As Surgeon GeneralCalling Gun Violence A Public Health Concern Is A Hyper-Partisan PositionThe Net Effect of Obamacare Is Far Fewer People InsuredMichele Bachman Calls Illegal Immigration a war against the American peopleMichele Bachman Calls Healthcare Reform Bill the crown jewel of socialismMichele Bachman Has Used Her Role On The Intelligence Committee to Spread Baseless

Conspiracy Theories

POLITICAL RUMBLE, A JAVA CLIENT/SERVER APPLICATION 98

Warren and the new Consumer Financial Protection Bureau as an immensely powerful, unaccountable organization

Elizabeth Warren Supports an expansion of Social SecurityEconomic Populism Is a Dead End for Democrats and The de Blasio-Warren Agenda Won't

Travel

tvads.wdat

TV Ad slams GOP snails pace on healthcare reformTV Ad John Boehner Opposes additional aid to states for teachers and MedicaidTV Ad John Boehner quoted as saying that Obamacare gutted the 40-hour work weekTV Ad John Boehner Attempts to craft immigration reformTV Ad John Boehner Accuses President Obama of trying to poison the wellTV Ad As John Boehner Travels Democrats Pass a long-term reauthorization of the

Highway Trust FundTV Ad attacks President Obamas DogTV Ad Rick Perry Disparages Obamas Church attendanceTV Ad Accuses President Obama of practicing Deceit Not LeadershipTV Ad Accuse President Obama of Slow Reaction in Ebola CrisisTV Ad Rudy Guliani Says Obamas Support of Bilingulaism Reinforces Anti-AmericanismTV Ad John Boehner Vows GOP will Rise to the Challenge of Immigration OrderTV Ad slams GOP, Health Insurance CEOTV Ad DNC Slams Republicans as a party of hypocritesTV Ad Republicans Report a growing chorus of Rumors Bachman To RetireTV Ad Michele Bachman Only Wins Election by 2%TV Ad Michele Bachman No Longer Considered A Rising Star in Republican CirclesTV Ad Congressional Ethics Office Investigating Michele BachmanTV Ad DNC Healthcare Ad Targets wavering DEM senators

POLITICAL RUMBLE, A JAVA CLIENT/SERVER APPLICATION 99

TV Ad Elizabeth Warren is trying to fit round-peg national messages into the square-hole of Massachusetts politics

TV Ad Some Democrats Have No Idea Why Elizabeth Warren Was promoted To LeadershipTV Ad Scott Brown Questions Warrens claims of EhnicityTV Ad Elizabeth Warren Pledges She Wont Run for President in 2016TV Ad Elizabeth Warren Accused of Being a Socialist

POLITICAL RUMBLE, A JAVA CLIENT/SERVER APPLICATION 100

UML Diagrams

Figure 1. UML Use Case Diagram

POLITICAL RUMBLE, A JAVA CLIENT/SERVER APPLICATION 101

Figure 2. Sequence Diagram #1

POLITICAL RUMBLE, A JAVA CLIENT/SERVER APPLICATION 102

Figure 3. Sequence Diagram #2

POLITICAL RUMBLE, A JAVA CLIENT/SERVER APPLICATION 103

Figure 4. Sequence Diagram #3

POLITICAL RUMBLE, A JAVA CLIENT/SERVER APPLICATION 104

Figure 5. Sequence Diagram #4

POLITICAL RUMBLE, A JAVA CLIENT/SERVER APPLICATION 105

Figure 6. Sequence Diagram #5

POLITICAL RUMBLE, A JAVA CLIENT/SERVER APPLICATION 106

Figure 7. Sequence Diagram #6

POLITICAL RUMBLE, A JAVA CLIENT/SERVER APPLICATION 107

Figure 8. UML Collaboration Diagram

POLITICAL RUMBLE, A JAVA CLIENT/SERVER APPLICATION 108

Application Screenshots

Figure 9. Server console message alerts you that the server is running.

POLITICAL RUMBLE, A JAVA CLIENT/SERVER APPLICATION 109

Figure 10. Player 1 connects and is offered the menu to select their character for game play.

POLITICAL RUMBLE, A JAVA CLIENT/SERVER APPLICATION 110

Figure 11. After player 1 has selected a character the server acknowledges their selection

and tells the player to wait for an opponent to connect.

POLITICAL RUMBLE, A JAVA CLIENT/SERVER APPLICATION 111

Figure 12. If a player enters a selection that is not within the correct parameters they are

given a warning and instructed what the correct options are for selecting a character.

POLITICAL RUMBLE, A JAVA CLIENT/SERVER APPLICATION 112

Figure 13. When player 2 is connected and has selected a character the game play begins.

Player 2 is told to wait for player 1 to move and player 1 is alerted to make the first

game play.

POLITICAL RUMBLE, A JAVA CLIENT/SERVER APPLICATION 113

Figure 14. Player 1 is alerted that player 2 has connected and is told what character their

opponent has selected. In addition the player is given the Combat Menu to make the first

game play.

POLITICAL RUMBLE, A JAVA CLIENT/SERVER APPLICATION 114

Figure 15. Player 1 has selected attack option 1 to send a taunt. They are presented with a

Taunt Menu that is tailored to the specific character their opponent has chosen.

POLITICAL RUMBLE, A JAVA CLIENT/SERVER APPLICATION 115

Figure 16. Player 1; Michele Bachman, has sent a taunt to player 2, Barack Obama. Player 2

is alerted that player 1 has sent a taunt. Player 2 is now notified it is their turn and

they are given the Combat Menu.

POLITICAL RUMBLE, A JAVA CLIENT/SERVER APPLICATION 116

Figure 17. Player 2 has entered attack method #2, Start a Smear Campaign and they are

presented with a list of potential smear campaigns that are tailored specifically to the

character that player 1 has selected.

POLITICAL RUMBLE, A JAVA CLIENT/SERVER APPLICATION 117

Figure 18. Player 2 is notified that their smear campaign was started and the server sends

the calculations to let player know the results of their smear campaign.

POLITICAL RUMBLE, A JAVA CLIENT/SERVER APPLICATION 118

Figure 19. Game control is returned to player 1 and they are advised that the smear campaign

was started against them and told of the effect it had on the game scores. The opponents

score is also reported to player 1.

POLITICAL RUMBLE, A JAVA CLIENT/SERVER APPLICATION 119

Figure 20. Player 1 has entered the Combat Menu option to start a television ad campaign

against their opponent. They are given a list of potential television ads that is

tailored specifically to the character their opponent has chosen for game play.

POLITICAL RUMBLE, A JAVA CLIENT/SERVER APPLICATION 120

Figure 21. After player 1 selects the televsions ad campaign, the results of the ad campaign

are reported to player 1 and the game play control is transferred to player 2. Player 1

is told to wait for player 2's response.

POLITICAL RUMBLE, A JAVA CLIENT/SERVER APPLICATION 121

Figure 22. Player 2 is notified that player 1 started a television ad against them and they

are advised of what this cost regarding energy points. Player 2 is presented with the

combat menu and given game control to respond.

POLITICAL RUMBLE, A JAVA CLIENT/SERVER APPLICATION 122

Figure 23. Player 2 has entered the Combat Menu option #4, to host a political fund raising

event. They are presented with the fund raising event option as well as a menu of what

each event costs to host and what the potential gain would be of hosting that particular

event.

POLITICAL RUMBLE, A JAVA CLIENT/SERVER APPLICATION 123

Figure 24. After player 2 launches the political fund raising event they are presented with

the results of their event, their own energy score is updated and their opponents present

energy score is reported. Game control returns to player 1 and player 2 is told to wait

while player 1 plays.

POLITICAL RUMBLE, A JAVA CLIENT/SERVER APPLICATION 124

Figure 25. Game control is now returned to player 1 and the results of the opponents fund

raising event are reported to player 1 along with their opponents present energy score.

POLITICAL RUMBLE, A JAVA CLIENT/SERVER APPLICATION 125

Figure 26. Player 1 has entered the Taunt Menu from the Combat Menu and is presented with a

list of taunts tailored to the character which their opponent has selected. Player 1

decides they do not like any of the options so they enter ‘back’ to go back to the Combat

Menu.

POLITICAL RUMBLE, A JAVA CLIENT/SERVER APPLICATION 126

Figure 27. Player 1 is returned to the Combat Menu.

POLITICAL RUMBLE, A JAVA CLIENT/SERVER APPLICATION 127

Figure 28. Player 2 has entered the Television Ad Menu to select a television ad to disparage

their player 1. The ad options that player 2 is presented with are tailored specifically

to the character that player 1 chose for game play.

POLITICAL RUMBLE, A JAVA CLIENT/SERVER APPLICATION 128

Figure 29. After player 2 starts the television ad campaign the results are reported to them

and game control is returned to player 1. Player 2 is told to wait while player 1 makes

their game play.

POLITICAL RUMBLE, A JAVA CLIENT/SERVER APPLICATION 129

Figure 30. Player 1 is presented with the Combat Menu and advised that player 2 launched a

disparagin television ad against them and advised of what the associated energy point

results were. They are also advised of their opponents present point level.

POLITICAL RUMBLE, A JAVA CLIENT/SERVER APPLICATION 130

Figure 31. When one or both of the players runs out of energy the game ends and they are

advised of the game results.