GitHub - ineiw/rsp
Contribute to ineiw/rsp development by creating an account on GitHub.
github.com
영상
코드
player 인터페이스
package rsp;
// 플레이어 인터페이스
public interface player {
int rock = 0;
int sissor = 1;
int paper = 2;
String[] rspList = {"바위","가위","보"}; // 위 정수형 값과 인덱싱 해주기위해 string 배열 선언
public Hand getHand(); // hand 형 값 가져오기
public void setHand(String hand); // hand 세팅
public void setHand(); // 랜덤으로 hand 세팅
public int whichWin(player player); // 플레이어 중 이긴사람의 uid 리턴
public int getUid(); // uid 가져오기
}
player 인터페이스를 구현한 추상 클래스 rspAbs
package rsp;
// person 과 computer 에 공통으로 구현되는 메소드와 변수 구현 player 인터페이스 상속
public abstract class rspAbs implements player {
Hand hand = new Hand(); // Hand 객체 생성
int uid; // uid 선언
@Override // hand 형 객체 리턴
public Hand getHand() {
return this.hand;
}
@Override // 이긴 사람 의 uid 리턴 비기면 -1 리턴
/*
* 주먹
* \
* | 가위
* /
* 보자기
* 이므로 바로 다음 값과 다다음 값이 각각 이기고 진다.
*/
public int whichWin(player player) { // 인터페이스 player 가 상속되었으니 player 형으로 값 받기 가능
if ((this.getHand().getId()+1)%3 == player.getHand().getId())
return this.getUid();
else if((this.getHand().getId()+2)%3 == player.getHand().getId())
return player.getUid();
return -1;
}
@Override // uid 리턴
public int getUid() {
return this.uid;
}
}
rspAbs 를 상속받은 클래스 Person 과 computer 클래스
package rsp;
public class Person extends rspAbs{
public Person(int uid) { // 생성자로 uid 세팅
this.uid = uid;
}
@Override
public void setHand(String input) { // string 형 변수 input을 int 형으로 바꾼후 id 와 name 으로 각각세팅하여 저장
int id = Integer.parseInt(input);
this.hand.setHand(id,rspList[id]);
}
@Override
public void setHand() { // computer 를 위한 랜덤 세팅 함수
// TODO Auto-generated method stub
}
}
package rsp;
public class computer extends rspAbs {
public computer(int uid) { // 컴퓨터 생성자로 uid 세팅
this.uid = uid;
}
@Override // 랜덤으로 hand 객체 세팅
public void setHand() {
int rand = (int)(Math.random()*3);
this.hand.setHand(rand,rspList[rand]);
}
@Override // person 을 위한 hand 세팅 함수
public void setHand(String hand) {
// TODO Auto-generated method stub
}
}
가위바위보 의 정보를 저장하기 위한 Hand 클래스
package rsp;
// Hand 클래스는 id 와 name 인 각각 int 와 string 형을 가진다. id 와 name 을 가져오는 get 함수와 set 함수가 있다.
public class Hand {
int id;
String name;
public void setHand(int id,String name) { // set 함수
this.id = id;
this.name = name;
}
public int getId() { // get 함수
return this.id;
}
public String getName() { // get 함수
return this.name;
}
}
가위바위보 테스트 클래스
package rsp;
import java.util.Scanner;
public class TestRsp {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// Person 과 computer 객체 생성
Person man = new Person(12);
computer com = new computer(23);
// input 입력 받기
String input = "a";
while (true) {
System.out.println("==============\n[바위 : (0) , 가위 : (1) , 보자기 : (2) , 나가기 : (q)]");
input = sc.nextLine(); // 입력받기
// 0,1,2,q 만 받기
if (!(input.equals("0") || input.equals("1") ||input.equals("2") ||input.equals("q"))) {
System.out.println("q,0,1,2 중 하나를 고르시오.");
continue;
}
if (input.equals("q")) // q 면 while 문 탈출
break;
man.setHand(input); // setHand 함수로 인풋변수 값 세팅하기
com.setHand(); // com 의 setHand 로 랜덤하게 값 세팅하기
int uid = man.whichWin(com); // man 과 com 중 이긴 플레이어의 uid 저장
System.out.println("나 : "+man.getHand().getName()+"\n상대방 : "+com.getHand().getName()+"\n=============="); // 상태 출력
if(uid == man.getUid()) // man 이 이겻으면 win 출력
System.out.println("WIN");
else if(uid == com.getUid()) // man 이 졋으면 lose 출력
System.out.println("LOSE");
else // 비겼으면 draw 출력
System.out.println("DRAW");
}
}
}