Rabu, 13 November 2019

Implementasi Game Kartu dengan Library

Implementasi game kartu di Java :

Main.java :


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!!");
        Pemain [] playerCom = new Pemain[2];
        Pemain [] player1 = new Pemain[2];
      
        for (int i = 0; i < playerCom.length; i++) {
            playerCom[i] = new Pemain();
        }
      
        for (int i = 0; i < playerCom.length; i++) {
            player1[i] = new Pemain();
        }
     
        Random tandRandom = new Random();
        player1[0].kartunya();
        player1[1].kartunya();
        playerCom[0].kartunya();
        playerCom[1].kartunya();
                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;
                    }
                  
                  
                }

   }
   
        static void finish(Pemain[] playerCom, Pemain[] player1) {
            int jumlah_kartu1=0, jumlah_kartu2=0;
            System.out.println("Kartumu :");
            for (int i = 0; i < playerCom.length; i++) {
                player1[i].currentKartu();
                jumlah_kartu1 += player1[i].getAngka();
            }
            System.out.println("Kartu 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_kartu1 );
            System.out.println("Punya Komputer: " + 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!!!");
        }
    }
  
}


Pemain.java :

import java.util.Random;

public class Pemain{
    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;
    }
}



Tidak ada komentar:

Posting Komentar