Results 1 to 7 of 7
- 10-20-2009, 02:00 PM #1
Member
- Join Date
- Oct 2009
- Posts
- 4
- Rep Power
- 0
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");
}
}
}
__________________________________________________ ___________________
- 10-20-2009, 02:04 PM #2
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
-
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.
- 10-21-2009, 05:15 PM #4
Member
- Join Date
- Oct 2009
- Posts
- 4
- Rep Power
- 0
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
- 11-05-2009, 02:35 PM #5
Member
- Join Date
- Oct 2009
- Posts
- 4
- Rep Power
- 0
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!"); } } } }I have no idea what to do nextJava 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; } } } }Last edited by aznkid1221; 11-06-2009 at 05:05 PM.
- 11-05-2009, 02:39 PM #6
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
- 11-06-2009, 05:05 PM #7
Member
- Join Date
- Oct 2009
- Posts
- 4
- Rep Power
- 0
Similar Threads
-
BattleShip Helm Needed with GUI
By Kinyo in forum EclipseReplies: 0Last Post: 03-15-2009, 12:04 AM -
Battleship game
By kathyla18 in forum New To JavaReplies: 2Last Post: 02-26-2009, 09:42 PM -
Battleship help..im confused
By stepjerd1 in forum Java 2DReplies: 4Last Post: 01-23-2009, 01:35 AM -
Java Battleship Game Help PLEASE
By mars_red in forum New To JavaReplies: 0Last Post: 02-12-2008, 01:09 AM -
Java BattleShip game help
By mars_red in forum Advanced JavaReplies: 0Last Post: 02-12-2008, 12:58 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks