Results 1 to 7 of 7
  1. #1
    aznkid1221 is offline Member
    Join Date
    Oct 2009
    Posts
    4
    Rep Power
    0

    Default needs help making battleship in java

    I have to make a battleship in java for school, i got some of the codes tats already came with the project. Can someone tell me what other classes i need, and what methods i need to write in them?

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;

    public class BattleScreen extends JFrame {

    private BattlePanel bp;

    public BattleScreen(){
    super("Battle On");
    bp = new BattlePanel(10);
    getContentPane().add(bp);
    setSize(800,400);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    }





    public static void main(String [] args){
    new BattleScreen().show();
    }

    }
    __________________________________________________ ______________

    public class Coor {

    int x,y;

    public Coor(int x, int y){
    this.x = x;
    this.y = y;

    }

    public int x(){
    return x;
    }

    public int y(){
    return y;
    }

    public boolean equals(Object o){
    if (o instanceof Coor){
    return x == ((Coor)o).x && y == ((Coor)o).y;
    }

    return false;
    }

    public String toString(){
    return x+" , "+y;
    }
    }
    __________________________________________________ _______________
    import java.util.*;

    public class Runner {

    public static void main(String [] s){
    Scanner sc = new Scanner(System.in);

    System.out.println("Please enter the size of the board (> 10)");
    int size = sc.nextInt();

    ComputerBoard computer = new ComputerBoard(size,size);
    Board player = new Board(size,size);

    printPlayerBoard(player);

    int count = -1;
    boolean success = false;
    System.out.println("Do you wish to randomize?(1 or 0)");
    int rand = sc.nextInt();
    if(rand == 1) {
    player.randomize(5);
    printPlayerBoard(player);
    }
    else {
    while (count < 5){
    count ++;
    while(!success){
    System.out.println("Please enter the X coordinate for the ship "+(count+2)+ " long");
    int x = sc.nextInt();
    System.out.println("Please enter the Y coordinate for the ship "+(count+2)+ " long");
    int y = sc.nextInt();
    System.out.println("Please enter the direction the ship will be placed. (1 = North, 2 = East, 3 = South, 4=West)");
    int direction = sc.nextInt();

    success = player.placeShip(new Ship(count+2),new Coor(x,y),direction);
    }

    printPlayerBoard(player);
    success = false;

    }
    }
    while(!computer.gameOver() && !player.gameOver()){
    playerMove(computer, sc);
    computerMove(player, computer);

    }

    if(computer.gameOver()){
    System.out.println("YOU WIN!!");

    }
    else {
    System.out.println("Computer Wins");
    }


    }



    public static void printPlayerBoard(Board b){
    for (int i=0;i<b.height();i++){
    for(int j=0;j<b.width();j++){
    if(b.status(new Coor(j,i)) == 1 || b.status(new Coor(j,i)) == 2){
    System.out.print("S");
    }
    else {
    System.out.print("0");
    }
    }
    System.out.println();
    }

    }

    public static void printOpponentBoard(Board b){
    System.out.println("-=-=-=-=-=-=-=-=-=-=Computer's board=-=-=-=-=-=-=-=-");
    for (int i=0;i<b.height();i++){
    for(int j=0;j<b.width();j++){
    int status = b.status(new Coor(i,j));
    if( status == 1){
    System.out.print("S");
    }
    else if (status == -1){
    System.out.print("M");
    }
    else {
    System.out.print("0");
    }
    }
    System.out.println();
    }
    }


    public static void playerMove(Board comp, Scanner sc){
    System.out.println("Please enter the X coordinate of your shot");
    int x = sc.nextInt();
    System.out.println("Please enter the Y coordinate of your shot");
    int y = sc.nextInt();
    if(comp.shot(new Coor(x,y))){
    System.out.println("HIT!!");
    }
    else {
    System.out.println("MISS!");
    }
    printOpponentBoard(comp);


    }

    public static void computerMove(Board player, ComputerBoard comp){

    if(comp.makeMove(player)){
    System.out.println("Computer Hits");
    }
    else {
    System.out.println("Computer Misses");
    }
    }
    }
    __________________________________________________ ___________________

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    Quote Originally Posted by aznkid1221 View Post
    .. Can someone tell me what other classes i need, and what methods i need to write in them?

    ...
    Didn't that sound strange as you were typing it?

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    If you ask for our suggestions without first trying to do it yourself, you'll be cheating yourself out of a valuabe experience. Why not give it a go first, and then if your ideas don't work out come on back and we can look at your code. Much luck.

  4. #4
    aznkid1221 is offline Member
    Join Date
    Oct 2009
    Posts
    4
    Rep Power
    0

    Default

    i have tried, made 4 prototypes, 2 of which i use those codes above and trying to fill in the missing ones, and another 2 i tried making a new battleship project from beginning

  5. #5
    aznkid1221 is offline Member
    Join Date
    Oct 2009
    Posts
    4
    Rep Power
    0

    Default

    this is what i have so far
    Java Code:
     public class BattleScreen {
    	public static void main(String [] args){
    	int[][]myfield = new int [10][10];
    	int[][]compfield = new int [10][10];
    	int[][]pos = new int [10][10];
    	Board board = new Board(myfield);
    	Board comboard = new Board(compfield);
    	int hit =0;
    	int comphit=0;
    	
    	Ship boat = new Ship(2,"Boat");
    	boat.placeShip(myfield);
    	boat.placeShip(compfield);
    	
    	Ship cruise = new Ship(3,"Cruise");
    	cruise.placeShip(myfield);
    	cruise.placeShip(compfield);
    	
    	Ship sub = new Ship(4,"SubMarine");
    	sub.placeShip(myfield);
    	sub.placeShip(compfield);
    	
    	Ship battle = new Ship(5,"BattleShip");
    	battle.placeShip(myfield);
    	battle.placeShip(compfield);
    	
    	Ship air = new Ship(6,"AirCraftCarrier");
    	air.placeShip(myfield);
    	air.placeShip(compfield);
    	
    	while (hit <20 && comphit<20){
    	board.displayField(myfield);
    	comboard.displayField(compfield);
    	run.attack(pos,compfield);
    	run.attack(pos,myfield);
    	}
    	if(hit==20)
    		System.out.println("YOU WIN!");
    	if(comphit==20)
    		System.out.println("COMPUTER WIN!");
    	
    	}
    	
    }
    Java Code:
    public class Board {
    		int row;
    		int column;
    		Scanner reader = new Scanner(System.in);
    		int size = reader.nextInt();
    
    	public Board(int [][] a){
    		for(int col=1;col<a.length;col++)
       {
          for(int row=1;row<a.length-1;row++)
          {
             a[row][col] = 0;
          }
       }   
        }
    	
    	
    	public void displayField(int [][]a){
    		System.out.println("      Battle Ship!");
            System.out.println("      ----------------");
            System.out.println("      ");
            System.out.println("    1 2 3 4 5 6 7 8 9 10");
            System.out.println("    -----------------");
       int rownum = 1;
       for(int row=0;row<a.length-1;row++)
       { 
          System.out.print(rownum + " |");
          rownum++;
          for(int col=0;col<a[0].length-1;col++)
          {
             System.out.print(" " + a[row][col]);
          }
          System.out.println();
       }	
    	}
      }
    Java Code:
    import java.util.Random;
    import java.util.Scanner;
    
    
    public final class run {
    	int hit;
    	int comphit;
    	public run(){
    		
    	}
    	public static void attack(int [][]a,int [][]b){	
    		int row=0;
    	    int col=0;
    	    int value=0;
    	    int hit=0;
           {
                 Scanner reader = new Scanner(System.in);
                 System.out.println("X space Y of target");
                 row = reader.nextInt();
                 col = reader.nextInt();
                 if(row<=0||col<=0||row>10||col>10){
                    System.out.println("coordinateds out of board. Use different value");
    						System.out.println();
                 }
                 
                  
              while(row<=0||col<=0||row>10||col>10);
    				
              if(b[col-1][row-1] == 2 ||b[col-1][row-1] == 3){
                 System.out.println("You already used this coordinate! Use another one!");
              }
           } 
                 
            while(b[col-1][row-1] == 2 || b[col-1][row-1] == 3);{
        
           if(a[col-1][row-1]==1) 
           {
              b[col-1][row-1] =3 ;
              System.out.println("Hit!");
              hit++;
    
           }
           else
           {
              b[col-1][row-1] = 2;
              System.out.println("Missed!");
           }  
            }
        }
    	public static int randomrow(){
    		Random r = new Random();
            int row = r.nextInt(10);
            return row;
    	}
    	public static int randomcol(){
    		Random r = new Random();
             int col = r.nextInt(10);
             return col;
    	}
    	
    	public static void compattack(int [][]a,int [][]b){	
    		int row=0;
    	    int col=0;
    	    int value=0;
    	    int comphit=0;
    	       {
    	    	     randomrow();
    	    	     randomcol();
    	    	     
    	          if(b[col-1][row-1] == 2 ||b[col-1][row-1] == 3){
    	        	  randomrow();
    		    	  randomcol(); ;
    	          }
    	       } 
    	             
    	        while(b[col-1][row-1] == 2 || b[col-1][row-1] == 3);{
    	    
    	       if(a[col-1][row-1]==1) 
    	       {
    	          b[col-1][row-1] =3 ;
    	          System.out.println("Hit!");
    	          comphit++;
    	       }
    	       else
    	       {
    	          b[col-1][row-1] = 2;
    	          System.out.println("Missed!");
    	       } 
    	    }
    	
    	}
    	
    }
    Java Code:
    import java.util.Random;
    
    
    public class Ship {
    	int size;
    	String name;
    	public Ship(int s, String string){
    		size =s;
    		name=string;
    	}
    	
    
    	public void placeShip(int [][]a)
    	{
    	   Random r = new Random();
    	   int orientation = r.nextInt(2);
    	   if(orientation == 0)//Vertical Ship
    	   {
    		  int x=r.nextInt(10);
    		  int y=r.nextInt(10-size);
    		  for(int z=0;z<size;z++)
    		  {
    			 a[y+z][x] = 1;
    		  }				 
    	   }
    	   else//Horizontal Ship
    	   {
    		  int x=r.nextInt(10-size);
    		  int y=r.nextInt(10);
    		  for(int z=0;z<size;z++)
    		  {
    			 a[y][x+z] = 1;
    		  } 
    	   }
    	}
    
    }
    I have no idea what to do next
    Last edited by aznkid1221; 11-06-2009 at 05:05 PM.

  6. #6
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    Quote Originally Posted by aznkid1221 View Post
    this is what i have so far
    ...
    I have no idea what to do next
    Does it compile? Does it run correctly? If not what's the problem and where are you stuck with coming up with a solution?

  7. #7
    aznkid1221 is offline Member
    Join Date
    Oct 2009
    Posts
    4
    Rep Power
    0

Similar Threads

  1. BattleShip Helm Needed with GUI
    By Kinyo in forum Eclipse
    Replies: 0
    Last Post: 03-15-2009, 12:04 AM
  2. Battleship game
    By kathyla18 in forum New To Java
    Replies: 2
    Last Post: 02-26-2009, 09:42 PM
  3. Battleship help..im confused
    By stepjerd1 in forum Java 2D
    Replies: 4
    Last Post: 01-23-2009, 01:35 AM
  4. Java Battleship Game Help PLEASE
    By mars_red in forum New To Java
    Replies: 0
    Last Post: 02-12-2008, 01:09 AM
  5. Java BattleShip game help
    By mars_red in forum Advanced Java
    Replies: 0
    Last Post: 02-12-2008, 12:58 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •