Ejemplo n.º 1
0
        public GameVM()
        {
            Cups = new CupVM[14];
            for (var i = 0; i < Cups.Length; ++i)
            {
                Cups[i] = new CupVM {
                    Stones = 4, Index = i
                }
            }
            ;

            Cups.Last().IsScore = Cups.Take(Cups.Length / 2).Last().IsScore = true;
            Cups.Last().Stones  = Cups.Take(Cups.Length / 2).Last().Stones = 0;

            State = GameState.Client;
        }
Ejemplo n.º 2
0
 private CupVM Next(CupVM cup)
 {
     return(Cups.SingleOrDefault(c => c.Index == cup.Index + 1) ?? Cups.First());
 }
Ejemplo n.º 3
0
        private CupVM Oposite(CupVM cup)
        {
            var count = Cups.Count();

            return(Cups.Single(c => c.Index == (count - 2) - cup.Index));
        }
Ejemplo n.º 4
0
        public bool Play(CupVM cup)
        {
            //GET THE CURRENT AND OPONENT PLAYERS
            var current = State == GameState.Host ? Host : Client;
            var other   = State == GameState.Client ? Client : Host;

            if (cup.Owner != current)
            {
                return(false);
            }

            //GET THE CUP WITH PLAYER INDEX-BASED
            if (cup.Stones == 0)
            {
                return(false);
            }

            //PLACE EACH STONE ON THE NEXT CUP (SKIP OPONENT SCORE CUP)
            var stones = cup.Stones;

            cup.Stones = 0;

            CupVM nextCup = cup;

            do
            {
                nextCup = Next(nextCup);

                if (nextCup.IsScore && nextCup.Owner != current)
                {
                    continue;
                }

                nextCup.Stones++;
                stones--;
            } while (stones != 0);

            //IF END IN SCORE CUP, PLAY AGAIN (NO CHANGES TO GAMESTATE)
            if (nextCup.IsScore)
            {
                if (CupCount(current) == 0 || CupCount(other) == 0)
                {
                    Collect();
                    State = GameState.Finished;
                }
                return(true);
            }

            //IF END IN EMPTY CUP, SCORE 1 + OPONENT CUP
            if (nextCup.Stones == 1)
            {
                var opositeCup = Oposite(nextCup);
                Score(current).Stones += nextCup.Stones + opositeCup.Stones;
                nextCup.Stones         = opositeCup.Stones = 0;
            }

            //IF GAME ENDED, REPORT SO
            if (CupCount(current) == 0 || CupCount(other) == 0)
            {
                Collect();
                State = GameState.Finished;
                return(true);
            }

            //SWITCH THE PLAY
            State = State == GameState.Host ? GameState.Client : GameState.Host;
            return(true);
        }
Ejemplo n.º 5
0
        public GameVM()
        {
            Cups = new CupVM[14];
            for (var i = 0; i < Cups.Length; ++i)
                Cups[i] = new CupVM { Stones = 4, Index = i };

            Cups.Last().IsScore = Cups.Take(Cups.Length / 2).Last().IsScore = true;
            Cups.Last().Stones = Cups.Take(Cups.Length / 2).Last().Stones = 0;

            State = GameState.Client;
        }
Ejemplo n.º 6
0
 private CupVM Oposite(CupVM cup)
 {
     var count = Cups.Count();
     return Cups.Single(c => c.Index == (count - 2) - cup.Index);
 }
Ejemplo n.º 7
0
 private CupVM Next(CupVM cup)
 {
     return Cups.SingleOrDefault(c => c.Index == cup.Index + 1) ?? Cups.First();
 }
Ejemplo n.º 8
0
        public bool Play(CupVM cup)
        {
            //GET THE CURRENT AND OPONENT PLAYERS
            var current = State == GameState.Host ? Host : Client;
            var other = State == GameState.Client ? Client : Host;

            if (cup.Owner != current) return false;

            //GET THE CUP WITH PLAYER INDEX-BASED
            if (cup.Stones == 0) return false;

            //PLACE EACH STONE ON THE NEXT CUP (SKIP OPONENT SCORE CUP)
            var stones = cup.Stones;
            cup.Stones = 0;

            CupVM nextCup = cup;
            do
            {
                nextCup = Next(nextCup);

                if (nextCup.IsScore && nextCup.Owner != current)
                    continue;

                nextCup.Stones++;
                stones--;
            } while (stones != 0);

            //IF END IN SCORE CUP, PLAY AGAIN (NO CHANGES TO GAMESTATE)
            if (nextCup.IsScore)
            {
                if (CupCount(current) == 0 || CupCount(other) == 0)
                {
                    Collect();
                    State = GameState.Finished;
                }
                return true;
            }

            //IF END IN EMPTY CUP, SCORE 1 + OPONENT CUP
            if (nextCup.Stones == 1)
            {
                var opositeCup = Oposite(nextCup);
                Score(current).Stones += nextCup.Stones + opositeCup.Stones;
                nextCup.Stones = opositeCup.Stones = 0;
            }

            //IF GAME ENDED, REPORT SO
            if (CupCount(current) == 0 || CupCount(other) == 0)
            {
                Collect();
                State = GameState.Finished;
                return true;
            }

            //SWITCH THE PLAY
            State = State == GameState.Host ? GameState.Client : GameState.Host;
            return true;
        }