Advertisement
dc1394

Untitled

Jan 13th, 2020
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.02 KB | None | 0 0
  1. // 2020/1/12(日)
  2. // 2020/1/14(火)L8改変
  3. import java.util.*;
  4. import java.util.stream.*;
  5.  
  6. public class HitAndBlow
  7. {
  8.     private List<String> answer;
  9.     private int blow;
  10.     private int hit;
  11.     private List<String> inputnum;
  12.     private Scanner scan = new Scanner(System.in);
  13.  
  14.     public HitAndBlow()
  15.     {
  16.         List<String> strs = IntStream.range(0, 10)
  17.                                      .mapToObj(Integer::valueOf)
  18.                                      .map(i -> String.valueOf(i))
  19.                                      .collect(Collectors.toList());
  20.  
  21.         Collections.shuffle(strs);
  22.  
  23.         answer = strs.stream().skip(6).collect(Collectors.toList());
  24.     }
  25.  
  26.     public void playgame()
  27.     {
  28.         System.out.println("ヒットアンドブローをします");
  29.  
  30.         boolean isNext = true;
  31.         for (int i = 1; isNext; i++)
  32.         {
  33.             this.inputnum();
  34.             if (this.checkhitandblow())
  35.             {
  36.                 System.out.println("正解です!ゲームを終了します。");
  37.                 return;
  38.             }
  39.  
  40.             System.out.println(i + "回目のトライ " + hit + "ヒット!" + blow + "ブロー!です");
  41.             System.out.print("続けますか? 続ける:1を入力 止める:1以外を入力:");
  42.  
  43.             switch (scan.nextLine())
  44.             {
  45.                 case "1":
  46.                     break;
  47.  
  48.                 default:
  49.                     isNext = false;
  50.                     break;
  51.             }
  52.         }
  53.  
  54.         System.out.println("正解は" + String.join("", answer) + "でした");
  55.     }
  56.  
  57.     private boolean checkhitandblow()
  58.     {
  59.         List<String> templist = new ArrayList<String>();
  60.  
  61.         hit = 0;
  62.         for (int i = 0; i < answer.size(); i++)
  63.         {
  64.             if (answer.get(i).equals(inputnum.get(i)))
  65.             {
  66.                 hit++;
  67.                 templist.add(inputnum.get(i));
  68.             }
  69.         }
  70.  
  71.         if (hit == 4)
  72.         {
  73.             return true;
  74.         }
  75.  
  76.         List<String> templist2 = new ArrayList<String>(inputnum);
  77.         templist2.removeAll(templist);
  78.  
  79.         blow = (int)(answer.stream()
  80.                            .filter(s -> templist2.contains(s))
  81.                            .count());
  82.  
  83.         return false;
  84.     }
  85.  
  86.     private void inputnum()
  87.     {
  88.         do
  89.         {
  90.             System.out.print("4桁の数字を入力してください:");
  91.             String line = scan.nextLine();
  92.  
  93.             // 数値に変換可能か
  94.             try
  95.             {
  96.                 Integer.parseInt(line);
  97.             }
  98.             catch (NumberFormatException nfex)
  99.             {
  100.                 continue;
  101.             }
  102.  
  103.             // 入力は4桁か
  104.             if (line.length() == 4)
  105.             {
  106.                 inputnum = Arrays.asList(line.split(""));
  107.                 return;
  108.             }
  109.         } while (true);
  110.     }
  111.  
  112.     public static void main(String[] args)
  113.     {
  114.         new HitAndBlow().playgame();
  115.     }
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement