Commit 35bbcdac authored by james.clarke2's avatar james.clarke2

hio

parent fa2e95f0
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="src" path=""/>
<classpathentry kind="output" path=""/>
</classpath>
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>Battleship-2</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.applet.*;
public class GUI { public class GUI extends javax.swing.JFrame implements ActionListener
{
} JButton[][] grid;
JTextField userField;
JTextField computerField;
game curGame;
public GUI(grid User, grid Computer, game newGame){
curGame = newGame;
JFrame guiFrame = new JFrame();
JPanel userPanel = new JPanel(new GridLayout(3,1, 5, 5));
JPanel computerPanel = new JPanel(new GridLayout(3,1, 5, 5));
JLabel userLabel = new JLabel("User", SwingConstants.CENTER);
JLabel computerLabel = new JLabel("Computer", SwingConstants.CENTER);
userField = new JTextField();
computerField = new JTextField();
userField.setEditable(false);
computerField.setEditable(false);
userLabel.setFont(new Font("SanSerif",Font.BOLD, 16));
computerLabel.setFont(new Font("SanSerif",Font.BOLD, 16));
userPanel.add(userLabel);
computerPanel.add(computerLabel);
makeGrid(userPanel, User, "user");
makeGrid(computerPanel, Computer, "ai");
userPanel.add(userField);
computerPanel.add(computerField);
guiFrame.setLayout(new GridLayout(1,2, 10, 10));
guiFrame.add(userPanel);
guiFrame.add(computerPanel);
guiFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
guiFrame.setTitle("Battleship");
guiFrame.setSize(1200, 800);
guiFrame.setLocationRelativeTo(null);
guiFrame.setVisible(true);
}
public void makeGrid(JPanel panel, grid newGrid, String name){
grid = new JButton[newGrid.height][newGrid.width];
JPanel pan = new JPanel(new GridLayout(newGrid.height,newGrid.width));
for(int y=0; y<newGrid.height; y++){
for(int x=0; x<newGrid.width; x++){
grid[x][y]=new JButton("("+x+","+y+")");
grid[x][y].putClientProperty("coordinates", Integer.toString(x) + ":" + Integer.toString(y) + ";");
if (name == "user"){
grid[x][y].putClientProperty("player", "user");
}else{
grid[x][y].putClientProperty("player", "ai");
}
grid[x][y].addActionListener(this);
pan.add(grid[x][y]);
}
}
panel.add(pan);
}
public void actionPerformed(ActionEvent e){
JButton o = (JButton)e.getSource();
String name = o.getText();
String property = (String) o.getClientProperty("coordinates");
String player = (String) o.getClientProperty("player");
int indexColon = property.indexOf(":");
int indexSemi = property.indexOf(";");
int x = Integer.parseInt(property.substring(0, indexColon));
int y = Integer.parseInt(property.substring(indexColon+1, indexSemi));
if (player == "ai"){
if(curGame.playerTurn){
curGame.attack(x, y, userField, computerField);
}
}
}
}
\ No newline at end of file
import java.awt.*;
import javax.swing.*;
public class game { public class game
{
boolean playerTurn;
player User;
player Computer;
public game(int size){
User = new player(false, size);
Computer = new player(true, size);
User.GenerateShips(User);
Computer.GenerateShips(Computer);
playerTurn = true;
}
public void attack(int x, int y, JTextField user, JTextField computer){
JOptionPane.showMessageDialog(null, x + " " + y, "InfoBox: " + "James", JOptionPane.INFORMATION_MESSAGE);
if (playerTurn){
if (Computer.hit(x, y)){
user.setText(user.getText() + "\r\n" + "You have hit the ship!");
}else{
user.setText(user.getText() + "\r\n" + "You've missed the ship!");
}
}else{
if (User.hit(x, y)){
//Computer Hit Ship
}else{
//Computer Didn't Hit Ship
}
}
}
} }
import javax.swing.JOptionPane;
public class grid { public class grid
{
int width = 0;
int height = 0;
public grid(int width, int height){
this.width = width;
this.height = height;
}
} }
public class player
{
boolean[][] attacked;
boolean[][] ships;
ship[] shipList = new ship[8];
int size;
public player(boolean computer, int size){
this.size = size;
attacked = new boolean[size][size];
ships = new boolean[size][size];
public class player { if (computer){
//Generate Ship Location Here
// shipList[0].coordinates[0] = "(1, 3)";
// ships[1][3] = true;
// shipList[1].coordinates[1] = "(2, 3)";
// ships[2][3] = true;
// shipList[2].coordinates[2] = "(3, 3)";
// ships[3][3] = true;
// shipList[3].coordinates[3] = "(4, 3)";
// ships[4][3] = true;
// shipList[4].coordinates[4] = "(5, 3)";
// ships[5][3] = true;
}
}
public boolean hit(int x, int y){
attacked[x][y] = true;
if (ships[x][y]){
return true;
}
return false;
}
public void GenerateShips(player player) {
shipList[0] = new ship("Aircraft Carrier", 5, player);
shipList[1] = new ship("Battleship 1", 4, player);
shipList[2] = new ship("Battleship 2", 4, player);
shipList[3] = new ship("Destroyer 1", 3, player);
shipList[4] = new ship("Destroyer 2", 3, player);
shipList[5] = new ship("Patrol Boat 1", 2, player);
shipList[6] = new ship("Patrol Boat 1", 2, player);
shipList[7] = new ship("Patrol Boat 1", 2, player);
}
} }
public class ship {
public player player;
public String name;
public int length;
public boolean destroyed;
public String[] coordinates = new String[this.length];
public ship(String name, int length, player player) {
this.name = name;
this.length = length;
destroyed = false;
this.player = player;
}
}
import javax.swing.JOptionPane;
public class welcome { public class welcome
{
public static void main (String[] args){
String input;
boolean valid = false;
int size = 0;
while(!valid) {
input = JOptionPane.showInputDialog("How many rows and columns do you want? (Min 6)", 10);
size = Integer.parseInt(input);
if (size > 5) {
valid = true;
}
}
grid userGrid = new grid(size, size);
grid computerGrid = new grid(size, size);
game newGame = new game(size);
GUI main = new GUI(userGrid, computerGrid, newGame);
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment