본문 바로가기

Java/java 기초

awt로 가위바위보 프로그램 만들기

import java.awt.Button;
import java.awt.Frame;
import java.awt.Label;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.awt.print.Paper;
import java.util.Random;

public class WindowTest extends Frame implements WindowListener, ActionListener {
	Label title, player, com;

	Label playerStatusLabel, comStatusLabel, resultLabel;

	Button rockBtn, scissorsBtn, paperBtn;

	int win, lose, draw = 0;

	String status = "??", comStatus1 = "??", result1 = "??";
	
	int userChoice = 0;
	int scissors = 1;
	int rock = 2;
	int paper = 3;
	int winCount = 0;
	int drawCount = 0;
	int loseCount = 0;
	String answer;
	Random rand = new Random();

	public WindowTest() {
		super("가위 바위 보 게임");
		setLayout(null);

		title = new Label(win + "승 " + lose + "패 " + draw + "무");
		title.setBounds(100, 50, 200, 30);
		title.setAlignment(Label.CENTER);
		add(title);

		player = new Label("Player");
		player.setBounds(100, 70, 70, 40);
		add(player);

		com = new Label("Com");
		com.setBounds(240, 70, 70, 40);
		add(com);

		playerStatusLabel = new Label();
		playerStatusLabel.setBounds(100, 150, 50, 30);
		playerStatusLabel.setText(status);
		add(playerStatusLabel);

		resultLabel = new Label();
		resultLabel.setBounds(170, 150, 50, 30);
		resultLabel.setText(result1);
		add(resultLabel);

		comStatusLabel = new Label();
		comStatusLabel.setBounds(240, 150, 50, 30);
		comStatusLabel.setText(comStatus1);
		add(comStatusLabel);

		setSize(640, 480);
		setLocation(100, 0);
		setVisible(true);

		scissorsBtn = new Button("가위");
		scissorsBtn.setBounds(100, 200, 50, 20);
		scissorsBtn.addActionListener(this);
		add(scissorsBtn);

		rockBtn = new Button("바위");
		rockBtn.setBounds(150, 200, 50, 20);
		rockBtn.addActionListener(this);
		add(rockBtn);

		paperBtn = new Button("보");
		paperBtn.setBounds(200, 200, 50, 20);
		paperBtn.addActionListener(this);
		add(paperBtn);

		addWindowListener(this);
		
	}
	
	public void loop() {
		//랜덤 생성
		int randomChoice = rand.nextInt(3) + 1;

		if (userChoice - randomChoice == 0) {
			resultLabel.setText("비겼습니다");
			draw ++;
			
		} else if (userChoice - randomChoice == -1) {
			resultLabel.setText("졌습니다");
			lose++;
			
		} else {
			resultLabel.setText("이겼습니다");
			win++;
			
		} 
		
		if(userChoice == 1) {
			playerStatusLabel.setText("가위");
		} else if(userChoice ==2) {
			playerStatusLabel.setText("바위");
		} else if (userChoice == 3) {
			playerStatusLabel.setText("보");
		}
		
		
		if(randomChoice == 1) {
			comStatusLabel.setText("가위");
		} else if(randomChoice ==2) {
			comStatusLabel.setText("바위");
		} else if (randomChoice == 3) {
			comStatusLabel.setText("보");
		}
		
		title.setText(win + "승 " + lose + "패 " + draw + "무");

		}
	

	@Override
	public void windowActivated(WindowEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void windowClosed(WindowEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void windowClosing(WindowEvent arg0) {
		// TODO Auto-generated method stub
		System.exit(0);
	}

	@Override
	public void windowDeactivated(WindowEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void windowDeiconified(WindowEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void windowIconified(WindowEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void windowOpened(WindowEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		
		Button btn = (Button) e.getSource();
		System.out.println(btn.getLabel());
		if(btn.getLabel().equals("가위")) {
			userChoice = 1;
			playerStatusLabel.setText("가위");
			loop();
		}else if (btn.getLabel().equals("바위")) {
			userChoice = 2;
			playerStatusLabel.setText("바위");
			loop();
		}else if (btn.getLabel().equals("보")) {
			userChoice = 3;
			playerStatusLabel.setText("보");
			loop();
		}
		
	}

}

 

 

public class mainClass {
	public static void main(String[] args) {
		new WindowTest();
	}
}

 

 

 

 

 

'Java > java 기초' 카테고리의 다른 글

네트워크 공부  (0) 2019.12.09
awt - 이미지 올리기  (0) 2019.12.06
awt - 응용  (0) 2019.12.05
awt- 버튼 클릭 이벤트  (0) 2019.12.05
awt - Panel  (0) 2019.12.05