Attached File(s)
-
Lab7.zip (17.81K)
Number of downloads: 56




Posted 19 November 2012 - 04:38 AM
Lab7.zip (17.81K)
Posted 19 November 2012 - 04:42 AM
Posted 19 November 2012 - 05:17 AM
JackOfAllTrades, on 19 November 2012 - 04:42 AM, said:
**
* Code to play War Ship.
*
* @Quang Pham
* @10/8/12
*/
import java.util.Scanner;
import javax.swing.*;
import static java.lang.Math.*;
public class WarShip
{
private Player p1;
private Player p2;
//p2 = computer
private static String id = "ws";
String firstName;
String lastName;
String shipName;
private static int idNum = 100;
private Ship s1;
int numberOfShots = 0;
int guessCol = 0;
int guessRow = 0;
int win = 0;
int loss = 0;
int games = 0;
Scanner keyboard = new Scanner(System.in);
static final int gameSize = 5;
char [][] game = new char[gameSize][gameSize];
char [][] enemy = new char[gameSize][gameSize];
//Warship constructor- call the constuctor for the ship
//To randomize the enemy ship placement, I suggest 5 different locations for the ship. Call
//the random number generator to choose which of the 5 locations to use. You may be creative and find
//different techniques, but the ship is not placed in the same place every game.
public WarShip()
{
String msg = "Welcome to War Ship! \n\n";
msg = msg + " You will be playing against the computer, her name is Terrie!";
JOptionPane.showMessageDialog(null, msg);
p1 = new Player();
p2 = new Player("Computer");
s1 = new Ship();//let the user enter the name of the ship in the default constructor
initBoards();
}
public void playGame()
{
String s0 = null;
int count = 0;
//int numberOfShots = 0;
s0 = JOptionPane.showInputDialog("Enter the number of shots you want to make (usually under 15): ");
numberOfShots = Integer.parseInt(s0);
while (count < numberOfShots)
{
initBoards();
generateRandomShipLocation();
displayMenu();
hitOrMiss();
//displayResults();
count++;
}
}
private void initBoards()
{
int [][]GameBoard = new int [6][6];
int [][]EnemyBoard = new int [6][6];
final int ROWS = 6;
final int COLS = 6;
//initialize both boards
for (int row = 0; row < ROWS; row++)
{ for (int col = 0; col < COLS; col++)
{
GameBoard[col][row] = 0;
}
}
for (int row = 0; row < ROWS; row++)
{ for (int col = 0; col < COLS; col++)
{
EnemyBoard[col][row] = 0;
}
}
}
public String generateRandomShipLocation()
{
//5 ship locations
String p2Choice = " ";
String A2 = " ", B3= " ", C4= " ", A1= " ", B2 = " ";
int shipLocation = 0;
int shipValues;
initBoards();
int[][]EnemyBoard = new int[5][5];
shipLocation = (int)(Math.random() * 10)%5;
switch (shipLocation)
{
case 0:
p2Choice = A2;
//shipValues = A2, A3, B2, B3
shipValues = EnemyBoard[1][2];
shipValues = EnemyBoard[1][3];
shipValues = EnemyBoard[2][2];
shipValues = EnemyBoard[2][3];
break;
case 1:
p2Choice = B3;
//shipValues = B3, B4, C3, C4;
shipValues = EnemyBoard[2][3];
shipValues = EnemyBoard[2][4];
shipValues = EnemyBoard[3][3];
shipValues = EnemyBoard[3][4];
break;
case 2:
p2Choice = C4;
//shipValues = C3, D3, C4, D4;
shipValues = EnemyBoard[3][4];
shipValues = EnemyBoard[4][4];
shipValues = EnemyBoard[3][4];
shipValues = EnemyBoard[4][4];
break;
case 3:
p2Choice = A1;
//shipValues = A1, A2, B1, B2;
shipValues = EnemyBoard[1][1];
shipValues = EnemyBoard[1][2];
shipValues = EnemyBoard[2][1];
shipValues = EnemyBoard[2][2];
break;
case 4:
p2Choice = B2;
//shipValues = B2, B3, C2, C3;
shipValues = EnemyBoard[2][2];
shipValues = EnemyBoard[2][3];
shipValues = EnemyBoard[3][2];
shipValues = EnemyBoard[3][3];
break;
}
return p2Choice;
//return shipValues[][];
}
public int displayMenu()
//method to display the main menu
{
GameBoard();
//EnemyBoard();
//displayGameBoard(game);
//displayEnemyBoard(game);
//displayWarShipArray();
int choice = 0;
int menu = 0;
//int guessCol = 0;
//int guessRow = 0;
int COLS;
int ROWS;
String p2Choice = " ";
System.out.println("Menu:");
System.out.println("1. Fire Shot");
System.out.println("2. Show Solution");
System.out.println("3. Quit");
System.out.println("Enter menu choice.");
choice = keyboard.nextInt();
switch(choice)
{
case 1: System.out.println("Enter a Column Number: ");
guessCol = keyboard.nextInt();
//guessCol = columnCheck();
System.out.println("Enter a Row Number: ");
guessRow = keyboard.nextInt();
//guessRow = rowCheck();
hitOrMiss();
break;
case 2: System.out.println("Solution Shown: ");
System.out.println("Here's where the war ship was.");
locationOfEnemyShip();
break;
case 3: System.out.println("Program Ended.");
displayResults();
break;
default: System.out.println("Invalid Input.");
}
return menu;
}
public static void GameBoard()
{
System.out.println(" WAR SHIP GAME (Game Board) ");
final int ROWS = 6;
final int COLS = 6;
char[][] values = { { ' ', 'A', 'B', 'C', 'D', 'E' },
{ '1', '_', '_', '_', '_', '_' },
{ '2', '_', '_', '_', '_', '_' },
{ '3', '_', '_', '_', '_', '_' },
{ '4', '_', '_', '_', '_', '_' },
{ '5', '_', '_', '_', '_', '_' }};
System.out.println(" ");
for (int row = 0; row < ROWS; row++)
{ for (int col = 0; col < COLS; col++)
{ System.out.print(" " + values[row][col]);
}
System.out.println("\n");
}
System.out.println(" ");
}
public static void EnemyBoard()
{
System.out.println(" WAR SHIP GAME (Enemy Board) ");
final int ROWS = 6;
final int COLS = 6;
char[][] values = { { ' ', 'A', 'B', 'C', 'D', 'E' },
{ '1', '_', '_', '_', '_', '_' },
{ '2', '_', '_', '_', '_', '_' },
{ '3', '_', '_', '_', '_', '_' },
{ '4', '_', '_', '_', '_', '_' },
{ '5', '_', '_', '_', '_', '_' }};
System.out.println(" ");
for (int row = 0; row < ROWS; row++)
{ for (int col = 0; col < COLS; col++)
{ System.out.print(" " + values[row][col]);
}
System.out.println("\n");
}
System.out.println(" ");
}
public void displayGameBoard(char[][] board)
{
System.out.println(" WAR SHIP GAME (Game Board)");
int i = 0, j = 0;
for (i = 0; i < board.length; i++)
{ for (j = 0; j < board.length; j++)
System.out.print(board[i][j] + "__");
System.out.println();
}
System.out.println();
}
public void displayEnemyBoard(char[][] board)
{
System.out.println(" WAR SHIP GAME (Enemy Board)");
int i = 0, j = 0;
for (i = 0; i < board.length; i++)
{ for (j = 0; j < board.length; j++)
System.out.print(board[i][j] + "__");
System.out.println();
}
System.out.println();
}
public void displayWarShipArray()
{
int number = 1;
System.out.println(" ");
System.out.println(" A B C D E ");
for (int i = 0; i < 5; i++)
{
System.out.println(number + " __" + " " + "__" + " " + "__" + " " + "__" + " " + "__" );
number++;
}
System.out.println(" ");
}
public void locationOfEnemyShip()
{
int COLS = 5;
int ROWS = 5;
int col = 0;
int row = 0;
generateRandomShipLocation();
int [][]EnemyBoard = new int[6][6];
int [][]shipValues = new int[6][6];
EnemyBoard[col][row] = shipValues[col][row];
EnemyBoard[col][row] = '+';
for(col = 0; col < COLS; col++)
{
for(row = 0; row < ROWS; row++)
{
System.out.print(" " + EnemyBoard[col][row]);
}
System.out.println("\n");
}
EnemyBoard();
displayMenu();
}
public void hitOrMiss()
{
//int guessCol = 0;
//int guessRow = 0;
int [][]GameBoard = new int[6][6];
int shipValues = 0;
int COLS = 5;
int ROWS = 5;
//print an x for shots
//print an * for hits
if(GameBoard[guessCol][guessRow] == shipValues)
{
GameBoard[guessCol][guessRow] = '*';
System.out.println("Hit!");
win++;
for(int col = 0; col < COLS; col++)
{
for(int row = 0; row < ROWS; row++)
{
System.out.print("__" + GameBoard[col][row]);
}
System.out.println("\n");
}
}
else
{
GameBoard[guessCol][guessRow] = 'x';
System.out.println("Miss!");
loss++;
for(int col = 0; col < COLS; col++)
{
for(int row = 0; row < ROWS; row++)
{
System.out.print("__" + GameBoard[col][row]);
}
System.out.println("\n");
}
}
displayMenu();
games++;
}
//public static int columnCheck();
{
}
//public static int rowCheck();
{
}
public void displayResults()
{
boolean WarShipSunk;
System.out.println( numberOfShots + " shots were fired.");
System.out.println( win + " shots hit.");
System.out.println( loss + " shots missed.");
System.out.println( games + " games played.");
if (WarShipSunk= true)
{
System.out.println("For " + numberOfShots + " shots " + p1 + " was a winner!!!");
System.out.println(p1);
System.out.println(p2);
System.out.println(s1);
System.out.println( firstName );
System.out.println( lastName );
System.out.println( shipName );
}
else if(WarShipSunk = false)
{
System.out.println("For " + numberOfShots + " shots " + p1 + " was a loser.");
}
}
}
Lab7.zip (17.81K)
Posted 20 November 2012 - 03:44 PM
Quang Pham, on 19 November 2012 - 04:38 AM, said:
/**
* Code to play War Ship.
*
* @Quang Pham
* @10/8/12
*/
import java.util.Scanner;
import javax.swing.*;
import static java.lang.Math.*;
public class WarShip
{
private Player p1;
private Player p2;
//p2 = computer
private static String id = "ws";
private static int idNum = 100;
private Ship s1;
int numberOfShots = 0;
int Win = 0;
int Loss = 0;
int guessCol = 0;
int guessRow = 0;
Scanner keyboard = new Scanner(System.in);
static final int gameSize = 5;
char [][] game = new char[gameSize][gameSize];
char [][] enemy = new char[gameSize][gameSize];
//Warship constructor- call the constuctor for the ship
//To randomize the enemy ship placement, I suggest 5 different locations for the ship. Call
//the random number generator to choose which of the 5 locations to use. You may be creative and find
//different techniques, but the ship is not placed in the same place every game.
public WarShip()
{
String msg = "Welcome to War Ship! \n\n";
msg = msg + " You will be playing against the computer, her name is Terrie!";
JOptionPane.showMessageDialog(null, msg);
p1 = new Player(newId());
p2 = new Player(newId(),"Terrie","Computer");
s1 = new Ship();//let the user enter the name of the ship in the default constructor
initBoards();
}
public void playGame()
{
String s0 = " ";
int count = 0;
//int numberOfShots = 0;
s0 = JOptionPane.showInputDialog("Enter the number of shots you want to make (usually under 15): ");
numberOfShots = Integer.parseInt(s0);
while (count < numberOfShots)
{
initBoards();
generateRandomShipLocation();
displayMenu();
hitOrMiss();
//displayResults();
count++;
}
}
private void initBoards()
{
char [][]GameBoard = new char [6][6];
char [][]EnemyBoard = new char [6][6];
int COLS = 6;
int ROWS = 6;
//initialize both boards
for (int row = 0; row < ROWS; row++)
{ for (int col = 0; col < COLS; col++)
{
GameBoard[col][row] = 0;
}
}
for (int row = 0; row < ROWS; row++)
{ for (int col = 0; col < COLS; col++)
{
EnemyBoard[col][row] = 0;
}
}
}
public void generateRandomShipLocation()
{
//5 ship locations
String p2Choice = " ";
String A2 = " ", B3= " ", C4= " ", A1= " ", B2 = " ";
int shipLocation = 0;
int values =2;
char shipValues = 0;
initBoards();
char[][]EnemyBoard = new char[6][6];
//shipValues = new int[(int)Math.random() + values.length][(int)Math.random() * values[0].length];
shipLocation = (int)(Math.random() * 10)%5;
switch (shipLocation)
{
case 0:
p2Choice = A2;
//shipValues = A2, A3, B2, B3
shipValues = EnemyBoard[1][2] = '+';
shipValues = EnemyBoard[1][3] = '+';
shipValues = EnemyBoard[2][2] = '+';
shipValues = EnemyBoard[2][3] = '+';
break;
case 1:
p2Choice = B3;
//shipValues = B3, B4, C3, C4;
shipValues = EnemyBoard[2][3] = '+';
shipValues = EnemyBoard[2][4] = '+';
shipValues = EnemyBoard[3][3] = '+';
shipValues = EnemyBoard[3][4] = '+';
break;
case 2:
p2Choice = C4;
//shipValues = C3, D3, C4, D4;
shipValues = EnemyBoard[3][4] = '+';
shipValues = EnemyBoard[4][4] = '+';
shipValues = EnemyBoard[3][4] = '+';
shipValues = EnemyBoard[4][4] = '+';
break;
case 3:
p2Choice = A1;
//shipValues = A1, A2, B1, B2;
shipValues = EnemyBoard[1][1] = '+';
shipValues = EnemyBoard[1][2] = '+';
shipValues = EnemyBoard[2][1] = '+';
shipValues = EnemyBoard[2][2] = '+';
break;
case 4:
p2Choice = B2;
//shipValues = B2, B3, C2, C3;
shipValues = EnemyBoard[2][2] = '+';
shipValues = EnemyBoard[2][3] = '+';
shipValues = EnemyBoard[3][2] = '+';
shipValues = EnemyBoard[3][3] = '+';
break;
}
/*
return p2Choice;
return shipLocation;
return shipValues;
return values;
*/
}
public int displayMenu()
//method to display the main menu
{
//GameBoard();
//EnemyBoard();
//displayGameBoard(game);
//displayEnemyBoard(game);
//displayWarShipArray();
int choice = 0;
int menu = 0;
//int guessCol = 0;
//int guessRow = 0;
int COLS;
int ROWS;
String p2Choice = " ";
System.out.println("Menu:");
System.out.println("1. Fire Shot");
System.out.println("2. Show Solution");
System.out.println("3. Quit");
System.out.println("Enter menu choice.");
choice = keyboard.nextInt();
switch(choice)
{
case 1: System.out.println("Enter a Column Number: ");
guessCol = keyboard.nextInt();
//guessCol = columnCheck();
System.out.println("Enter a Row Number: ");
guessRow = keyboard.nextInt();
//guessRow = rowCheck();
hitOrMiss();
break;
case 2: System.out.println("Solution Shown: ");
System.out.println("Here's where the enemy ship was.");
displayLocationOfEnemyShip();
break;
case 3: System.out.println("Program Ended.");
break;
default: System.out.println("Invalid Input.");
}
displayResults();
return menu;
}
public static void GameBoard()
{
System.out.println(" WAR SHIP GAME (Game Board) ");
final int COLS = 6;
final int ROWS = 6;
char[][] values = { { ' ', 'A', 'B', 'C', 'D', 'E' },
{ '1', '_', '_', '_', '_', '_' },
{ '2', '_', '_', '_', '_', '_' },
{ '3', '_', '_', '_', '_', '_' },
{ '4', '_', '_', '_', '_', '_' },
{ '5', '_', '_', '_', '_', '_' }};
System.out.println(" ");
for (int col = 0; col < COLS; col++)
{ for (int row = 0; row < ROWS; row++)
{ System.out.print(" " + values[col][row]);
}
System.out.println("\n");
}
System.out.println(" ");
}
public static void EnemyBoard()
{
System.out.println(" WAR SHIP GAME (Enemy Board) ");
final int COLS = 6;
final int ROWS = 6;
char[][] values = { { ' ', 'A', 'B', 'C', 'D', 'E' },
{ '1', '_', '_', '_', '_', '_' },
{ '2', '_', '_', '_', '_', '_' },
{ '3', '_', '_', '_', '_', '_' },
{ '4', '_', '_', '_', '_', '_' },
{ '5', '_', '_', '_', '_', '_' }};
System.out.println(" ");
for (int col = 0; col < COLS; col++)
{ for (int row = 0; row < ROWS; row++)
{ System.out.print(" " + values[col][row]);
}
System.out.println("\n");
}
System.out.println(" ");
}
public void displayGameBoard(char[][] board)
{
System.out.println(" WAR SHIP GAME (Game Board)");
int i = 0, j = 0;
for (i = 0; i < board.length; i++)
{ for (j = 0; j < board.length; j++)
System.out.print(board[i][j] + "__");
System.out.println();
}
System.out.println();
}
public void displayEnemyBoard(char[][] board)
{
System.out.println(" WAR SHIP GAME (Enemy Board)");
int i = 0, j = 0;
for (i = 0; i < board.length; i++)
{ for (j = 0; j < board.length; j++)
System.out.print(board[i][j] + "__");
System.out.println();
}
System.out.println();
}
public void displayWarShipArray()
{
int number = 1;
System.out.println(" ");
System.out.println(" A B C D E ");
for (int i = 0; i < 5; i++)
{
System.out.println(number + " __" + " " + "__" + " " + "__" + " " + "__" + " " + "__" );
number++;
}
System.out.println(" ");
}
public void hitOrMiss()
{
//int guessCol = 0;
//int guessRow = 0;
final int shipCOL = 2;
final int shipROW = 2;
char [][]GameBoard = new char[6][6];
//int [][]shipValues = new int[(int)Math.random() + values.length][(int)Math.random() * values[0].length];
int shipValues = 0;
final int COLS = 6;
final int ROWS = 6;
//print an x for shots
//print an * for hits
System.out.println(" WAR SHIP GAME (Game Board) ");
System.out.println(" ");
if(GameBoard[guessCol][guessRow] == shipValues)
{
System.out.println("Hit!");
GameBoard[guessCol][guessRow] = '*';
GameBoard[1][0] = 'A';
GameBoard[2][0] = 'B';
GameBoard[3][0] = 'C';
GameBoard[4][0] = 'D';
GameBoard[5][0] = 'E';
GameBoard[0][1] = '1';
GameBoard[0][2] = '2';
GameBoard[0][3] = '3';
GameBoard[0][4] = '4';
GameBoard[0][5] = '5';
for(char col = 0; col < COLS; col++)
{
for(char row = 0; row < ROWS; row++)
{
System.out.print("__" + GameBoard[col][row]);
}
System.out.println("\n");
}
p1.addWin();
}
else
{
System.out.println("Miss!");
System.out.println(" ");
GameBoard[guessCol][guessRow] = 'x';
GameBoard[1][0] = 'A';
GameBoard[2][0] = 'B';
GameBoard[3][0] = 'C';
GameBoard[4][0] = 'D';
GameBoard[5][0] = 'E';
GameBoard[0][1] = '1';
GameBoard[0][2] = '2';
GameBoard[0][3] = '3';
GameBoard[0][4] = '4';
GameBoard[0][5] = '5';
for(char col = 0; col < COLS; col++)
{
for(char row = 0; row < ROWS; row++)
{
System.out.print(" _" + GameBoard[col][row]);
}
System.out.println("\n");
}
p1.addLoss();
}
System.out.println(" guessCol is " + guessCol + " guessRow is " + guessRow);
displayMenu();
}
//public static int columnCheck();
{
}
//public static int rowCheck();
{
}
public void displayLocationOfEnemyShip()
{
int shipLocation;
int shipValues;
final int COLS = 6;
final int ROWS = 6;
char[][]EnemyBoard = new char [6][6];
char[][]EnemyShip = new char [2][2];
generateRandomShipLocation();
//shipValues = '+';
System.out.println(" ");
System.out.println(" WAR SHIP GAME (Enemy Board) ");
//EnemyBoard[int col][int row] = '+';
EnemyBoard[1][0] = 'A';
EnemyBoard[2][0] = 'B';
EnemyBoard[3][0] = 'C';
EnemyBoard[4][0] = 'D';
EnemyBoard[5][0] = 'E';
EnemyBoard[0][1] = '1';
EnemyBoard[0][2] = '2';
EnemyBoard[0][3] = '3';
EnemyBoard[0][4] = '4';
EnemyBoard[0][5] = '5';
for(char col = 0; col < COLS; col++)
{
for(char row = 0; row < ROWS; row++)
{
System.out.print(" _" + EnemyBoard[col][row]);
}
System.out.println("\n");
}
System.out.println(" ");
}
public void displayResults()
{
int x = 0;
int y = 0;
boolean warShipSunk;
System.out.println( numberOfShots + " shots were fired.");
System.out.println( Win + " shots hit.");
System.out.println( Loss + " shots missed.");
warShipSunk = s1.getSunk();
if (warShipSunk= true)
{
System.out.println("For " + numberOfShots + " shots " + p1 + " was a winner!!!");
}
else if(warShipSunk = false)
{
System.out.println("For " + numberOfShots + " shots " + p1 + " was a loser.");
}
System.out.println(" ");
}
private static String newId()
{
idNum++;
return id + String.valueOf(idNum);
}
}
Lab7.2.zip (13.16K)
Posted 21 November 2012 - 03:35 PM
Quang Pham, on 19 November 2012 - 04:38 AM, said:
Lab7.3.zip (13.64K)
Posted 21 November 2012 - 08:19 PM
Quote
Posted 22 November 2012 - 12:35 AM
/**
* Code to play War Ship.
*
* @Quang Pham
* @10/8/12
*/
import java.util.Scanner;
import javax.swing.*;
import static java.lang.Math.*;
public class WarShip
{
private Player p1;
private Player p2;
//p2 = computer
private static String id = "ws";
private static int idNum = 100;
private Ship s1;
int numberOfShots = 0;
int Win = 0;
int Loss = 0;
int guessCol = 0;
int guessRow = 0;
int shipLocation = 0;
char shipValues = 0;
int p2Choice = 0;
Scanner keyboard = new Scanner(System.in);
static final int gameSize = 5;
char [][] game = new char[gameSize][gameSize];
char [][] enemy = new char[gameSize][gameSize];
//Warship constructor- call the constuctor for the ship
//To randomize the enemy ship placement, I suggest 5 different locations for the ship. Call
//the random number generator to choose which of the 5 locations to use. You may be creative and find
//different techniques, but the ship is not placed in the same place every game.
public WarShip()
{
String msg = "Welcome to War Ship! \n\n";
msg = msg + " You will be playing against the computer, her name is Terrie!";
JOptionPane.showMessageDialog(null, msg);
p1 = new Player(newId());
p2 = new Player(newId(),"Terrie","Computer");
s1 = new Ship();//let the user enter the name of the ship in the default constructor
initBoards();
}
public void playGame()
{
String s0 = " ";
int count = 0;
//int numberOfShots = 0;
s0 = JOptionPane.showInputDialog("Enter the number of shots you want to make (usually under 15): ");
numberOfShots = Integer.parseInt(s0);
while (count < numberOfShots)
{
initBoards();
generateRandomShipLocation();
displayMenu();
hitOrMiss();
//displayResults();
count++;
}
}
private void initBoards()
{
char [][]GameBoard = new char [6][6];
char [][]EnemyBoard = new char [6][6];
int COLS = 6;
int ROWS = 6;
//initialize both boards
for (int row = 0; row < ROWS; row++)
{ for (int col = 0; col < COLS; col++)
{
GameBoard[col][row] = 0;
}
}
for (int row = 0; row < ROWS; row++)
{ for (int col = 0; col < COLS; col++)
{
EnemyBoard[col][row] = 0;
}
}
}
public String generateRandomShipLocation()
{
//5 ship locations
String p2Choice = " ";
String A2 = " ", B3= " ", C4= " ", A1= " ", B2 = " ";
int shipLocation = 0;
char shipValues = 0;
initBoards();
char[][]EnemyBoard = new char[6][6];
//shipValues = new int[(int)Math.random() + values.length][(int)Math.random() * values[0].length];
shipLocation = (int)(Math.random() * 10)%5;
switch (shipLocation)
{
case 0:
p2Choice = A2;
//shipValues = A2, A3, B2, B3
shipValues = EnemyBoard[1][2] = '+';
shipValues = EnemyBoard[1][3] = '+';
shipValues = EnemyBoard[2][2] = '+';
shipValues = EnemyBoard[2][3] = '+';
break;
case 1:
p2Choice = B3;
//shipValues = B3, B4, C3, C4;
shipValues = EnemyBoard[2][3] = '+';
shipValues = EnemyBoard[2][4] = '+';
shipValues = EnemyBoard[3][3] = '+';
shipValues = EnemyBoard[3][4] = '+';
break;
case 2:
p2Choice = C4;
//shipValues = C3, D3, C4, D4;
shipValues = EnemyBoard[3][4] = '+';
shipValues = EnemyBoard[4][4] = '+';
shipValues = EnemyBoard[3][4] = '+';
shipValues = EnemyBoard[4][4] = '+';
break;
case 3:
p2Choice = A1;
//shipValues = A1, A2, B1, B2;
shipValues = EnemyBoard[1][1] = '+';
shipValues = EnemyBoard[1][2] = '+';
shipValues = EnemyBoard[2][1] = '+';
shipValues = EnemyBoard[2][2] = '+';
break;
case 4:
p2Choice = B2;
//shipValues = B2, B3, C2, C3;
shipValues = EnemyBoard[2][2] = '+';
shipValues = EnemyBoard[2][3] = '+';
shipValues = EnemyBoard[3][2] = '+';
shipValues = EnemyBoard[3][3] = '+';
break;
}
return p2Choice;
//return shipLocation;
//return shipValues;
}
public int displayMenu()
//method to display the main menu
{
//GameBoard();
//EnemyBoard();
//displayGameBoard(game);
//displayEnemyBoard(game);
//displayWarShipArray();
int choice = 0;
int menu = 0;
//int guessCol = 0;
//int guessRow = 0;
int COLS;
int ROWS;
String p2Choice = " ";
System.out.println("Menu:");
System.out.println("1. Fire Shot");
System.out.println("2. Show Solution");
System.out.println("3. Quit");
System.out.println("Enter menu choice.");
choice = keyboard.nextInt();
switch(choice)
{
case 1: System.out.println("Enter a Column Number: ");
guessCol = keyboard.nextInt();
//guessCol = columnCheck();
System.out.println("Enter a Row Number: ");
guessRow = keyboard.nextInt();
//guessRow = rowCheck();
hitOrMiss();
break;
case 2: System.out.println("Solution Shown: ");
System.out.println("Here's where the enemy ship was.");
displayLocationOfEnemyShip();
break;
case 3: System.out.println("Program Ended.");
break;
default: System.out.println("Invalid Input.");
}
displayResults();
return menu;
}
public static void GameBoard()
{
System.out.println(" WAR SHIP GAME (Game Board) ");
final int COLS = 6;
final int ROWS = 6;
char[][] values = { { ' ', 'A', 'B', 'C', 'D', 'E' },
{ '1', '_', '_', '_', '_', '_' },
{ '2', '_', '_', '_', '_', '_' },
{ '3', '_', '_', '_', '_', '_' },
{ '4', '_', '_', '_', '_', '_' },
{ '5', '_', '_', '_', '_', '_' }};
System.out.println(" ");
for (int col = 0; col < COLS; col++)
{ for (int row = 0; row < ROWS; row++)
{ System.out.print(" " + values[col][row]);
}
System.out.println("\n");
}
System.out.println(" ");
}
public static void EnemyBoard()
{
System.out.println(" WAR SHIP GAME (Enemy Board) ");
final int COLS = 6;
final int ROWS = 6;
char[][] values = { { ' ', 'A', 'B', 'C', 'D', 'E' },
{ '1', '_', '_', '_', '_', '_' },
{ '2', '_', '_', '_', '_', '_' },
{ '3', '_', '_', '_', '_', '_' },
{ '4', '_', '_', '_', '_', '_' },
{ '5', '_', '_', '_', '_', '_' }};
System.out.println(" ");
for (int col = 0; col < COLS; col++)
{ for (int row = 0; row < ROWS; row++)
{ System.out.print(" " + values[col][row]);
}
System.out.println("\n");
}
System.out.println(" ");
}
public void displayGameBoard(char[][] board)
{
System.out.println(" WAR SHIP GAME (Game Board)");
int i = 0, j = 0;
for (i = 0; i < board.length; i++)
{ for (j = 0; j < board.length; j++)
System.out.print(board[i][j] + "__");
System.out.println();
}
System.out.println();
}
public void displayEnemyBoard(char[][] board)
{
System.out.println(" WAR SHIP GAME (Enemy Board)");
int i = 0, j = 0;
for (i = 0; i < board.length; i++)
{ for (j = 0; j < board.length; j++)
System.out.print(board[i][j] + "__");
System.out.println();
}
System.out.println();
}
public void displayWarShipArray()
{
int number = 1;
System.out.println(" ");
System.out.println(" A B C D E ");
for (int i = 0; i < 5; i++)
{
System.out.println(number + " __" + " " + "__" + " " + "__" + " " + "__" + " " + "__" );
number++;
}
System.out.println(" ");
}
public void hitOrMiss()
{
//int guessCol = 0;
//int guessRow = 0;
final int shipCOL = 2;
final int shipROW = 2;
char [][]GameBoard = new char[6][6];
//int [][]shipValues = new int[(int)Math.random() + values.length][(int)Math.random() * values[0].length];
int shipValues = 0;
final int COLS = 6;
final int ROWS = 6;
//print an x for shots
//print an * for hits
System.out.println(" ");
if(GameBoard[guessCol][guessRow] == shipValues)
{
System.out.println("Hit!");
System.out.println(" ");
System.out.println(" WAR SHIP GAME (Game Board) ");
GameBoard[guessCol][guessRow] = '*';
GameBoard[1][0] = '1';
GameBoard[2][0] = '2';
GameBoard[3][0] = '3';
GameBoard[4][0] = '4';
GameBoard[5][0] = '5';
GameBoard[0][1] = 'A';
GameBoard[0][2] = 'B';
GameBoard[0][3] = 'C';
GameBoard[0][4] = 'D';
GameBoard[0][5] = 'E';
for(char col = 0; col < COLS; col++)
{
for(char row = 0; row < ROWS; row++)
{
System.out.print("__" + GameBoard[col][row]);
}
System.out.println("\n");
}
p1.addWin();
}
else
{
System.out.println("Miss!");
System.out.println(" ");
System.out.println(" WAR SHIP GAME (Game Board) ");
GameBoard[guessCol][guessRow] = 'x';
GameBoard[1][0] = '1';
GameBoard[2][0] = '2';
GameBoard[3][0] = '3';
GameBoard[4][0] = '4';
GameBoard[5][0] = '5';
GameBoard[0][1] = 'A';
GameBoard[0][2] = 'B';
GameBoard[0][3] = 'C';
GameBoard[0][4] = 'D';
GameBoard[0][5] = 'E';
for(char col = 0; col < COLS; col++)
{
for(char row = 0; row < ROWS; row++)
{
System.out.print(" _" + GameBoard[col][row]);
}
System.out.println("\n");
}
p1.addLoss();
}
System.out.println(" guessCol is " + guessCol + " guessRow is " + guessRow);
displayMenu();
}
//public static int columnCheck();
{
}
//public static int rowCheck();
{
}
public void displayLocationOfEnemyShip()
{
char col = 0;
char row = 0;
final int COLS = 6;
final int ROWS = 6;
char[][]EnemyBoard = new char [6][6];
char[][]EnemyShip = new char [2][2];
generateRandomShipLocation();
System.out.println(" ");
System.out.println(" WAR SHIP GAME (Enemy Board) ");
shipValues = '+';
EnemyBoard[col][row] = '+';
EnemyShip[col][row] = '+';
EnemyBoard[1][0] = '1';
EnemyBoard[2][0] = '2';
EnemyBoard[3][0] = '3';
EnemyBoard[4][0] = '4';
EnemyBoard[5][0] = '5';
EnemyBoard[0][1] = 'A';
EnemyBoard[0][2] = 'B';
EnemyBoard[0][3] = 'C';
EnemyBoard[0][4] = 'D';
EnemyBoard[0][5] = 'E';
for(col = 0; col < COLS; col++)
{
for(row = 0; row < ROWS; row++)
{
System.out.print(" _" + EnemyBoard[col][row]);
}
System.out.println("\n");
}
System.out.println(" ");
System.out.println("Ship location: " + shipLocation);
System.out.println("Ship Values: " + shipValues);
System.out.println("p2Choice: " + p2Choice);
//System.out.println(EnemyBoard[col][row]);
//System.out.println(EnemyShip[col][row]);
System.out.println(" ");
}
public void displayResults()
{
int x = 0;
int y = 0;
boolean warShipSunk;
System.out.println( numberOfShots + " shots were fired.");
System.out.println( Win + " shots hit.");
System.out.println( Loss + " shots missed.");
warShipSunk = s1.getSunk();
if (warShipSunk= true)
{
System.out.println("For " + numberOfShots + " shots " + p1 + " was a winner!!!");
}
else if(warShipSunk = false)
{
System.out.println("For " + numberOfShots + " shots " + p1 + " was a loser.");
}
System.out.println(" ");
displayMenu();
}
private static String newId()
{
idNum++;
return id + String.valueOf(idNum);
}
}
Posted 22 November 2012 - 12:40 AM
Lab7.4.zip (13.35K)
Posted 22 November 2012 - 02:46 AM
Posted 22 November 2012 - 10:55 AM
Posted 23 November 2012 - 04:41 AM
Posted 27 November 2012 - 04:48 AM
GregBrannon, on 23 November 2012 - 04:41 AM, said:
Lab7.5.zip (12.59K)
Posted 27 November 2012 - 05:11 AM
pbl, on 27 November 2012 - 04:51 AM, said:
Quote
/**
* Code to play War Ship.
*
* @Quang Pham
* @10/8/12
*/
import java.util.Scanner;
import javax.swing.*;
import static java.lang.Math.*;
public class WarShip
{
private Player p1;
private Player p2;
//p2 = computer
private static String id = "ws";
private static int idNum = 100;
private Ship s1;
int numberOfShots = 0;
int Win = 0;
int Loss = 0;
int guessCol = 0;
int guessRow = 0;
int shipLocation = 0;
String p2Choice = " ";
char shipValues1 = 0;
char shipValues2 = 0;
char shipValues3 = 0;
char shipValues4 = 0;
public static char [][]GameBoard = new char[6][6];
public static char [][]EnemyBoard = new char[6][6];
Scanner keyboard = new Scanner(System.in);
//static final int gameSize = 5;
//char [][] game = new char[gameSize][gameSize];
//char [][] enemy = new char[gameSize][gameSize];
//Warship constructor- call the constuctor for the ship
//To randomize the enemy ship placement, I suggest 5 different locations for the ship. Call
//the random number generator to choose which of the 5 locations to use. You may be creative and find
//different techniques, but the ship is not placed in the same place every game.
public WarShip()
{
String msg = "Welcome to War Ship! \n\n";
msg = msg + " You will be playing against the computer, her name is Terrie!";
JOptionPane.showMessageDialog(null, msg);
p1 = new Player(newId());
p2 = new Player(newId(),"Terrie","Computer");
s1 = new Ship();//let the user enter the name of the ship in the default constructor
//initBoards();
}
public void playGame()
{
String s0 = " ";
int count = 0;
//int numberOfShots = 0;
s0 = JOptionPane.showInputDialog("Enter the number of shots you want to make (usually under 15): ");
numberOfShots = Integer.parseInt(s0);
while (count < numberOfShots)
{
initBoards();
displayMenu();
displayLocationOfEnemyShip();
hitOrMiss();
colCheck();
rowCheck();
count++;
}
}
private void initBoards()
{
char [][]GameBoard = new char [6][6];
char [][]EnemyBoard = new char [6][6];
int COLS = 6;
int ROWS = 6;
//initialize both boards
for (int row = 0; row < ROWS; row++)
{ for (int col = 0; col < COLS; col++)
{
GameBoard[col][row] = 0;
}
}
for (int row = 0; row < ROWS; row++)
{ for (int col = 0; col < COLS; col++)
{
EnemyBoard[col][row] = 0;
}
}
}
public int displayMenu()
//method to display the main menu
{
//GameBoard();
//EnemyBoard();
//displayGameBoard(game);
//displayEnemyBoard(game);
//displayWarShipArray();
int choice = 0;
int menu = 0;
//int guessCol = 0;
//int guessRow = 0;
int COLS;
int ROWS;
String p2Choice = " ";
System.out.println("Menu:");
System.out.println("1. Fire Shot");
System.out.println("2. Show Solution");
System.out.println("3. Quit");
System.out.println("Enter menu choice.");
choice = keyboard.nextInt();
switch(choice)
{
case 1: System.out.println("Enter a Column Number: ");
guessCol = keyboard.nextInt();
//guessCol = columnCheck();
System.out.println("Enter a Row Number: ");
guessRow = keyboard.nextInt();
//guessRow = rowCheck();
hitOrMiss();
break;
case 2: System.out.println("Solution Shown: ");
System.out.println("Here's where the enemy ship was.");
displayLocationOfEnemyShip();
break;
case 3: System.out.println("Program Ended.");
displayResults();
break;
default: System.out.println("Invalid Input.");
}
return menu;
}
public String displayLocationOfEnemyShip()
{
char col = 0;
char row = 0;
final int COLS = 6;
final int ROWS = 6;
char[][]EnemyBoard = new char [6][6];
char[][]EnemyShip = new char [2][2];
//generateRandomShipLocation
//5 ship locations
int shipLocations = 0;
String p2Choice = " ";
shipLocation = (int)(Math.random() * 10)%5;
switch (shipLocation)
{
case 0:
p2Choice = "A2";
//shipValues = A2, A3, B2, B3
EnemyBoard[1][2] = '+';
EnemyBoard[1][3] = '+';
EnemyBoard[2][2] = '+';
EnemyBoard[2][3] = '+';
break;
case 1:
p2Choice = "B3";
//shipValues = B3, B4, C3, C4;
EnemyBoard[2][3] = '+';
EnemyBoard[2][4] = '+';
EnemyBoard[3][3] = '+';
EnemyBoard[3][4] = '+';
break;
case 2:
p2Choice = "C4";
//shipValues = C3, D3, C4, D4;
EnemyBoard[3][4] = '+';
EnemyBoard[4][4] = '+';
EnemyBoard[3][5] = '+';
EnemyBoard[4][5] = '+';
break;
case 3:
p2Choice = "A1";
//shipValues = A1, A2, B1, B2;
EnemyBoard[1][1] = '+';
EnemyBoard[1][2] = '+';
EnemyBoard[2][1] = '+';
EnemyBoard[2][2] = '+';
break;
case 4:
p2Choice = "B2";
//shipValues = B2, B3, C2, C3;
EnemyBoard[2][2] = '+';
EnemyBoard[2][3] = '+';
EnemyBoard[3][2] = '+';
EnemyBoard[3][3] = '+';
break;
}
System.out.println("p2Choice is: " + p2Choice);
System.out.println(" ");
System.out.println(" WAR SHIP GAME (Enemy Board) ");
//EnemyShip[col][row] = '+';
EnemyBoard[1][0] = 'A';
EnemyBoard[2][0] = 'B';
EnemyBoard[3][0] = 'C';
EnemyBoard[4][0] = 'D';
EnemyBoard[5][0] = 'E';
EnemyBoard[0][1] = '1';
EnemyBoard[0][2] = '2';
EnemyBoard[0][3] = '3';
EnemyBoard[0][4] = '4';
EnemyBoard[0][5] = '5';
for(row = 0; row < ROWS; row++)
{
for(col = 0; col < COLS; col++)
{
System.out.print(" _" + EnemyBoard[col][row]);
}
System.out.println("\n");
}
System.out.println(" ");
System.out.println("Ship location: " + shipLocation);
System.out.println("p2Choice: " + p2Choice);
System.out.println("EnemyBoard[guessCol][guessRow] is: " + EnemyBoard[guessCol][guessRow]);
System.out.println("ShipValues are: " +
//System.out.println(EnemyShip[col][row]);
return p2Choice;
//return shipLocation;
return shipValues1;
return shipValues2;
return shipValues3;
return shipValues4;
}
public void hitOrMiss()
{
final int shipCOL = 2;
final int shipROW = 2;
char [][]GameBoard = new char[6][6];
char [][]EnemyBoard = new char[6][6];
final int COLS = 6;
final int ROWS = 6;
//print an x for shots
//print an * for hits
System.out.println(" ");
if(EnemyBoard[guessCol][guessRow] == '+')
{
System.out.println("Hit!");
System.out.println(" ");
System.out.println(" WAR SHIP GAME (Game Board) ");
GameBoard[guessCol][guessRow] = '*';
GameBoard[1][0] = 'A';
GameBoard[2][0] = 'B';
GameBoard[3][0] = 'C';
GameBoard[4][0] = 'D';
GameBoard[5][0] = 'E';
GameBoard[0][1] = '1';
GameBoard[0][2] = '2';
GameBoard[0][3] = '3';
GameBoard[0][4] = '4';
GameBoard[0][5] = '5';
for(char row = 0; row < ROWS; row++)
{
for(char col = 0; col < COLS; col++)
{
System.out.print("__" + GameBoard[col][row]);
}
System.out.println("\n");
}
p1.addWin();
}
else
{
System.out.println("Miss!");
System.out.println(" ");
System.out.println(" WAR SHIP GAME (Game Board) ");
GameBoard[guessCol][guessRow] = 'x';
GameBoard[1][0] = 'A';
GameBoard[2][0] = 'B';
GameBoard[3][0] = 'C';
GameBoard[4][0] = 'D';
GameBoard[5][0] = 'E';
GameBoard[0][1] = '1';
GameBoard[0][2] = '2';
GameBoard[0][3] = '3';
GameBoard[0][4] = '4';
GameBoard[0][5] = '5';
for(char row = 0; row < ROWS; row++)
{
for(char col = 0; col < COLS; col++)
{
System.out.print(" _" + GameBoard[col][row]);
}
System.out.println("\n");
}
p1.addLoss();
}
System.out.println(" guessCol is " + guessCol + " guessRow is " + guessRow);
System.out.println(" GameBoard[guessCol][guessRow] is " + GameBoard[guessCol][guessRow]);
System.out.println(" EnemyBoard[guessCol][guessRow] is " + EnemyBoard[guessCol][guessRow]);
displayMenu();
}
public boolean colCheck()
{
boolean validshot = true;
if (guessCol > 0 && guessCol < 6)
{
validshot = true;
}
else
validshot = false;
System.out.println("Your shot must be between columns 1 and 5.");
return validshot;
}
public boolean rowCheck()
{
boolean validshot = true;
if (guessRow > 0 && guessRow < 6)
{
validshot = true;
}
else
validshot = false;
System.out.println("Your shot must be between rows 1 and 5.");
return validshot;
}
public void displayResults()
{
int x = 0;
int y = 0;
boolean warShipSunk;
System.out.println( numberOfShots + " shots were fired.");
System.out.println( Win + " shots hit.");
System.out.println( Loss + " shots missed.");
warShipSunk = s1.getSunk();
if (warShipSunk= true)
{
System.out.println("For " + numberOfShots + " shots " + p1 + " was a winner!!!");
System.out.println("Player sank " + s1 + "!");
System.out.println("Ship is: " + s1);
}
else if(warShipSunk = false)
{
System.out.println("For " + numberOfShots + " shots " + p1 + " was a loser.");
}
System.out.println(" ");
}
private static String newId()
{
idNum++;
return id + String.valueOf(idNum);
}
}
|
|
Query failed: connection to localhost:3312 failed (errno=111, msg=Connection refused).
|
