Senin, 10 Juni 2013

Membuat Aplikasi Game Domino Dengan Java



TUGAS Besar Sistem Client Server

Game domino Dengan Netbeans java..















/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Domino;
import java.util.*;

import javax.swing.JOptionPane;

class Variables {


   static Dice dice=new Dice (49);

   static tiles tile=new tiles();

   static String [] character=new String[3];

   static int charcount=0;

   static int player1[][]=new int[2][49];

   static int player2[][]=new int[2][49];

   static int dicevalue;

   static boolean check;

   static boolean checkroll;

   static boolean checkroll1;

   static boolean checkgame=true;

   static int highhead1=0;

   static int highhead2=0;

   static int hightail1=0;

   static int hightail2=0;

   static int interval[][]=new int[2][98];

   static int x2=49;

   static int y2=49;
   static int xx2=49;

   static int yy2=49;

   static int toplace;

   static int counter1;

   static int counterplayer1;

   static int counterplayer2;

   static int temp;

}



 class Dice {

    Random r =new Random();

    int sides;

 //constructor

    public Dice(int s){

       sides = s;

    }

 //roll method depending on sides up there

    public int roll(){



    return r.nextInt(sides);

    }

}



 class tiles {

  static int headcount=0;

  static int tailcount=0;

  static int a=0;

  static int b=0;

  int handler[][]=new int [2][49];

    public void dominotiles(){

        for(int x=0;x<49;x++){

            for(int y=0; y<2; y++){

                if(y==0){

                    if(headcount==7){

                        headcount=0;

                        a=a+1;

                    }

                    if(headcount<=6){

                    handler[y][x]=a;

                    headcount=headcount+1;

                    }

                }

                else {

                    if(tailcount==7){

                        tailcount=0;

                    }

                    if(tailcount<=6){

                     handler[y][x]=tailcount;

                    tailcount=tailcount+1;

                    }

                }

            }

        }

    }

}



public class  Gapleh extends Variables {

    public static void main(String[]args){

      tile.dominotiles();

      for(int x=0;x<49;x++){

        for(int y=0; y<2; y++){

                    player1[y][x]=7;

                    player2[y][x]=7;

        }

      }

     for(int x=0;x<98;x++)

        for(int y=0;y<2;y++){

                interval[y][x]=7;

        }

      newCharacter();
      newCharacter();
      JOptionPane.showMessageDialog(null,character[0]+" Di Kocok Secara Acak");
      for(int x=0;x<7;x++){
          check=true;
        while(check){
           dicevalue=dice.roll();
            if(tile. handler[0][dicevalue]<7 && tile. handler[1][dicevalue]<7){
                player1[0][dicevalue]=tile. handler[0][dicevalue];
                player1[1][dicevalue]=tile. handler[1][dicevalue];
                tile. handler[0][dicevalue]=7;
                tile. handler[1][dicevalue]=7;
                if(highhead1<player1[0][dicevalue]){

                    highhead1=player1[0][dicevalue];
                    hightail1=player1[1][dicevalue];
                }
                check=false;
            }
        }
      }
            System.out.println(character[0]+"'s tiles");
            for(int x=0;x<49;x++){
                    if(player1[0][x]<7&&player1[1][x]<7){
                        System.out.print("("+player1[0][x]+","+player1[1][x]+")");

                        System.out.println();
                    }
            }
      JOptionPane.showMessageDialog(null,character[1]+" Di Kocok Secara Acak");
      for(int x=0;x<7;x++){
          check=true;
        while(check){
           dicevalue=dice.roll();
            if(tile. handler[0][dicevalue]<7 && tile. handler[1][dicevalue]<7){
                player2[0][dicevalue]=tile. handler[0][dicevalue];
                player2[1][dicevalue]=tile. handler[1][dicevalue];

                tile. handler[0][dicevalue]=7;
                tile. handler[1][dicevalue]=7;
                if(highhead2<player2[0][dicevalue]){
                    highhead2=player2[0][dicevalue];
                    hightail2=player2[1][dicevalue];
                }
                check=false;
            }
        }
      }
            System.out.println(character[1]+"'s tiles");
            for(int x=0;x<49;x++){
                    if(player2[0][x]<7&&player2[1][x]<7){
                        System.out.print(player2[0][x]+"\t"+player2[1][x]);
                        System.out.println();
                    }
            }
      if(highhead1>highhead2){
          JOptionPane.showMessageDialog(null,character[0]+" Mangga Tipayun");
          game();
      }
      else if(highhead1<highhead2){
          JOptionPane.showMessageDialog(null,character[1]+" Sok Duluan Mainkan");
          character[2]=character[0];
          character[0]=character[1];
          character[1]=character[2];
          for(int x=0;x<49;x++){
              temp=player1[0][x];
              player1[0][x]=player2[0][x];
              player2[0][x]=temp;
              temp=player1[1][x];
              player1[1][x]=player2[1][x];
              player2[1][x]=temp;
          }
          game();
      }
      else{
          if(hightail1>hightail2){
              JOptionPane.showMessageDialog(null,character[0]+" Mangga Tipayun");
              game();
          }
          else{
              JOptionPane.showMessageDialog(null,character[1]+" Sok Duluan Mainkan");
                character[2]=character[0];
                character[0]=character[1];
                character[1]=character[2];
                for(int x=0;x<49;x++){
                    temp=player1[0][x];
                    player1[0][x]=player2[0][x];
                    player2[0][x]=temp;
                    temp=player1[1][x];
                    player1[1][x]=player2[1][x];
                    player2[1][x]=temp;
                }
              game();
          }
      }
            
      
    }
    public static void newCharacter(){
        if (charcount<2){
            character[charcount]=JOptionPane.showInputDialog("Isi Nama:");
            charcount+=1;
        }
    }
    public static void printgamecycle(){
        System.out.println("Aksi Permainan");
        for(int x=0;x<98;x++){
                if(interval[0][x]<7&&interval[0][x]<7)
                System.out.println(interval[0][x]+"\t"+interval[1][x]);
        }
    }
    
    public static void game(){
        check=true;
        while(check){
            while(check){
                try{
                highhead1=Integer.parseInt(JOptionPane.showInputDialog(character[0]+"'s turn\nEnter Masukan angka kartu domino anda Mas :"));
                hightail1=Integer.parseInt(JOptionPane.showInputDialog(character[0]+"'s turn\nEnter Masukan angka kartu domino anda Mas:"));
                check=false;
                }
                catch(NumberFormatException e){
                JOptionPane.showMessageDialog(null,"ga ada Di kartu punya Mu Mas","ERROR",JOptionPane.ERROR_MESSAGE);
                }
            }
            
            if(highhead1<=6&&highhead1>=0&&hightail1<=6&&hightail1>=0){
                   for(int x=0;x<49;x++){
                      if(player1[0][x]==highhead1&&player1[1][x]==hightail1){
                         interval[0][x2]=player1[0][x];
                         interval[1][y2]=player1[1][x];
                         player1[0][x]=7;
                         player1[1][x]=7;
                      }
                  }
                check=false;
            printgamecycle();
            }
            
      }
        
        
while(checkgame){
    counter1=0;
    for(int x=0;x<49;x++){
        if(tile. handler[0][x]==7&&tile. handler[1][x]==7)
            counter1+=1;
    }
    if(counter1==49){
        for(int x=0;x<49;x++){
            if(player1[0][x]<7&&player1[1][x]<7)
                counterplayer1+=1;
            if(player2[0][x]<7&&player2[1][x]<7)
                counterplayer2+=1;
        }
        if(counterplayer1<counterplayer2){
            JOptionPane.showMessageDialog(null,character[0]+"Wins");
            checkgame=false;
            check=false;
            checkroll=false;
            checkroll1=false;
        }
        else if(counterplayer1>counterplayer2){
            JOptionPane.showMessageDialog(null,character[1]+"Wins");
            checkgame=false;
            check=false;
            checkroll=false;
            checkroll1=false;
        }
        else if(counterplayer1==counterplayer2){
            JOptionPane.showMessageDialog(null,"DRAW!!!");
            checkgame=false;
            check=false;
            checkroll=false;
            checkroll1=false;
        }
    }
        while(true){
            check=false;
            checkroll1=true;
            while(checkroll1){
                checkroll=true;
                for(int x=0;x<49;x++){
                    if(highhead1==player2[0][x]||highhead1==player2[1][x]||hightail1==player2[0][x]||hightail1==player2[1][x]){
                        check=true;
                        checkroll=false;
                        checkroll1=false;
                    }
                    else if(counter1==49){
                        check=false;
                        checkroll=false;
                        checkroll1=false;
                    }
                    
                } 
                while(checkroll){
                    dicevalue=dice.roll();
                        if(tile. handler[0][dicevalue]<7 && tile. handler[1][dicevalue]<7){
                            JOptionPane.showMessageDialog(null,character[1]+" chose to pass, required to get new domino from boneyard");
                            player2[0][dicevalue]=tile. handler[0][dicevalue];
                            player2[1][dicevalue]=tile. handler[1][dicevalue];
                            tile. handler[0][dicevalue]=7;
                            tile. handler[1][dicevalue]=7;
                            highhead2=highhead1;
                            hightail2=hightail1;
                            check=false;
                            checkroll=false;
                            checkroll1=false;
                        }
                        counter1=0;
                        for(int x=0;x<49;x++){
                        if(tile. handler[0][x]==7&&tile. handler[1][x]==7)
                        counter1+=1;
                        }
                        if(counter1==49){
                            check=false;
                            checkroll=false;
                            checkroll1=false;
                        }
                 }    
            }
            System.out.println(character[1]+"'s tiles");
            for(int x=0;x<49;x++){
                if(player2[0][x]<7&&player2[1][x]<7)
                System.out.println("("+player2[0][x]+","+player2[1][x]+")");
            }
            while(check){
                while(check){
                try{
                highhead2=Integer.parseInt(JOptionPane.showInputDialog(character[1]+"'s turn\nEnter a head tile number to use:"));
                hightail2=Integer.parseInt(JOptionPane.showInputDialog(character[1]+"'s turn\nEnter a tail tile number to use:"));
                    while(check){
                    toplace=Integer.parseInt(JOptionPane.showInputDialog("Where to place the tiles:\n[0]head portion\n[1]tail portion"));
                    if(toplace==0||toplace==1){
                        check=false;
                    }
                    else
                    JOptionPane.showMessageDialog(null,"WRONG INPUT","Warning",JOptionPane.WARNING_MESSAGE);
                    }
                check=false;
                }
                catch(NumberFormatException e){
                JOptionPane.showMessageDialog(null,"WRONG INPUT","ERROR",JOptionPane.ERROR_MESSAGE);
                }
                }
                check=true;
            if(toplace==0){ 
                if(highhead2==highhead1){
                   for(int x=0;x<49;x++){
                      if(player2[0][x]==highhead2&&player2[1][x]==hightail2){
                         x2-=1;
                         y2-=1;
                         interval[0][x2]=player2[1][x];
                         interval[1][y2]=player2[0][x];
                         highhead2=player2[1][x];
                         hightail2=hightail1;
                         player2[0][x]=7;
                         player2[1][x]=7;
                         check=false;
                         
                      }
                  }
                }
                else if(hightail2==highhead1){
                     for(int x=0;x<49;x++){
                      if(player2[0][x]==highhead2&&player2[1][x]==hightail2){
                         x2-=1;
                         y2-=1;
                         interval[0][x2]=player2[0][x];
                         interval[1][y2]=player2[1][x];
                         hightail2=hightail1;
                         player2[0][x]=7;
                         player2[1][x]=7;
                         check=false;
                      }
                  }
                }
            }
            
            else if(toplace==1){
                if(highhead2==hightail1){
                   for(int x=0;x<49;x++){
                      if(player2[0][x]==highhead2&&player2[1][x]==hightail2){
                         xx2+=1;
                         yy2+=1;
                         interval[0][xx2]=player2[0][x];
                         interval[1][yy2]=player2[1][x];
                         highhead2=highhead1;
                         player2[0][x]=7;
                         player2[1][x]=7;
                         check=false;
                      }
                  }
                }
                else if(hightail2==hightail1){
                    for(int x=0;x<49;x++){
                      if(player2[0][x]==highhead2&&player2[1][x]==hightail2){
                         xx2+=1;
                         yy2+=1;
                         interval[0][xx2]=player2[1][x];
                        interval[1][yy2]=player2[0][x];
                         highhead2=highhead1;
                         hightail2=player2[0][x];
                         player2[0][x]=7;
                         player2[1][x]=7;
                         check=false;
                      }
                  }
                }
            }

            }
            printgamecycle();
                counterplayer2=0;
                for(int x=0;x<49;x++){
                if(player2[0][x]<7&&player2[1][x]<7)
                counterplayer2+=1;
                }
                if(counterplayer2==0){
                JOptionPane.showMessageDialog(null,character[1]+" Wins!!!");
                checkgame=false;
                check=false;
                checkroll=false;
                checkroll1=false;
                }
                else{
                check=false;
                checkroll1=true;
                }
        break;
    }
                
         
        while(true){ 
            while(checkroll1){
                checkroll=true;
                for(int x=0;x<49;x++){
                    if(highhead2==player1[0][x]||highhead2==player1[1][x]||hightail2==player1[0][x]||hightail2==player1[1][x]){
                        check=true;
                        checkroll=false;
                        checkroll1=false;
                    }
                    else if(counter1==49){
                        check=false;
                        checkroll=false;
                        checkroll1=false;
                    }
                }
                while(checkroll){
                    dicevalue=dice.roll();
                        if(tile. handler[0][dicevalue]<7 && tile. handler[1][dicevalue]<7){
                            JOptionPane.showMessageDialog(null,character[0]+" chose to pass, required to get new domino from boneyard");
                            player1[0][dicevalue]=tile. handler[0][dicevalue];
                            player1[1][dicevalue]=tile. handler[1][dicevalue];
                            tile. handler[0][dicevalue]=7;
                            tile. handler[1][dicevalue]=7;
                            highhead1=highhead2;
                            hightail1=hightail2;
                            check=false;
                            checkroll=false;
                            checkroll1=false;
                        }
                        counter1=0;
                        for(int x=0;x<49;x++){
                        if(tile. handler[0][x]==7&&tile. handler[1][x]==7)
                        counter1+=1;
                        }
                        if(counter1==49){
                            check=false;
                            checkroll=false;
                            checkroll1=false;
                        }
                }
            }
            System.out.println(character[0]+"'s tiles");
            for(int x=0;x<49;x++){
                if(player1[0][x]<7&&player1[1][x]<7)
                System.out.println(player1[0][x]+"\t"+player1[1][x]);
            }
            while(check){
                while(check){
                try{
                highhead1=Integer.parseInt(JOptionPane.showInputDialog(character[0]+"'s turn\nEnter a your head tile number to use:"));
                hightail1=Integer.parseInt(JOptionPane.showInputDialog(character[0]+"'s turn\nEnter a your tail tile number to use:"));

                while(check){

                    toplace=Integer.parseInt(JOptionPane.showInputDialog("Where to place the tiles:\n[0]head portion\n[1]tail portion"));

                    if(toplace==0||toplace==1){

                    check=false;

                    }

                    else

                    JOptionPane.showMessageDialog(null,"WRONG INPUT","Warning",JOptionPane.WARNING_MESSAGE);

                    }

                check=false;

                }

                catch(NumberFormatException e){

                JOptionPane.showMessageDialog(null,"WRONG INPUT","ERROR",JOptionPane.ERROR_MESSAGE);

                }

                }

                check=true;

            if(toplace==0){ 

                if(highhead1==highhead2){

                   for(int x=0;x<49;x++){

                      if(player1[0][x]==highhead1&&player1[1][x]==hightail1){

                         x2-=1;

                         y2-=1;

                         interval[0][x2]=player1[1][x];

                         interval[1][y2]=player1[0][x];

                         highhead1=player1[1][x];

                         hightail1=hightail2;

                         player1[0][x]=7;

                         player1[1][x]=7;

                         check=false;

                         

                      }

                  }

                }

                else if(hightail1==highhead2){

                     for(int x=0;x<49;x++){

                      if(player1[0][x]==highhead1&&player1[1][x]==hightail1){

                         x2-=1;

                         y2-=1;

                         interval[0][x2]=player1[0][x];

                         interval[1][y2]=player1[1][x];

                         hightail1=hightail2;

                         player1[0][x]=7;

                         player1[1][x]=7;

                         check=false;

                      }

                  }

                }

            }

            

            else if(toplace==1){

                if(highhead1==hightail2){

                   for(int x=0;x<49;x++){

                      if(player1[0][x]==highhead1&&player1[1][x]==hightail1){

                        xx2+=1;

                         yy2+=1;

                         interval[0][xx2]=player1[0][x];

                         interval[1][yy2]=player1[1][x];

                         highhead1=highhead2;

                         player1[0][x]=7;

                         player1[1][x]=7;

                         check=false;

                      }

                  }

                }

                else if(hightail1==hightail2){

                    for(int x=0;x<49;x++){

                      if(player1[0][x]==highhead1&&player1[1][x]==hightail1){

                         xx2+=1;

                         yy2+=1;

                         interval[0][xx2]=player1[1][x];

                         interval[1][yy2]=player1[0][x];

                         highhead1=highhead2;

                         hightail1=player1[0][x];

                         player1[0][x]=7;

                         player1[1][x]=7;

                         check=false;

                      }

                  }

                }

            }



            }

            printgamecycle();

                counterplayer1=0;

                for(int x=0;x<49;x++){

                if(player1[0][x]<7&&player1[1][x]<7)

                counterplayer1+=1;

                }

                if(counterplayer1==0){

                JOptionPane.showMessageDialog(null,character[0]+" LOE MENANG!!!");

                checkgame=false;

                check=false;

                checkroll=false;

                checkroll1=false;

                }

        break;

    }

}





}

}

Tidak ada komentar:

Posting Komentar