Skip to content

Instantly share code, notes, and snippets.

@hatone
Created April 24, 2012 11:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hatone/2478872 to your computer and use it in GitHub Desktop.
Save hatone/2478872 to your computer and use it in GitHub Desktop.
ヒットアンドブロー
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.util.HashSet;
import java.util.InputMismatchException;
import java.lang.StringBuffer;
/**
*HitAndBlowは、四桁の重複しない数字列を解答とし、その値を予測するゲームである。
*任意の数字と解答の数字が桁まで一致してる場合をヒット、
*任意の数字と解答の数字が一致していた場合をブロウとし、それぞれ数を表示する。
*4つの数字が全て完全に一致(ヒット)した場合に、終了する。
*
*@author 大島孝子
*/
public class HitAndBlow {
/**
*メインメソッド
*
*@param args 起動オプション
*/
public static void main(String[] args) {
String answerLine = randomNumber();
System.out.println("入力してください。");
while (true) {
String line = inputNumber();
if (line.equals("") ) {
continue;
}
if (checkAnswer (line, answerLine) ) {
break;
}
}
}
/**
*解答の成生
*4桁の重複しない数字の列をランダムに生成します。
*
*@return 整数4桁で構成される文字列
*/
private static String randomNumber() {
int[] numArray = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
for (int i = 0; i < 5; i++) {
int a = (int) (Math.random() * 10);
int b = (int) (Math.random() * 10);
int temp = numArray[a];
numArray[a] = numArray[b];
numArray[b] = temp;
}
StringBuffer stringBuffer = new StringBuffer(4);
for (int i = 0; i < 4; i++) {
stringBuffer.append( String.valueOf( numArray[i]));
}
return stringBuffer.toString();
}
/**
*入力メソッド
*4桁の数字列を入力します。
*
*
*@return 整数4桁で構成される文字列
*
*@exception InputMismatchException 入力数字列のフォーマットが不正である
*@exception NumberFormatException 数値の中に文字列が入っている
*@exception IOException 入力数字列が空である
*/
private static String inputNumber() {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String line = "";
try {
line = reader.readLine();
if (line.length() == 0) {
System.out.println( "入力されていません。" );
throw new InputMismatchException();
}
Integer.parseInt(line);
line = line.replace("+", " ");
line = line.replace("-", " ");
if (line.length() != 4) {
System.out.println("入力が4桁ではありません。");
throw new InputMismatchException();
}
if (numFormat(line) != true) {
System.out.println("数値が重複しています。");
throw new InputMismatchException();
}
} catch (NumberFormatException e) {
System.out.println( "入力は数値のみです。" );
return "";
} catch (InputMismatchException e) {
return "";
} catch (IOException e) {
return "";
}
return line;
}
/**
*文字列の重複判別
*
*@param s チェックされる整数4桁で構成される文字列
*@return 判別結果
*/
private static boolean numFormat(String s) {
//charc型
//right than string
HashSet<String> set = new HashSet<String>();
for (int i = 0; i < s.length(); i++) {
set.add( String.valueOf( s.charAt(i) ) );
}
return set.size() == 4;
}
/**
*解答の評価
*
*@param inputStr ユーザが入力した文字列
*@param randomStr 解答の文字列
*
*@return 判定結果
*/
private static boolean checkAnswer(String inputStr, String randomStr) {
int hit = 0;
int blow = 0;
for (int i = 0; i < 4; i++) {
if (inputStr.charAt(i) == randomStr.charAt(i)) {
hit++;
}
if (randomStr.indexOf(inputStr.charAt(i)) != -1) {
blow++;
}
}
boolean ret = false;
if (hit == 4) {
System.out.println("おめでとうございます!");
ret=true;
}else {
System.out.println("ヒット:" + hit + " ブロウ:" + (blow-hit));
}
return ret;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment