Main
using System;
using System.Collections;
using System.Collections.Generic;
using Sifteo;
using Libraries;

namespace App
{
    // Purpose:
    // Cube types.
    // There are two players and one judge that decides the winner.
    public enum CubeType
    {
        e_player1,
        e_player2,
        e_player3,
        e_player4,
        e_judge,
        e_none
    }

    // Purpose:
    // Represent the different game modes that can be played.
    public enum GameType
    {
        e_rockPaperScissor,
        e_pirateZombieMonkeyRobot
    }

    // Purpose:
    // Match result for a player.
    public enum ResultState
    {
        e_win,
        e_lose,
        e_tie,
        e_none
    }

    // Store end of game result for a player.
    // After each match each player result should be stored.
    public class GameResult
    {
        // Purpose:
        // Constructor.  Set the properties.
        public GameResult(ResultState result,
                          String choice)
        {
            m_result = result;
            m_choice = choice;
        }

        // Whether the game was won/lost/tied.
        public ResultState m_result = ResultState.e_none;
       
        // What the selection had been.
        public String m_choice = "None";
    }

    // Purpose:
    // Data object to store player stats.
    public class GameStats
    {  
        // Purpose:
        // Default constructor.  Set up for a 4 player game.
        public GameStats()
        {
            m_results.Add(CubeType.e_player1, new List<GameResult>());
            m_results.Add(CubeType.e_player2, new List<GameResult>());
            m_results.Add(CubeType.e_player3, new List<GameResult>());
            m_results.Add(CubeType.e_player4, new List<GameResult>());
        }

        // player    - Which player the stat is for.
        // result    - Did the player win, what was the result?
        // selection - What the player selected in the match.
        public void AddStat(CubeType player,
                            ResultState result,
                            String selection)
        {
            List<GameResult> results = GetPlayerResults(player);
            if(null != results)
                results.Add(new GameResult(result, selection));
        }
       
        // Purpose:
        // Retrive the sum of how many wins/loses/ties the player has.
        //
        // player     - Which player to retrive the stats for.
        // winLoseTie - Which result to get the sum of.
        public int GetSumWinLoseTie(CubeType player,
                                    ResultState winLoseTie)
        {
            int count = 0;
            List<GameResult> results = GetPlayerResults(player);   

            if((null != results) && (ResultState.e_none != winLoseTie))
            {
                foreach(GameResult result in results)
                {
                    if(winLoseTie == result.m_result)
                        ++count;
                }
            }

            return count;
        }

        // Purpose:
        // Log all the current win/lose stats.
        public void LogWinLoseStats()
        {
            Log.Debug("Player 1:W: {0}, L: {1}, T: {2}", GetSumWinLoseTie(CubeType.e_player1, ResultState.e_win),
                                                         GetSumWinLoseTie(CubeType.e_player1, ResultState.e_lose),
                                                         GetSumWinLoseTie(CubeType.e_player1, ResultState.e_tie));
           
            Log.Debug("Player 2:W: {0}, L: {1}, T: {2}", GetSumWinLoseTie(CubeType.e_player2, ResultState.e_win),
                                                         GetSumWinLoseTie(CubeType.e_player2, ResultState.e_lose),
                                                         GetSumWinLoseTie(CubeType.e_player2, ResultState.e_tie));
           
            Log.Debug("Player 3:W: {0}, L: {1}, T: {2}", GetSumWinLoseTie(CubeType.e_player3, ResultState.e_win),
                                                         GetSumWinLoseTie(CubeType.e_player3, ResultState.e_lose),
                                                         GetSumWinLoseTie(CubeType.e_player3, ResultState.e_tie));
           
            Log.Debug("Player 4:W: {0}, L: {1}, T: {2}", GetSumWinLoseTie(CubeType.e_player4, ResultState.e_win),
                                                         GetSumWinLoseTie(CubeType.e_player4, ResultState.e_lose),
                                                         GetSumWinLoseTie(CubeType.e_player4, ResultState.e_tie));
        }

        // Purpose:
        // Retrive the results for the given player.
        //
        // Return:
        // Value is the results for the player, null if no set of results was found.
        private List<GameResult> GetPlayerResults(CubeType player)
        {
            if(m_results.ContainsKey(player))
                return m_results[player];
            else
                return null;
        }
       
        // Store stats for each player as a seperate entry.
        private Dictionary<CubeType, List<GameResult>> m_results = new Dictionary<CubeType, List<GameResult>>();
    }

   
// Purpose:
// Play a game of rock/paper/scissors.
// Requires at least three cubes.  One cube for each player and one cube that acts as the judge.
// Make selections on the player cubes and shake to set.  Each extra cube allows a additional player, up to four players.
// Put the player cubes with the judge cube to determine the winner.
public class RPSApp : BasicApp
{
    // Rules to use when playing the game.
    // Use a different rules object for different game rules.
    public BaseRules m_rules = new RPSRules();

    // Purpose:
    // Returns how many cubes are attached to the application. 
    public int GetNumCubes()
    {
        return m_cubeWrappers.Count;
    }

    // Purpose:
    // Retrive the cube with the given ID as long as it is a player cube (not judge, etc).
    //
    // ID - ID of the cube to get.
    //
    // Return:
    // Value is the cube wrapper if found, null if not.
    public PlayerCubeWrapper GetPlayerWrapper(CubeType ID)
    {
        if(IsPlayer(ID))
        {
            foreach(GameCubeWrapper wrapper in m_cubeWrappers)
            {
                if(ID == wrapper.m_cubeType)
                    return (PlayerCubeWrapper)wrapper;
            }
        }
   
        return null;
    }

    // Purpose:
    // Check if the given ID is a player ID.
    //
    // Return:
    // Value is true if the ID is for a player, false if not.
    public bool IsPlayer(CubeType ID)
    {
        return ((CubeType.e_player1 == ID) || (CubeType.e_player2 == ID)|| (CubeType.e_player3 == ID)|| (CubeType.e_player4 == ID));
    }

    // Purpose:
    // Returns the wrapper for the judge cube, null if the judge dosn't exist.
    public JudgeCubeWrapper GetJudgeWrapper()
    {
        foreach(GameCubeWrapper wrapper in m_cubeWrappers)
        {
            if(CubeType.e_judge == wrapper.m_cubeType)
                return (JudgeCubeWrapper)wrapper;
        }

        return null;
    }

    // Purpose:
    // Setup the application.
    public override void Setup()
    {
        // Loop through all the cubes and set them up.
        for(int ind = 0; ind < CubeSet.Count; ++ind)         
        {
            // Create a wrapper object for each cube. The wrapper object allows us
            // to bundle a cube with extra information and behavior.
            GameCubeWrapper wrapper = null;
            bool ignore = false;
   
            if(0 == ind)
            {
                wrapper = new JudgeCubeWrapper(this, CubeSet[ind], CubeType.e_judge);
                wrapper.m_allowButtonPress = true;
            }
            else if(1 == ind)
            {
                wrapper = new PlayerCubeWrapper(this, CubeSet[ind], CubeType.e_player1);
                wrapper.m_allowButtonPress = false;
            }
            else if(2 == ind)
            {
                wrapper = new PlayerCubeWrapper(this, CubeSet[ind], CubeType.e_player2);
                wrapper.m_allowButtonPress = false;
            }
            else if(3 == ind)
            {
                wrapper = new PlayerCubeWrapper(this, CubeSet[ind], CubeType.e_player3);
                wrapper.m_allowButtonPress = false;
            }
            else if(4 == ind)
            {
                wrapper = new PlayerCubeWrapper(this, CubeSet[ind], CubeType.e_player4);
                wrapper.m_allowButtonPress = false;
            }
            else
                ignore = true;  // Only use 5 cubes at most.  Ignore others.
           
            if(!ignore)
            {
                m_cubeWrappers.Add(wrapper);
                wrapper.Render(true);
            }
        }

    // Handle events.
    CubeSet.NeighborAddEvent += OnNeighborAdd;
    CubeSet.NeighborRemoveEvent += OnNeighborRemove;
   
    foreach(GameCubeWrapper wrapper in m_cubeWrappers)
    {
        if(CubeType.e_judge == wrapper.m_cubeType)
            ((JudgeCubeWrapper)wrapper).StartTitleScreen();
    }
   }

    // Purpose:
    // Called when a cube is placed next to another.
    // cube1, cube2 - Two cubes that are now next to each other.
    // side1, side2 - Which side the neighber cube is on.
    private void OnNeighborAdd(Cube cube1, Cube.Side side1, Cube cube2, Cube.Side side2)  
    {
        Log.Debug("Neighbor add: {0}.{1} <-> {2}.{3}", cube1.UniqueId, side1, cube2.UniqueId, side2);
       
        GameCubeWrapper wrapper1 = (GameCubeWrapper)cube1.userData;
        GameCubeWrapper wrapper2 = (GameCubeWrapper)cube2.userData;

        Cube.Side side = Cube.Side.NONE;
       
        // Both cubes are game cubes.
        // Handle updating the judge.
        if ((null != wrapper1) && (null != wrapper2))
        {
            JudgeCubeWrapper judge = null;
            PlayerCubeWrapper player = null;

            // The cube is the judge cube.
            // Handle adding the other cube.
            if(CubeType.e_judge == wrapper1.m_cubeType)
            {
                judge = (JudgeCubeWrapper)cube1.userData;
                player = (PlayerCubeWrapper)wrapper2;
                side = side1;
            }
            else if(CubeType.e_judge == wrapper2.m_cubeType)
            {
                judge = (JudgeCubeWrapper)cube2.userData;
                player = (PlayerCubeWrapper)wrapper1;
                side = side2;
            }

            // Player can't be added.  Ignore it.
            if((null != player) && player.m_isEliminated)
                return;

            // In judgement state, removing shouldn't change the arrow.
            if((null != judge) && (JudgeCubeWrapper.JudgeState.e_matchResult == judge.m_state))
                return;

            // Add the player to the judge if possible and handle the game ending.
            if((null != judge) && (null != player) && (player.IsSelectionSet()))
            {
                judge.SetCubeAtSide(side, player);

                // Check if the pieces are all ready and handle the match end.
                // Show the winner, etc.
                judge.HandleEndMatch();
            }
        }
    }

    // Purpose:
    // Called when two cubes that were neighbord are move away from each other..
    // cube1, cube2 - Two cubes that are now not next to each other.
    // side1, side2 - Which side the neighber cube was on.
    private void OnNeighborRemove(Cube cube1, Cube.Side side1, Cube cube2, Cube.Side side2)  
    {
        GameCubeWrapper wrapper = (GameCubeWrapper)cube1.userData;
        if ((null != wrapper) && (CubeType.e_judge == wrapper.m_cubeType))
        {
            JudgeCubeWrapper judge = (JudgeCubeWrapper)cube1.userData;

            // In judgement state, removing shouldn't change the arrow.
            if((null != judge) && (JudgeCubeWrapper.JudgeState.e_matchResult == judge.m_state))
                return;

            judge.SetCubeAtSide(side1, null);
        }
       
        wrapper = (GameCubeWrapper)cube2.userData;
        if ((null != wrapper) && (CubeType.e_judge == wrapper.m_cubeType))
        {
            JudgeCubeWrapper judge = (JudgeCubeWrapper)cube2.userData;

            // In judgement state, removing shouldn't change the arrow.
            if((null != judge) && (JudgeCubeWrapper.JudgeState.e_matchResult == judge.m_state))
                return;

            judge.SetCubeAtSide(side2, null);
        }
    }

    // Game images to use.
    public Images m_images = new Images();

    // In game text.
    public Text m_text = new Text();
  }
}
 
Website copyright (c) 2012 Benjamin Millek, all rights reserved.