TheBulgarianWolf

Cows and bulls game

Apr 2nd, 2020
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.34 KB | None | 0 0
  1. package softuni;
  2.  
  3. import java.util.Scanner;
  4.  
  5.  
  6.  
  7. public class SoftUni {
  8.  
  9.    
  10.     public static void main(String[] args){
  11.         Scanner sc = new Scanner(System.in);
  12.         System.out.print("Enter your number: ");
  13.         int guessNumber = Integer.parseInt(sc.nextLine());
  14.         System.out.print("Enter the number of bulls: ");
  15.         int targetBulls = Integer.parseInt(sc.nextLine());
  16.         System.out.print("Enter the number of cows: ");
  17.         int targetCows = Integer.parseInt(sc.nextLine());
  18.         boolean solutionFound = false;
  19.        
  20.         int guessDigit1 = (guessNumber / 1000) % 10;
  21.         int guessDigit2 = (guessNumber / 100) % 10;
  22.         int guessDigit3 = (guessNumber / 10) % 10;
  23.         int guessDigit4 = (guessNumber / 1) % 10;
  24.        
  25.        
  26.         for(int digit1 = 1;digit1 <= 9;digit1++){
  27.             for(int digit2 = 1;digit2 <= 9;digit2++){
  28.                 for(int digit3 = 1;digit3 <= 9;digit3++){
  29.                     for(int digit4 = 1;digit4 <= 9;digit4++){
  30.                        
  31.                         int currentBulls = 0;
  32.                         int currentCows = 0;
  33.                         int digitToCheck = digit1;
  34.                         int digitToCheck2 = digit2;
  35.                         int digitToCheck3 = digit3;
  36.                         int digitToCheck4 = digit4;
  37.                         int guess1 = guessDigit1;
  38.                         int guess2 = guessDigit2;
  39.                         int guess3 = guessDigit3;
  40.                         int guess4 = guessDigit4;
  41.                        
  42.                        
  43.                        
  44.                         if(digitToCheck == guess1){
  45.                             currentBulls++;
  46.                             digitToCheck = -2;
  47.                             guess1 = -1;
  48.                         }
  49.                        
  50.                         if(digitToCheck2 == guess2){
  51.                             currentBulls++;
  52.                             digitToCheck2 = -2;
  53.                             guess2 = -1;
  54.                         }
  55.                        
  56.                         if(digitToCheck3 == guess3){
  57.                             currentBulls++;
  58.                             digitToCheck3 = -2;
  59.                             guess3 = -1;
  60.                         }
  61.                        
  62.                         if(digitToCheck4 == guess4){
  63.                             currentBulls++;
  64.                             digitToCheck4 = -2;
  65.                             guess4 = -1;
  66.                         }
  67.                        
  68.                         if(digitToCheck == guess2){
  69.                             currentCows++;
  70.                             guess2 = -1;
  71.                         }
  72.                         else if(digitToCheck == guess3){
  73.                             currentCows++;
  74.                             guess3 = -1;
  75.                         }
  76.                         else if(digitToCheck == guess4){
  77.                             currentCows++;
  78.                             guess4 = -1;
  79.                         }
  80.                        
  81.                        
  82.                         if(digitToCheck2 == guess1){
  83.                             currentCows++;
  84.                             guess1 = -1;
  85.                         }
  86.                         else if(digitToCheck2 == guess3){
  87.                             currentCows++;
  88.                             guess3 = -1;
  89.                         }
  90.                         else if(digitToCheck2 == guess4){
  91.                             currentCows++;
  92.                             guess4 = -1;
  93.                         }
  94.                        
  95.                         if(digitToCheck3 == guess1){
  96.                             currentCows++;
  97.                             guess1 = -1;
  98.                         }
  99.                         else if(digitToCheck3 == guess2){
  100.                             currentCows++;
  101.                             guess2 = -1;
  102.                         }
  103.                         else if(digitToCheck3 == guess4){
  104.                             currentCows++;
  105.                             guess4 = -1;
  106.                         }
  107.                        
  108.                         if(digitToCheck4 == guess1){
  109.                             currentCows++;
  110.                             guess1 = -1;
  111.                         }
  112.                         else if(digitToCheck4 == guess2){
  113.                             currentCows++;
  114.                             guess2 = -1;
  115.                         }
  116.                         else if(digitToCheck4 == guess3){
  117.                             currentCows++;
  118.                             guess3 = -1;
  119.                         }
  120.                        
  121.                        
  122.                         if(currentBulls == targetBulls && currentCows == targetCows){
  123.                             System.out.print("" + digit1 + digit2 + digit3 + digit4);
  124.                             solutionFound = true;
  125.                            
  126.                             if(solutionFound){
  127.                                 System.out.print(" ");
  128.                             }
  129.                            
  130.                            
  131.                         }
  132.                     }
  133.                 }
  134.             }
  135.         }
  136.        
  137.         if(solutionFound == false){
  138.             System.out.println("No");
  139.         }
  140.     }
  141.    
  142. }
Add Comment
Please, Sign In to add comment