public bool CanPass(string playerName) { if (m_GameState != GAME_STATUS.PlayingCards) { return(false); } if (string.IsNullOrEmpty(m_CurrentTurnPlayerName)) { return(false); } if (m_CurrentTurnPlayerName != playerName) { return(false); } Player lastPlayer = null; Player currentPlayer = null; int currentPlayerIndex = -1; for (var i = 0; i < m_Players.Count; i++) { if (m_Players[i].PlayerName == m_CurrentTurnPlayerName) { currentPlayer = m_Players[i]; currentPlayerIndex = i; } if (!string.IsNullOrEmpty(m_LastPlayerName) && m_Players[i].PlayerName == m_LastPlayerName) { lastPlayer = m_Players[i]; } } if (currentPlayer == null) { return(false); } List <Card> lastPlay = m_LastPlay == null || m_LastPlay.Count == 0 ? null : new List <Card>(); if (lastPlay != null && m_LastPlay != null) { lastPlay.AddRange(m_LastPlay); } // if it's first play if (lastPlayer == null) { return(false); // cannot pass } else { if (lastPlayer == currentPlayer) { return(false); // cannot pass } else { int nextPlayerIndex = (currentPlayerIndex + 1) % m_Players.Count; var nextPlayer = m_Players[nextPlayerIndex]; var nextPlayerCards = nextPlayer.CurrentHand.GetCards(); if (nextPlayerCards.Count == 1) { if (lastPlay != null && lastPlay.Count == 1) { var best = BigTwoLogic.TryToGetBestSingle(currentPlayer.CurrentHand.GetCards()); if (best != null && best.Count > 0) { var cards = currentPlayer.CurrentHand.GetCards(best); if (BigTwoLogic.CheckBetterSingle(cards[0], lastPlay[0])) { return(false); // cannot pass } } } else if (lastPlay != null && lastPlayer == nextPlayer) { var best = BigTwoLogic.TryToGiveOutBest(currentPlayer.CurrentHand.GetCards(), lastPlay.Count); if (best != null && best.Count > 0) { var cards = currentPlayer.CurrentHand.GetCards(best); if (BigTwoLogic.CheckBetterCards(cards, lastPlay)) { return(false); // cannot pass } } } } return(true); } } //return true; }
public bool AcceptPlay(string playerName, List <int> cardList) { if (m_GameState != GAME_STATUS.PlayingCards) { return(false); } if (string.IsNullOrEmpty(m_CurrentTurnPlayerName)) { return(false); } if (m_CurrentTurnPlayerName != playerName) { return(false); } Player lastPlayer = null; Player currentPlayer = null; int currentPlayerIndex = -1; for (var i = 0; i < m_Players.Count; i++) { if (m_Players[i].PlayerName == m_CurrentTurnPlayerName) { currentPlayer = m_Players[i]; currentPlayerIndex = i; } if (!string.IsNullOrEmpty(m_LastPlayerName) && m_Players[i].PlayerName == m_LastPlayerName) { lastPlayer = m_Players[i]; } } if (currentPlayer == null) { return(false); } var playerCards = currentPlayer.CurrentHand.GetCards(); List <Card> playCards = new List <Card>(); foreach (var idx in cardList) { if (idx < 0 || idx >= playerCards.Count) { return(false); } else { playCards.Add(playerCards[idx]); } } Hand playHand = playCards.Count > 0 ? new Hand(playCards) : null; if (playHand != null) { playHand.SortCards(); } List <Card> lastPlay = m_LastPlay == null || m_LastPlay.Count == 0 ? null : new List <Card>(); if (lastPlay != null && m_LastPlay != null) { lastPlay.AddRange(m_LastPlay); } // if it's first play if (lastPlayer == null) { if (playHand == null) { return(false); // cannot pass } if (playHand.IndexOfCard("3D") < 0) { return(false); // must contain Diamond 3 } //if (playCards.Count == 3) return false; // not allow triple as first play var playHandCards = playHand.GetCards(); if (BigTwoLogic.CheckBetterCards(playHandCards, null)) { m_LastTurnPlay.Clear(); m_LastTurnPlay.AddRange(playHandCards); m_LastPlay.Clear(); m_LastPlay.AddRange(playHandCards); m_LastPlayerName = string.Copy(m_CurrentTurnPlayerName); m_LastTurnPlayerName = string.Copy(m_CurrentTurnPlayerName); foreach (var playCard in playCards) { currentPlayer.CurrentHand.Discard(playCard.ToString()); } currentPlayer.CurrentHand.SortCards(); int nextPlayerIndex = (currentPlayerIndex + 1) % m_Players.Count; m_CurrentTurnPlayerName = m_Players[nextPlayerIndex].PlayerName; m_CurrentTurns++; return(true); } else { return(false); } } else { int nextPlayerIndex = (currentPlayerIndex + 1) % m_Players.Count; var nextPlayer = m_Players[nextPlayerIndex]; var nextPlayerCards = nextPlayer.CurrentHand.GetCards(); if (nextPlayerCards.Count == 1) { if (cardList == null || cardList.Count == 0) // want to pass { if (lastPlay != null && lastPlay.Count == 1) { var best = BigTwoLogic.TryToGetBestSingle(currentPlayer.CurrentHand.GetCards()); if (best != null && best.Count > 0) { var cards = currentPlayer.CurrentHand.GetCards(best); if (BigTwoLogic.CheckBetterSingle(cards[0], lastPlay[0])) { return(false); // cannot pass } } } else if (lastPlay != null && lastPlayer == nextPlayer) { var best = BigTwoLogic.TryToGiveOutBest(currentPlayer.CurrentHand.GetCards(), lastPlay.Count); if (best != null && best.Count > 0) { var cards = currentPlayer.CurrentHand.GetCards(best); if (BigTwoLogic.CheckBetterCards(cards, lastPlay)) { return(false); // cannot pass } } } } else { if (cardList.Count == 1 && cardList[0] != playerCards.Count - 1) { return(false); // must be the biggest one } } } if (lastPlayer == currentPlayer) { if (playHand == null) { return(false); // cannot pass } var playHandCards = playHand.GetCards(); if (BigTwoLogic.CheckBetterCards(playHandCards, null)) { m_LastTurnPlay.Clear(); m_LastTurnPlay.AddRange(playHandCards); m_LastPlay.Clear(); m_LastPlay.AddRange(playHandCards); m_LastPlayerName = string.Copy(m_CurrentTurnPlayerName); m_LastTurnPlayerName = string.Copy(m_CurrentTurnPlayerName); foreach (var playCard in playCards) { currentPlayer.CurrentHand.Discard(playCard.ToString()); } if (currentPlayer.CurrentHand.GetNumberOfCards() == 0) { m_CurrentTurnPlayerName = ""; m_GameState = GAME_STATUS.EndRound; } else { currentPlayer.CurrentHand.SortCards(); m_CurrentTurnPlayerName = nextPlayer.PlayerName; } m_CurrentTurns++; return(true); } else { return(false); } } else { if (playHand == null) // pass { m_LastTurnPlay.Clear(); m_LastTurnPlayerName = string.Copy(m_CurrentTurnPlayerName); m_CurrentTurnPlayerName = nextPlayer.PlayerName; m_CurrentTurns++; return(true); } else { var playHandCards = playHand.GetCards(); if (BigTwoLogic.CheckBetterCards(playHandCards, lastPlay)) { m_LastTurnPlay.Clear(); m_LastTurnPlay.AddRange(playHandCards); m_LastPlay.Clear(); m_LastPlay.AddRange(playHandCards); m_LastPlayerName = string.Copy(m_CurrentTurnPlayerName); m_LastTurnPlayerName = string.Copy(m_CurrentTurnPlayerName); foreach (var playCard in playCards) { currentPlayer.CurrentHand.Discard(playCard.ToString()); } if (currentPlayer.CurrentHand.GetNumberOfCards() == 0) { m_CurrentTurnPlayerName = ""; m_GameState = GAME_STATUS.EndRound; } else { currentPlayer.CurrentHand.SortCards(); m_CurrentTurnPlayerName = nextPlayer.PlayerName; } m_CurrentTurns++; return(true); } else { return(false); } } } } //return false; }