APCS程式檢測 -實作題1051029 第4題 棒球遊戲
會使用到二維陣列,如果不清楚可以先來這裡看看
https://husking-studio.com/cpp-2d-array/
壘包推進
本題的重點在壘包推進的過程
準備 1 個陣列,裡面有 4 個 int
程式碼
//壘包推進
void BaseRun(int* pBasePlayer, int nPlayerNumber, int* pScore, int nNum)
{
for (int i = 0; i < nNum; i++)
{
//模擬跑壘
for (int j = (BASE_NUM - 1); j >= 1; j--) //複製前 1 個元素
pBasePlayer[j] = pBasePlayer[j - 1];
//設定擊球員上壘
if(i == 0) //擊球員
pBasePlayer[0] = nPlayerNumber;
else
pBasePlayer[0] = 0;
//計算分數
if (pBasePlayer[BASE_NUM - 1] > 0) //如果推進過程中本壘有值,分數+1
*pScore = (*pScore) + 1;
}
}
完整程式碼
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
const int BASE_NUM = 4;
const int PLAYER_NUM = 9;
const int HIT_NUM = 5;
void PlayerHit(string strHit, int* pBasePlayer, int nPlayerNumber, int *pOut, int *pScore); //打擊情況
void BaseRun(int* pBasePlayer, int nPlayerNumber, int* pScore, int nNum); //壘包推進
void ClearBase(int* pBasePlayer); //清空壘包
void Print(string strHit, int* pBasePlayer, int* pScore, int* pOut); //印出檢查
int main()
{
string strPlayer[PLAYER_NUM][HIT_NUM]; //宣告二維陣列
for (int i = 0; i < PLAYER_NUM; i++)
for (int j = 0; j < HIT_NUM; j++)
strPlayer[i][j].assign("");
for (int i = 0; i < PLAYER_NUM; i++)
{
string strInput;
getline(cin, strInput);
stringstream delim(strInput);
string pch;
getline(delim, pch, ' ');
int a = stoi(pch);
for (int j = 0; j < a; j++)
{
getline(delim, pch, ' ');
strPlayer[i][j].assign(pch);
}
}
string strInput2;
getline(cin, strInput2);
int nOver = stoi(strInput2); //程式結束的累計總出局數
int nOut = 0; //累計總出局數
int nScore = 0; //分數
int nBasePlayer[BASE_NUM]; //壘包情況
ClearBase(nBasePlayer);
bool bOver = false;
for (int j = 0; j < HIT_NUM; j++) //直的,所以從 column 開始
{
for (int i = 0; i < PLAYER_NUM; i++)
{
if (strPlayer[i][j] == "")
continue;
PlayerHit(strPlayer[i][j], nBasePlayer, i+1, &nOut, &nScore);
if (nOut >= nOver)
{
bOver = true;
break;
}
}
if (bOver == true)
break;
}
cout << nScore << endl;
system("pause");
return 0;
}
//打擊情況
void PlayerHit(string strHit, int * pBasePlayer, int nPlayerNumber, int* pOut, int* pScore)
{
if (strHit == "SO" || strHit == "FO" || strHit == "GO") //出局
{
*pOut = (*pOut) + 1; //出局數+1
if ((*pOut) % 3 == 0) //出局數達3的倍數,清空壘包
{
ClearBase(pBasePlayer);
}
}
else if(strHit == "1B") //一壘打
{
BaseRun(pBasePlayer, nPlayerNumber, pScore, 1); //推進 1 次
}
else if (strHit == "2B") //二壘打
{
BaseRun(pBasePlayer, nPlayerNumber, pScore, 2); //推進 2 次
}
else if (strHit == "3B") //三壘打
{
BaseRun(pBasePlayer, nPlayerNumber, pScore, 3); //推進 3 次
}
else if (strHit == "HR") //全壘打
{
BaseRun(pBasePlayer, nPlayerNumber, pScore, 4); //推進 4 次
}
//Print(strHit, pPlayerBase, pScore, pOut); //印出檢查
}
//壘包推進
void BaseRun(int* pBasePlayer, int nPlayerNumber, int* pScore, int nNum)
{
for (int i = 0; i < nNum; i++)
{
//模擬跑壘
for (int j = (BASE_NUM - 1); j >= 1; j--) //複製前 1 個元素
pBasePlayer[j] = pBasePlayer[j - 1];
//設定擊球員上壘
if(i == 0) //擊球員
pBasePlayer[0] = nPlayerNumber;
else
pBasePlayer[0] = 0;
//計算分數
if (pBasePlayer[BASE_NUM - 1] > 0) //如果推進過程中本壘有值,分數+1
*pScore = (*pScore) + 1;
}
}
//清空壘包
void ClearBase(int* pBasePlayer)
{
for (int i = 0; i < BASE_NUM; i++)
pBasePlayer[i] = 0;
}
//印出檢查
void Print(string strHit, int* pPlayerBase, int* pScore, int* pOut)
{
cout << strHit << " ";
cout << "(";
for (int i = 0; i < BASE_NUM; i++)
{
cout << pPlayerBase[i] << " ";
}
cout << ") ";
cout << *pOut << " ";
cout << *pScore << endl;
}
附上整個例題的跑壘過程