Advertisement
SteelGolem

c# yahtzee (completed)

Feb 4th, 2023
701
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.18 KB | Gaming | 0 0
  1.  
  2. Random random = new Random();
  3. int[] dice = new int[5];
  4. bool[] reroll_toggle = new bool[5];
  5. int max_rerolls = 2;
  6. int rerolls_left = max_rerolls;
  7. // dice[] store 1..6 and k input will be '1'..'6'
  8. // so to make it easier on my brain, i'll
  9. // index singles[] with 1..6 and ignore singles[0]
  10. int[] singles = new int[] { 0, -1, -1, -1, -1, -1, -1 };
  11. int kind3 = -1;
  12. int kind4 = -1;
  13. int full = -1;
  14. int small = -1;
  15. int large = -1;
  16. int kind5 = -1;
  17. int chance = -1;
  18. int extra_yahtzees = 0;
  19. while (true)
  20. {
  21.     Console.Clear();
  22.     score_card();
  23.     Console.Write("\npress a key to roll dice...");
  24.     if (!reroll_toggle.Contains(true))
  25.         Console.ReadKey(true);
  26.     Console.Write("\n\n");
  27.     roll_dice();
  28.     reroll_toggle = new bool[5];
  29.     for (int i = 0; i < 5; i++)
  30.         Console.Write("[" + dice[i] + "] ");
  31.     Console.Write("\n\n(" + rerolls_left + " rerolls left)");
  32.     Console.Write("\nchoose scoring");
  33.     if (rerolls_left > 0) Console.Write(" or press r to reroll");
  34.     Console.Write("...");
  35.     bool got_input = false;
  36.     bool scored = false;
  37.     while (!got_input)
  38.     {
  39.         char k = Console.ReadKey(true).KeyChar;
  40.         if ("12345678fsl90r".Contains(k))
  41.         {
  42.             if (k >= '1' && k <= '6')
  43.             {
  44.                 int digit = k - '0';
  45.                 if (singles[digit] == -1)
  46.                 {
  47.                     singles[digit] = count_singles(digit);
  48.                     scored = true;
  49.                 }
  50.             }
  51.             if (k == '7' && kind3 == -1)
  52.             {
  53.                 if (is_kind3()) kind3 = add_all();
  54.                 else kind3 = 0;
  55.                 scored = true;
  56.             }
  57.             if (k == '8' && kind4 == -1)
  58.             {
  59.                 if (is_kind4()) kind4 = add_all();
  60.                 else kind4 = 0;
  61.                 scored = true;
  62.             }
  63.             if (k == 'f' && full == -1)
  64.             {
  65.                 if (is_full()) full = 25;
  66.                 else full = 0;
  67.                 scored = true;
  68.             }
  69.             if (k == 's' && small == -1)
  70.             {
  71.                 if (is_small()) small = 30;
  72.                 else small = 0;
  73.                 scored = true;
  74.             }
  75.             if (k == 'l' && large == -1)
  76.             {
  77.                 if (is_large()) large = 40;
  78.                 else large = 0;
  79.                 scored = true;
  80.             }
  81.             if (k == '9' && kind5 != 0)
  82.             {
  83.                 if (is_kind5())
  84.                 {
  85.                     if (kind5 == -1) kind5 = 50;
  86.                     else extra_yahtzees += 100;
  87.                 }
  88.                 else kind5 = 0;
  89.                 scored = true;
  90.             }
  91.             if (k == '0' && chance == -1)
  92.             {
  93.                 chance = add_all();
  94.                 scored = true;
  95.             }
  96.             if (k == 'r' && rerolls_left > 0)
  97.             {
  98.                 Console.Write("\rnumber key to toggle, enter to finish\r");
  99.                 Console.CursorTop -= 3;
  100.                 select_reroll();
  101.                 rerolls_left--;
  102.                 got_input = true;
  103.             }
  104.             if (scored)
  105.             {
  106.                 rerolls_left = max_rerolls;
  107.                 got_input = true;
  108.             }
  109.         }
  110.     }
  111.     if (!reroll_toggle.Contains(true))
  112.     {
  113.         // redraw scorecard and wait
  114.         var old_pos = Console.GetCursorPosition();
  115.         Console.SetCursorPosition(0, 0);
  116.         score_card();
  117.         Console.SetCursorPosition(old_pos.Left, old_pos.Top);
  118.         if (singles[1] != -1 && singles[2] != -1 && singles[3] != -1 &&
  119.             singles[4] != -1 && singles[5] != -1 && singles[6] != -1 &&
  120.             kind3 != -1 && kind4 != -1 && full != -1 &&
  121.             small != -1 && large != -1 && kind5 != -1 && chance != -1)
  122.             break;
  123.         Console.Write("\n\npress a key...");
  124.         Console.ReadKey(true);
  125.     }
  126. }
  127. Console.Write("\n\ngame over!");
  128. int score = kind3 + kind4 + full + small + large +
  129.     kind5 + chance + extra_yahtzees;
  130. for (int i = 1; i <= 6; i++)
  131.     score += singles[i];
  132. Console.Write("\ntotal score: " + score);
  133. while (true) ;
  134.  
  135. void score_card()
  136. {
  137.     Console.Write(
  138.         "1) ones            (add only ones)    " + singles[1] + " \n" +
  139.         "2) twos            (add only twos)    " + singles[2] + " \n" +
  140.         "3) threes        (add only threes)    " + singles[3] + " \n" +
  141.         "4) fours          (add only fours)    " + singles[4] + " \n" +
  142.         "5) fives          (add only fives)    " + singles[5] + " \n" +
  143.         "6) sixes          (add only sixes)    " + singles[6] + " \n" +
  144.         "7) 3 of a kind      (add all dice)    " + kind3 + " \n" +
  145.         "8) 4 of a kind      (add all dice)    " + kind4 + " \n" +
  146.         "f) full house          (25 points)    " + full + " \n" +
  147.         "s) small straight 4    (30 points)    " + small + " \n" +
  148.         "l) large straight 5    (40 points)    " + large + " \n" +
  149.         "9) yahtzee (5kind)     (50 points)    " + kind5 + " \n" +
  150.         "0) chance           (add all dice)    " + chance + " \n" +
  151.         "   extra yahtzees     (100 points)    " + extra_yahtzees + " \n");
  152. }
  153.  
  154. void roll_dice()
  155. {
  156.     if (reroll_toggle.Contains(true))
  157.     {
  158.         for (int i = 0; i < 5; i++)
  159.             if (reroll_toggle[i])
  160.                 dice[i] = random.Next(6) + 1;
  161.     }
  162.     else
  163.     {
  164.         for (int i = 0; i < 5; i++)
  165.             dice[i] = random.Next(6) + 1;
  166.     }
  167. }
  168.  
  169. int count_singles(int face)
  170. {
  171.     int total = 0;
  172.     for (int i = 0; i < 5; i++)
  173.         if (dice[i] == face) total += face;
  174.     return total;
  175. }
  176.  
  177. bool is_kind3()
  178. {
  179.     List<int> list = dice.ToList();
  180.     list.Sort();
  181.     int first = 2; // last 3
  182.     if (list[0] == list[1]) first = 0; // first 3
  183.     else if (list[1] == list[2]) first = 1; // middle 3
  184.     int match = list[first];
  185.     bool fail = false;
  186.     for (int i = 0; i < 3; i++)
  187.         if (list[first + i] != match) fail = true;
  188.     return !fail;
  189. }
  190.  
  191. bool is_kind4()
  192. {
  193.     List<int> list = dice.ToList();
  194.     list.Sort();
  195.     int first = 1; // last 4
  196.     if (list[0] == list[1]) first = 0; // first 4
  197.     int match = list[first];
  198.     bool fail = false;
  199.     for (int i = 0; i < 4; i++)
  200.         if (list[first + i] != match) fail = true;
  201.     return !fail;
  202. }
  203.  
  204. bool is_kind5()
  205. {
  206.     int match = dice[0];
  207.     bool fail = false;
  208.     for (int i = 0; i < 5; i++)
  209.         if (dice[i] != match) fail = true;
  210.     return !fail;
  211. }
  212.  
  213. bool is_full()
  214. {
  215.     List<int> list = dice.ToList();
  216.     list.Sort();
  217.     if (list[0] == list[1] && list[3] == list[4])
  218.         if (list[2] == list[0] || list[2] == list[4])
  219.             return true;
  220.     return false;
  221. }
  222.  
  223. bool is_small()
  224. {
  225.     // 1234 run
  226.     bool fail = false;
  227.     for (int i = 1; i <= 4; i++)
  228.         if (!dice.Contains(i)) fail = true;
  229.     if (!fail) return true;
  230.     // 2345 run
  231.     fail = false;
  232.     for (int i = 2; i <= 5; i++)
  233.         if (!dice.Contains(i)) fail = true;
  234.     if (!fail) return true;
  235.     // 3456 run
  236.     fail = false;
  237.     for (int i = 3; i <= 6; i++)
  238.         if (!dice.Contains(i)) fail = true;
  239.     if (!fail) return true;
  240.     return false;
  241. }
  242.  
  243. bool is_large()
  244. {
  245.     // 12345 run
  246.     bool fail = false;
  247.     for (int i = 1; i <= 5; i++)
  248.         if (!dice.Contains(i)) fail = true;
  249.     if (!fail) return true;
  250.     // 23456 run
  251.     fail = false;
  252.     for (int i = 2; i <= 6; i++)
  253.         if (!dice.Contains(i)) fail = true;
  254.     if (!fail) return true;
  255.     return false;
  256. }
  257.  
  258. int add_all()
  259. {
  260.     int total = 0;
  261.     for (int i = 0; i < 5; i++)
  262.         total += dice[i];
  263.     return total;
  264. }
  265.  
  266. void select_reroll()
  267. {
  268.     reroll_toggle = new bool[] { false, false, false, false, false };
  269.     while (true) // go until enter ('\r')
  270.     {
  271.         char k = ' ';
  272.         while(true)
  273.         {
  274.             k = Console.ReadKey(true).KeyChar;
  275.             if ("12345\r".Contains(k)) break;
  276.         }
  277.         if (k == '\r') break;
  278.         int i = k - '0' - 1;
  279.         reroll_toggle[i] = !reroll_toggle[i];
  280.         Console.Write("\r");
  281.         for (i = 0; i < 5; i++)
  282.         {
  283.             if (reroll_toggle[i]) Console.Write("[*] ");
  284.             else Console.Write("[" + dice[i] + "] ");
  285.         }
  286.     }
  287. }
  288.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement