/// <summary> /// Make the specified player join the team on the specified position /// </summary> public void Join(Player player, PlayerPosition position) { if ( ((position == PlayerPosition.North || position == PlayerPosition.East) && Player1 != null) || ((position == PlayerPosition.South || position == PlayerPosition.West) && Player2 != null)) { throw new InvalidOperationException("Another player already holding this position"); } switch (position) { case PlayerPosition.North: case PlayerPosition.East: Player1 = player; break; case PlayerPosition.South: case PlayerPosition.West: Player2 = player; break; } }
private GameSession PrepareGameSession() { GameSession gameSession = new GameSession(); Player north = new Player(Guid.NewGuid(), "North"); Player south = new Player(Guid.NewGuid(), "South"); Player east = new Player(Guid.NewGuid(), "East"); Player west = new Player(Guid.NewGuid(), "West"); gameSession.Join(north, PlayerPosition.North); gameSession.Join(south, PlayerPosition.South); gameSession.Join(east, PlayerPosition.East); gameSession.Join(west, PlayerPosition.West); //--1st bid: East 1 Diamonds gameSession.PlaceBid(Bid.CreateBid(gameSession.GetPlayer(PlayerPosition.East),1, Suit.Diamonds)); //--2nd bid: North Pass gameSession.PlaceBid(Bid.CreatePassBid(gameSession.GetPlayer(PlayerPosition.North))); //--3rd bid: West Pass gameSession.PlaceBid(Bid.CreatePassBid(gameSession.GetPlayer(PlayerPosition.West))); //--4th bid: South 1 Spades gameSession.PlaceBid(Bid.CreateBid(gameSession.GetPlayer(PlayerPosition.South),1, Suit.Spades)); //--5th bid: East 2 Diamonds gameSession.PlaceBid(Bid.CreateBid(gameSession.GetPlayer(PlayerPosition.East),2, Suit.Diamonds)); //--6th bid: North Pass gameSession.PlaceBid(Bid.CreatePassBid(gameSession.GetPlayer(PlayerPosition.North))); //--7th bid: West Pass gameSession.PlaceBid(Bid.CreatePassBid(gameSession.GetPlayer(PlayerPosition.West))); //--8th bid: South 2 Spades gameSession.PlaceBid(Bid.CreateBid(gameSession.GetPlayer(PlayerPosition.South),2, Suit.Spades)); //--9th bid: East Pass gameSession.PlaceBid(Bid.CreatePassBid(gameSession.GetPlayer(PlayerPosition.East))); //--10th bid: North Pass gameSession.PlaceBid(Bid.CreatePassBid(gameSession.GetPlayer(PlayerPosition.North))); //--11th bid: West Pass gameSession.PlaceBid(Bid.CreatePassBid(gameSession.GetPlayer(PlayerPosition.West))); //--Auction Closed =>NS - 2 Spades return gameSession; }
/// <summary> /// Place the specified card by the specified player in the current trick. /// And evaluate the trick to determine the winner /// </summary> public void PlaceCard(Player player, Card card) { PlayerPosition position = gameSession.GetPlayerPosition(player); if (position == CurrentTurn) { Card tempCard; if (!CurrentTrick.TryGetValue(position, out tempCard)) { if (ValidatePlay(position, card)) { CurrentTrick[position] = card; PlayerCards[position].Remove(card); if (CurrentTrick.Values.Count == 1) { //First card in the trick CurrentTrickBaseSuit = card.Suit; } if (CurrentTrick.Values.Count >= 4) { //Trick is complete -> evalueate PlayerPosition trickWinner = EvaluateCurrentTrick(); CurrentTurn = trickWinner; switch (trickWinner) { case PlayerPosition.North: case PlayerPosition.South: TricksWon[TeamPosition.NorthSouth]++; break; case PlayerPosition.East: case PlayerPosition.West: TricksWon[TeamPosition.EastWest]++; break; } CleanCurrentTrick(); currentNumberOfTricks++; if (IsComplete) { gameSession.GameComplete(); } } else { NextTurn(); } } else { throw new ArgumentException("The played card is invalid"); } } else { throw new InvalidOperationException("The player already placed card for the current trick"); } } else { throw new InvalidOperationException("The player has played not in the right turn"); } }
/// <summary> /// Makes the current turn player play the specified card /// </summary> public void PlaceCard(Player player, Card card) { if (Status == GameSessionStatus.GamePlay && gamePlay != null) { gamePlay.PlaceCard(player, card); } }
/// <summary> /// Make the specified player leave the current game session /// </summary> public void Leave(Player player) { if (teams[TeamPosition.NorthSouth].IsMember(player)) { teams[TeamPosition.NorthSouth].Leave(player); } else if (teams[TeamPosition.EastWest].IsMember(player)) { teams[TeamPosition.EastWest].Leave(player); } else { //TODO: throw an exception that the player is not enrolled in the game } }
/// <summary> /// Make the specified player join the current game session on the /// specified position (North, South, East, West) /// </summary> public void Join(Player player, PlayerPosition position) { if (Status == GameSessionStatus.WaitingForPlayers) { Team team = null; switch (position) { case PlayerPosition.North: case PlayerPosition.South: team = teams[TeamPosition.NorthSouth]; break; case PlayerPosition.East: case PlayerPosition.West: team = teams[TeamPosition.EastWest]; break; } team.Join(player, position); if (teams[TeamPosition.NorthSouth].IsReady && teams[TeamPosition.EastWest].IsReady) { Status = GameSessionStatus.Bidding; } } else { //TODO: throw exception that the game cannot accept new members } }
/// <summary> /// Get the position of the specified player /// </summary> public PlayerPosition GetPlayerPosition(Player player) { return Players .Where(p => p.Value == player) .Select(p => p.Key).FirstOrDefault(); }
/// <summary> /// Return the current state of the game play including /// number of tricks won by each team, number of cards held by each player /// , the current player cards, the cards in the current trick /// </summary> /// <returns></returns> public GamePlayState GetGamePlayState(Player player) { if (Status == GameSessionStatus.GamePlay) { if (gamePlay != null) { PlayerPosition position = GetPlayerPosition(player); return new GamePlayState() { CurrentCards = gamePlay.PlayerCards[position], CurrentTurn = gamePlay.CurrentTurn, CurrentTrick = gamePlay.CurrentTrick, CurrentTrickBaseSuit = gamePlay.CurrentTrickBaseSuit, TricksWon = gamePlay.TricksWon }; } } else { throw new InvalidOperationException("Game is not in the Game Play state"); } return null; }
/// <summary> /// Checks if the specified player is member of the team /// </summary> public bool IsMember(Player player) { return (Player1 == player || Player2 == player); }
/// <summary> /// Make the specified player leave the team /// </summary> /// <param name="player"></param> public void Leave(Player player) { if (player == Player1) { Player1 = null; } else { Player2 = null; } }
private GameSession PrepareGameSession() { GameSession gameSession = new GameSession(); Player north = new Player(Guid.NewGuid(), "North"); Player south = new Player(Guid.NewGuid(), "South"); Player east = new Player(Guid.NewGuid(), "East"); Player west = new Player(Guid.NewGuid(), "West"); gameSession.Join(north, PlayerPosition.North); gameSession.Join(south, PlayerPosition.South); gameSession.Join(east, PlayerPosition.East); gameSession.Join(west, PlayerPosition.West); return gameSession; }