Skip to content
This repository has been archived by the owner on Apr 25, 2020. It is now read-only.

Commit

Permalink
Important* I have just heard from other student in p5 Mr.A class sayi…
Browse files Browse the repository at this point in the history
…ng not allowed to place other group's ship on our program.

So the gui style has changed (to become super convenient to our player). When we are doing tournament, just select "AI combat" in the first popup window when starts the game.
  • Loading branch information
yichunShen committed Jun 14, 2018
1 parent b30c05a commit 24309b6
Show file tree
Hide file tree
Showing 9 changed files with 80 additions and 33 deletions.
Binary file removed gBattleship.png
Binary file not shown.
Binary file removed gCarrier.png
Binary file not shown.
Binary file removed gCruiser.png
Binary file not shown.
Binary file removed gDestroyer.png
Binary file not shown.
Binary file added gResponse.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added gSeparator.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed gSubmarine.png
Binary file not shown.
10 changes: 7 additions & 3 deletions src/game.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public class game extends JPanel{
JLabel bgi = new JLabel(new ImageIcon("gameBgi.png"));
JLabel timerBoard = new JLabel(new ImageIcon("gTimer.png"));
static JLabel timerLabel = new JLabel("00:00:00");
static JLabel mapSeparator = new JLabel(new ImageIcon("gSeparator.png"));
static JLabel uHit = new JLabel("0");
static JLabel uMis = new JLabel("0");
static JLabel eHit = new JLabel("0");
Expand All @@ -36,7 +37,7 @@ public class game extends JPanel{
JLabel enemBoard = new JLabel(new ImageIcon("gEnemySide.png"));
String[] labelStr = {"1","2","3","4","5","6","7","8","9","10","A","B","C","D","E","F","G","H","I","J"};
final static Color darkBlue = new Color(0,0,40);
final static Color fogBlue = new Color(0,0,80);
final static Color fogBlue = new Color(80,80,180);
final static Color darkRed = new Color(150,40,40);
static JLabel[][] userMap = new JLabel[10][10];
static JLabel[][] enemMap = new JLabel[10][10];
Expand Down Expand Up @@ -101,6 +102,9 @@ public void mouseClicked(MouseEvent e) {
if(uHit.getText().equals("17") ||eHit.getText().equals("17")){
system.checkWin();
}
if(system.AIcombat){
return;
}
}
if(userTurn&&system.inGame){
JLabel source = (JLabel)e.getSource();
Expand Down Expand Up @@ -151,8 +155,8 @@ public void mouseReleased(MouseEvent e) {
public game(){
setSize(1300,700);
setLayout(null);
// system.askUser.setBounds(bInsets.left+150,bInsets.top+150,500,500);
// add(system.askUser);
mapSeparator.setBounds(bInsets.left+647,bInsets.top+125,6,525);
add(mapSeparator);
winWord.setBounds(bInsets.left,bInsets.top+190,1300,300);
winWord.addMouseListener(hide);
add(winWord);
Expand Down
103 changes: 73 additions & 30 deletions src/system.java
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,6 @@ public void mouseClicked(MouseEvent e) {
} catch (IOException e1) {
e1.printStackTrace();
}
// remove drag functions, start timer
}
}

Expand Down Expand Up @@ -211,32 +210,38 @@ public void windowClosing(java.awt.event.WindowEvent windowEvent) {

public void enterGame() {
if (!inGame) {
// Object[] modeOptions = { "AI Combat", "Human against AI" };
// do {
// int firsthand = JOptionPane.showOptionDialog(null, "Choose a game mode", "Game mode",
// JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, new ImageIcon("gNani.png"), modeOptions,
// modeOptions[0]);
// if (firsthand == 0) {
// firstHand = "User";
// game.userTurn = true;
// } else {
// firstHand = "AI";
// game.userTurn = false;
// }
// } while (!areYouSure(firstHand));
Object[] modeOptions = { "AI Combat", "Human against AI" };
int modeIndex;
do {
Object[] options = { "User", "AI" };
int firsthand = JOptionPane.showOptionDialog(null, "Battle started from:", "First hand",
JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, new ImageIcon("gNani.png"), options,
options[0]);
if (firsthand == 0) {
modeIndex = JOptionPane.showOptionDialog(null, "Choose a game mode", "Game mode",
JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, new ImageIcon("gNani.png"), modeOptions,
modeOptions[0]);
AIcombat = (modeIndex==0);
} while (!areYouSure(modeOptions[modeIndex].toString()));
Object[] firstHandOptions = { "User", "AI" };
if(AIcombat){
for(int i =0;i<10;i++){
for(int j =0;j<10;j++){
game.userMap[i][j].setBackground(Color.white);
game.userMap[i][j].removeMouseListener(gameInter.unitDis);
}
}
firstHandOptions[0] = "Them";
firstHandOptions[1] = "Us";
}
int firsthandIndex;
do {
firsthandIndex = JOptionPane.showOptionDialog(null, "Battle started from:", "First hand",
JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, new ImageIcon("gNani.png"), firstHandOptions,
firstHandOptions[0]);
if (firsthandIndex == 0) {
firstHand = "User";
game.userTurn = true;
} else {
firstHand = "AI";
game.userTurn = false;
}
} while (!areYouSure(firstHand));
} while (!areYouSure(firstHandOptions[firsthandIndex].toString()));
Object[] diOptions = { "Expert - Raymond", "Random", "Expert - David","Extreme" };
do {
difficulty = JOptionPane.showOptionDialog(null, "Choose a difficulty level:", "Difficulty",
Expand Down Expand Up @@ -459,14 +464,24 @@ public static String convertMessage(int x, int y) {
return (Character.toString((char) (y + 65)) + Integer.toString(x + 1)).toUpperCase();
}

public static void fireResult(String result) {
public static void fireResult(String result, int nameIndex) {
if(AIcombat){
JOptionPane.showMessageDialog(null,
"That was: "+result,
"Response to other AI",
JOptionPane.INFORMATION_MESSAGE,
new ImageIcon("gResponse.png"));
}
game.userTurn = false;
if (result.equals("MISS")) {
game.countIncre(game.uMis);
game.enemMap[y][x].setBackground(game.darkBlue);
} else {
game.countIncre(game.uHit);
game.enemMap[y][x].setBackground(game.darkRed);
game.enemMap[y][x].setForeground(Color.white);
game.enemMap[y][x].setHorizontalAlignment(JLabel.CENTER);
game.enemMap[y][x].setText(result.substring(nameIndex,nameIndex+2));
}
}

Expand Down Expand Up @@ -528,7 +543,34 @@ public static void game() throws IOException {
Battleship.shipLengths[i]);
}

public static String getFire(int y, int x) {
public static String getFire(int y, int x, String AIcoor) {
if(AIcombat){
Object[] reOptions = { "MISS", "HIT", "SUNK" };
int responseIndex = -1;
do {
responseIndex = JOptionPane.showOptionDialog(null, "Shot: "+AIcoor, "Get Response",
JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, new ImageIcon("gNani.png"), reOptions,
reOptions[0]);
} while (!areYouSure(reOptions[responseIndex].toString()));
if(responseIndex==0){
game.countIncre(game.eMis);
return "MISS";
}else{
game.countIncre(game.eHit);
String ultResponse = "HIT, ";
if(responseIndex==2){
ultResponse += "SUNK ";
}
Object[] hitShipOptions = Battleship.shipNames;
int hitShipIndex = -1;
do {
hitShipIndex = JOptionPane.showOptionDialog(null, "Response from other AI?", "Get Response",
JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, new ImageIcon("gNani.png"), hitShipOptions,
hitShipOptions[0]);
} while (!areYouSure(hitShipOptions[hitShipIndex].toString()));
return ultResponse+Battleship.shipNames[hitShipIndex];
}
}
Color unitStatus = game.userMap[x][y].getBackground();
if (unitStatus.getBlue()==0) {
String shipName = "";
Expand Down Expand Up @@ -640,7 +682,7 @@ public static void AIcheck(boolean isNotFirst) {
if (userShot.shipType == null) {// Miss
userShot.status = SquareTypes.MISS;
System.out.println("MISS");
fireResult("MISS");
fireResult("MISS",-1);
} else {// Hit
Battleship.homeGrid[y][x].status = SquareTypes.HIT;
for (int i = 0; i < Battleship.homeShips.length; i++)
Expand All @@ -665,7 +707,7 @@ public static void AIcheck(boolean isNotFirst) {
Battleship.homeShipsSunk[shipNumber] = false;
if (Battleship.homeShipsSunk[shipNumber]) {
System.out.println("HIT, SUNK " + Battleship.homeShips[shipNumber].shipName);
fireResult("HIT");
fireResult("HIT, SUNK " + Battleship.homeShips[shipNumber].shipName,10);

// Check for win (all ships sunk)
flag = true;
Expand All @@ -678,11 +720,11 @@ public static void AIcheck(boolean isNotFirst) {
userWin = true;
} else{
System.out.println("HIT, " + ship.shipName);
fireResult("HIT");
fireResult("HIT, " + ship.shipName,5);
}
} else {
System.out.println("HIT, " + ship.shipName);
fireResult("HIT");
fireResult("HIT, " + ship.shipName,5);
}
}
Battleship.enemyShotLog.add(Battleship.homeGrid[y][x]);// Add enemy shot to log
Expand All @@ -695,14 +737,15 @@ public static void AIRound() {
AIShot = Amadeus.aim(AIShot, Battleship.enemyGrid, Battleship.shipLengths);
Battleship.displayPD(Battleship.enemyGrid);
Battleship.homeShotLog.add(AIShot);// Add home shot to log

String AIshot = (Character.toString((char) (AIShot.y + 65)) + Integer.toString(AIShot.x + 1)).toUpperCase();
// Print AI's shot's y-x coordinate converted to Battleship standards (e.g. A1)
System.out.println("AI's shot coordinates: "
+ (Character.toString((char) (AIShot.y + 65)) + Integer.toString(AIShot.x + 1)).toUpperCase());
+ AIshot);
// Get user's response
System.out.println("HIT or MISS?");
input = getFire(AIShot.x, AIShot.y).toUpperCase();
input = getFire(AIShot.x, AIShot.y, AIshot).toUpperCase();
System.out.println("getInput--------------: "+input);

// AI hit a ship
if (input.contains("HIT")) {
AIShot.status = SquareTypes.HIT;
Expand All @@ -726,7 +769,7 @@ public static void AIRound() {
break;
}
Battleship.enemyShipsSunk[temp] = true;

// Check if AI has won
AIWin = true;
for (boolean b : Battleship.enemyShipsSunk)
Expand Down

0 comments on commit 24309b6

Please sign in to comment.