Pages

Jumat, 01 Maret 2013

Russian Roullette ... entah apa namanya

berhubung kemarin sempat iseng-iseng ngobrol sama teman. dapat deh istilah ini, iseng-iseng lagi bikin kodingannya. semoga algo-nya bener
Kelas 

public class Chamber {
    
    private int id;
    private boolean bullet;
    
    public Chamber(){
        this.id=0;
        this.bullet = false;
    }
    
    public Chamber(int id, boolean bullet){
        this.id = id;
        this.bullet = bullet;        
    }
    
    public void setId(int id){
        this.id = id;
    }
    
    public void setBullet(boolean bullet){
        this.bullet = bullet;
    }
    
    public int getId(){
        return this.id;
    }
    
    public boolean getBullet(){
        return this.bullet;
    }    
    
    
} 



yang bawah ni, kelas utamanya :
import java.util.Random;
import java.util.Scanner;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author aisyah
 */
public class Pistol {
    
    private Chamber[] roller;
    public final int numHole = 7;
    //private int startPoint;
    private Chamber currentChamber;
    private Random random = new Random();
    public String[] player;
    public int numPlayer;
    
    
    
    public Pistol(int player){
        this.numPlayer = player;
        this.roller = new Chamber[this.numHole-1];        
        this.player = new String[player];
        for(int i=0;i<this.numHole-1;i++){
            this.roller[i] = new Chamber();
            this.roller[i].setId(i);
            this.roller[i].setBullet(false);
        }
        this.currentChamber = this.roller[0];
    }
    
    public void setPlayer(int player, String name){
        if(player<this.numPlayer-1 && player>-1){
            this.player[player] = name;
            //System.out.println(this.player[player]);
        }else{
            System.out.println("nomor pemain tidak sesuai jumlah pemain");
        }
        
    }
    
    public void fillBullet(int chamber){
        if(chamber>this.numHole-1){
            System.out.println("chamber hanya enam buah");            
        }else{
            this.roller[chamber].setBullet(true);
        }
        
    }
    
    /*
     * memutar roller
     */
    public boolean spinRevolver(){
        int current = this.randNum(this.numHole);        
        while(true){
            current = (current == 0) ? this.randNum(this.numHole) : current;
            if(current>0){
                this.setCurrentChamber(current);
                return true;
            }
        }  
    }
    
    public void initChamber(int i){
            this.roller[i] = new Chamber(i,false);
            //this.roller[i].setId(i);
            //this.roller[i].setBullet(false);
       
    }
    
    /*
     * memicu pemantik
     */
    public void strikeFire(int player){
        //this.spinRevolver();
        int current = this.currentChamber.getId();
        this.currentChamber = (current==5)?this.roller[0]:this.roller[current+1];
        if(this.currentChamber.getBullet()==true){
            System.out.println("BAM!! player : "+this.player[player]+ " die!");
            System.out.println("Permainan selesai!");
            System.exit(0);
        }else{
            System.out.println("klik!! you are still safe!");
        }
        
    }
    
    /*
     * random number, untuk memutar roller
     * @param titik mulai, 
     * @param nomor lubang di roller
     */
    private int randNum(int numChamber){
        
        return this.random.nextInt(numChamber);
    }
    
    /*
     * titik mulai pada hole yg selaras dengan pemantik.
     * default titik 0. setelah berputar akan berubah
     */
    private Chamber getCurrentChamber(){
        
        return this.currentChamber;
    }
    
    /*titik mulai
     * set titik mulai
     */
    private void setCurrentChamber(int current){
        for(int i=0; i<this.roller.length; i++){
          int j = i+1;
          if((j == current)) this.currentChamber = this.roller[i];
        }
        
        
    }
    
    public static void main(String[] args){
        
        int player = 6;
        Pistol pispot = new Pistol(player);
        /*for(int b=0;b<6;b++){
            //System.out.println(b);
            pispot.initChamber(b);
        }*/
        Scanner scanner = new Scanner(System.in);
        //======= isi peluru di chamber
        System.out.println("Isi peluru dulu !");
        int bull = scanner.nextInt();
        pispot.fillBullet(bull);
        //======= set pemain
        System.out.println("\nSet nama pemain dulu !");
        
        for(int c=0; c<player;c++){
            //System.out.println(c);
            String pemain = scanner.next();
            pispot.setPlayer(c, pemain);
        }
        
        //======= permainan dimulai
        int i = 0;
        while(i < player ){
            System.out.println("\nPilihan :"+i);
            System.out.println("1. puter revolver");
            System.out.println("2. tarik pemicu");
            System.out.println("Giliran pemain ke-"+(i+1)+" : "+pispot.player[i]);
            int pilihan = scanner.nextInt();
            
            switch(pilihan){
                case 1:
                    pispot.spinRevolver();
                    break;
                case 2:
                    pispot.strikeFire(i);
                    i++;
                    break;
                default:
                    System.out.println("pilihan tidak ada\n\n");
                    break;
            }
            
            
            if(i==player){
                i=0;
            }
        }
    }
    
}

klo tampilannya masih gini :
Pilihan :2
1. puter revolver
2. tarik pemicu
Giliran pemain ke-3 : d
2
BAM!! player : d die!
Permainan selesai!

0 komentar:

Posting Komentar