Advertisement
dc1394

Untitled

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