private int GetScoreOfChosenWay(ChosenWayToPlay wayToPlay) { int score = 0; // If history has no domino if (history.horizontalDominoes.Count == 0) { if (wayToPlay.chosenDomino.direction == DominoController.Direction.Horizontal) { int value = wayToPlay.chosenDomino.leftValue + wayToPlay.chosenDomino.rightValue; score = (value % 5 == 0) ? value : 0; } else { int value = wayToPlay.chosenDomino.upperValue + wayToPlay.chosenDomino.lowerValue; score = (value % 5 == 0) ? value : 0; } return(score); } // Else that history has at least 1 domino DominoController copiedDomino = Instantiate <DominoController>(wayToPlay.chosenDomino); HistoryController copiedHistory = Instantiate <HistoryController>(history); // Simulate to place a domino and then calculate the sum PlaceDomino(copiedDomino, wayToPlay.chosenPlace, copiedHistory); copiedHistory.Add(copiedDomino, wayToPlay.chosenPlace); score = Utility.GetSumOfHistoryDominoes(copiedHistory.horizontalDominoes, copiedHistory.verticalDominoes, copiedHistory.spinner); score = score % 5 == 0 ? score : 0; Destroy(copiedDomino.gameObject); Destroy(copiedHistory.gameObject); return(score); }
private bool CanPlayDominoBlockOpponent(ChosenWayToPlay wayToPlay) { if (opponent.dominoControllers.Count < lastNumberOfOpponentDominoes || wayToPlay.chosenPlace == null) { return(false); } HashSet <int> missingNumber = GetOpponentMissingNumber(); if (missingNumber.Count > 0) { if (history.horizontalDominoes.Count > 0) { if (history.horizontalDominoes[0] == wayToPlay.chosenPlace) { return(missingNumber.Contains(Utility.GetNextOutsideNumber(wayToPlay.chosenDomino, wayToPlay.chosenPlace, Model.PositionInHistory.Left))); } else if (history.horizontalDominoes[history.horizontalDominoes.Count - 1] == wayToPlay.chosenPlace) { return(missingNumber.Contains(Utility.GetNextOutsideNumber(wayToPlay.chosenDomino, wayToPlay.chosenPlace, Model.PositionInHistory.Right))); } else if (history.verticalDominoes.Count > 0) { if (history.verticalDominoes[0] == wayToPlay.chosenPlace) { return(missingNumber.Contains(Utility.GetNextOutsideNumber(wayToPlay.chosenDomino, wayToPlay.chosenPlace, Model.PositionInHistory.Upper))); } else if (history.verticalDominoes[history.verticalDominoes.Count - 1] == wayToPlay.chosenPlace) { return(missingNumber.Contains(Utility.GetNextOutsideNumber(wayToPlay.chosenDomino, wayToPlay.chosenPlace, Model.PositionInHistory.Lower))); } } } } return(false); }
IEnumerator WaitToPlay() { yield return(new WaitForSeconds(1.5F + Random.Range(0F, .5F))); if (history.horizontalDominoes.Count > 0) { placesToPlay = PlacesToPlay(); if (placesToPlay.Count == 0) { DrawDomino(); placesToPlay = PlacesToPlay(); if (placesToPlay.Count == 0) { nextState = NextState.Wait; gameController.PlayerIsBlocked(this); yield break; } } } var waysToPlay = new List <ChosenWayToPlay>(); if (history.horizontalDominoes.Count == 0) { foreach (var domino in dominoControllers) { waysToPlay.Add(new ChosenWayToPlay(domino, null)); } } else { foreach (var entry in placesToPlay) { var list = entry.Value; foreach (var chosenPlace in list) { var chosenWayToPlay = new ChosenWayToPlay(entry.Key, chosenPlace); waysToPlay.Add(chosenWayToPlay); } } } // From small to large //waysToPlay.Sort((x, y) => GetScoreOfChosenWay(x) - GetScoreOfChosenWay(y)); var bestWayToPlay = waysToPlay[waysToPlay.Count - 1]; PlaceDomino(bestWayToPlay.chosenDomino, bestWayToPlay.chosenPlace, history); dominoControllers.Remove(bestWayToPlay.chosenDomino); gameController.PlayerPlayDomino(this, bestWayToPlay.chosenDomino, bestWayToPlay.chosenPlace); }
private void Update() { switch (nextState) { case NextState.Wait: return; case NextState.Draw: if (history.horizontalDominoes.Count > 0) { placesToPlay = PlacesToPlay(); if (placesToPlay.Count == 0) { base.DrawDomino(); placesToPlay = PlacesToPlay(); if (placesToPlay.Count == 0) { nextState = NextState.Wait; gameController.PlayerIsBlocked(this); return; } } } nextState = NextState.Play; break; case NextState.Play: List <ChosenWayToPlay> waysToPlay = new List <ChosenWayToPlay>(); if (history.horizontalDominoes.Count == 0) { foreach (DominoController domino in dominoControllers) { waysToPlay.Add(new ChosenWayToPlay(domino, null)); } } else { foreach (KeyValuePair <DominoController, List <DominoController> > entry in placesToPlay) { List <DominoController> list = entry.Value; foreach (DominoController chosenPlace in list) { ChosenWayToPlay chosenWayToPlay = new ChosenWayToPlay(entry.Key, chosenPlace); waysToPlay.Add(chosenWayToPlay); } } } int historyDominoesNum = history.horizontalDominoes.Count + history.verticalDominoes.Count - (history.spinner == null ? 0 : 1); GameStatusModel gameStatus = GetGameStatus(); // From small to large waysToPlay.Sort(delegate(ChosenWayToPlay x, ChosenWayToPlay y) { if (historyDominoesNum <= 18) { int xScore = GetScoreOfChosenWay(x); int yScore = GetScoreOfChosenWay(y); return(xScore - yScore); } ChosenWayToPlayModel xModel = new ChosenWayToPlayModel(Utility.DominoControllerToDominoModel(x.chosenDomino), Utility.DominoControllerToDominoModel(x.chosenPlace)); PlayActionModel xPlayActionModel = new PlayActionModel(gameStatus, xModel); WinLoseStatusModel xWinLoseStatus; if (winLoseDict.ContainsKey(xPlayActionModel)) { xWinLoseStatus = winLoseDict[xPlayActionModel]; } else { xWinLoseStatus = new WinLoseStatusModel(); xWinLoseStatus.wins = 1; xWinLoseStatus.loses = 1; } double xWinRate = (double)xWinLoseStatus.wins / (xWinLoseStatus.wins + xWinLoseStatus.loses); ChosenWayToPlayModel yModel = new ChosenWayToPlayModel(Utility.DominoControllerToDominoModel(y.chosenDomino), Utility.DominoControllerToDominoModel(y.chosenPlace)); PlayActionModel yPlayActionModel = new PlayActionModel(gameStatus, yModel); WinLoseStatusModel yWinLoseStatus; if (winLoseDict.ContainsKey(yPlayActionModel)) { yWinLoseStatus = winLoseDict[xPlayActionModel]; } else { yWinLoseStatus = new WinLoseStatusModel(); yWinLoseStatus.wins = 1; yWinLoseStatus.loses = 1; } double yWinRate = (double)yWinLoseStatus.wins / (yWinLoseStatus.wins + yWinLoseStatus.loses); if (xWinRate < yWinRate) { return(-1); } else if (xWinRate == yWinRate) { return(0); } return(1); }); ChosenWayToPlay bestWayToPlay = waysToPlay[waysToPlay.Count - 1]; PlaceDomino(bestWayToPlay.chosenDomino, bestWayToPlay.chosenPlace, history); dominoControllers.Remove(bestWayToPlay.chosenDomino); if (historyDominoesNum > 18) { ChosenWayToPlayModel bestPlayModel = new ChosenWayToPlayModel(Utility.DominoControllerToDominoModel(bestWayToPlay.chosenDomino), Utility.DominoControllerToDominoModel(bestWayToPlay.chosenPlace)); PlayActionModel bestPlayActionModel = new PlayActionModel(gameStatus, bestPlayModel); if (winLoseDict.ContainsKey(bestPlayActionModel)) { winLoseStatusList.Add(winLoseDict[bestPlayActionModel]); } else { WinLoseStatusModel winLoseStatus = new WinLoseStatusModel(); winLoseStatus.wins = 1; winLoseStatus.loses = 1; winLoseDict.Add(bestPlayActionModel, winLoseStatus); winLoseStatusList.Add(winLoseStatus); } } // Debug Debug.Log("Chosen Domino: " + bestWayToPlay.chosenDomino.leftValue + ", " + bestWayToPlay.chosenDomino.rightValue + ", " + bestWayToPlay.chosenDomino.upperValue + ", " + bestWayToPlay.chosenDomino.lowerValue); if (bestWayToPlay.chosenPlace != null) { Debug.Log("Chosen Place: " + bestWayToPlay.chosenPlace.leftValue + ", " + bestWayToPlay.chosenPlace.rightValue + ", " + bestWayToPlay.chosenPlace.upperValue + ", " + bestWayToPlay.chosenPlace.lowerValue); } Debug.Log(Environment.StackTrace); nextState = NextState.Wait; gameController.PlayerPlayDomino(this, bestWayToPlay.chosenDomino, bestWayToPlay.chosenPlace); break; } }
private void Update() { switch (nextState) { case NextState.Wait: return; case NextState.Draw: if (history.horizontalDominoes.Count > 0) { placesToPlay = PlacesToPlay(); if (placesToPlay.Count == 0) { base.DrawDomino(); placesToPlay = PlacesToPlay(); if (placesToPlay.Count == 0) { nextState = NextState.Wait; gameController.PlayerIsBlocked(this); return; } } } nextState = NextState.Play; break; case NextState.Play: List <ChosenWayToPlay> waysToPlay = new List <ChosenWayToPlay>(); if (history.horizontalDominoes.Count == 0) { foreach (DominoController domino in dominoControllers) { waysToPlay.Add(new ChosenWayToPlay(domino, null)); } } else { foreach (KeyValuePair <DominoController, List <DominoController> > entry in placesToPlay) { List <DominoController> list = entry.Value; foreach (DominoController chosenPlace in list) { ChosenWayToPlay chosenWayToPlay = new ChosenWayToPlay(entry.Key, chosenPlace); waysToPlay.Add(chosenWayToPlay); } } } // From small to large waysToPlay.Sort(delegate(ChosenWayToPlay x, ChosenWayToPlay y) { int xScore = GetScoreOfChosenWay(x); int yScore = GetScoreOfChosenWay(y); return(xScore - yScore); }); ChosenWayToPlay bestWayToPlay = waysToPlay[waysToPlay.Count - 1]; PlaceDomino(bestWayToPlay.chosenDomino, bestWayToPlay.chosenPlace, history); dominoControllers.Remove(bestWayToPlay.chosenDomino); // Debug Debug.Log("Chosen Domino: " + bestWayToPlay.chosenDomino.leftValue + ", " + bestWayToPlay.chosenDomino.rightValue + ", " + bestWayToPlay.chosenDomino.upperValue + ", " + bestWayToPlay.chosenDomino.lowerValue); if (bestWayToPlay.chosenPlace != null) { Debug.Log("Chosen Place: " + bestWayToPlay.chosenPlace.leftValue + ", " + bestWayToPlay.chosenPlace.rightValue + ", " + bestWayToPlay.chosenPlace.upperValue + ", " + bestWayToPlay.chosenPlace.lowerValue); } Debug.Log(Environment.StackTrace); nextState = NextState.Wait; gameController.PlayerPlayDomino(this, bestWayToPlay.chosenDomino, bestWayToPlay.chosenPlace); break; } }