Ejemplo n.º 1
0
        public void JugarFicha(Jugador jugador, Ficha ficha, int? ladoPreferido = null)
        {
            if (_turnoActual == -1)
                throw new Exception("El partido ha terminado");

            if (jugador != Jugadores[_turnoActual])
                throw new Excepciones.JugadorIntentoJugarEnTurnoErroneoException();

            if (!jugador.PoseeFicha(ficha))
                throw new Excepciones.JugadorIntentoJugarFichaQueNoPoseeException();

            if (Fichas.Count == 0)
            {
                //Quiere decir que nunca se ha jugado
                if (_cantidadPartidasJugadas == 0 && !ficha.Equals(new Ficha(6, 6)))
                    throw new Excepciones.JuegoNoComenzoConDobleSeisException();

                Fichas.Add(ficha);
                jugador.Fichas.Remove(ficha);
                ManejarSiguienteTurno();
            }
            else
            {
                if (Fichas.Any(f => f.Valor.Equals(ficha.Valor)))
                {
                    throw new FichaRepetidaException();
                }

                //Se revisa si el jugador tiene intencion de un lado especifico
                if (ladoPreferido.HasValue)
                {
                    if (ficha.Valor.A == ExtremoIzq && ficha.Valor.B == ExtremoDer ||
                        ficha.Valor.B == ExtremoIzq && ficha.Valor.A == ExtremoDer)
                    {
                        ProcesarJugada(jugador, ficha, ladoPreferido == ExtremoDer);
                        return;
                    }
                }

                //De lo contrario se intenta colocar la ficha en el tablero, comenzando por el lado izquierdo
                if (Fichas.First().PuedeJugarA(ficha))
                {
                    ProcesarJugada(jugador, ficha, true);
                }
                else if (Fichas.Last().PuedeJugarB(ficha))
                {
                    ProcesarJugada(jugador, ficha);
                }
                else
                {
                    throw new JugadaInvalidaException();
                }

            }
        }