計算機科学のブログ

reading and writing files - using Statements, System IO, StreamWriter, StreamReader

Head First C#: A Learner’s Guide to Real-World Programming with C# and .NET Core (Andrew Stellman(著)、Jennifer Greene(著)、O’Reilly Media)のChapter 10(reading and writing files - Save the last byte for me!)、p.549(Exercise)の解答を求めてみる。

コード

Program.cs

using System;
using System.IO;

namespace MyConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            for (int n = 0; n < 5; n++)
            {
                Console.WriteLine($"{n + 1}回目");
                var filename = "deckofcards.txt";
                Deck deck = new Deck();
                deck.Shuffle();
                for (int i = deck.Count - 1; i > 10; i--)
                {
                    deck.RemoveAt(i);
                }
                deck.WriteCards(filename);
                Deck cardsToRead = new Deck(filename);
                foreach (var card in cardsToRead)
                {
                    Console.WriteLine(card.Name);
                }
            }
        }
    }
}

コード

Deck.cs

using System;
using System.Collections.Generic;
using System.IO;

namespace MyConsoleApp
{
    public class Deck:List<Card>
    {
        private static Random random = new Random();
        public Deck()
        {
            Reset();
        }
        public Deck(string filename)
        {
            using (var sr = new StreamReader(filename))
            {
                while (!sr.EndOfStream)
                {
                    var nextCard = sr.ReadLine();
                    var cardParts = nextCard.Split(new char[] { ' ' });
                    var suit = cardParts[2] switch
                    {
                        "Diamonds" => Suits.Diamonds,
                        "Clubs" => Suits.Clubs,
                        "Hearts" => Suits.Hearts,
                        "Spades" => Suits.Spades,
                        _ => throw new InvalidDataException(cardParts[2]),
                    };
                    var value = cardParts[0] switch
                    {
                        "Ace" => Values.Ace,
                        "Two" => Values.Two,
                        "Three" => Values.Three,
                        "Four" => Values.Four,
                        "Five" => Values.Five,
                        "Six" => Values.Six,
                        "Seven" => Values.Seven,
                        "Eight" => Values.Eight,
                        "Nine" => Values.Nine,
                        "Ten" => Values.Ten,
                        "Jack" => Values.Jack,
                        "Queen" => Values.Queen,
                        "King" => Values.King,
                        _ => throw new InvalidDataException(cardParts[0]),
                    };
                    Add(new Card(value, suit));
                }
            }
        }
        public void Reset()
        {
            Clear();
            for (int suit = 0; suit < 4; suit++)
            {
                for (int value = 1; value < 14; value++)
                {
                    Add(new Card((Values)value, (Suits)suit));
                }
            }
        }
        public void Shuffle()
        {
            List<Card> cards = new List<Card>(this);
            Clear();
            while (cards.Count > 0)
            {
                int i = random.Next(cards.Count);
                Add(cards[i]);
                cards.RemoveAt(i);
            }
        }
        public Card Deal(int index)
        {
            Card card = base[index];
            RemoveAt(index);
            return card;
        }
        public void WriteCards(string filename)
        {
            using (var sw = new StreamWriter(filename))
            {
                foreach (var item in this)
                {
                    sw.WriteLine(item.Name);
                }
            }
        }
    }
}

入出力結果(Terminal, Zsh)

1回目
Queen of Clubs
Six of Diamonds
Ace of Hearts
Five of Hearts
Ten of Spades
Four of Diamonds
Nine of Spades
Four of Hearts
Jack of Spades
Seven of Spades
Seven of Hearts
2回目
Eight of Diamonds
Seven of Spades
Six of Diamonds
Ten of Diamonds
Six of Spades
Ten of Hearts
Queen of Diamonds
Six of Clubs
Two of Hearts
Queen of Spades
King of Hearts
3回目
Nine of Hearts
Eight of Diamonds
Five of Hearts
Nine of Spades
Queen of Hearts
Six of Diamonds
Ten of Diamonds
Six of Clubs
Two of Hearts
Ten of Clubs
Five of Clubs
4回目
King of Hearts
Three of Diamonds
Four of Spades
Eight of Clubs
Ten of Clubs
King of Diamonds
Nine of Spades
Ace of Clubs
Six of Spades
Six of Clubs
Seven of Hearts
5回目
Queen of Hearts
Ten of Hearts
Eight of Spades
Ace of Spades
King of Hearts
Ace of Hearts
Two of Clubs
Queen of Diamonds
Five of Spades
Ace of Diamonds
Jack of Hearts