using System.Collections;
using System.Collections.Generic;
public class CommonFunc : MonoBehaviour {
public static PlayerData playerData;
public static int itemCount = 4;
private static int checkNo = 7;
public const int nameSize = 5;
public static int[] checkNoPos = new int[]{2, 12, 22, 55}; // チェック用の文字を入れる場所
public static readonly string[] wordList = new string[]{
public static readonly string[] inputWordList = new string[]{
public static string CreateSaveWord()
string resultNo = CreateSaveWordNo();
Debug.LogWarning(resultNo + " : " + resultNo.Length);
resultNo = AddCheckNo(resultNo);
return CreateSaveWordStr(resultNo);
private static string AddCheckNo(string resultNo)
3文字目が文字数引く上記のランダム数-7のbitと同じもの
4文字目がこれ以外のビットの1の数が奇数だったら1
int rand = UnityEngine.Random.Range(0, 4);
string str1 = ChangeString(Convert.ToString(rand, 2), 2);
string str2 = resultNo.Substring(resultNo.Length - rand - 7, 1);
for(int i = 0; i < resultNo.Length; i++)
if(resultNo.Substring(i, 1) == "1") count++;
if(str1.Substring(0, 1) == "1") count++;
if(str1.Substring(0, 1) == "1") count++;
if(count % 2 == 1) str3 = "1";
Debug.Log (str1 + ":" + str2 + ":"+ str3);
string password = resultNo.Substring(0, checkNoPos[0]) + str1.Substring(0, 1) +
resultNo.Substring(checkNoPos[0], checkNoPos[1] - checkNoPos[0]) + str1.Substring(1, 1) +
resultNo.Substring(checkNoPos[1], checkNoPos[2] - checkNoPos[1]) + str2 +
resultNo.Substring(checkNoPos[2], checkNoPos[3] - checkNoPos[2]) + str3 +
resultNo.Substring(checkNoPos[3], resultNo.Length - checkNoPos[3]);
public static bool LoadSaveWord(string result)
playerData = new PlayerData();
for(int i = 0; i < result.Length; i++)
int wordNo = GetWordNo(result.Substring(i, 1));
no += ChangeString(Convert.ToString(wordNo, 2), 6);
string str1 = no.Substring(checkNoPos[0], 1) + no.Substring(checkNoPos[1] + 1, 1);
string str2 = no.Substring(checkNoPos[2] + 2, 1);
string str3 = no.Substring(checkNoPos[3] + 3, 1);
Debug.Log (str1 + ":" + str2 + ":"+ str3);
for(int i = 0; i < no.Length; i++)
if(i != checkNoPos[0] && i != checkNoPos[1] + 1 && i != checkNoPos[2] + 2 && i != checkNoPos[3] + 3 )
password += no.Substring(i, 1);
Debug.LogWarning (password);
int randomNo = Convert.ToInt32(str1, 2);
if(str2 != password.Substring(password.Length - randomNo - 7, 1))
for(int i = 0; i < password.Length; i++)
if(password.Substring(i, 1) == "1") count++;
if(str1.Substring(0, 1) == "1") count++;
if(str1.Substring(0, 1) == "1") count++;
if(count % 2 != int.Parse(str3))
Debug.Log ("wordNo : " + password.Length);
for(int i = 0; i < nameSize; i++)
int num = Convert.ToInt32(password.Substring(i * 6, 6), 2);
loadName += inputWordList[num];
int loadExp = Convert.ToInt32(password.Substring(6 * nameSize, 14), 2);
int loadMoney = Convert.ToInt32(password.Substring(44, 14), 2);
int[] loadItemCountList = new int[itemCount];
playerData.PlayerName = loadName;
playerData.Exp = loadExp;
playerData.Money = loadMoney;
int[] itemCountList = new int[itemCount];
for(int i = 0; i < itemCount; i++)
itemCountList[i] = Convert.ToInt32(password.Substring(58 + i * 4, 4), 2);
playerData.ItemCountList = itemCountList;
private static string CreateSaveWordStr(string resultNo)
int passwordCount = resultNo.Length / 6;
for(int i = 0; i < passwordCount; i++)
int num = Convert.ToInt32(resultNo.Substring(i * 6, 6), 2);
private static string CreateSaveWordNo()
for(int i = 0; i < nameSize; i++)
int no = GetInputWordNo(playerData.PlayerName.Substring(i, 1));
resultNo += ChangeString(Convert.ToString(no, 2), 6);
resultNo += ChangeString(Convert.ToString(playerData.Exp, 2), 14);
resultNo += ChangeString(Convert.ToString(playerData.Money, 2), 14);
for(int i = 0; i < itemCount; i++)
resultNo += ChangeString(Convert.ToString(playerData.ItemCountList[i], 2), 4);
private static string ChangeString(string str, int count)
while(str.Length < count)
private static int GetWordNo(string str)
for(int i = 0; i < wordList.Length; i++)
private static int GetInputWordNo(string str)
for(int i = 0; i < inputWordList.Length; i++)
if(str == inputWordList[i])