vonko1988

CardWars

Jul 22nd, 2014
277
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.52 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Numerics;
  4.  
  5. class CardWars
  6. {
  7.     static void Main()
  8.     {
  9.         int n = int.Parse(Console.ReadLine());
  10.  
  11.         //in these lists will be stored the cards drawn for each player
  12.         List<string> playerOneCards = new List<string>();
  13.         List<string> playerTwoCards = new List<string>();
  14.  
  15.         string currentCard = "";
  16.  
  17.         //draw the cards
  18.         for (int gameRound = 0; gameRound < n; gameRound++)
  19.         {
  20.             //player one cards for each game
  21.             for (int j = 0; j < 3; j++)
  22.             {
  23.                 currentCard = Console.ReadLine();
  24.                 playerOneCards.Add(currentCard);
  25.             }
  26.             //player two cards for each game
  27.             for (int j = 0; j < 3; j++)
  28.             {
  29.                 currentCard = Console.ReadLine();
  30.                 playerTwoCards.Add(currentCard);
  31.             }
  32.         }
  33.  
  34.         //the card types and their scores will be stored in char arrays
  35.         string[] cardTypes = { "2", "3", "4", "5", "6", "7", "8", "9", "10", "A", "J", "Q", "K", "Y", "Z", "X" };
  36.         int[] cardScores = { 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 11, 12, 13 };
  37.  
  38.         string output = "";
  39.  
  40.         BigInteger playerOneTotalScore = 0;
  41.         BigInteger playerOneWonGames = 0;
  42.  
  43.         BigInteger playerTwoTotalScore = 0;
  44.         BigInteger playerTwoWonGames = 0;
  45.  
  46.         int offset = 0;
  47.  
  48.         //calculate the socres
  49.         for (int gameRound = 0; gameRound < n; gameRound++)
  50.         {
  51.             bool playerOneWinCard = false;
  52.             bool playerTwoWinCard = false;
  53.  
  54.             BigInteger playerOneCurrentScore = 0;
  55.             BigInteger playerTwoCurrentScore = 0;
  56.  
  57.             //check the cards of player one for this round
  58.             for (int i = 0; i < 3; i++)
  59.             {
  60.                 int currentIndexPlayerOne = Array.IndexOf(cardTypes, playerOneCards[i + offset]);
  61.  
  62.                 if (currentIndexPlayerOne != 13 && currentIndexPlayerOne != 14 && currentIndexPlayerOne != 15)
  63.                 {
  64.                     playerOneCurrentScore += cardScores[currentIndexPlayerOne];
  65.                 }
  66.                 else if (currentIndexPlayerOne == 13)
  67.                 {
  68.                     playerOneTotalScore -= 200;
  69.                 }
  70.                 else if (currentIndexPlayerOne == 14)
  71.                 {
  72.                     playerOneTotalScore *= 2;
  73.                 }
  74.                 else if (currentIndexPlayerOne == 15)
  75.                 {
  76.                     playerOneWinCard = true;
  77.                 }
  78.             }
  79.  
  80.             //check the cards of player two for this round
  81.             for (int i = 0; i < 3; i++)
  82.             {
  83.                 int currentIndexPlayerTwo = Array.IndexOf(cardTypes, playerTwoCards[i + offset]);
  84.  
  85.                 if (currentIndexPlayerTwo != 13 && currentIndexPlayerTwo != 14 && currentIndexPlayerTwo != 15)
  86.                 {
  87.                     playerTwoCurrentScore += cardScores[currentIndexPlayerTwo];
  88.                 }
  89.                 else if (currentIndexPlayerTwo == 13)
  90.                 {
  91.                     playerTwoTotalScore -= 200;
  92.                 }
  93.                 else if (currentIndexPlayerTwo == 14)
  94.                 {
  95.                     playerTwoTotalScore *= 2;
  96.                 }
  97.                 else if (currentIndexPlayerTwo == 15)
  98.                 {
  99.                     playerTwoWinCard = true;
  100.                 }
  101.             }
  102.  
  103.             //check who is the winner of the round
  104.             if (!playerOneWinCard && !playerTwoWinCard &&
  105.                 playerOneCurrentScore > playerTwoCurrentScore)
  106.             {
  107.                 playerOneTotalScore += playerOneCurrentScore;
  108.                 playerOneWonGames++;
  109.             }
  110.             else if (!playerOneWinCard && !playerTwoWinCard &&
  111.                 playerTwoCurrentScore > playerOneCurrentScore)
  112.             {
  113.                 playerTwoTotalScore += playerTwoCurrentScore;
  114.                 playerTwoWonGames++;
  115.             }
  116.             else if (playerOneWinCard && playerTwoWinCard)
  117.             {
  118.                 playerOneTotalScore += 50;
  119.                 playerTwoTotalScore += 50;
  120.             }
  121.             else if (playerOneWinCard && !playerTwoWinCard)
  122.             {
  123.                 output = "X card drawn! Player one wins the match!";
  124.                 break;
  125.             }
  126.             else if (!playerOneWinCard && playerTwoWinCard)
  127.             {
  128.                 output = "X card drawn! Player two wins the match!";
  129.                 break;
  130.             }
  131.  
  132.             offset += 3;
  133.         }
  134.  
  135.         //print the final result
  136.         if (output == "")
  137.         {
  138.             if (playerOneTotalScore > playerTwoTotalScore)
  139.             {
  140.                 Console.WriteLine("First player wins!");
  141.                 Console.WriteLine("Score: {0}", playerOneTotalScore);
  142.                 Console.WriteLine("Games won: {0}", playerOneWonGames);
  143.             }
  144.             else if (playerTwoTotalScore > playerOneTotalScore)
  145.             {
  146.                 Console.WriteLine("Second player wins!");
  147.                 Console.WriteLine("Score: {0}", playerTwoTotalScore);
  148.                 Console.WriteLine("Games won: {0}", playerTwoWonGames);
  149.             }
  150.             else if (playerOneTotalScore == playerTwoTotalScore)
  151.             {
  152.                 Console.WriteLine("It's a tie!");
  153.                 Console.WriteLine("Score: {0}", playerOneTotalScore);
  154.             }
  155.         }
  156.         else
  157.         {
  158.             Console.WriteLine(output);
  159.         }
  160.     }
  161. }
Advertisement
Add Comment
Please, Sign In to add comment