1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

オフラインリアルタイムどう書く第三回の参考問題をC#で

Last updated at Posted at 2012-08-20

オフラインリアルタイムどう書く第三回の参考問題

GenerateメソッドがC#っぽいところかな。

Program.cs
using System;
using System.Collections.Generic;

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine(Calculate("s"));
        Console.WriteLine(Calculate("sss"));
        Console.WriteLine(Calculate("bbbb"));
        Console.WriteLine(Calculate("ssbbbb"));
        Console.WriteLine(Calculate("hsbhfhbh"));
        Console.WriteLine(Calculate("psbpfpbp"));
        Console.WriteLine(Calculate("ppp"));
        Console.WriteLine(Calculate("ffffs"));
        Console.WriteLine(Calculate("ssspfffs"));
        Console.WriteLine(Calculate("bbbsfbppp"));
        Console.WriteLine(Calculate("sssbbbbsbhsbppp"));
        Console.WriteLine(Calculate("ssffpffssp"));
    }

    static string Calculate(string input)
    {
        return string.Join(",", Count.Generate(input));
    }
}

struct Count
{
    public int Outs;
    public int Strikes;
    public int Balls;

    public void Take(char action)
    {
        switch (action)
        {
            case 's':
                this.IncreaseStrikes();
                break;
            case 'f':
                if (this.Strikes < 2) this.IncreaseStrikes();
                break;
            case 'b':
                this.IncreaseBalls();
                break;
            case 'h':
                this.Advance();
                break;
            case 'p':
                this.IncreaseOuts();
                break;
            default:
                break;
        }
    }

    private void IncreaseStrikes()
    {
        this.Strikes++;
        if (this.Strikes == 3) this.IncreaseOuts();
    }

    private void IncreaseOuts()
    {
        this.Strikes = 0;
        this.Balls = 0;
        this.Outs++;
        if (this.Outs == 3) this.Outs = 0;
    }

    private void IncreaseBalls()
    {
        this.Balls++;
        if (this.Balls == 4) this.Advance();
    }

    private void Advance()
    {
        this.Strikes = 0;
        this.Balls = 0;
    }

    public override string ToString()
    {
        return string.Concat(this.Outs, this.Strikes, this.Balls);
    }

    public static IEnumerable<Count> Generate(string input)
    {
        var current = new Count();
        foreach (var action in input)
        {
            current.Take(action);
            yield return current;
        }
    }
}
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?