Battleship GUI Game | 2005-11-13

Portfolio » Application Programming

BattleShipGUI extends JFrame


 
  import javax.swing.JFrame;
  import javax.swing.JOptionPane;
  public class JavaGame2 {
  /**
  * One play logic for one player
  * 
  * @param gui
  *            the battle ship gui
  * @param player
  *            the playing player
  * @param name
  *            the name of the player
  */
  public void play(BattleShipGUI gui, GameBoard player, String name) {
  // Loop until the player input the command correctly or the player
  // didn't hit the ship
  while (************) {
  gui.setBoard(player.toString());
  gui.setLable(player.checkSunk() + "\nThe current player is: "
  + name);
  String msg = "Player " + name
  + ", enter the coordinates in this format: 0,0\n";
  String input = JOptionPane.showInputDialog(gui, msg);
  if (input != null) {
  // Player enter -1
  if (input.equals("-1"))
  System.exit(0);
  int x = GameBoard.getCoordinateX(input);
  int y = GameBoard.getCoordinateY(input);
  if (************)
  // The player didn't enter the coordinate with correct
  // format or wrong range
  JOptionPane
  .showMessageDialog(gui,
  "Please enter the coordinate in correct format or in correct range.");
  else if (player.isHit(x, y))
  // Player enter a coordinate which is hit already
  JOptionPane.showMessageDialog(gui,
  "Please enter the coordinate which is not hit.");
  else {
  if () {
  // Hit the ship
  JOptionPane.showMessageDialog(gui,
  "YOU HIT HIM! GO AGAIN!");
  } else {
  // Missed and break the loop
  JOptionPane.showMessageDialog(gui, "YOU MISSED!");
  break;
  }
  if (player.allSunk()) {
  JOptionPane.showMessageDialog(gui, "YOU WON THE GAME.");
  System.exit(0);
  }
  }
  } else {
  // Player click the cancel button
  System.exit(0);
  }
  }
  }
  /**
  * Play the game in turns
  * 
  * @param player1
  *            player game board 1
  * @param player2
  *            player game board 2
  */
  public void Proc(GameBoard player1, GameBoard player2) {
  // Display the battle ship gui
  BattleShipGUI gui = new BattleShipGUI();
  gui.setVisible(true);
  gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  // Get the name for each player
  String playerName1 = JOptionPane.showInputDialog(gui,
  "Please enter the name for the player 1.");
  String playerName2 = JOptionPane.showInputDialog(gui,
  "Please enter the name for the player 2.");
  // Play until the game end
  for (int i = 0;; ++i) {
  if (i % 2 == 0) {
  play(gui, player2, playerName2);
  } else {
  play(gui, player1, playerName1);
  }
  }
  }
  /**
  * @param args
  */
  public static void main(String[] args) {
  GameBoard player1 = new GameBoard(args[0]);
  GameBoard player2 = new GameBoard(args[1]);
  ***
  drive.Proc(player1, player2);
  }
  }

 

Source Code 2:

import java.awt.BorderLayout;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import java.awt.Dimension;

public class BattleShipGUI extends JFrame {

private static final long serialVersionUID = 1L;

private JPanel jContentPane = null;

private JTextArea boardTextArea = null;

private JTextArea label = null;

/**
* This is the default constructor
*/
public BattleShipGUI() {
super();
initialize();
}

/**
* This method initializes this
*
* @return void
*/
private void initialize() {
this.setSize(420, 540);
this.setContentPane(getJContentPane());
this.setTitle("JFrame");
}

/**
* This method initializes jContentPane
*
* @return javax.swing.JPanel
*/
private JPanel getJContentPane() {
if (jContentPane == null) {
jContentPane = new JPanel();
jContentPane.setLayout(new BorderLayout());
jContentPane.add(getBoardTextArea(), BorderLayout.CENTER);
jContentPane.add(getLabel(), BorderLayout.SOUTH);
}
return jContentPane;
}

/**
* This method initializes boardTextArea
*
* @return javax.swing.JTextArea
*/
private JTextArea getBoardTextArea() {
if (boardTextArea == null) {
boardTextArea = new JTextArea();
}
************
}

/**
* Display the board to the text area
*
* @param board
* the board content
*/
public void setBoard(String board) {
boardTextArea.setText(board);
}

/**
* Set the lable of the game
*
* @param lable
* the lable content
*/
public void setLable(String lable) {
label.setText(lable);
}

/**
* This method initializes label
*
* @return javax.swing.JTextArea
*/
private JTextArea getLabel() {
if (label == null) {
label = new JTextArea();
************
label.setBackground(this.getBackground());
}
return label;
}
 
}

 

*** indicated that some partial codes has been removed