|
한글자씩만 적어야됩니다. ㅋㅋㅋ
main.java
package hangman;
import java.io.*;
public class Main {
BufferedReader m_key;
computer m_com;
public Main()
{
m_key=new BufferedReader(new InputStreamReader(System.in));
m_com=new computer();
}
public static void main(String[] args)
{
Main m=new Main();
m.run();
}
public char getKey()
{
String msg;
char a;
msg="error";
try{
System.out.println("알파벳을 입력하세요");
msg=m_key.readLine();
}
catch(IOException e)
{
}
a=msg.charAt(0);
return a;
}
public void run()
{
int result;
char msg;
while(true)
{
System.out.println("한글자씩 입력해주세요");
System.out.println("힌트:"+m_com.m_user);
msg=getKey();
result=m_com.checkLevel(msg);
m_com.ShowHangMan();
if(1==result)
{
m_com.ShowEndMsg(true);
break;
}
if(2==result)
{
m_com.ShowEndMsg(false);
break;
}
}
}
}
computer.java
package hangman;
import java.util.*;
public class computer {
private String[] m_key={
"Boolean",
"Integer",
"String",
"System"
};
StringBuffer m_user;
public String m_word;
int m_level;
int m_index; //index변수 실행하기
public computer() // 랜덤한 단어 할당하는 함수
{
Random myrand=new Random();//랜덤객체 할당
m_index=myrand.nextInt(m_key.length-1);//배열의 갯수만큼 랜덤한 숫자 할당하기
m_word=m_key[m_index];//index에서 m_word로 단어 넣기
m_level=0;//m_level 0으로 초기화
m_user=new StringBuffer();
for(int i=0;i<m_word.length();i++)
{
m_user.append("_");//유저append에 단어갯수만큼 _ 추가
}
}
void ShowEndMsg(boolean isWin)
{
String msg;
if(true==isWin)
{
msg="사형수를 구하셨습니다.";
}
else
{
msg="사형이 집행 되었습니다.";
}
System.out.println(msg);
}
void ShowHangMan()
{
System.out.println("사형 집행 단계");
switch(m_level)
{
case 0:
System.out.println(" ┌────┐");
System.out.println(" ▲ │");
System.out.println(" │");
System.out.println(" │");
System.out.println(" │");
System.out.println(" │");
break;
case 1:
System.out.println(" ┌────┐");
System.out.println(" ▲ │");
System.out.println(" ┼ │");
System.out.println(" │");
System.out.println(" │");
System.out.println(" │");
break;
case 2:
System.out.println(" ┌────┐");
System.out.println(" ▲ │");
System.out.println("┌┼┐ │");
System.out.println(" │");
System.out.println(" │");
System.out.println(" │");
break;
case 3:
System.out.println(" ┌────┐");
System.out.println(" ▲ │");
System.out.println("┌┼┐ │");
System.out.println(" │ │");
System.out.println(" │");
System.out.println(" │");
break;
case 4:
System.out.println(" ┌────┐");
System.out.println(" ▲ │");
System.out.println("┌┼┐ │");
System.out.println(" │ │");
System.out.println(" ┴ │");
System.out.println(" │");
break;
case 5:
System.out.println(" ┌────┐");
System.out.println(" ▲ │");
System.out.println("┌┼┐ │");
System.out.println(" │ │");
System.out.println("┌┴┐ │");
System.out.println(" │");
break;
default:
System.out.println(" ┌────┐");
System.out.println(" │");
System.out.println(" │");
System.out.println(" │");
System.out.println(" │");
System.out.println(" │");
break;
}
}
int checkEnd()
{
String t_msg=m_user.toString();
if(t_msg.equals(m_key[m_index]))
{
return 1;
}
if(5<=m_level)
{
return 2;
}
return 0;
}
int checkLevel(char msg)
{
boolean find;
find=false;
for(int i=0;i< m_key[m_index].length();i++)
{
char a;
char b;
a=(char)m_key[m_index].charAt(i);
b=msg;
if(m_key[m_index].charAt(i)==msg)
{
if('_'==m_user.charAt(i))
{
m_user.setCharAt(i, msg);
find=true;
break;
}
}
}
if(false==find)
{
m_level++;
}
return checkEnd();
}
}
'기타 > 프로그래밍 분류' 카테고리의 다른 글
This version of the rendering library is more recent than your version of ADT plug-in. Please update ADT plug-in (0) | 2016.04.07 |
---|---|
android - 기본 Package Explorer 알아보기 (0) | 2016.04.06 |
please fix them before running your application (0) | 2016.04.05 |
Android - 어플실행해보기 (0) | 2016.04.04 |
'Building workspace' has encountered a problem (0) | 2016.04.03 |