■ Java による作成例
import java.io.*;
import java.util.*;
class Hangman
{
private String Answer;
private boolean Correct[];
private String Alphabet;
private int Mistake;
public Hangman()
{
String aAnswerWord[] = {
"TIGER", "CAT", "DOG", "LION", "FOX",
"WOLF", "HORSE", "COW", "GOAT", "ZEBRA",
"ELEPHANT", "PENGUIN", "GIRAFFE", "ANTEATER" };
Random RandObj = new Random();
int n = RandObj.nextInt(aAnswerWord.length);
Answer = aAnswerWord[n];
Correct = new boolean[Answer.length()];
for (int i = 0;i < Answer.length(); i++)
Correct[i] = false;
Alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
Mistake = 0;
}
public void checkAnswer(char ch)
{
boolean bMatch = false;
for (int i = 0; i < Answer.length() ; i++)
{
if (Answer.charAt(i) == ch)
{
Correct[i] = true;
bMatch = true;
}
}
Alphabet = Alphabet.replace(ch, ' ');
if (bMatch == false)
Mistake++;
}
public boolean checkFinish()
{
boolean bCorrect = true;
for (int i = 0;i < Answer.length(); i++)
{
if (Correct[i] == false)
bCorrect = false;
}
if (bCorrect)
{
showHangman(-1);
System.out.print(Answer + "\n");
return true;
}
else if (Mistake == 6)
{
showHangman(Mistake);
System.out.print(Answer + "\n");
return true;
}
return false;
}
private void showHangman(int mistake)
{
System.out.print(" +-----+\n");
System.out.print(" | |\n");
if (mistake == 0)
{
System.out.print(" |\n");
System.out.print(" |\n");
System.out.print(" |\n");
System.out.print(" |\n");
System.out.print("/|\n");
}
else if (mistake == 1)
{
System.out.print(" | (+_+;\n");
System.out.print(" |\n");
System.out.print(" |\n");
System.out.print(" |\n");
System.out.print("/|\n");
}
else if (mistake == 2)
{
System.out.print(" | (+_+;\n");
System.out.print(" | |Y|\n");
System.out.print(" |\n");
System.out.print(" |\n");
System.out.print("/|\n");
}
else if (mistake == 3)
{
System.out.print(" | (+_+;\n");
System.out.print(" | o/|Y|\n");
System.out.print(" |\n");
System.out.print(" |\n");
System.out.print("/|\n");
}
else if (mistake == 4)
{
System.out.print(" | (+_+;\n");
System.out.print(" | o/|Y|\o\n");
System.out.print(" |\n");
System.out.print(" |\n");
System.out.print("/|\n");
}
else if (mistake == 5)
{
System.out.print(" | (+_+;\n");
System.out.print(" | o/|Y|\o\n");
System.out.print(" | /\n");
System.out.print(" | ~~\n");
System.out.print("/|\n");
}
else if (mistake == 6)
{
System.out.print(" | (@_@;\n");
System.out.print(" | o/|Y|\o\n");
System.out.print(" | / \\n");
System.out.print(" | ~~ ~~\n");
System.out.print("/|\n");
}
else
{
System.out.print(" |\n");
System.out.print(" | (^_^)\n");
System.out.print(" | o/|Y|\o\n");
System.out.print(" | / \\n");
System.out.print("/| ~~ ~~\n");
}
}
public void showState()
{
showHangman(Mistake);
System.out.print(Alphabet + "\n");
for (int i = 0; i < Answer.length(); i++)
{
if (Correct[i] == false)
System.out.print("_ ");
else
System.out.print(
Answer.substring(i, i + 1) + " ");
}
System.out.print("\n");
}
public char inputAnswer(BufferedReader br) throws IOException
{
for (;;)
{
System.out.print("? ");
String in = br.readLine().toUpperCase();
if (in.length() > 0)
return in.charAt(0);
}
}
}
class Program07
{
public static void main(String[] args) throws IOException
{
BufferedReader br = new BufferedReader(
new InputStreamReader(System.in));
System.out.print("--- HANGMAN ---\n\n");
int reply;
do{
Hangman game = new Hangman();
for (;;){
game.showState();
char ch = game.inputAnswer(br);
game.checkAnswer(ch);
if (game.checkFinish())
break;
}
System.out.print("AGAIN (1=YES; 0=NO!)?");
reply = Integer.parseInt( br.readLine() );
}while( reply != 0 );
}
}