// Battleship modified November 5, 1996 // By David Dao // Project Part (2) to play across net // // Original JAVA code to play BATTLESHIP - April 26, 1995 // By Nathaniel G. Auvil and Patrick Gillisse // // CSCI 406 Internet Programming for Dr. Roger Webster import java.applet.*; import java.awt.*; import java.io.*; import java.util.*; import java.lang.*; import java.net.*; //==================================================================== public class netBattle extends Applet implements Runnable{ final int max=10; final int num_ships=5; final String blanks=" "; AudioClip blowup, sinking, greatshotkid, taps; boolean game_starting = false; // set to true to begin game boolean first_point = true; // flag for ship placement boolean game_over = false; boolean p1_ships_placed = false; boolean start_firing = false; boolean otherPlayer_turn = false; // tells if player1 is up boolean ship_hit = false; boolean recieved_board = false; // flag for streamlistner String opponent_move; // buffer to hold opponent move int com_x, com_y, start_x, start_y; int[][] ship_loc= new int [4][2]; // position for hit ships char[][] ship_info= new char[4][2]; char dir_found='N'; int ships_found=0; char ship_letter= ' '; // holds letter value of hit ship Panel player1, player2; Button[][] play1 = new Button[10][10]; Button[][] play2 = new Button [10][10]; char p1_grid[][]= new char [max][max]; char p2_grid[][]= new char [max][max]; int placed_ships=5; int firstx, firsty; TextArea display= new TextArea(2, 60); String message= "Player 1 enter starting point of aircraft carrier"; int num_players; int sunk_player1=0, sunk_player2=0, hit_ship1[]= new int [num_ships], hit_ship2[]= new int [num_ships]; Thread thread; // create a thread to run in // variables needed for net window String param; NCWindow window; // the popup window to do the internet work Image[] images; // array of images to be loaded MediaTracker tracker; // a media tracker to keep track of loading progress //######################################################################### // initialize variables needed for game public void init() { String s_num_players= getParameter("num_players"); num_players= Integer.parseInt(s_num_players); sinking= getAudioClip(getCodeBase(), "going_down.au"); blowup= getAudioClip(getCodeBase(), "blowup.au"); greatshotkid= getAudioClip(getCodeBase(), "greatshotkid.au"); // taps= getAudioClip(getCodeBase(), "taps.au"); player1 = new Panel(); // create a panel player2 = new Panel(); player1.resize(150,150); player2.resize(250,250); add(player1); add(player2); add(display); //this is for message display display.setEditable(false); display.insertText(message, 0); player1.setBackground(Color.blue); player1.setLayout(new GridLayout(10, 10, 1, 1)); player2.setBackground(Color.red); player2.setLayout(new GridLayout(10, 10, 1, 1)); showStatus("Creating battleship grid"); for(int i =0; i(max-1)) || (area[x][i] != '-')) ret_val=false; } break; case 3: for(i=y; i>(y-placed_ships); i--) { if ((i<0) || (area[x][i] != '-')) ret_val=false; } break; case 0: for(i=x; i>(x-placed_ships); i--) { if ((i<0) || (area[i][y] != '-')) ret_val=false; } break; case 2: for(i=x; i<(x+placed_ships); i++) { if ((i>(max-1)) || (area[i][y] != '-')) ret_val=false; } break; } // end of switch (dir) return ret_val; } //######################################################################### // the x and y should be reversed public void human_place_ships(Button b[][], char area[][], int y, int x) { if ((area[x][y] == '-') && (first_point) && (usable_spot(area, x, y))) { switch(placed_ships) { case 5: { b[x][y].setLabel(ship_letter()); area[x][y]=ship_letterc(); message_out("Place endpoint of aircraft carrier(5 spaces long)"); first_point=false; break; } case 4: { b[x][y].setLabel(ship_letter()); area[x][y]=ship_letterc(); message_out("Place endpoint of battleship(4 spaces long)"); first_point=false; break; } case 3: { b[x][y].setLabel(ship_letter()); area[x][y]=ship_letterc(); message_out("Place endpoint of submarine(3 spaces long)"); first_point=false; break; } case 2: { b[x][y].setLabel(ship_letter()); area[x][y]=ship_letterc(); message_out("Place endpoint of PT boat(2 spaces long)"); first_point=false; break; } case 1: { b[x][y].setLabel(ship_letter()); area[x][y]=ship_letterc(); placed_ships--; retag_buttons(b); //reset all button labels break; } } //end of switch statement firstx=x; //store the initial position firsty=y; } //********after first point of ship is placed. else if ((area[x][y] == '-') && !(first_point)) { if (check_xy(b, area, x, y)) { first_point=true; placed_ships--; //ship placed successfully message_out("Place starting point of " + ship_name()); } } else { message_out("Location is unavailable. Try again."); } } //######################################################################### // This checks if the placement is legal public boolean check_xy(Button b[][], char area[][], int x, int y) { boolean result=true; if (firsty==y) { if (firstx+(placed_ships-1)==x) insert_down(b, area, x, y); else if (firstx-(placed_ships-1)==x) insert_up(b, area, x, y); else result=false; } else if (firstx==x) { if (firsty+(placed_ships-1)==y) insert_right(b, area, x, y); else if (firsty-(placed_ships-1)==y) insert_left(b, area, x, y); else result=false; } else { //otherwise it is not legal so need new end-point result= false; } if (!(result)) message_out("Endpoint is not legal. Try again."); return result; } //######################################################################### // This method returns the ship String for corresponding placed_ships value public String ship_letter() { String letter=" "; switch(placed_ships) { case 1: letter="H"; break; case 2: letter="P"; break; case 3: letter="S"; break; case 4: letter="B"; break; case 5: letter="A"; break; } return letter; } //######################################################################### // This method returns the ship String for corresponding placed_ships value public char ship_letterc() { char letter=' '; switch(placed_ships) { case 1: letter='H'; break; case 2: letter='P'; break; case 3: letter='S'; break; case 4: letter='B'; break; case 5: letter='A'; break; } return letter; } //######################################################################### public void insert_down(Button b[][], char area[][], int x, int y) { int i; for(i=firstx; i<=x; i++) { b[i][firsty].setLabel(ship_letter()); area[i][firsty]=ship_letterc(); } } //######################################################################### public void insert_up(Button b[][], char area[][], int x, int y) { int i; for(i=firstx; i>=x; i--) { b[i][firsty].setLabel(ship_letter()); area[i][firsty]=ship_letterc(); } } //######################################################################### public void insert_right(Button b[][], char area[][], int x, int y) { int i; for(i=firsty; i<=y; i++) { b[firstx][i].setLabel(ship_letter()); area[firstx][i]=ship_letterc(); } } //######################################################################### public void insert_left(Button b[][], char area[][], int x, int y) { int i; for(i=firsty; i>=y; i--) { b[firstx][i].setLabel(ship_letter()); area[firstx][i]=ship_letterc(); } } //######################################################################### // This method reveals the playing fields at the end of the game public void reveal(Button[][] b, char[][] area) { int ii, jj; for (ii=0; ii