//students don't learn by creating a huge professional project by typing the pro
gram from beginning to
//end, learn by picking a program 1. people interested in, 2. start at the begin
ning in small
//steps with complete tiny, ever growing programs adding layers of sophisticatio
n 3. can't spend too
//long on a project, they will lose interest. 4. In teaching classes, make it w
ork in the main
//then move it to the class, 5. start with a console application (if possible),
then
//migrate it into an appliet
//need to show the boards in such a way they appear at the right time, also need
to declare a winner
//tracks boats as they are hit, reports what they are, how many times hit, if su
nk, then fixes the
//boards for boardhits and the regular board
import java.io.IOException;
import java .util.Random;
class battleship14
{
public static void main(String args[]) //throws java.io.IOException
{
battleship b1=new battleship();
battleship b2=new battleship();
Random numValues1=new Random();
String guess="";
int guessRow=0; //wierd...get an error if not initialized when s
ent into
int guessCol=0; //the function b1.hitBoard(guessRow,guessCol)...
why?
int bo1[][]=new int [10][12];
int bo2[][]=new int[10][12];
bo1=b1.boardMaker(numValues1);
bo2=b2.boardMaker(numValues1);
for (int i=0; i<20; i++) //allows 5 hits to test the game with
{
do
{
System.out.print("Give a row 1 to 10 to fire at:
");
guess=GetConsoleString();
guessRow = Integer.parseInt(guess);
if (guessRow<1 || guessRow >10)
System.out.println("Error: choose a posi
tion from 1 to 10");
}while(guessRow<1 || guessRow >10);
do
{
System.out.print("Give the column 1 to 12 to fir
e at: ");
guess=GetConsoleString();
System.out.print("HIT\n");
//does
e can be checked
direction=(int)(numValues.nextDouble()*2)+1;
if (direction==1) //1 means horiontal
{
ranCol=(int)(numValues.nextDouble()*(12-i));//=Math
.abs(numValues.nextInt()%12 + 1);
ranRow=(int)(numValues.nextDouble()*10);
}
if (direction==2) //2 means vertical
{
ranCol=(int)(numValues.nextDouble()*12);//=Math.abs
(numValues.nextInt()%12 + 1);
ranRow=(int)(numValues.nextDouble()*(10-i));
}
for(int i2=0; i2< 10; i2++) //rows
{
for (int j=0; j<12; j++) //collumns
{
if (direction==1 && ranRow==i2 &
& j>=ranCol && j<ranCol +i )
if (board[i2][j]!=0)
goodchoice=0;
if (direction==2 && ranCol==j &&
i2>=ranRow && i2<ranRow +i)
if (board[i2][j]!=0)
goodchoice=0;
}
}
if (goodchoice==1)
{
for(int i2=0; i2< 10; i2++) //rows
{
for (int j=0; j<12; j++) //collumns
{
if (direction==1 && ranRow==i2 &
& j>=ranCol && j<ranCol +i )
board[i2][j]=i;
if (direction==2 && ranCol==j &&
i2>=ranRow && i2<ranRow +i)
board[i2][j]=i;
}
}
}
}while(goodchoice==0);
}
return board;
}
//prints board and ships********************
public void printBoard()
{
for(int i2=0; i2< 10; i2++) //rows
{
for (int j=0; j<12; j++) //collumns
{
System.out.print(board[i2][j]);
}
System.out.println();
}
//System.out.println();
}
//records a mi
ss
//System.out.print("MISS\n");
}
for (int i2=0; i2<10; i2++)//displays hit board
{
for (int j=0; j<12; j++)
{
System.out.print(HB[i2][j]);
}
System.out.println();
}
System.out.println();
if (board[hr-1][hc-1] !=0)
{
if (boat2>1) //says boat 2 is sunk
{
System.out.println("Boat 2, a PT boat, i
s sunk");
kill=2;
}
if (boat3>2) //says boat 2 is sunk
{
System.out.println("Boat 3, a destroyer,
is sunk");
kill=3;
}
if (boat4>3) //says boat 2 is sunk
{
System.out.println("Boat 4, a battleship
, is sunk");
kill=4;
}
if (boat5>4) //says boat 2 is sunk
{
System.out.println("Boat 5, an aircraft,
carrier is sunk");
kill=5;
}
}
if (board[hr-1][hc-1] ==0)
{
//HB[hr-1][hc-1]=9;
System.out.print("MISS\n");
}
if (kill>=0)
{
for (int i2=0; i2<10; i2++)
{
for(int j=0; j<12; j++)
{
if (HB[i2][j]==kill)
{
HB[i2][j]=9;
board[i2][j]=0;
if (boat2==2)
boat2=0;
if (boat3==3)
boat3=0;
if (boat4==4)
boat4=0;
if (boat5==5)
boat5=0;
}
}
}
kill=-1;
}
//if (board
return HB;
}