Advertisement
chefache

Untitled

Aug 13th, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.21 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace Bulls_and_Cows_12._08._19
  6. {
  7. class Program
  8. {
  9. static void Main(string[] args)
  10. {
  11. Console.Write("Please enter your number: ");
  12.  
  13. int enterNum = int.Parse(Console.ReadLine());
  14. List<int> myNum = new List<int>();
  15. while (enterNum > 0)
  16. {
  17. myNum.Add(enterNum % 10);
  18. enterNum /= 10;
  19. }
  20. myNum.Reverse();
  21.  
  22. var compNum = new List<int>(4);
  23. var cowList = new List<int>(4);
  24. var bullList = new List<int>(4);
  25.  
  26. Random compRndNum = new Random();
  27.  
  28. compNum.Add(compRndNum.Next(0, 10));
  29. compNum.Add(compRndNum.Next(0, 10));
  30. compNum.Add(compRndNum.Next(0, 10));
  31. compNum.Add(compRndNum.Next(0, 10));
  32.  
  33. int cowCount = 0;
  34.  
  35. while (cowList.Count < 4)
  36. {
  37. while ((compNum[0] == 0) || (compNum[0] == compNum[1] || compNum[0] == compNum[2] || compNum[0] == compNum[3] ||
  38. compNum[1] == compNum[0] || compNum[1] == compNum[2] || compNum[1] == compNum[3] ||
  39. compNum[2] == compNum[0] || compNum[2] == compNum[1] || compNum[2] == compNum[3] ||
  40. compNum[3] == compNum[0] || compNum[3] == compNum[1] || compNum[3] == compNum[2]))
  41. {
  42. if (compNum[0] == 0)
  43. {
  44. Console.WriteLine($"Number is: {string.Join("", compNum)}");
  45. System.Threading.Thread.Sleep(1200);
  46. Console.WriteLine("ERROR - There is number start with zero");
  47. System.Threading.Thread.Sleep(1200);
  48. compNum.Clear();
  49. Console.WriteLine("Pick new number");
  50. System.Threading.Thread.Sleep(1200);
  51. compNum.Add(compRndNum.Next(0, 10));
  52. compNum.Add(compRndNum.Next(0, 10));
  53. compNum.Add(compRndNum.Next(0, 10));
  54. compNum.Add(compRndNum.Next(0, 10));
  55. }
  56. if (compNum[0] == compNum[1] || compNum[0] == compNum[2] || compNum[0] == compNum[3] ||
  57. compNum[1] == compNum[0] || compNum[1] == compNum[2] || compNum[1] == compNum[3] ||
  58. compNum[2] == compNum[0] || compNum[2] == compNum[1] || compNum[2] == compNum[3] ||
  59. compNum[3] == compNum[0] || compNum[3] == compNum[1] || compNum[3] == compNum[2])
  60. {
  61. Console.WriteLine($"Number is: {string.Join("", compNum)}");
  62. System.Threading.Thread.Sleep(1200);
  63. Console.WriteLine("ERROR - There is two equal numbers");
  64. System.Threading.Thread.Sleep(1200);
  65. compNum.Clear();
  66. Console.WriteLine("Pick new number");
  67. System.Threading.Thread.Sleep(1200);
  68. compNum.Add(compRndNum.Next(0, 10));
  69. compNum.Add(compRndNum.Next(0, 10));
  70. compNum.Add(compRndNum.Next(0, 10));
  71. compNum.Add(compRndNum.Next(0, 10));
  72. }
  73. }
  74. if (myNum.Contains(compNum[0]) && (!cowList.Contains(compNum[0])))
  75. {
  76. cowCount++;
  77. Console.WriteLine($"Number is: {string.Join("", compNum)}");
  78. System.Threading.Thread.Sleep(1000);
  79. Console.WriteLine($"I found cow, its number: {compNum[0]}.");
  80. System.Threading.Thread.Sleep(1000);
  81. cowList.Add(compNum[0]);
  82.  
  83. }
  84. if (myNum.Contains(compNum[1]) && (!cowList.Contains(compNum[1])))
  85. {
  86. cowCount++;
  87. Console.WriteLine($"Number is: {string.Join("", compNum)}");
  88. System.Threading.Thread.Sleep(1000);
  89. Console.WriteLine($"I found cow, its number: {compNum[1]}.");
  90. System.Threading.Thread.Sleep(1000);
  91. cowList.Add(compNum[1]);
  92. }
  93. if (myNum.Contains(compNum[2]) && (!cowList.Contains(compNum[2])))
  94. {
  95. cowCount++;
  96. Console.WriteLine($"Number is: {string.Join("", compNum)}");
  97. System.Threading.Thread.Sleep(1000);
  98. Console.WriteLine($"I found cow, its number: {compNum[2]}.");
  99. System.Threading.Thread.Sleep(1000);
  100. cowList.Add(compNum[2]);
  101. }
  102. if (myNum.Contains(compNum[3]) && (!cowList.Contains(compNum[3])))
  103. {
  104. cowCount++;
  105. Console.WriteLine($"Number is: {string.Join("", compNum)}");
  106. System.Threading.Thread.Sleep(1000);
  107. Console.WriteLine($"I found cow, its number: {compNum[3]}.");
  108. System.Threading.Thread.Sleep(1000);
  109. cowList.Add(compNum[3]);
  110. }
  111. compNum.Clear();
  112. compNum.Add(compRndNum.Next(0, 10));
  113. compNum.Add(compRndNum.Next(0, 10));
  114. compNum.Add(compRndNum.Next(0, 10));
  115. compNum.Add(compRndNum.Next(0, 10));
  116. }
  117. Console.WriteLine($"All the cows are: {string.Join("", cowList)}");
  118. System.Threading.Thread.Sleep(1200);
  119. if (cowList.Count == 4)
  120. {
  121. Console.WriteLine("");
  122. Console.WriteLine("Start serching for bulls !!!");
  123. System.Threading.Thread.Sleep(1200);
  124.  
  125. for (int i = 0; i < myNum.Count; i++)
  126. {
  127. if (cowList[i] == myNum[0])
  128. {
  129. Console.WriteLine($"Current number is: {cowList[i]}");
  130. System.Threading.Thread.Sleep(1200);
  131. var rightPosition = myNum.IndexOf(myNum[0]);
  132. Console.WriteLine($"Rigth position is: {myNum.IndexOf(myNum[0])}");
  133. System.Threading.Thread.Sleep(1200);
  134. Console.WriteLine($"I will insert {cowList[i]} at {myNum.IndexOf(myNum[0])} position.");
  135. System.Threading.Thread.Sleep(1200);
  136. bullList.Insert(rightPosition, cowList[i]);
  137. }
  138.  
  139. }
  140. for (int i = 0; i < myNum.Count; i++)
  141. {
  142. if (cowList[i] == myNum[1])
  143. {
  144. Console.WriteLine($"Current number is: {cowList[i]}");
  145. System.Threading.Thread.Sleep(1200);
  146. var rightPosition = myNum.IndexOf(myNum[1]);
  147. Console.WriteLine($"Rigth position is: {myNum.IndexOf(myNum[1])}");
  148. System.Threading.Thread.Sleep(1200);
  149. Console.WriteLine($"I will insert {cowList[i]} at {myNum.IndexOf(myNum[1])} position.");
  150. System.Threading.Thread.Sleep(1200);
  151. bullList.Insert(rightPosition, cowList[i]);
  152. }
  153.  
  154. }
  155. for (int i = 0; i < myNum.Count; i++)
  156. {
  157. if (cowList[i] == myNum[2])
  158. {
  159. Console.WriteLine($"Current number is: {cowList[i]}");
  160. System.Threading.Thread.Sleep(1200);
  161. var rightPosition = myNum.IndexOf(myNum[2]);
  162. Console.WriteLine($"Rigth position is: {myNum.IndexOf(myNum[2])}");
  163. System.Threading.Thread.Sleep(1200);
  164. Console.WriteLine($"I will insert {cowList[i]} at {myNum.IndexOf(myNum[2])} position.");
  165. System.Threading.Thread.Sleep(1200);
  166. bullList.Insert(rightPosition, cowList[i]);
  167. }
  168.  
  169. }
  170. for (int i = 0; i < myNum.Count; i++)
  171. {
  172. if (cowList[i] == myNum[3])
  173. {
  174. Console.WriteLine($"Current number is: {cowList[i]}");
  175. System.Threading.Thread.Sleep(1200);
  176. var rightPosition = myNum.IndexOf(myNum[3]);
  177. Console.WriteLine($"Rigth position is: {myNum.IndexOf(myNum[3])}");
  178. System.Threading.Thread.Sleep(1200);
  179. Console.WriteLine($"I will insert {cowList[i]} at {myNum.IndexOf(myNum[3])} position.");
  180. System.Threading.Thread.Sleep(1200);
  181. bullList.Insert(rightPosition, cowList[i]);
  182. }
  183.  
  184. }
  185. }
  186. Console.WriteLine($"I Guess !!!\nYour umber is: {string.Join("", bullList)} !!!");
  187. }
  188.  
  189. }
  190. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement