override public Card decideOnCard(CardDecision decision, RpsAgent otherPlayer) { Card opponentsCard = decisionToCard(decision); Dictionary <Card, int> cardCounts = parentAI.getCardCounts(); //The AI will counter the player or match the player depending on which is more out of balance if (parentAI.IndexOfCardInHand(counterCard(opponentsCard)) != -1 && (cardCounts.Values.Aggregate(0, Mathf.Max) - cardCounts.Values.Aggregate(cardCounts.Count, Mathf.Max)) > 1) { return(counterCard(opponentsCard)); } else if (cardCounts[opponentsCard] == cardCounts.Values.Aggregate(0, Mathf.Max)) { return(opponentsCard); } else if (isProbable(decision) && (cardCounts.Values.Aggregate(0, Mathf.Max) - cardCounts.Values.Aggregate(cardCounts.Count, Mathf.Max)) > 1) { //return the card we have the most of return(cardCounts.First(entry => entry.Value == cardCounts.Values.Aggregate(0, Mathf.Max)).Key); } else { //Shouldn't happen, but if it does, return a random card return((Card)Random.Range(0, 2)); } }
public void loadInfo(RpsAgent player, bool isAIPlayer) { //if(this.player == player) //{ // return; //} GetComponent <RectTransform>().anchoredPosition = initialPosition; targetPosition = GetComponent <RectTransform>().anchoredPosition + new Vector2(-300, 0); this.player = player; /*need to set the associated player in the future * observeButton.GetComponent<ChatButton>().associatedPlayer = player; */ chatButton.interactable = !isAIPlayer; challengeButton.interactable = !player.IsInMatch && player.stars > 0; observeButton.interactable = player.IsInMatch && player.stars > 0; chatButton.GetComponent <ChatButton>().associatedPlayer = player; isInView = true; //TODO: Maybe some cool stuff where the player's name hashes to a unique color scheme //this.GetComponent<Image>().color = new Color(player.name.); }
private void SetLabelText(GameObject label, RpsAgent player) { label.transform.FindChild("Name").GetComponent <Text>().text = player.AgentName; label.transform.FindChild("Stats").GetComponent <Text>().text = "Remaining Cards: " + player.CardCount() + "\n" + "Stars: " + player.stars; }
override public Card decideOnCard(CardDecision decision, RpsAgent otherPlayer) { //If we don't know what the opponent will play, assume they are playing the card with the highest number in play if (decision == CardDecision.Random) { Dictionary <Card, int> totalCardCounts = GameObject.FindObjectOfType <ChallengeLobbyManager>().getTotalCardCounts(); Card highestCardInPlay = totalCardCounts.Keys.Aggregate(Card.Rock, (x, y) => totalCardCounts[x] > totalCardCounts[y] ? x : y); if (parentAI.IndexOfCardInHand(counterCard(highestCardInPlay)) != -1) { return(counterCard(highestCardInPlay)); } else { return(highestCardInPlay); } } Card opponentsCard = decisionToCard(decision); //The AI will do their best to counter the opponent if (parentAI.IndexOfCardInHand(counterCard(opponentsCard)) != -1) { return(counterCard(opponentsCard)); } else { return(opponentsCard); } }
public static void RespondToRequest(RpsAgent victim, RpsAgent initiator, bool accepted) { int requestIndex = instance.requests.FindIndex(r => r.victim == victim && r.initiator == initiator); if (requestIndex < 0) { Debug.Log("Server: " + victim.AgentName + " tried responding to a match that does not exist."); // TODO: Notify victim. return; } else { MatchRequest request = instance.requests[requestIndex]; if (accepted) { // Spawn new match. Debug.Log("Server: Match created between " + initiator.AgentName + " and " + victim.AgentName); RpsMatch match = GameObject.Instantiate <RpsMatch>(instance.RpsMatchPrefab); match.Initialize(request.initiator, request.victim); match.transform.SetParent(instance.transform); instance.ongoingMatches.Add(match); NetworkServer.Spawn(match.gameObject); } else { // Tell initiator that the victim declined. } instance.requests.RemoveAt(requestIndex); } }
override public bool decideOnPlayer(CardDecision decision, RpsAgent otherPlayer) { Card opponentsCard = decisionToCard(decision); //Only accept requests we know we can win return(parentAI.IndexOfCardInHand(counterCard(opponentsCard)) != -1); }
public void RpcNotifyMatchRequest(GameObject initiatorObj, float timeout) { if (OnMatchRequest != null) { RpsAgent initator = initiatorObj.GetComponent <RpsAgent>(); OnMatchRequest(initator, timeout); } }
private CardDecision getCardDecision(RpsAgent otherPlayer) { List <Card> otherMoves = otherPlayer.GetMoveSequence(); CardTree cardTree = getCardTree(otherMoves); // Look up the most likely move based on N-gram tree // Tries to do N-gram first, if not enough trials, decreases N and tries again (until N == 1) for (int nGramN = nGramAmount; nGramAmount >= 1; nGramAmount--) { Queue <Card> lastNMoves = new Queue <Card>(); for (int i = Mathf.Max(otherMoves.Count - nGramN, 0); i < otherMoves.Count; i++) { lastNMoves.Enqueue(otherMoves[i]); } CardTree cardTreeNode = cardTree; while (lastNMoves.Count > 0) { cardTreeNode = cardTreeNode.Traverse(lastNMoves.Dequeue()); } if (cardTreeNode.totalCount < minNGramSamplesNeeded) { continue; } Card likelyCard = cardTreeNode.MostLikelyCard(); Dictionary <Card, int> opponentCardCounts = otherPlayer.getCardCounts(); if (likelyCard == Card.Rock) { if (opponentCardCounts[Card.Paper] == 0 && opponentCardCounts[Card.Scissors] == 0) { return(CardDecision.DefiniteRock); } return(CardDecision.ProbableRock); } else if (likelyCard == Card.Paper) { if (opponentCardCounts[Card.Scissors] == 0 && opponentCardCounts[Card.Rock] == 0) { return(CardDecision.DefinitePaper); } return(CardDecision.ProbablePaper); } else if (likelyCard == Card.Scissors) { if (opponentCardCounts[Card.Rock] == 0 && opponentCardCounts[Card.Paper] == 0) { return(CardDecision.DefiniteScissors); } return(CardDecision.ProbableScissors); } } return(CardDecision.Random); }
public override void LoadForMatch(RpsAgent player) { base.LoadForMatch(player); if (!(player is RpsHumanPlayer)) { Debug.LogError("HumanHandUI loading a non-human player!"); return; } isInteractable = true; }
// Use this for initialization void Start() { player = transform.parent.GetComponent <RpsAgent>(); this.gameObject.transform.position = player.challegeLobbyPosition; this.GetComponent <SpriteRenderer>().sprite = defaultSprite; this.render = this.GetComponent <SpriteRenderer>(); this.nameTag = this.GetComponentInChildren <TextMesh>(); nameTag.text = player.AgentName; }
public void DisplayMatchRequest(RpsAgent initator, float timeout) { if (!player.isLocalPlayer) { return; } MatchRequestPanel panel = GameObject.Instantiate <MatchRequestPanel>(matchRequestPanelPrefab); panel.transform.SetParent(this.transform, false); panel.Initialize(player, initator, timeout); }
override public bool decideOnPlayer(CardDecision decision, RpsAgent otherPlayer) { Card opponentsCard = decisionToCard(decision); Dictionary <Card, int> totalCardCounts = GameObject.FindObjectOfType <ChallengeLobbyManager>().getTotalCardCounts(); Card highestCardInPlay = totalCardCounts.Keys.Aggregate(Card.Rock, (x, y) => totalCardCounts[x] > totalCardCounts[y] ? x : y); if (parentAI.IndexOfCardInHand(counterCard(opponentsCard)) != -1) { return(highestCardInPlay == opponentsCard); } return(highestCardInPlay != opponentsCard); }
public override void NotifyMatchRequest(RpsAgent initiator, float timeout) { //Instantly respond to the request because we're going to respond to someone else soon if (requestDecisionTimer > 0) { base.RespondToMatchRequest(initiator, false); return; } this.requestInitiator = initiator; requestDecision = makeRequestDecision(initiator); requestDecisionTimer = SLOW_DECISION_TIME; }
override public Card decideOnCard(CardDecision decision, RpsAgent otherPlayer) { Card opponentsCard = decisionToCard(decision); //The AI blindly decides to counter the player decision or match it if possible if (parentAI.IndexOfCardInHand(counterCard(opponentsCard)) != -1) { return(counterCard(opponentsCard)); } else { return(opponentsCard); } }
public void DisplayHumanMatchUI(RpsAgent opponent) { ExitObserveMatch(); EndMatchPanel remnantPanel = this.GetComponentInChildren <EndMatchPanel>(); if (remnantPanel != null) { Destroy(remnantPanel.gameObject); } PrepareHumanHand(); PrepareNorthHand(opponent); SetMatchUIActive(true); }
public void CmdObserveMatch(GameObject playerToObserveObj) { RpsAgent p1 = playerToObserveObj.GetComponent <RpsAgent>(); RpsMatch match = MatchManager.FindMatchWithPlayer(p1); if (match != null) { RpcNotifyObserveMatch(true, match.playerA.gameObject, match.playerB.gameObject); } else { RpcNotifyObserveMatch(false, null, null); } }
public override int getSelectedCard(RpsAgent otherPlayer) { CardDecision opponentsDecision = getCardDecision(otherPlayer); Card bestCard = this.currentStrategyState.decideOnCard(opponentsDecision, otherPlayer); int index = IndexOfCardInHand(bestCard); if (index > -1) { selectedCard = index; } selectedCard = returnRandomCardFromHand(); return(selectedCard); }
public void DisplayObservationMatch(bool existsMatch, RpsAgent p1, RpsAgent p2) { if (!existsMatch) { EndMatchPanel panel = GameObject.Instantiate <EndMatchPanel>(endMatchPanelPrefab); panel.transform.SetParent(this.transform, false); panel.SetText("Unable to observe match."); } else { PrepareNorthHand(p2); PrepareSouthHand(p1); SetMatchUIActive(true); isSpectating = true; } }
// for users to apply settings from their lobby player object to their in-game player object public override bool OnLobbyServerSceneLoadedForPlayer(GameObject lobbyPlayer, GameObject gamePlayer) { base.OnLobbyServerSceneLoadedForPlayer(lobbyPlayer, gamePlayer); RpsAgent agent = gamePlayer.GetComponent <RpsAgent>(); //Set the players name agent.AgentName = playerNames[lobbyPlayer.GetComponent <NetworkLobbyPlayer>().slot]; //Create the player's lobby sprite at randomized locations AssignChallengeLobbyPosition(agent); //Disable it's in match UI //gamePlayer.GetComponent<RpsHumanPlayer>().IsHandUIVisible = false; Debug.Log("player spawned"); return(true); }
public void CmdRequestMatch(NetworkInstanceId victimID) { GameObject obj = NetworkServer.FindLocalObject(victimID); if (obj == null) { Debug.Log("Player requested match with non-existing object."); return; } RpsAgent victim = obj.GetComponent <RpsAgent>(); if (victim == null) { Debug.Log("Player requested match with non-player object."); return; } RequestMatch(victim); }
public void CmdRespondToMatchRequest(NetworkInstanceId initiatorID, bool accepted) { GameObject obj = NetworkServer.FindLocalObject(initiatorID); if (obj == null) { Debug.Log("Player requested match with non-existing object."); return; } RpsAgent initiator = obj.GetComponent <RpsAgent>(); if (initiator == null) { Debug.Log("Player requested match with non-player object."); return; } RespondToMatchRequest(initiator, accepted); }
private void AssignChallengeLobbyPosition(RpsAgent agent, int maxTries = 50) { Collider2D agentCollider = agent.GetComponentInChildren <Collider2D>(); bool freeSpace = false; float bestMargin = -1.0f; Vector3 posWithBestMargin = agent.transform.position; for (int trial = 0; trial < maxTries && !freeSpace; trial++) { freeSpace = true; agent.challegeLobbyPosition = new Vector3(UnityEngine.Random.Range(-5.0f, 5.0f), UnityEngine.Random.Range(-3.0f, 3.0f), 0); agent.transform.position = agent.challegeLobbyPosition; float worstOverlap = 1000000.0f; foreach (Collider2D col in occupiedSpaces) { if (agentCollider.bounds.Intersects(col.bounds)) { freeSpace = false; float dist = (agentCollider.bounds.center - col.bounds.center).sqrMagnitude; if (dist < worstOverlap) { worstOverlap = dist; } } } if (!freeSpace) { if (worstOverlap > bestMargin) { bestMargin = worstOverlap; posWithBestMargin = agent.transform.position; } } } if (!freeSpace) { Debug.LogWarning("Server: Could not find good space for " + agent.AgentName + " in lobby. " + "Using best spot " + posWithBestMargin + " with margin " + bestMargin + "."); agent.challegeLobbyPosition = posWithBestMargin; } occupiedSpaces.Add(agentCollider); }
public virtual void LoadForMatch(RpsAgent player) { this.player = player; isInteractable = true; this.gameObject.SetActive(true); ClearHand(); CreateHand(); RepositionHand(); if (!isHidden) { cardVisuals[CurrentCard].Hover(); lastHoveredCard = 0; } else { CurrentCard = player.CardCount() / 2; lastHoveredCard = CurrentCard; } }
override public bool decideOnPlayer(CardDecision decision, RpsAgent otherPlayer) { Card opponentsCard = decisionToCard(decision); //Only accept requests which can leave our hand balanced Dictionary <Card, int> cardCounts = parentAI.getCardCounts(); if (parentAI.IndexOfCardInHand(counterCard(opponentsCard)) != -1) { return(cardCounts.Values.Aggregate(0, Mathf.Max) - cardCounts[counterCard(opponentsCard)] <= 1); } else if (parentAI.IndexOfCardInHand(opponentsCard) != -1) { return(cardCounts.Values.Aggregate(0, Mathf.Max) - cardCounts[opponentsCard] <= 1); } else { //If we can't beat the card, we'll challenge if our hand is sufficiently out of whack in terms of balance return(isProbable(decision) && (cardCounts.Values.Aggregate(0, Mathf.Max) - cardCounts.Values.Aggregate(cardCounts.Count, Mathf.Max)) > 1); } }
public static void StartMatchRequest(RpsAgent initiator, RpsAgent victim) { if (initiator.stars <= 0) { Debug.Log("You lost and can't request matches"); return; } if (initiator == victim) { Debug.LogWarning("Server: Player trying to request match with himself."); return; } if (victim.IsInMatch) { Debug.Log("Player is already in a match"); return; } if (instance.requests.Exists(r => (r.initiator == initiator))) { Debug.LogWarning("Server: Initiator Player has already requested a match."); return; } if (victim.CardCount() == 0 || victim.stars <= 0) { Debug.LogWarning("Server: Victim has no cards to play with."); return; } Debug.Log("Server creating a match request. Initiator: " + initiator.AgentName + " Victim: " + victim.AgentName); MatchRequest matchRequest = new MatchRequest(initiator, victim); instance.requests.Add(matchRequest); // Tell otherPlayer that they have a match request waiting. victim.NotifyMatchRequest(initiator, matchRequest.timeRemaining); }
//Adding the AI public override void OnLobbyServerSceneChanged(string sceneName) { base.OnLobbyServerSceneChanged(sceneName); Debug.Log("Server: moving into scene " + sceneName + "(lobbySCene is : " + lobbyScene + ")"); if (sceneName == lobbyScene) { ResetFields(); } //Now to add all the AI players for (int i = 0; i < aiPlayerCount; i++) { GameObject aiPlayer = Instantiate(this.aiPlayer); RpsAgent agent = aiPlayer.GetComponent <RpsAI>(); agent.AgentName = "Unit 0" + i; //Create the player's lobby sprite at randomized locations AssignChallengeLobbyPosition(agent); NetworkServer.Spawn(aiPlayer); } }
public static RpsMatch FindMatchWithPlayer(RpsAgent player) { return(instance.ongoingMatches.Find(match => match.playerA == player || match.playerB == player)); }
public virtual void BeginMatch(RpsAgent opponent) { this.IsInMatch = true; this.hasSelectedCardForMatch = false; this.selectedCard = -1; }
public virtual int getSelectedCard(RpsAgent otherPlayer) { throw new MissingReferenceException("getSelectedCard not implemented??"); }
public void RequestMatch(RpsAgent victim) { MatchManager.StartMatchRequest(this, victim); }