Anda di halaman 1dari 7

//big problem, enter a 9 for a column and I get an error message

//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("guess= " + guess + ", guessC


ol="+guessCol + ", guessRow="+ guessRow+"\n");
guessCol = Integer.parseInt(guess);
if (guessCol<1 || guessCol >12)
{
System.out.println("Error: choose a posi
tion from 1 to 12");
}
}while (guessCol<1 || guessCol >12);
//System.out.print("guessCol="+guessCol + ", guessRow="+
guessRow+"\n");
//if (bo1[guessRow-1][guessCol-1] !=0) //adjusts rows a
nd cols so user
//

System.out.print("HIT\n");

//does

n't have to start with 0


//if (bo1[guessRow-1][guessCol-1] ==0)
//
System.out.print("MISS\n");
b1.hitBoard(guessRow,guessCol);
b1.printBoard();
}
}
//**************************************
public static String GetConsoleString() //throws java.io.IOException
{
int noMoreInput=-1;
char enterKeyHit= '\n';
int InputChar;
StringBuffer InputBuffer= new StringBuffer(30);
try
{
InputChar=System.in.read();
while(InputChar != noMoreInput)
{
if((char)InputChar != enterKeyHit)
{
if (InputChar<='0' || InputChar>'9') //h
andles if any other characters
{
//enter other than numbers
InputChar='0';
}
InputBuffer.append((char)InputCh
ar);
//}
}
else
{
InputBuffer.setLength(InputBuffer.length
()-1);
if (InputBuffer.length()==0)//prevents a
n error if key return is pressed and nothing inputted
{
InputBuffer.append('0');
}
break;
}
InputChar=System.in.read(); //reads 2 codes the

char and return key (code 13)


}
}
catch(IOException IOX)
{
System.err.println(IOX);
}
return InputBuffer.toString();
}
}
//*******************************
class battleship
{
private int i=0;
private int guess=0;
private int ranCol=0;
private int ranRow=0;
private int boat2=0;//3 hole
private int boat3=0;//4 hole
private int boat4=0;//4 hole
private int boat5=0;//5 hole
private int direction=3;//1 is hor 2 is vert
private int [] x1= new int[12]; //the board for player 1
private int [] y1= new int[10];
private int [] x2= new int[12]; //player 2
private int [] y2= new int[10];
private int [][] board=new int[10][12];
private int goodchoice=1;
private int rows=10;
private int cols=12;
private Random numValues=new Random();
private int HB[][]=new int[10][12];
private int hitBoardCount=0;
public battleship() //unnecessary to create...java makes for but seems
good to do
{
}
public int[][] boardMaker( Random numValues)
{
//sets all spaces on the board to 0*********************
for (i=0;i<10; i++)
{
for (int j=0; j<12; j++)
{
board[i][j]=0;
}
}
//******************************************************
String InputString="";
//selects random numbers for upper left corner of the boat and
its direction
for (i=2; i<6; i++)
{
do
{
goodchoice=1; //set goodchoice to 1 so the next choic

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();
}

//purpose of the hitboard is to show where the shots go...


//precondition...a board with random boads already layed out, us
er specifies row, col of
//a hit. The entire array is initialized to 0's to show no shot
s fired
//postcondition..a matrix is reported that shows the location of
all the shots. If the
//shot is a miss the value stored in the array at that position
is a 9, if a ship is
//struck the number of the ship is revealed. The number of time
s a boat is hit is recored
//until the boat is sunk, this too is reported. If a boat is sun
k it is removed from the
//board
public int[][] hitBoard(int hr,int hc)
{
//int hit=0;
int kill=-1; //if set equal to 0 causes trouble later
hitBoardCount=hitBoardCount+1;
if (hitBoardCount==1) //need to only set the hit board
to 0's in the beginning
{
//or as
new hits occur, old hits will be wiped
for (int i2=0; i2<10; i2++)
{
for (int j=0; j<12; j++)
{
HB[i2][j]=0;
}
}
}
if (hitBoardCount>1)
hitBoardCount=2;
if (board[hr-1][hc-1] !=0)
{
HB[hr-1][hc-1]=board[hr-1][hc-1]; //sets hitboar
d equal to play board value
//of the ship
if (board[hr-1][hc-1]==2)
{
boat2=boat2+1;
System.out.print("HIT number " + boat2 +
" on ship " + board[hr-1][hc-1] + "\n"); //HB[hr-1][hc-1]=board[hr-1][hc-1]; /
/sets hitboard equal to play board value
//of the ship
}
if (board[hr-1][hc-1]==3)
{
boat3=boat3+1;
System.out.print("HIT number " + boat3 +
" on ship " + board[hr-1][hc-1] + "\n"); //HB[hr-1][hc-1]=board[hr-1][hc-1]; /
/sets hitboard equal to play board value
//of the ship
}
if (board[hr-1][hc-1]==4)
{
boat4=boat4+1;

System.out.print("HIT number " + boat4 +


" on ship " + board[hr-1][hc-1] + "\n"); //HB[hr-1][hc-1]=board[hr-1][hc-1]; /
/sets hitboard equal to play board value
//of the ship
}
if (board[hr-1][hc-1]==5)
{
boat5=boat5+1;
System.out.print("HIT number " + boat5 +
" on ship " + board[hr-1][hc-1] + "\n"); //HB[hr-1][hc-1]=board[hr-1][hc-1]; /
/sets hitboard equal to play board value
//of the ship
}
}
if (board[hr-1][hc-1] ==0)
{
HB[hr-1][hc-1]=9;

//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;
}

Anda mungkin juga menyukai