과제 #4.1

임성혁·2022년 8월 5일

자바 기초

목록 보기
18/32
import java.util.Random;

public class Slotmachine {


    private int chance;
    boolean liveordie = true;

    String[] shapes = new String[]{"♠", "♥", "♣", "◆"};
    String[] myShapes = new String[getChance()];
    int myPrize = 0;

    // 동일 횟수
    int spade;
    int heart;
    int diamond;
    int clover;

    void setChance(int a){
        chance = a;
    }
    int getChance(){
        return chance;
    }

    // 게임시작, 시작 시 기회 5회 자동으로 주어짐
    void gameStart() {
        setChance(5);
        System.out.println("Game Start!");
        System.out.println("Your have " + getChance() + " chance");
        System.out.println("Good luck!");
    }

    // 주사위 굴리기
    void roll(){
//        int randnumber = (int)(Math.random() * getChance());

        String[] randomShapes = new String[getChance()]; //

        for (int i = 0; i < getChance(); i++){
            String oneShape = shapes[(int)(Math.random() * getChance() - 1)]; // 모양 랜덤 뽑기

            randomShapes[i] = oneShape;
            System.out.print(randomShapes[i]);

//            System.out.print(shapes[(int)(Math.random() * getChance() - 1)]);
        } System.out.println();

        myShapes = randomShapes;
    }

    // 같은 모양 개수에 따른 상금
    void samecount(){
        for (int i = 0; i < getChance(); i++){
            if (myShapes[i] == "♠") {
                spade += 1;
            } else if (myShapes[i] == "♥") {
                heart += 1;
            } else if (myShapes[i] == "♣") {
                clover += 1;
            } else if (myShapes[i] == "◆") {
                diamond += 1;
            }
        }
    }

    // 개수 카운트 초기화
    void samecountreset(){
        spade = 0;
        heart = 0;
        diamond = 0;
        clover = 0;
    }

    void prize(){
        if (spade == 5 || heart == 5 || diamond == 5 || clover == 5) myPrize += 1000000000;
        else if (spade == 4 || heart == 4 || diamond == 4 || clover == 4) myPrize += 10000000;
        else if (spade == 3 || heart == 3 || diamond == 3 || clover == 3) {
            if (spade == 2 || heart == 2 || diamond == 2 || clover == 2) {
                myPrize += 5000000;
            } else myPrize += 1000000;
        }
        else if (spade == 3 || heart == 3 || diamond == 3 || clover == 3) myPrize += 100000;
        else if (spade == 2 || heart == 2 || diamond == 2 || clover == 2) {
            if (spade == 2 || heart == 2 || diamond == 2 || clover == 2) {
                myPrize += 50000;
            } else myPrize += 1000;
        }
    }

    void oneShot(){
        roll();
        samecount();
        prize();
        samecountreset();
    }

}
public class Homework4_1 {
    public static void main(String[] args) {

        Slotmachine casino = new Slotmachine();

        casino.gameStart();

        casino.oneShot();

        System.out.println();

        System.out.println(casino.spade);
        System.out.println(casino.heart);
        System.out.println(casino.diamond);
        System.out.println(casino.clover);


        System.out.println(casino.myPrize);





    }
}
profile
열정

0개의 댓글