ボールカウント:野球


Source.cpp
#include "Header.h"

void BallCount::initNowCountInfo()
{
    for(int i=0; i<COUNTINFONUMBER; i++){
        m_nowCountInfo[i] = 0;
    }
}

void BallCount::ballStrikeOutRenew()
{
    if(m_nowCountInfo[2] == 4){
        m_nowCountInfo[2] = 0;
        m_nowCountInfo[1] = 0;
    }
    if(m_nowCountInfo[1] == 3){
        m_nowCountInfo[2] = 0;
        m_nowCountInfo[1] = 0;
        m_nowCountInfo[0]++;
    }
    if(m_nowCountInfo[0] == 3){
        m_nowCountInfo[2] = 0;
        m_nowCountInfo[1] = 0;
        m_nowCountInfo[0] = 0;
    }
}

void BallCount::s_process()
{
    m_nowCountInfo[1]++;
}

void BallCount::b_process()
{
    m_nowCountInfo[2]++;
}

void BallCount::f_process()
{
    if(m_nowCountInfo[1] != 2)
        m_nowCountInfo[1]++;
}

void BallCount::h_process()
{
    m_nowCountInfo[2] = 4;
}

void BallCount::p_process()
{
    m_nowCountInfo[1] = 3;
}

void BallCount::nowCountInfoRenew(const char& addChar)
{
    std::string result = "";
    if(addChar == 's'){
        s_process();
    }
    if(addChar == 'b'){
        b_process();
    }
    if(addChar == 'f'){
        f_process();
    }
    if(addChar == 'h'){
        h_process();
    }
    if(addChar == 'p'){
        p_process();
    }
    ballStrikeOutRenew();
}

const std::string BallCount::changeString()
{
    std::string result = "";
    for(int i=0; i<COUNTINFONUMBER; i++){
        result += std::to_string(m_nowCountInfo[i]);
    }
    result += ",";
    return result;
}

const std::string BallCount::solve(const std::string& input)
{
    initNowCountInfo();
    std::string result;
    for(int i=0; i<input.size(); i++){
        nowCountInfoRenew(input.at(i));
        result += changeString();
    }
    result.erase(--result.end());
    return result;
}

void  BallCount::test(const std::string& inputStr)
{
    std::string input, output;
    int index = inputStr.find("-", 0);
    input = inputStr.substr(0,index-1);
    output = inputStr.substr(index+3);

    std::string result = solve(input);
    std::cout << result << std::endl;
    if(result == output){
        std::cout << "test is OK!" << std::endl;
    }else{
        std::cout << "test is NG!" << std::endl;
    }
}

int main()
{
    BallCount bc;
    bc.test("s -> 010");
    bc.test("sss -> 010,020,100");
    bc.test("bbbb -> 001,002,003,000");
    bc.test("ssbbbb -> 010,020,021,022,023,000");
    bc.test("hsbhfhbh -> 000,010,011,000,010,000,001,000");
    bc.test("psbpfpbp -> 100,110,111,200,210,000,001,100");
    bc.test("ppp -> 100,200,000");
    bc.test("ffffs -> 010,020,020,020,100");
    bc.test("ssspfffs -> 010,020,100,200,210,220,220,000");
    bc.test("bbbsfbppp -> 001,002,003,013,023,000,100,200,000");
    bc.test("sssbbbbsbhsbppp -> 010,020,100,101,102,103,100,110,111,100,110,111,200,000,100");
    bc.test("ssffpffssp -> 010,020,020,020,100,110,120,200,210,000");
    return 0;
}
Header.h
#ifndef _HEADER_H
#define _HEADER_H

#include <string>
#include <iostream>

#define COUNTINFONUMBER 3

class BallCount
{
public:
    void test(const std::string& inputStr);
    int m_nowCountInfo[COUNTINFONUMBER];
private:
    void initNowCountInfo();
    const std::string solve(const std::string& input);
    const std::string changeString();
    void nowCountInfoRenew(const char& addChar);
    void ballStrikeOutRenew();
    void s_process();
    void b_process();
    void f_process();
    void h_process();
    void p_process();
};

#endif