📄 battleshipdisplay.java
字号:
//Battleship game
import java.util.*;
import java.lang.*;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.net.*;
import java.lang.*;
/**
* Class for the GUI of Battleship.
*/
public class BattleshipDisplay extends java.applet.Applet implements MouseListener{
//For the GUI
/**
* The game that is being displayed
*/
Battleship bat;
/**
* The status to display after the user's guess
*/
String stat; //the status to display after each guess
/**
* Variable is set to true when user finishes
*/
boolean finish;
/**
* Initialize the applet. Creates a new Battleship game.
*/
public void init(){ //initialize the applet
bat=new Battleship();
stat=new String();
finish=false;
addMouseListener(this); //add to respond to mouseClicks
}
/**
* Method to paint on the applet.
* Displays the user's guess board and the status of computer's ships.
*/
public void paint(Graphics g){ //Method that paints on screen
g.setColor(Color.BLACK);
g.fillRect(0,0,600,320); //Clear the screen
if(!finish){
g.setColor(Color.WHITE);
g.drawString(stat,420,40); //display status of guess
int cstat[]; //obtain status of computer's ships
cstat=bat.compStat();
//display ship details and current status (number of hits)
g.drawString("Patrol Boat",420,140);
g.drawString(": 2 holes; "+cstat[0]+" hit",500, 140);
g.drawString("Destroyer",420,160);
g.drawString(": 3 holes; "+cstat[1]+" hit",500, 160);
g.drawString("Submarine",420,180);
g.drawString(": 3 holes; "+cstat[2]+" hit",500, 180);
g.drawString("Battleship",420,200);
g.drawString(": 4 holes; "+cstat[3]+" hit",500, 200);
g.drawString("Carrier",420,220);
g.drawString(": 5 holes; "+cstat[4]+" hit",500, 220);
g.drawString("# of Guesses = " + bat.numUserGuess(),420, 280);
//draws the guess board : 10 X 10 square
for (int i=0; i<11; i++)
g.drawLine(20,20+i*28, 300, 20+i*28);
for (int i=0; i<11; i++)
g.drawLine(20+i*28,20,20+i*28,300);
char ch='0';
for (int i=0; i<10; i++){ //marks column coordinates on top
g.drawString(ch+" ",30+28*i,13);
ch++;
}
ch='0';
for (int i=0; i<10; i++){ //marks row coordinates on side
g.drawString(ch+" ",10,40+28*i);
ch++;
}
int sqStat=0;
for (int r=0; r<10; r++) //marks the holes on user's guessboard
for (int c=0; c<10;c++){
sqStat=bat.guessStatus(new Location(r,c));
if (sqStat==2) {
g.setColor(Color.RED); //a location that was hit
g.fillOval(c*28+30, r*28+30, 10,10);
}
else if (sqStat==3) {
g.setColor(Color.WHITE); //a location guessed, but not hit
g.fillOval(c*28+30, r*28+30, 10,10);
}
}
}
else{ //user has finished the game
g.setColor(Color.WHITE);
g.drawString("Congrats! You have successfully finished the game", 170,150);
g.drawString("It took you "+ bat.numUserGuess() +" guesses",190,180);
}
}
/**
* Responds to mouse clicks. Carries out the user's guess.
*/
public void mouseClicked(MouseEvent e){ //responds to mouseClicks - user's guess
Graphics g=getGraphics();
int r,c,result;
Location uguess;
int cy=e.getY();
int cx=e.getX();
if(cx>20 && cx<300 && cy>20 && cy<300){ //user clicked on the board
r=(cy-20)/28;
c=(cx-20)/28;
uguess=new Location(r,c); //convert screen coordinates to location on board
if(bat.guessStatus(uguess)!=1)
stat="Already guessed"; //location has been guessed before
else{
result=bat.userGuess(uguess); //carry out the user's guess : result holds which ship (if any) is hit
switch(result){
case 0 : stat="Patrol Boat hit";break;
case 1 : stat="Destroyer hit";break;
case 2 : stat="Submarine hit";break;
case 3 : stat="Battleship hit";break;
case 4 : stat="Carrier hit";break;
case 5 : stat="You missed";break;
}
}
finish=bat.win(); //if the game is over
paint(g); //redisplay - with the changes
}
}
public void mouseReleased(MouseEvent e){
}
public void mouseEntered(MouseEvent e){
}
public void mouseExited(MouseEvent e){
}
public void mousePressed(MouseEvent e){
}
};
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -