AP TWENTY48

TWENTY48

Kali ini kita akan mencoba membuat game TWENTY48 , game ini mirip dengan 2048.
Dan berikut adalah soal untuk membuat game TWENTY48 :



Coding :


import java.util.ArrayList;
import java.util.Scanner;
import java.util.Stack;

public class Twenty48 {
   static ArrayList kartu = new ArrayList();
   static int score =0;
   public static void main(String[] args) {

       random1000Kartu();
    
       Stack s1 = new Stack();
       Stack s2 = new Stack();
       Stack s3 = new Stack();
       Stack s4 = new Stack();
       
       do{
          
           displayTumpukan(s1, s2, s3, s4);
     
           System.out.println("==============================================");
          
           System.out.println("Current Card : " + kartu.get(0) + " | Next Card : " + kartu.get(1) + " | "
           + "Score : " + score );
           System.out.println("==============================================");
           
           boolean valid = false;
           int lokasi = -1;
           while(valid==false){//7. jika tidak valid, maka ulangi
               System.out.print("Put on 1/2/3/4/5 (Trash) : ");
               Scanner scan = new Scanner(System.in);
               lokasi = scan.nextInt();
               if (lokasi < 5){
            
                   switch(lokasi){
                           case 1:
                               valid = cekLokasiValid(s1); break;
                           case 2:
                               valid = cekLokasiValid(s2); break;
                           case 3:
                               valid = cekLokasiValid(s3); break;
                           case 4:
                               valid = cekLokasiValid(s4); break;
                   }
               }else {
                   kartu.remove(0);
                   
                   score = score -2;
                   break;
               }
           }
       
           if(valid==true){
               switch(lokasi){
                   case 1:
                        masukkanKartuKeStack(s1); break;
                   case 2:
                       masukkanKartuKeStack(s2); break;
                   case 3:
                       masukkanKartuKeStack(s3); break;
                   case 4:
                       masukkanKartuKeStack(s4); break;
                   }
               
               kartu.remove(0);
           }
       }while(kartu.size()>0);
       System.out.println("YOU WIN !!");
   }
   public static void masukkanKartuKeStack(Stack s){
       
       s.push(kartu.get(0));

              int atas1 = Integer.parseInt(String.valueOf(s.pop()));
       int atas2=0;
       
       if(!s.isEmpty()) atas2 = Integer.parseInt(String.valueOf(s.peek()));
       
       while(!s.isEmpty() && atas1<2048 amp="" atas1="=" atas2="" span="">

           atas2 = Integer.parseInt(String.valueOf(s.pop()));
           int nilai_baru = atas1 + atas2;
           s.push(nilai_baru);
           
           score = score + nilai_baru;
           atas1 = Integer.parseInt(String.valueOf(s.pop()));
           if(!s.isEmpty())
               atas2 = Integer.parseInt(String.valueOf(s.peek()));
           else break;
       }
       s.push(atas1);
             if(atas1 == 2048){
           s.pop();
           score = score + 2048;
       }
       
       
   }
   
   
   
   public static boolean cekLokasiValid(Stack s){
       boolean cek=false;
       if(!s.isEmpty()){
           int atas_stack = Integer.parseInt(String.valueOf(s.peek()));
           int current_card = Integer.parseInt(String.valueOf(kartu.get(0)));
           if( atas_stack >= current_card){
               cek = true;
           }else cek = false;
       }else cek = true;
       
       return cek;
   }
   
   
   public static void displayTumpukan(Stack a, Stack b, Stack c, Stack d){
      
      
       if(a.isEmpty()){
           System.out.print("| - | ");
       }else{
           System.out.print("| " + a.peek() + " | ");
       }
       if(b.isEmpty()){
           System.out.print("| - | ");
       }else{
           System.out.print("| " + b.peek() + " | ");
       }
       if(c.isEmpty()){
           System.out.print("| - | ");
       }else{
           System.out.print("| " + c.peek() + " | ");
       }
       if(d.isEmpty()){
           System.out.print("| - | ");
       }else{
           System.out.print("| " + d.peek() + " | ");
       }
       
   }
   
   public static void random1000Kartu(){
       
       for(int i=0; i<1000 i="" span="">
       int pangkat = (int)(Math.random()*6)+1;
           int angka = (int) Math.pow(2, pangkat);
           kartu.add(angka);
       }
   }
   
}

Sekian postingan saya , semoga bermanfaat bagi anda.

Terimakasih

Comments