Advertisement
AvengersAssemble

CardsHeap

Feb 7th, 2014
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.68 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace Cards
  7. {
  8.     class Heap
  9.     {
  10.         public static void Initialize(int[] cards)
  11.         {
  12.             for (int i = 0; i < cards.Length; i++)
  13.                 cards[i] = 0;
  14.         }
  15.         public static void InputInitialize(int[] cards, int n)
  16.         {
  17.             for (int i = 0; i <n; i++)
  18.                 cards[i] = int.Parse(Console.ReadLine());
  19.         }
  20.         public static void RandomInitializer(int[] cards, int n)
  21.         {
  22.             Random rnd = new Random();
  23.             for (int i = 0; i < n; i++)
  24.                 cards[i] = rnd.Next(1, 14);
  25.         }
  26.         public static void AddCard(int[] cards, int card)
  27.         {
  28.             int n = 0;
  29.             while (cards[n] != 0)
  30.                 n++;
  31.             cards[n] = card;
  32.         }
  33.         public static int DrawCard(int[] cards)
  34.         {
  35.             int tmp = cards[0];
  36.             for (int i = 0; i < cards.Length - 1; i++)
  37.                 cards[i] = cards[i + 1];
  38.             cards[cards.Length - 1] = 0;
  39.             return tmp;
  40.         }
  41.         public static bool Empty(int[] cards)
  42.         {
  43.             for (int i = 0; i < cards.Length; i++)
  44.             {
  45.                 if (cards[i] != 0)
  46.                     return false;
  47.             }
  48.             return true;
  49.         }
  50.     }
  51. }
  52.  
  53. ***************
  54.  
  55. using System;
  56. using System.Collections.Generic;
  57. using System.Linq;
  58. using System.Text;
  59.  
  60. namespace Cards
  61. {
  62.     class Program
  63.     {
  64.         static void Main(string[] args)
  65.         {
  66.             int playerOneCard, playerTwoCard;
  67.             int[] heapOne, heapTwo;
  68.             heapOne = new int[52];
  69.             heapTwo = new int[52];
  70.             Heap.Initialize(heapOne);
  71.             Heap.RandomInitializer(heapOne, 26);
  72.             Heap.Initialize(heapTwo);
  73.             Heap.RandomInitializer(heapTwo, 26);
  74.             while(!Heap.Empty(heapOne) && !Heap.Empty(heapTwo))
  75.             {
  76.                 playerOneCard = Heap.DrawCard(heapOne);
  77.                 playerTwoCard = Heap.DrawCard(heapTwo);
  78.                 if(playerOneCard > playerTwoCard)
  79.                 {
  80.                     Heap.AddCard(heapOne, playerOneCard);
  81.                     Heap.AddCard(heapOne, playerTwoCard);
  82.                 }
  83.                 else
  84.                 {
  85.                     Heap.AddCard(heapTwo, playerTwoCard);
  86.                     Heap.AddCard(heapTwo, playerOneCard);
  87.                 }
  88.             }
  89.             if(Heap.Empty(heapOne))
  90.                 Console.WriteLine("Winner is player two!");
  91.             else
  92.                 Console.WriteLine("Winner is player one!");
  93.         }
  94.     }
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement