Penggunaan Library


Dengan Coding Main :




/**
 * Write a description of class main here.
 *
 * @author berto
 */
import java.io.IOException;
import java.util.Random;
import java.util.Scanner;
public class main
{
   private static Scanner scanner; 
   public static void main(String[]args) throws IOException 
   {
        int ulang = 5;
        int start = 1;
        System.out.println("Selamat datang di game tebak kartu");
        System.out.println("Dimana 0 = Tidak ada kartu ,1 = A , 11 = J , 12= Q , 13= K ");
        System.out.println("Dan kamu punya 5 kesempatan untuk menukar kartu");
        System.out.println("Selamat Bermain!!");
        BalikLayar [] playerCom = new BalikLayar[2];
        BalikLayar [] player1 = new BalikLayar[2];
       
        System.out.println("Musuh Mengambil kartu");
        untuk_delay(playerCom);
        System.out.println("Kamu Mengambil Kartu");
        untuk_delay(player1);
        System.out.println("Musuh Mengambil kartu");
        untuk_delay(playerCom);
        System.out.println("Kamu Mengambil Kartu");
        untuk_delay(player1);       
        Random tandRandom = new Random();
        player1[0].kartunya();
        player1[1].kartunya();
        playerCom[0].kartunya();
        playerCom[1].kartunya();
        while(start>0)
        {
                while (true) {
                    System.out.println("Kartumu: ");
         
                    player1[0].currentKartu();
                    player1[1].currentKartu();
                    boolean choose = tandRandom.nextBoolean();
                    int kartuke = tandRandom.nextInt(1);
                       
                    System.out.println();
                    System.out.println("Kartu Yang Ingin di ubah...(1/2/0 untuk Cukup)");           
                       
                    ulang--;
                       
                    if(ulang == 0) {
                        finish(playerCom, player1);
                        break;
                    }
                       
                    scanner = new Scanner(System.in);
                    int temp = scanner.nextInt();
                       
                    if(temp != 0) {
                        player1[temp-1].kartunya();
                        if(choose == true) {
                            playerCom[kartuke].kartunya();
                        }
                    }else {
                        finish(playerCom, player1);
                        break;
                    }
                }
                System.out.println("Mau Main Lagi...?");
                System.out.println(" 1 untuk Ya");
                System.out.println(" 0 untuk Tidak");
                scanner = new Scanner(System.in);
                int lagi = scanner.nextInt();
                if (lagi ==1)
                {
                    start++;
                    player1[0].kartunya();
                    player1[1].kartunya();
                }
                else
                {
                    start=0;
                }
         }
   }
       
        static void untuk_delay(BalikLayar [] player) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
           
            for (int i = 0; i < player.length; i++) {
                player[i] = new BalikLayar();
            }
        }
     
        static void finish(BalikLayar[] playerCom, BalikLayar[] player1) {
            int jumlah_kartu1=0, jumlah_kartu2=0;
            System.out.println("Punyamu:");
            for (int i = 0; i < playerCom.length; i++) {
                player1[i].currentKartu();
                jumlah_kartu1 += player1[i].getAngka();
            }
            System.out.println("Punya Komputer:");
            for (int i = 0; i < playerCom.length; i++) {
                playerCom[i].currentKartu();
                jumlah_kartu2 += playerCom[i].getAngka();
            }
            System.out.println("Hasil Akhirnya: ");
            System.out.println("Punyamu: " + "Jumlah angka: " + jumlah_kartu1 );
            System.out.println("Punya Komputer: " + "Jumlah angka: " + jumlah_kartu2 );
            if((jumlah_kartu1) < (jumlah_kartu2)) 
            {
                System.out.println("Maaf, Kamu kalah");
            }else if((jumlah_kartu1) == (jumlah_kartu2)) 
            {
                System.out.println("Skor Imbang");
            }else 
            {
                System.out.println("Selamat, Kamu menang!!!");
        }
    }
    
}


dan Coding BalikLayar :




/**
 *
 * @author berto
 */
import java.util.Random;
 
public class BalikLayar{
    private int panjang;
    private int lebar;
    private static String attribute[] = {"Keriting", "Diamond", "Hati", "Sekop"};
    private int x;
    private String stateBungaString;
   
    void kartunya() {
        x = setNomor();
        stateBungaString = attribute[setAttribute()];
    }
   
    void currentKartu() {
        System.out.print(" " + x + " ");
        System.out.println(stateBungaString);
    }
   
    int getAngka() {
        return x;
    }
  
   
    private int setNomor() {
        Random randomGenerator = new Random();
        int randomInt = randomGenerator.nextInt(13);
        return randomInt;
    }
   
    private int setAttribute() {
        Random randomGenerator = new Random();
        int randomInt2 = randomGenerator.nextInt(4);
        return randomInt2;
    }
}

Dan Hasilnya :


Comments