# Shell.cpp 1/27
command的input的部分初稿完成
```
#include <iostream>
#include <string.h>
#include <map>
using namespace std;
bool CmdCompare(const char* CmdToCmp); //直接compare command_title
bool StrCompare(char* context, const char* Compare); //compare maximum 20 characters
void SubContxt(char* subArr, char* mainArr, short begin, char target); //從指定位置開始分割字串直到指定字元
short CheckWind(char* wind); // 查看場、局風
short GetType(short title_id); // 取得花色
short GetDigit(short title_id); // 取得數字
short GetTimes(short title_id); // 取得同牌的次數
class Gameinfo{
public:
short PlayerID = 1, BWind = 1, SWind = 1,
OpenAt = 1, Master = 1, Mcombo = 0;
Gameinfo() {}
};
short card[23] = {0}; // 手牌最多到22張[5槓(20)+2]
short card_size = 0; // 手牌數
map<short, short> card_map; // 手牌搜尋用map
char command_title[20] = {0}; //指令
char command_contxt[70] = {0}; //指令參數
int main()
{
Gameinfo currentGame;
while (true)
{
memset(command_title, 0, 20 * sizeof(char)); //sting.h 僅用 memset
memset(command_contxt, 0, 70 * sizeof(char));
char inputchar;
bool is_title = true;
for (short title_size = 0, contxt_size = 0;
title_size < 20 && contxt_size < 70;)
{
inputchar = cin.get(); //輸入形式必須為 "<command> <context>[Enter]"
if (inputchar == '\n')
{
command_title[title_size] = 0;
command_contxt[contxt_size] = 0;
break;
}
if (!is_title)
{
command_contxt[contxt_size++] = inputchar;
continue;
}
else if (is_title && inputchar == ' ')
{
is_title = false;
continue;
}
else
command_title[title_size++] = inputchar;
}
cout << "Recieved: " << command_title << ' ' << command_contxt << endl;
////////////////////////////////////////////////////////////////////////////
// 開始(input)
if (CmdCompare("/start"))
currentGame.PlayerID = (short)command_contxt[3] - '0';
// 本局資訊(input)
else if (CmdCompare("/initGame"))
{
char subtxt[6] = {0};
SubContxt(subtxt, command_contxt, 0, ' ');
currentGame.BWind = CheckWind(subtxt);
SubContxt(subtxt, command_contxt, 0, ' ');
currentGame.SWind = CheckWind(subtxt);
currentGame.OpenAt = command_contxt[0] - '0';
currentGame.Master = command_contxt[2] - '0';
currentGame.Mcombo = command_contxt[4] - '0';
}
// 發牌(input)
else if (CmdCompare("/initCard"))
{
char subtxt[4] = {0};
//if莊家 17張手牌, else 16張手牌
if(currentGame.PlayerID == currentGame.Master)
card_size = 17;
else
card_size = 16;
for(short i = 0; i < card_size; i++)
{
SubContxt(subtxt, command_contxt, 0, ' ');
card[i] = (subtxt[0] - '0') * 100 + (subtxt[1] - '0') * 10 + (subtxt[2] - '0');
card_map[card[i]] = i;
}
}
// 摸(input)
else if (CmdCompare("/mo"))
{
card[card_size] = (command_contxt[0] - '0') * 100 + (command_contxt[1] - '0') * 10 + (command_contxt[2] - '0'); // 新摸入的牌一律放到最尾端
card_map[card[card_size]] = card_size;
card_size++;
}
// 詢問AI動作(input)
else if (CmdCompare("/ask"))
{
// throw丟(output)
if (command_contxt[0] == 't')
{
// collect info
}
// eat吃(output)
else if (command_contxt[0] == 'e')
{
// collect info
}
// pong碰(output)
else if (command_contxt[0] == 'p')
{
// collect info
}
// gong槓(output)
else if (command_contxt[0] == 'g')
{
short playerID = command_contxt[0] - '0';
if (playerID == currentGame.PlayerID) continue;
short gong_type = command_contxt[2] - '0';
/*
if (gong_type == 4) // 明槓
{
// collect info
}
else if (gong_type == 0) // 暗槓
{
// collect info
}
else if (gong_type == 1) // 加槓
{
// collect info
}
*/
}
// hu胡(output)
else if (command_contxt[0] == 'h')
{
// collect info
}
}
// 確認AI有連接(?)
else if (CmdCompare("/ready"))
{}
// 程式結束(input)
else if (CmdCompare("/quit"))
break;
// Error(input)
else
cout << "Input Error!";
}
return -1;
}
bool CmdCompare(const char* CmdToCmp)
{
bool flag = true;
for (short i = 0; command_title[i] != 0; i++)
if(command_title[i] != CmdToCmp[i] || i > 10)
{
flag = false;
break;
}
return flag;
}
bool StrCompare(char* context, const char* Compare) //compare maximum 20 characters
{
bool flag = true;
for (short i = 0; context[i] != 0; i++)
if(context[i] != Compare[i] || i > 20)
{
flag = false;
break;
}
return flag;
}
void SubContxt(char* subArr, char* mainArr, short begin, char target)
{
short i;
for (i = begin; *(mainArr + i - 1) != target && *(mainArr + i - 1) != 0; i++)
{
// cout << *(mainArr + i);
*(subArr + i) = *(mainArr + i);
}
for (short j = begin; *(mainArr + j) != 0; j++)
{
if (*(mainArr + j + i) != 0)
*(mainArr + j) = *(mainArr + j + i);
else
*(mainArr + j) = 0;
}
}
short CheckWind(char* wind)
{
switch (*wind)
{
case 'E':
return 1;
case 'S':
return 2;
case 'W':
return 3;
case 'N':
return 4;
default:
return -1;
}
}
short GetType(short title_id)
{
// 如果title_id > 999, print warning
if(title_id > 999)
cout << "[Warning] - The title_id > 999.\n";
// return type
short type = (title_id / 100) % 10; // %10是為了就算>999也能回傳百位數
return type;
}
short GetDigit(short title_id)
{
// 如果title_id > 999, print warning
if(title_id > 999)
cout << "[Warning] - The title_id > 999.\n";
// return type
short type = (title_id / 10) % 10; // %10是為了就算>999也能回傳十位數
return type;
}
short GetTimes(short title_id)
{
// 如果title_id > 999, print warning
if(title_id > 999)
cout << "[Warning] - The title_id > 999.\n";
// return type
short type = title_id % 10; // %10是為了就算>999也能回傳個位數
return type;
}
```