#include #include #include "bawling.h" //ボーリングクラス 実装 //------------------------------------------- // コンストラクタ //------------------------------------------- Bawling::Bawling() { } //------------------------------------------- // 初期化 //@par // なし //@ret // なし //------------------------------------------- void Bawling::Initialize( void ) { int i; //全ての投球を未決定にする for( i = 0; i < eMAX_FRAME; i++ ){ this->m_score[ i ].m_first = eUNDECIDED; this->m_score[ i ].m_second = eUNDECIDED; this->m_score[ i ].m_third = eUNDECIDED; this->m_score[ i ].m_total = eUNDECIDED; } } //------------------------------------------- // スコア入力 //@par // frame フレーム // first 一投目 // second 二投目 // third 三投目(10フレーム用) //@ret // なし //------------------------------------------- void Bawling::InputScore( int frame, int first, int second, int third ) { this->m_score[ frame ].m_first = first; this->m_score[ frame ].m_second = second; this->m_score[ frame ].m_third = third; //合計スコアの再計算 if( frame >= 2 ){ this->CalScore( frame-2 ); } if( frame >= 1 ){ this->CalScore( frame-1 ); } this->CalScore( frame ); } //------------------------------------------- // スコア計算 //@par // frame フレーム //@ret // なし //------------------------------------------- void Bawling::CalScore( int frame ) { int total; //今回の合計 int pre_total; //前回までの合計 int flag; //フラグ int i; int first, second, third; //前回までの合計を求める if( frame == 0 ){ pre_total = 0; }else{ pre_total = this->m_score[ frame - 1 ].m_total; } //その回のスコア first = this->m_score[ frame ].m_first; second = this->m_score[ frame ].m_second; third = this->m_score[ frame ].m_third; //フラグセット //ストライク if( first == 10 ){ flag = eSTRIKE_FLAG; } //スペア else if( first + second == 10 ){ flag = eSPARE_FLAG; } //通常 else{ flag = eNORMAL_FLAG; } //ストライク、スペアの場合 //ストライクは次のフレーム以降の2投分のスコアが入る //スペアは次の1投分のスコアが入る if( (flag & eSTRIKE_FLAG || flag & eSPARE_FLAG) && frame <= 8 ){ int next_score; //次のスコア int next_next_score; //次の次のスコア //次のスコア、次の次のスコアを探す next_score = eUNDECIDED; next_next_score = eUNDECIDED; for( i = frame + 1; i < eMAX_FRAME; i++ ){ if( this->m_score[ i ].m_first != eUNDECIDED ){ if( next_score == eUNDECIDED ){ next_score = this->m_score[ i ].m_first; }else if( next_next_score == eUNDECIDED ){ next_next_score = this->m_score[ i ].m_first; } } if( this->m_score[ i ].m_second != eUNDECIDED ){ if( next_score == eUNDECIDED ){ next_score = this->m_score[ i ].m_second; }else if( next_next_score == eUNDECIDED ){ next_next_score = this->m_score[ i ].m_second; } } if( this->m_score[ i ].m_third != eUNDECIDED ){ if( next_score == eUNDECIDED ){ next_score = this->m_score[ i ].m_third; }else if( next_next_score == eUNDECIDED ){ next_next_score = this->m_score[ i ].m_third; } } } //ストライクの場合 if( flag & eSTRIKE_FLAG ){ //次のスコア、次の次のスコアの合計 if( next_score != eUNDECIDED && next_next_score != eUNDECIDED ){ total = 10 + next_score + next_next_score; } //まだ未決定 else{ total = eUNDECIDED; } } //スペアの場合 else if( flag & eSPARE_FLAG ){ //次のスコアが決定しているとき if( next_score != eUNDECIDED ){ total = 10 + next_score; } //まだ未決定 else{ total = eUNDECIDED; } } } //ストライクでもスペアでもない場合 else{ if( third == eUNDECIDED ){ total = first + second; } //10フレーム用 else{ total = first + second + third; } } //合計スコアのストア if( total != eUNDECIDED ){ this->m_score[ frame ].m_total = pre_total + total; }else{ this->m_score[ frame ].m_total = eUNDECIDED; } } //------------------------------------------- // 日付入力 //@par // date 日付文字列 //@ret // なし //------------------------------------------- void Bawling::InputDate( char* date ) { strcpy( this->m_date, date ); } //------------------------------------------- // 結果表示 //@par // なし //@ret // なし //------------------------------------------- void Bawling::DispResult( void ) { int i; char *mark[] = { "G", "☆", "△", "−" };//ガーター、ストライク、スペア、ミス char buf[3][32]; printf(" ------------------- \n"); //日付表示 printf( "%s \n", this->m_date ); for( i = 0; i < eMAX_FRAME; i++ ){ //一投目 if( this->m_score[ i ].m_first == 10 ){ strcpy( buf[ 0 ], mark[ 1 ] ); }else if( this->m_score[ i ].m_first == 0 ){ strcpy( buf[ 0 ], mark[ 0 ] ); }else{ sprintf( buf[ 0 ], "%2d", this->m_score[ i ].m_first ); } //二投目 //10フレームの場合はストライクがあるので注意 if( i == 9 && this->m_score[ i ].m_first == 10 && this->m_score[ i ].m_second == 10 ){ strcpy( buf[ 1 ], mark[ 1 ] ); }else if( i == 9 && this->m_score[ i ].m_first == 10 && this->m_score[ i ].m_second == 0 ){ strcpy( buf[ 1 ], mark[ 0 ] ); }else if( this->m_score[ i ].m_first + this->m_score[ i ].m_second == 10 ){ strcpy( buf[ 1 ], mark[ 2 ] ); }else if( this->m_score[ i ].m_second == 0 ){ strcpy( buf[ 1 ], mark[ 3 ] ); }else if( this->m_score[ i ].m_second == eUNDECIDED ){ strcpy( buf[ 1 ], " " ); }else{ sprintf( buf[ 1 ], "%2d", this->m_score[ i ].m_second ); } //三投目(10フレーム目専用) if( this->m_score[ i ].m_third == 10 ){ strcpy( buf[ 2 ], mark[ 1 ] ); }else if( this->m_score[ i ].m_third == 0 ){ strcpy( buf[ 2 ], mark[ 0 ] ); }else if( this->m_score[ i ].m_third == eUNDECIDED ){ strcpy( buf[ 2 ], " " ); }else{ sprintf( buf[ 2 ], "%2d", this->m_score[ i ].m_third ); } //表示 printf("%2d ( %s %s %s ) %3d\n", i+1, buf[0], buf[1], buf[2], this->m_score[ i ].m_total ); } }