Created
November 26, 2016 16:01
-
-
Save JoeUnsung/da0e725cd834e634e8b55fb5ab17a872 to your computer and use it in GitHub Desktop.
Play hangman. Enjoy.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package joe; | |
import java.util.*; | |
public class P8_20101062_1 { | |
public static void main(String[] args){ | |
Random r = new Random(); | |
Scanner s = new Scanner(System.in); | |
String guessWords[] = {"algorithm", "university", "programming", "windows", "excel", "sogang"}; | |
String pickWord = guessWords[r.nextInt(6)], enterChar; | |
char temp_char; | |
int cnt = 0, cnt2 = 0, cnt3 = 0; | |
int[] temp_index = new int[pickWord.length()]; | |
for(int i = 0 ; i < pickWord.length(); i++){ | |
temp_index[i] = 0; | |
} | |
while(true){ | |
cnt2 = 0; cnt3 = 0; | |
System.out.print("Guess a word : "); | |
for(int i = 0 ; i < pickWord.length(); i++){ | |
if (temp_index[i] == 1) | |
System.out.print(pickWord.charAt(i)); | |
else | |
System.out.print("_"); | |
} | |
System.out.println(" "); | |
System.out.println("The number of correct guess : " + cnt); | |
System.out.print("Enter a guess character : " ); | |
enterChar = s.nextLine(); | |
System.out.println("letter Entered : " + enterChar ); | |
for(int i = 0 ; i < pickWord.length(); i++){ // | |
if (pickWord.charAt(i) == enterChar.charAt(0)){ | |
temp_char = enterChar.charAt(0); | |
temp_index[i] = 1; | |
cnt3++; | |
} | |
} | |
if(cnt3 != 0){ | |
System.out.println("Correct guess :>"); | |
} | |
else if ( cnt3 == 0){ | |
System.out.println("Sorry, Wrong guess :<"); | |
} | |
cnt++; // 한번의 guessing 종료. | |
System.out.println(" "); | |
for (int x = 0 ; x < pickWord.length(); x++){ // 모든 string을 다 맞췄는지 알아보는 for문. | |
if(temp_index[x] == 1){ | |
cnt2++; | |
} | |
} | |
// 현재 시도횟수를 체크하여, 종료할지 말지를 결정하는 라 | |
if (cnt2 == pickWord.length()){ | |
System.out.println("You Win :>"); | |
break; | |
} | |
else if(cnt == pickWord.length() ){ | |
System.out.println("Sorry, you lose :< the word was : " + pickWord); | |
break; | |
} | |
else{ | |
continue; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment