Solved

Method List explanation on a Cut index method

Posted on 2013-10-24
3
280 Views
Last Modified: 2013-11-23
Could someone give me an explanation line by line of the cut method bellow-- public void Cut(int location). I also included the overall class from where this method is located bellow. Thank you.


public void Cut(int location)
        {
            int cutIndex = cards.Count - location;
            Card[] newCards = new Card[cards.Count];
            cards.CopyTo(cutIndex, newCards, 0, location);
            cards.CopyTo(0, newCards, location, cutIndex);
            cards.Clear();
            cards.InsertRange(0, newCards);
        }

Open in new window


Card class bellow:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace UsingDeckClass
{
    /// <summary>
    /// Provides a class for a deck of cards
    /// </summary>
    public class Deck
    {
        #region Fields

        List<Card> cards = new List<Card>();

        #endregion

        #region Constructors

        /// <summary>
        /// Constructor
        /// </summary>
        public Deck()
        {
            // fill the deck with cards
            foreach (Suit suit in Enum.GetValues(typeof(Suit)))
            {
                foreach (Rank rank in Enum.GetValues(typeof(Rank)))
                {
                    cards.Add(new Card(rank.ToString(), suit.ToString()));
                }
            }
        }

        #endregion

        #region Properties

        /// <summary>
        /// Gets whether the deck is empty
        /// </summary>
        public bool Empty
        {
            get { return cards.Count == 0; }
        }

        #endregion

        #region Public methods

        /// <summary>
        /// Cuts the deck of cards at the given location
        /// </summary>
        /// <param name="location">the location at which to cut the deck</param>
        public void Cut(int location)
        {
            //int cutIndex = cards.Count - location;
            //List<Card> newCards = new List<Card>(cards.Count);
            //for (int i = cutIndex; i < cards.Count; i++)
            //{
            //    newCards.Add(cards[i]);
            //}
            //for (int i = 0; i < cutIndex; i++)
            //{
            //    newCards.Add(cards[i]);
            //}
            //cards = newCards;

            int cutIndex = cards.Count - location;
            Card[] newCards = new Card[cards.Count];
            cards.CopyTo(cutIndex, newCards, 0, location);
            cards.CopyTo(0, newCards, location, cutIndex);
            cards.Clear();
            cards.InsertRange(0, newCards);
        }

        /// <summary>
        /// Shuffles the deck
        /// 
        /// Reference: http://download.oracle.com/javase/1.5.0/docs/api/java/util/Collections.html#shuffle%28java.util.List%29
        /// </summary>
        public void Shuffle()
        {
            Random rand = new Random();
            for (int i = cards.Count - 1; i > 0; i--)
            {
                int randomIndex = rand.Next(i + 1);
                Card tempCard = cards[i];
                cards[i] = cards[randomIndex];
                cards[randomIndex] = tempCard;
            }
        }

        /// <summary>
        /// Takes the top card from the deck. If the deck is empty, returns null
        /// </summary>
        /// <returns>the top card</returns>
        public Card TakeTopCard()
        {
            if (!Empty)
            {
                Card topCard = cards[cards.Count - 1];
                cards.RemoveAt(cards.Count - 1);
                return topCard;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// Prints the contents of the deck
        /// </summary>
        public void Print()
        {
            foreach (Card card in cards)
            {
                Console.WriteLine(card.Rank + " of " + card.Suit);
            }
        }
        #endregion
    }
}

Open in new window

0
Comment
Question by:yguyon28
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 39597545
Basically it is just cut/pasting a section of the list whilst maintaining the ordering.
eg.
If the list is 1 2 3 4 5 6 7 after the function it may well be 5 6 7 1 2 3 4

It does it by creating a new array (large enough to hold all the current array) then copies from the current array the two sections, removes the contents of the original array then copies this complete temporary array back into the original.
0
 

Author Comment

by:yguyon28
ID: 39597567
great explanation but can I have a line by line explanation of my code?
0
 
LVL 44

Accepted Solution

by:
AndyAinscow earned 500 total points
ID: 39597603
public void Cut(int location)
        {
//zero based index in array, find where the actual cut is to take place
            int cutIndex = cards.Count - location;
//creating a new array
            Card[] newCards = new Card[cards.Count];
//copy one section to temp array
            cards.CopyTo(cutIndex, newCards, 0, location);
//copy other section to temp array
            cards.CopyTo(0, newCards, location, cutIndex);
//empty original array
            cards.Clear();
//copy temp array into the original array
            cards.InsertRange(0, newCards);
        }
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Suggested Courses

634 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question