Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.Color;
- import java.awt.Font;
- import java.awt.GridLayout;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import javax.swing.JButton;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- import javax.swing.JOptionPane;
- import javax.swing.JPanel;
- import java.util.Random;
- public class Game extends JFrame implements ActionListener{
- JLabel lblplayer = new JLabel("Player Score: ");
- JLabel lblai = new JLabel("Artificial Intelligence: ");
- JLabel lblplayers = new JLabel("0");
- JLabel lblais = new JLabel("0");
- int style = Font.BOLD | Font.ITALIC;
- Font font = new Font ("Garamond", style , 30);
- JButton[][]boxes = new JButton[12][12];
- static Random rand = new Random();
- JPanel board = new JPanel();
- JPanel Scorecard = new JPanel();
- static int aiboard[][] = new int[12][12];
- int userBoard[][] = new int[12][12];
- int playerScore = 0;
- int aiScore = 0;
- int CarrierCount = 0;
- int CruiserCount = 0;
- int DestroyerCount =0;
- int SubmarineCount = 0;
- boolean isplaying = false;
- boolean isplacingCarrier = false;
- boolean isplacingCruiser = false;
- boolean isplacingDestroyer = false;
- boolean isplacingSubmarine = true;
- int aiturns[][] = new int[12][12];
- int plturns[][] = new int[12][12];
- final int subID = 50; //just to tel that its a submarine
- final int desID = 51; //just to tel that its a destroyer
- final int cruiserID = 52; //just to tel that its a cruiser
- final int carriersubID = 53; //just to tel that its a carrier
- public Game(){
- super("Battleship Game");
- setSize(1200,700);
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- setResizable(false);
- setLocationRelativeTo(null);
- lblplayer.setFont(font);
- lblplayers.setFont(font);
- lblai.setFont(font);
- lblais.setFont(font);
- board.setLayout(new GridLayout(12,12));
- for(int row = 0; row<12 ; row++){
- for (int col = 0; col < 12; col++){
- boxes[row][col] = new JButton("");
- board.add(boxes[row][col]);
- boxes[row][col].setBackground(Color.CYAN);
- boxes[row][col].addActionListener(this);
- }
- }
- board.setBackground(Color.BLUE);
- Scorecard.setLayout(new GridLayout(1,4,5,5));
- Scorecard.add(lblplayer);
- Scorecard.add(lblplayers);
- Scorecard.add(lblai);
- Scorecard.add(lblais);
- add("North", Scorecard);
- add("Center", board);
- setVisible(true);
- }
- public static void main(String[] args) {
- new Game();
- createAi();
- JOptionPane.showMessageDialog(null, "Welcome to BAttleship Game XD");
- JOptionPane.showMessageDialog(null, "Place your three one-decker Submarines!");
- }
- @Override
- public void actionPerformed(ActionEvent e) {
- if(isplaying == false){
- for(int row = 0; row<12 ; row++){
- for (int col = 0; col < 12; col++){
- if(e.getSource() == boxes[row][col]){
- if(isplacingSubmarine == true && SubmarineCount<3){
- placeShip(row, col, 1, true, subID);
- SubmarineCount++;
- }else{
- isplacingSubmarine = false;
- isplacingDestroyer = true;
- if(isplacingDestroyer == true && DestroyerCount<2){
- Object[] options = {"Vertical","Horizontal"};
- int n = JOptionPane.showOptionDialog(this,
- "Please choose Orientation for Destroyer",
- "Battle Ship Game",JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE,null,options, options[0]);
- if(n == 0){
- placeShip(row, col, 2, true, desID);
- }else{
- placeShip(row, col, 2, false, desID);
- }
- DestroyerCount++;
- }else{
- isplacingDestroyer = false;
- isplacingCruiser = true;
- if(isplacingCruiser == true && CruiserCount<1){
- Object[] options = {"Vertical","Horizontal"};
- int n = JOptionPane.showOptionDialog(this,
- "Please choose Orientation for Cruiser",
- "Battle Ship Game",JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE,null,options, options[0]);
- if(n == 0){
- placeShip(row, col, 3, true, cruiserID);
- }else{
- placeShip(row, col, 3, false, cruiserID);
- }
- CruiserCount++;
- }else{
- isplacingCruiser = false;
- isplacingCarrier = true;
- if(isplacingCarrier == true && CarrierCount<1){
- Object[] options = {"Vertical","Horizontal"};
- int n = JOptionPane.showOptionDialog(this,
- "Please choose Orientation for Carrier",
- "Battle Ship Game",JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE,null,options, options[0]);
- if(n == 0){
- placeShip(row, col, 5, true, carriersubID);
- }else{
- placeShip(row, col, 5, false, carriersubID);
- }
- CarrierCount++;
- }else{
- Object[] options = {"Lets Get it On!","Wait a minute!"};
- int n = JOptionPane.showOptionDialog(this,
- "Do you want to start the game ?",
- "Battle Ship Game",JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE,null,options, options[0]);
- if(n == 0){
- isplaying = true;
- }
- }
- }
- }
- }
- break;
- }
- }
- }
- }else if(isplaying == true){
- for(int row = 0; row<12 ; row++){
- for (int col = 0; col < 12; col++){
- if(e.getSource() == boxes[row][col]){
- if(plturns[row][col] == 0){
- boxes[row][col].setBackground(Color.ORANGE);
- boxes[row][col].setText(boxes[row][col].getText() + "(PL)");
- if(aiboard[row][col] != 0){
- playerScore++;
- }
- plturns[row][col] = 1;
- AiMove();
- lblplayers.setText(""+playerScore);
- lblais.setText(""+aiScore);
- }else{
- JOptionPane.showMessageDialog(null, "Please Choose another Block!");
- }
- }
- }
- }
- }
- if(playerScore == 15){
- Object[] options = {"Restart the Game!","Exit"};
- int n = JOptionPane.showOptionDialog(this,
- "You win, Do yah wanna Restart the Game?",
- "Battle Ship Game",JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE,null,options, options[0]);
- if(n == 0){
- restartGame();
- }else{
- JOptionPane.showMessageDialog(null, "Thank You for Playing");
- System.exit(1);
- }
- }else if(aiScore == 15){
- Object[] options = {"Restart the Game!","Exit"};
- int n = JOptionPane.showOptionDialog(this,
- "You Lose, Do yah wanna Restart the Game?",
- "Battle Ship Game",JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE,null,options, options[0]);
- if(n == 0){
- restartGame();
- }else{
- JOptionPane.showMessageDialog(null, "Thank You for Playing");
- System.exit(1);
- }
- }
- }
- private void restartGame(){
- aiboard = new int[12][12];
- userBoard = new int[12][12];
- plturns = new int[12][12];
- aiturns = new int[12][12];
- playerScore = 0;
- aiScore = 0;
- CarrierCount = 0;
- CruiserCount = 0;
- DestroyerCount =0;
- SubmarineCount = 0;
- isplaying = false;
- isplacingCarrier = false;
- isplacingCruiser = false;
- isplacingDestroyer = false;
- isplacingSubmarine = true;
- for(int row = 0; row<12 ; row++){
- for (int col = 0; col < 12; col++){
- boxes[row][col].setBackground(Color.CYAN);
- boxes[row][col].setText("");
- }
- }
- }
- private void AiMove() {
- boolean valid = false;
- do{
- int row = rand.nextInt(12);
- int col = rand.nextInt(12);
- if(aiturns[row][col] == 0){
- boxes[row][col].setBackground(Color.MAGENTA);
- boxes[row][col].setText(boxes[row][col].getText() + "(AI)");
- if(userBoard[row][col] != 0){
- aiScore++;
- }
- valid = true;
- }
- }while(valid == false);
- }
- private static void createAi(){
- createAiSubmarine();
- createAiShips(11, 12, 2, 2, 51);
- createAiShips(10, 12, 3, 1, 52);
- createAiShips(8, 12, 5, 1, 53);
- for(int r = 0; r<12 ; r++){
- for (int c = 0; c < 12; c++){
- System.out.print(aiboard[r][c] +"\t" );
- }
- System.out.println();
- }
- }
- private static void createAiSubmarine(){
- int row;
- int col;
- for(int i = 0 ; i <3 ;i++){ // generates AI's submarines XD
- row = +rand.nextInt(12);
- col = +rand.nextInt(12);
- if(aiboard[row][col] !=0 ){
- i--;
- }else{
- aiboard[row][col] = 50;
- }
- }
- }
- private static void createAiShips(int lrow, int lcol, int noOfDeck,int noOfship, int id){
- boolean isVertical;
- int row;
- int col;
- for(int i = 0 ; i <noOfship ;i++){
- isVertical = rand.nextBoolean();
- boolean valid = true;
- if(isVertical){
- row = rand.nextInt(lrow);
- col = rand.nextInt(lcol);
- for(int i1 = 0; i1 < noOfDeck; i1++){
- if(aiboard[row + i1][col] !=0){
- valid = false;
- break;
- }
- }
- if(valid){
- for(int i1 = 0; i1 < noOfDeck; i1++){
- aiboard[row + i1][col] = id;
- }
- }else{
- i--;
- }
- }else{
- row = rand.nextInt(lcol);
- col = rand.nextInt(lrow);
- for(int i1 = 0; i1 < noOfDeck; i1++){
- if(aiboard[row][col + i1] !=0){
- valid = false;
- break;
- }
- }
- if(valid){
- for(int i1 = 0; i1 < noOfDeck; i1++){
- aiboard[row][col + i1] = id;
- }
- }else{
- i--;
- }
- }
- }
- }
- private void placeShip(int row, int col, int noOFDecks, boolean isVertical, int boatID){
- if(isVertical){
- if(boatID == subID){
- if(userBoard[row][col] != 0){
- JOptionPane.showMessageDialog(null, "Cant Place there!");
- SubmarineCount--;
- }else{
- boxes[row][col].setText("Submarine");
- boxes[row][col].setBackground(Color.RED);
- userBoard[row][col] = subID;
- }
- }else if(boatID == carriersubID){
- boolean valid = true;
- if(col < 12 && row < 8){
- for(int i = 0; i < noOFDecks; i++){
- if(userBoard[row + i][col] !=0){
- valid = false;
- break;
- }
- }
- if(valid){
- for(int i = 0; i < noOFDecks; i++){
- boxes[row + i][col].setText("Carrier");
- boxes[row + i][col].setBackground(Color.GRAY);
- userBoard[row + i][col] = carriersubID;
- }
- }else{
- JOptionPane.showMessageDialog(null, "Cant Place there!");
- CarrierCount--;
- }
- }else{
- JOptionPane.showMessageDialog(null, "Cant Place there!");
- CarrierCount--;
- }
- }else if(boatID == desID){
- boolean valid = true;
- if(col < 12 && row < 11){
- for(int i = 0; i < noOFDecks; i++){
- if(userBoard[row + i][col] !=0){
- valid = false;
- break;
- }
- }
- if(valid){
- for(int i = 0; i < noOFDecks; i++){
- boxes[row + i][col].setText("Destroyer");
- boxes[row + i][col].setBackground(Color.PINK);
- userBoard[row + i][col] = desID;
- }
- }else{
- JOptionPane.showMessageDialog(null, "Cant Place there!");
- DestroyerCount--;
- }
- }else{
- JOptionPane.showMessageDialog(null, "Cant Place there!");
- DestroyerCount--;
- }
- }else if(boatID == cruiserID){
- boolean valid = true;
- if(col < 12 && row < 10){
- for(int i = 0; i < noOFDecks; i++){
- if(userBoard[row + i][col] !=0){
- valid = false;
- break;
- }
- }
- if(valid){
- for(int i = 0; i < noOFDecks; i++){
- boxes[row + i][col].setText("Cruiser");
- boxes[row + i][col].setBackground(Color.GREEN);
- userBoard[row + i][col] = cruiserID;
- }
- }else{
- JOptionPane.showMessageDialog(null, "Cant Place there!");
- CruiserCount--;
- }
- }else{
- JOptionPane.showMessageDialog(null, "Cant Place there!");
- CruiserCount--;
- }
- }
- }else{
- if(boatID == subID){
- if(userBoard[row][col] != 0){
- JOptionPane.showMessageDialog(null, "Cant Place there!");
- SubmarineCount--;
- }else{
- boxes[row][col].setText("Submarine");
- boxes[row][col].setBackground(Color.RED);
- userBoard[row][col] = subID;
- }
- }else if(boatID == carriersubID){
- boolean valid = true;
- if(col < 8 && row < 12){
- for(int i = 0; i < noOFDecks; i++){
- if(userBoard[row][col + i] !=0){
- valid = false;
- break;
- }
- }
- if(valid){
- for(int i = 0; i < noOFDecks; i++){
- boxes[row][col + i].setText("Carrier");
- boxes[row][col + i].setBackground(Color.GRAY);
- userBoard[row][col + i] = carriersubID;
- }
- }else{
- JOptionPane.showMessageDialog(null, "Cant Place there!");
- CarrierCount--;
- }
- }else{
- JOptionPane.showMessageDialog(null, "Cant Place there!");
- CarrierCount--;
- }
- }else if(boatID == desID){
- boolean valid = true;
- if(col < 11 && row < 12){
- for(int i = 0; i < noOFDecks; i++){
- if(userBoard[row][col + i] !=0){
- valid = false;
- break;
- }
- }
- if(valid){
- for(int i = 0; i < noOFDecks; i++){
- boxes[row][col + i].setText("Destroyer");
- boxes[row][col + i].setBackground(Color.PINK);
- userBoard[row][col + i] = desID;
- }
- }else{
- JOptionPane.showMessageDialog(null, "Cant Place there!");
- DestroyerCount--;
- }
- }else{
- JOptionPane.showMessageDialog(null, "Cant Place there!");
- DestroyerCount--;
- }
- }else if(boatID == cruiserID){
- boolean valid = true;
- if(col < 10 && row < 12){
- for(int i = 0; i < noOFDecks; i++){
- if(userBoard[row][col + i] !=0){
- valid = false;
- break;
- }
- }
- if(valid){
- for(int i = 0; i < noOFDecks; i++){
- boxes[row][col + i].setText("Cruiser");
- boxes[row][col + i].setBackground(Color.GREEN);
- userBoard[row][col + i] = cruiserID;
- }
- }else{
- JOptionPane.showMessageDialog(null, "Cant Place there!");
- CruiserCount--;
- }
- }else{
- JOptionPane.showMessageDialog(null, "Cant Place there!");
- CruiserCount--;
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement