public GameState loadGame(out int playerTurn) { ludoBoard.loadSavedBoard(out playerTurn); GameState gameState = new GameState(); Piece[][] pieces = (Piece[][]) ludoBoard.State["pieces"]; return updateGameState(gameState, pieces); }
/// <summary> /// This is the method that UI will call in order to get a status update /// </summary> /// <param name="gameEvent"></param> /// <returns></returns> public GameState parseEvent(GameEvent gameEvent) { Colors playerID = gameEvent.Player; piece chosenPieceID = gameEvent.Piece; Piece[][] pieces = (Piece[][]) ludoBoard.State["pieces"]; Piece chosenPiece = pieces[(int)playerID][chosenPieceID]; dice dice = gameEvent.Dice; Player[] players = (Player[]) ludoBoard.State["players"]; Player player = players[(int)playerID]; GameState gameState = new GameState(); bool isPieceActivated = false; Nest[] nests = (Nest[]) ludoBoard.State["nests"]; Debug.Write("\nRules: Deciding action for player: " + player.PlayerID + ", with piece: " + chosenPiece.PieceID + ", player rolled: " + dice); if (!chosenPiece.Active) { isPieceActivated = tryActivate(playerID, dice, chosenPieceID); Debug.Write(String.Format("\nRules: Tried activating new piece: {0}", isPieceActivated)); if (ludoBoard.Instruction != Instructions.CollisionWithSelf) { ludoBoard.Instruction = (isPieceActivated) ? Instructions.Introduce : Instructions.NotIntroduce; } } else { Debug.Write("\nRules: Trying to move piece."); bool hasPieceMoved = tryMove(chosenPiece, dice); } Debug.WriteLine("\n Instruction = " + gameState.Instruction); return updateGameState(gameState, pieces); }
/// <summary> /// If there is a player with higher result than the others, make them the winner /// Otherwise, do nothing /// </summary> /// <param name="gameEvent"></param> /// <param name="winner"></param> /// <returns></returns> private GameState decideStartingPlayer(GameEvent gameEvent) { GameState gameState = new GameState(); Colors winner; Dictionary<Colors, dice> diceResults = gameEvent.Dices; Debug.WriteLine("\ndiceResults check"); // Create ordered List var orderedResults = diceResults.OrderBy(results => results.Value).ToList(); // Check if there are two equally maximum results int last = orderedResults[numOfPlayers - 1].Value; int nextLast = orderedResults[numOfPlayers - 2].Value; bool isTie = (last == nextLast); if (isTie) { Debug.WriteLine("\n it's a Tie"); gameState.StartingPlayer = -1; } else { winner = orderedResults[numOfPlayers - 1].Key; gameState.StartingPlayer = (int)winner; Debug.Write("\nWinner of round robin is: " + winner); ActiveGame = true; // TODO: ??? } return gameState; }
private GameState updateGameState(GameState gameState, Piece[][] pieces) { gameState.Pieces = exportPieces(pieces); gameState.Instruction = exportInstruction(); return gameState; }
/// <summary> /// Exports status to int[]-friendly API handling /// </summary> /// <returns></returns> public GameState ToArray() { GameState gameState = new GameState(); gameState.Nests = nestsToArray(); gameState.Squares = squaresToArray(squares, numOfSquaresPerSide); gameState.ExitSquares = squaresToArray(exitSquares, numOfExitSquaresPerSide); return gameState; }
private void StartGame() { ruleEngine = new RuleEngine(); gameState = new GameState(); gameEvent = new GameEvent(); pieces = new List<Piece>(); hasGameStarted = true; createTurnString(ref turn); PlayerTurn = turn; gameEvent.Player = LudoRules.Colors.Blue; chooseTalkingBubble("/images/started.png"); initializePieces(); }
/// <summary> /// When the "roll dice" button has been clicked - communicate with rule engine /// </summary> /// <param name="sender"></param> /// <param name="e"></param> void RollDiceAndHandleEvents(object sender, RoutedEventArgs e) { if (hasGameStarted) { // Force player to choose the right piece if (!hasChosenPiece) { chooseTalkingBubble("/images/choosepieceid.png"); } else if (hasChosenPiece && (ChosenPieceColor != gameEvent.Player)) { chooseTalkingBubble("/images/chooserightpieceid.png"); } // Piece now chosen,try rolling the dice else { Dice newDice = new Dice(); int diceRoll = newDice.rollDice(); showDice("/images/dice" + diceRoll.ToString() + ".jpg"); // Let the rule engine do its magic and update the game state, then update GUI gameState = parseNewEvent(diceRoll); changePieces(gameState); showInstructions(diceRoll); // Prepare next turn resetInGameValues(); gameEvent.Player = switchTurn(); createTurnString(ref turn); PlayerTurn = turn; } } }
void LoadGame(object sender, RoutedEventArgs e) { int assignedTurn; StartGame(); try { gameState = ruleEngine.loadGame(out assignedTurn); changePieces(gameState); gameEvent.Player = (LudoRules.Colors)assignedTurn; createTurnString(ref turn); PlayerTurn = turn; } catch (Exception exception) { Debug.WriteLine("Exception: {0}", exception); } }
/// <summary> /// Update GUI bindings when rule engine changes the state of the pieces /// </summary> /// <param name="gameState"></param> private void changePieces(GameState gameState) { int x, y; int pieceIndexInNest = 0; foreach (var pieceInfo in gameState.Pieces) { var piecePosition = pieceInfo[0]; var pieceColor = pieceInfo[1]; var pieceSteps = pieceInfo[2]; if (piecePosition == -1) // in nest { x = nestPositions[pieceIndexInNest][0]; y = nestPositions[pieceIndexInNest][1]; } else if (piecePosition == 44 || pieceSteps == 44) // exited, nowhere { x = 0; y = 0; pieceImages[pieceIndexInNest].Visibility = Visibility.Hidden; } else // in squares { int addPos = 0; if (pieceSteps >= 40) // in exit squares { switch (pieceColor) { case (int) LudoRules.Colors.Blue: addPos += 12; break; case (int) LudoRules.Colors.Red: addPos += 0; break; case (int) LudoRules.Colors.Yellow: addPos += 4; break; case (int) LudoRules.Colors.Green: addPos += 8; break; } x = squarePositions[pieceSteps + 1 + addPos][0]; y = squarePositions[pieceSteps + 1 + addPos][1]; } else { x = squarePositions[piecePosition + 1 + addPos][0]; y = squarePositions[piecePosition + 1 + addPos][1]; } } pieces[pieceIndexInNest].X = x; pieces[pieceIndexInNest].Y = y; pieceIndexInNest++; } // Change Property Pieces = pieces; }