JAVA – Random Dice (중복없는 랜덤 주사위)

랜덤 주사위 1~6개 돌리기

package hello;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionAdapter;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;

import javax.swing.*;

class Dice{
	int x,y; // 위치
	Color c; // 주사위 배경 색상
	int curr; // 현재 주사위 면
	
	public Dice(int x,int y,Color c,int curr) {
		this.x=x;
		this.y=y;
		this.c=c;
		this.curr=curr;
	}
	
	public void setcurr(int curr) {
		this.curr=curr;
	}
	
	public void paint(Graphics g) {
		// 주사위 배경
		g.setColor(c);
		g.fillRect(x,y,70,65);
		
		// 주사위 테두리
		g.setColor(Color.black);
		g.drawRect(x,y,70,65);
		
		// 주사위 점 
		if (curr>=1) g.fillRect(x+10,y+10,15,15);
		if (curr>=2) g.fillRect(x+30,y+10,15,15);
		if (curr>=3) g.fillRect(x+50,y+10,15,15);
		if (curr>=4) g.fillRect(x+10,y+40,15,15);
		if (curr>=5) g.fillRect(x+30,y+40,15,15);
		if (curr>=6) g.fillRect(x+50,y+40,15,15);
	}
}

class MyFrame extends JFrame{
	ArrayList<Integer> nums;
	ArrayList<Dice> ds;
	JLabel lb,lb2;
	int size,ran;
	final int csize=7;
	int[] compare=new int[csize];
	
	class MyPanel extends JPanel {
		public MyPanel(int r) {
			size=r;
			
			setLayout(new BorderLayout());
			nums=new ArrayList<Integer>(size);
			ds=new ArrayList<Dice>(size);
			
			for(int i=0;i<size;i++) {
				ran=(int)((Math.random()*6)+1);
				if(compare[ran]==0) {
					nums.add(i,ran);
					compare[ran]=1;}
				else
					i--;
			}
			
			if(size>=1) ds.add( new Dice(115,50,Color.white,nums.get(0)) );
			if(size>=2) ds.add( new Dice(285,50,Color.white,nums.get(1)) );
			if(size>=3) ds.add( new Dice(115,150,Color.white,nums.get(2)) );
			if(size>=4) ds.add( new Dice(285,150,Color.white,nums.get(3)) );
			if(size>=5) ds.add( new Dice(115,250,Color.white,nums.get(4)) );
			if(size>=6) ds.add( new Dice(285,250,Color.white,nums.get(5)) );
			
			lb=new JLabel("                                               - 현재 주사위 상태: "+nums);
			lb2=new JLabel("                                                         < 절대 중복 안 나옴!! >");
			lb2.setOpaque(true);
			lb2.setForeground(Color.red); 
			
			add(lb,BorderLayout.NORTH);
			add(lb2,BorderLayout.SOUTH);
			
			requestFocus();
			setFocusable(true);
			
			addKeyListener(new KeyAdapter(){
				public void keyPressed(KeyEvent e) {
					if(e.getKeyCode()==KeyEvent.VK_ENTER) {
						// compare배열 초기화
						for(int i=0; i<csize; i++)
							compare[i]=0;
						
						for(int i=0;i<size;i++) {
							int ran=(int)((Math.random()*6)+1);
							if(compare[ran]==0) {
								nums.set(i,ran);
								compare[ran]=1;}
							else
								i--;
						}
						
						for(int j=0;j<size;j++)
							ds.get(j).setcurr(nums.get(j));
						
						repaint();
						lb.setText("                                               - 현재 주사위 상태: "+nums);
					}
				}
			}
			);
		}
		
		@Override
		public void paintComponent(Graphics g) {
			super.paintComponent(g);
			for(int i=0;i<size;i++)
				ds.get(i).paint(g);
		}
	}
	
	public MyFrame(String r,semiFrame obj) {
		setTitle("주사위 "+r+"개");
		setLocation(700,150);
		setSize(500,400);
		int r2=Integer.parseInt(r);
		add(new MyPanel(r2));
		setVisible(true);
		
		addWindowListener(new WindowAdapter(){
			public void windowClosing(WindowEvent e) {
				obj.setVisible(true);
			}
		});
		
	}
	
}

class semiFrame extends JFrame{
	String receive;
	
	public semiFrame(){
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setSize(330,100);
		setLocation(700,150);
		setLayout(new FlowLayout());
		setTitle("주사위 선택");
		String[] st= {"1","2","3","4","5","6"};
		JComboBox cb=new JComboBox(st);
		cb.setSelectedIndex(5);
		JButton bt=new JButton("선택 완료");
		add(cb);
		add(bt);
		setVisible(true);
		bt.addActionListener(e->{
			receive=(String)cb.getSelectedItem();
			setVisible(false);
			MyFrame p=new MyFrame(receive,this);
		});
		
	}
}

public class Game{
	public static void main(String[] args) {
		semiFrame s=new semiFrame();
	}
}

Leave a Reply

Your email address will not be published. Required fields are marked *