public void EncodeModified(string fileName) { StreamReader file = new StreamReader(fileName); string line = null; int count = 0; while ((line = file.ReadLine()) != null) { //Helpfunctions.Instance.logg(line); if (count % 1000 == 0) { Console.WriteLine("Finished " + count + " lines."); } GameRecord gameRecord = JsonConvert.DeserializeObject <GameRecord>(line); int lastEntity = 1000; foreach (StateKeyInfo stKeyInfo in gameRecord.playSec) { PlayerKeyInfo p1Info = stKeyInfo.attackPlayer; PlayerKeyInfo p2Info = stKeyInfo.defensePlayer; bool isOwnTurn = p1Info.turn == 0 ? true : false; lastEntity = stKeyInfo.nextEntity; Playfield tempPf = null; if (isOwnTurn) { tempPf = new Playfield(lastEntity, isOwnTurn, p1Info, p2Info); } else { tempPf = new Playfield(lastEntity, isOwnTurn, p2Info, p1Info); } stKeyInfo.turnSt = 1; Player mPlayer = tempPf.getCurrentPlayer(true); Player ePlayer = tempPf.getCurrentPlayer(false); int length = stKeyInfo.attackPlayer.handcardJsonList.Count; stKeyInfo.attackPlayer.handcardJsonList.Clear(); foreach (Handmanager.Handcard hc in mPlayer.owncards) { PlayerKeyInfo.CardKeyInfo hcInfo = new PlayerKeyInfo.CardKeyInfo(hc, true, tempPf); stKeyInfo.attackPlayer.handcardJsonList.Add(hcInfo); } length = stKeyInfo.defensePlayer.handcardJsonList.Count; stKeyInfo.defensePlayer.handcardJsonList.Clear(); foreach (Handmanager.Handcard hc in ePlayer.owncards) { PlayerKeyInfo.CardKeyInfo hcInfo = new PlayerKeyInfo.CardKeyInfo(hc, true, tempPf); stKeyInfo.defensePlayer.handcardJsonList.Add(hcInfo); } LinkedList <Action> actionSeq = searchValidSeq(tempPf, p1Info); stKeyInfo.attackPlayer.canPlayHeroPower = new List <int>(); stKeyInfo.attackPlayer.playedActionJsonList = new List <PlayerKeyInfo.ActionKeyInfo>(); stKeyInfo.attackPlayer.handcardChange = new List <List <PlayerKeyInfo.CardKeyInfo> >(); foreach (Action action in actionSeq) { if (action.actionType == actionEnum.useHeroPower || action.actionType == actionEnum.playcard) { PlayerKeyInfo ownkeyInfo, enemykeyInfo; if (isOwnTurn) { ownkeyInfo = new PlayerKeyInfo(tempPf.playerFirst, tempPf.homeDeck, true, tempPf); enemykeyInfo = new PlayerKeyInfo(tempPf.playerSecond, tempPf.awayDeck, false, tempPf); } else { ownkeyInfo = new PlayerKeyInfo(tempPf.playerSecond, tempPf.awayDeck, false, tempPf); enemykeyInfo = new PlayerKeyInfo(tempPf.playerFirst, tempPf.homeDeck, true, tempPf); } StateKeyInfo interState = new StateKeyInfo(tempPf.nextEntity, ownkeyInfo, enemykeyInfo, false, 0.0); stKeyInfo.attackPlayer.handcardChange.Add(interState.attackPlayer.handcardJsonList); bool canPlayHeroPower = mPlayer.ownAbilityReady && mPlayer.mana >= 2; if (canPlayHeroPower) { stKeyInfo.attackPlayer.canPlayHeroPower.Add(1); } else { stKeyInfo.attackPlayer.canPlayHeroPower.Add(0); } } Debug.Assert(stKeyInfo.attackPlayer.canPlayHeroPower.Count == stKeyInfo.attackPlayer.handcardChange.Count); tempPf.getNextEntity(); tempPf.doAction(action); stKeyInfo.attackPlayer.playedActionJsonList.Add(new PlayerKeyInfo.ActionKeyInfo(action)); if (debug) { action.print(); } } lastEntity = tempPf.getNextEntity() + 1; //Console.WriteLine("==================="); //Console.WriteLine("End turn Next Entity:" + lastEntity); //Console.WriteLine("==================="); } count++; Helpfunctions.Instance.WriteResultToFileAbs(fileName + ".3.txt", JsonConvert.SerializeObject(gameRecord)); } }
public void Encode(string fileName) { StreamReader file = new StreamReader(fileName); string line = null; int count = 0; while ((line = file.ReadLine()) != null) { if (count % 100 == 0) { Console.WriteLine("Read " + count + " lines."); } GameRecord gameRecord = JsonConvert.DeserializeObject <GameRecord>(line); List <StateKeyInfo> playSec = new List <StateKeyInfo>(); int lastEntity = 1000; foreach (StateKeyInfo stKeyInfo in gameRecord.playSec) { PlayerKeyInfo p1Info = stKeyInfo.attackPlayer; PlayerKeyInfo p2Info = stKeyInfo.defensePlayer; bool isOwnTurn = p1Info.turn == 0 ? true : false; Playfield tempPf = null; if (isOwnTurn) { tempPf = new Playfield(lastEntity, isOwnTurn, p1Info, p2Info); } else { tempPf = new Playfield(lastEntity, isOwnTurn, p2Info, p1Info); } stKeyInfo.nextEntity = tempPf.nextEntity; stKeyInfo.turnSt = 1; Player mPlayer = tempPf.getCurrentPlayer(true); Player ePlayer = tempPf.getCurrentPlayer(false); int length = stKeyInfo.attackPlayer.handcardJsonList.Count; stKeyInfo.attackPlayer.handcardJsonList.Clear(); foreach (Handmanager.Handcard hc in mPlayer.owncards) { PlayerKeyInfo.CardKeyInfo hcInfo = new PlayerKeyInfo.CardKeyInfo(hc, true, tempPf); stKeyInfo.attackPlayer.handcardJsonList.Add(hcInfo); } length = stKeyInfo.defensePlayer.handcardJsonList.Count; stKeyInfo.defensePlayer.handcardJsonList.Clear(); foreach (Handmanager.Handcard hc in ePlayer.owncards) { PlayerKeyInfo.CardKeyInfo hcInfo = new PlayerKeyInfo.CardKeyInfo(hc, true, tempPf); stKeyInfo.defensePlayer.handcardJsonList.Add(hcInfo); } //hero power stKeyInfo.attackPlayer.canPlayHeroPower = new List <int>(); bool canPlayHeroPower = mPlayer.ownAbilityReady && mPlayer.mana >= 2; if (canPlayHeroPower) { stKeyInfo.attackPlayer.canPlayHeroPower.Add(1); } else { stKeyInfo.attackPlayer.canPlayHeroPower.Add(0); } foreach (PlayerKeyInfo.ActionKeyInfo actionKeyInfo in p1Info.playedActionJsonList) { Action action = CreateActionFromInfo(tempPf, actionKeyInfo); tempPf.getNextEntity(); tempPf.doAction(action); PlayerKeyInfo ownkeyInfo, enemykeyInfo; if (isOwnTurn) { ownkeyInfo = new PlayerKeyInfo(tempPf.playerFirst, tempPf.homeDeck, true, tempPf); enemykeyInfo = new PlayerKeyInfo(tempPf.playerSecond, tempPf.awayDeck, false, tempPf); } else { ownkeyInfo = new PlayerKeyInfo(tempPf.playerSecond, tempPf.awayDeck, false, tempPf); enemykeyInfo = new PlayerKeyInfo(tempPf.playerFirst, tempPf.homeDeck, true, tempPf); } StateKeyInfo interState = new StateKeyInfo(tempPf.nextEntity, ownkeyInfo, enemykeyInfo, false, 0.0); //playSec.Add(interState); if (action.own != null) { actionKeyInfo.ownName = action.own.name.ToString(); } if (action.target != null) { actionKeyInfo.targetName = action.target.name.ToString(); } if (action.actionType == actionEnum.playcard || action.actionType == actionEnum.useHeroPower) { if (stKeyInfo.attackPlayer.handcardChange == null) { stKeyInfo.attackPlayer.handcardChange = new List <List <PlayerKeyInfo.CardKeyInfo> >(); } stKeyInfo.attackPlayer.handcardChange.Add(interState.attackPlayer.handcardJsonList); canPlayHeroPower = mPlayer.ownAbilityReady && mPlayer.mana >= 2; if (action.actionType == actionEnum.useHeroPower) { Debug.Assert(canPlayHeroPower == false); actionKeyInfo.cardName = "fireblast"; } else { actionKeyInfo.cardName = action.card.card.name.ToString(); } if (canPlayHeroPower) { stKeyInfo.attackPlayer.canPlayHeroPower.Add(1); } else { stKeyInfo.attackPlayer.canPlayHeroPower.Add(0); } } } playSec.Add(stKeyInfo); lastEntity = tempPf.getNextEntity() + 1; //Console.WriteLine("==================="); //Console.WriteLine("End turn Next Entity:" + lastEntity); //Console.WriteLine("==================="); } gameRecord.playSec = playSec; count++; Helpfunctions.Instance.WriteResultToFileAbs(fileName + ".2.txt", JsonConvert.SerializeObject(gameRecord)); } }
//4:0; 3:0; 2:0; 4:1; 3:1; 4:2; 2:1, 3:2; 4:3; 1:0 //4:4; 3:3, 2:2, 1:1, 0.0 //reverse line 1 public static StateFeature normalFeaturization(StateKeyInfo stKeyInfo) { PlayerKeyInfo mPlayer = stKeyInfo.attackPlayer; PlayerKeyInfo ePlayer = stKeyInfo.defensePlayer; StateFeature ret = new StateFeature(9); ret.featrueArray[0] = new Feature("Global", new int[4]); //ownhp, enemyhp, ownmana, enemymana; ret.featrueArray[0].data[0] = mPlayer.heroInfo.Hp; ret.featrueArray[0].data[1] = mPlayer.maxMana; ret.featrueArray[0].data[2] = ePlayer.heroInfo.Hp; ret.featrueArray[0].data[3] = ePlayer.maxMana; ret.featrueArray[1] = new Feature("OwnMinionHp", new int[FeatureConst.Instance.minionCardArray.Length]); ret.featrueArray[2] = new Feature("OwnMinion", new int[FeatureConst.Instance.minionCardArray.Length]); foreach (PlayerKeyInfo.MinionKeyInfo m in mPlayer.minionJsonList) { int idx = FeatureConst.Instance.cardStrIdxDict[m.name]; ret.featrueArray[1].data[idx] = Math.Max(ret.featrueArray[1].data[idx], m.Hp); ret.featrueArray[2].data[idx]++; } ret.featrueArray[3] = new Feature("OwnCardFeature", new int[FeatureConst.Instance.cardArray.Length]); foreach (PlayerKeyInfo.CardKeyInfo hc in mPlayer.handcardJsonList) { int idx = FeatureConst.Instance.cardStrIdxDict[hc.cardName]; ret.featrueArray[3].data[idx]++; } ret.featrueArray[4] = new Feature("OwnPlayedFeature", new int[10]); ret.featrueArray[5] = new Feature("OwnPlayableFeature", new int[10][]); for (int i = 0; i < 10; i++) { ret.featrueArray[5].data[i] = new int[FeatureConst.Instance.cardArray.Length]; } List <List <PlayerKeyInfo.CardKeyInfo> > playableList = new List <List <PlayerKeyInfo.CardKeyInfo> >(); playableList.Add(mPlayer.handcardJsonList); if (mPlayer.handcardChange != null) { playableList.AddRange(mPlayer.handcardChange); } int playedCount = 0; int playedCardCount = 0; foreach (PlayerKeyInfo.ActionKeyInfo actionKeyInfo in mPlayer.playedActionJsonList) { if (actionKeyInfo.actionType == actionEnum.playcard || actionKeyInfo.actionType == actionEnum.useHeroPower) { List <PlayerKeyInfo.CardKeyInfo> playable = playableList[playedCount]; foreach (PlayerKeyInfo.CardKeyInfo hc in playable) { if (hc.playable) { ret.featrueArray[5].data[playedCount][FeatureConst.Instance.cardStrIdxDict[hc.cardName]] = 1; } if (mPlayer.canPlayHeroPower[playedCount] == 1) { ret.featrueArray[5].data[playedCount][FeatureConst.Instance.cardStrIdxDict["fireblast"]] = 1; } } int playedActionIdx; if (actionKeyInfo.actionType == actionEnum.playcard) { playedActionIdx = FeatureConst.Instance.cardStrIdxDict[mPlayer.playedCardJsonList[playedCardCount].cardName]; playedCardCount++; } else { playedActionIdx = FeatureConst.Instance.cardStrIdxDict["fireblast"]; } ret.featrueArray[4].data[playedCount] = playedActionIdx; playedCount++; } } ret.featrueArray[6] = new Feature("EnemyMinionHp", new int[FeatureConst.Instance.minionCardArray.Length]); ret.featrueArray[7] = new Feature("EnemyMinion", new int[FeatureConst.Instance.minionCardArray.Length]); foreach (PlayerKeyInfo.MinionKeyInfo m in ePlayer.minionJsonList) { int idx = FeatureConst.Instance.cardStrIdxDict[m.name]; ret.featrueArray[6].data[idx]++; ret.featrueArray[7].data[idx] = Math.Max(ret.featrueArray[7].data[idx], m.Hp); } ret.featrueArray[8] = new Feature("EnemyCardFeature", new int[FeatureConst.Instance.cardArray.Length]); foreach (PlayerKeyInfo.CardKeyInfo hc in ePlayer.handcardJsonList) { int idx = FeatureConst.Instance.cardStrIdxDict[hc.cardName]; ret.featrueArray[8].data[idx]++; } return(ret); }