Battleship
<< StoryTextApplet | FinalProjectsTrailIndex | SimpleGameTutorial >>
Cell.java
import java.awt.Color; import java.awt.Graphics; import java.awt.Rectangle; public class Cell { private Color color; private Rectangle r; private boolean hit,selected; private String location; public final String rows="ABCDEFGHIJ"; public Cell(int row, int col, int x, int y, int size){ r = new Rectangle(x,y,size, size); setColor(Color.CYAN); setLocation(rows.substring(row, row+1)+"-"+(col+1)); } public void draw(Graphics g){ g.setColor(Color.BLACK); if (isSelected()) g.setColor(Color.MAGENTA); g.fillRect(r.x, r.y, r.width, r.height); g.setColor(color); g.fillRect(r.x+4, r.y+4, r.width-8, r.height-8); if (isHit()){ g.setColor(Color.RED); int s=r.width/4; g.fillOval(r.x+s, r.y+s, 2*s, 2*s); } } public boolean contains(int x, int y){ return r.contains(x, y); } public boolean isHit() { return hit; } public void setHit(boolean hit) { this.hit = hit; } public boolean isSelected() { return selected; } public void setSelected(boolean selected) { this.selected = selected; } public String getLocation() { return location; } public void setLocation(String location) { this.location = location; } public Color getColor(){ return color; } public void setColor(Color c){ color=c; } }
Battleship.java
import java.applet.Applet; import java.awt.Color; import java.awt.Graphics; import java.awt.Image; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.awt.event.MouseMotionListener; public class BattleShip extends Applet implements MouseListener, MouseMotionListener { private Cell[][] board; private String message; private Image screen2; private Graphics g2; public void init(){ addMouseListener(this); addMouseMotionListener(this); screen2 = createImage(800,600); g2 = screen2.getGraphics(); message="Battleship"; board = new Cell[10][10]; for (int r=0; r<10;r++) for (int c=0; c<10; c++){ board[r][c]=new Cell(r,c, 70+c*50, 80+r*50, 50); } } public void paint(Graphics g){ g2.setColor(Color.WHITE); g2.fillRect(0, 0, 800, 600); for (int r=0; r<10;r++) for (int c=0; c<10; c++){ board[r][c].draw(g2); } g2.setColor(Color.BLACK); g2.drawString(message, 20, 50); g.drawImage(screen2, 0,0,this); } public void update(Graphics g){ paint(g); //get rid of flicker with this method } @Override public void mouseClicked(MouseEvent e) {} @Override public void mousePressed(MouseEvent e) {} @Override public void mouseReleased(MouseEvent e) { int x=e.getX(); int y=e.getY(); for (int r=0; r<10;r++) for (int c=0; c<10; c++){ if(board[r][c].contains(x, y)){ board[r][c].setColor(Color.BLUE); //test if a hit // if so, board[r][c].setHit(true); } } repaint(); } @Override public void mouseEntered(MouseEvent e) {} @Override public void mouseExited(MouseEvent e) {} @Override public void mouseDragged(MouseEvent e) {} @Override public void mouseMoved(MouseEvent e) { int x=e.getX(); int y=e.getY(); for (int r=0; r<10;r++) for (int c=0; c<10; c++){ if(board[r][c].contains(x, y)){ board[r][c].setSelected(true); message=board[r][c].getLocation(); }else{ board[r][c].setSelected(false); } } repaint(); } }
Ship.java
public class Ship { private String[] locs; private boolean[] hits; public final String rows="ABCDEFGHIJ"; public Ship(){ int size = randomInt(2,4); locs = new String[size]; hits = new boolean[size]; for (int i=0;i<size;i++) hits[i]=false; int a= randomInt(0, 10-size); int b= randomInt(0, 9); if(Math.random()<.5){//vertical for(int i=0;i<size;i++) locs[i]=getLocation(a+1, b); } else {//horiz for(int i=0;i<size;i++) locs[i]=getLocation(b, a+1); } } public String getLocation(int row, int col){ return rows.substring(row, row+1)+"-"+(col+1); } public int randomInt(int min, int max){ min = Math.min(min, max); max = Math.max(min, max); return min+(int)((max-min)*Math.random()); } public int size(){ return locs.length; } public boolean contains(String loc){ for(int i=0; i<size(); i++) if (loc.equals(locs[i])) return true; return false; } public boolean overlaps(Ship other){ for(int i=0; i<size(); i++) if (other.contains(locs[i])) return true; return false; } public void hit(String loc){ if (!this.contains(loc)) return; for(int i=0; i<size(); i++) if (loc.equals(locs[i])) hits[i]=true; } public boolean isSunk(){ for (int i=0;i<size(); i++) if (hits[i]==false) return false; return true; } }