Beispiel #1
0
        public static void Jugada(String text, List <Jugador> listaJugadores, int proximoturno, Socket current)
        {
            if (text.Contains("jugada"))
            {
                Ficha  t            = JsonConvert.DeserializeObject <Ficha>(strtablerito);
                bool   EsTurno      = false;
                bool   jugadavalida = true;
                int    salto        = 0;
                int    tamano       = 0;
                string original     = "";
                string nuevo        = "";
                Jugada jug          = JsonConvert.DeserializeObject <Jugada>(text);
                int    contador     = 0;
                foreach (var item in jug.jugada)
                {
                    if (contador == 0)
                    {
                        original = item;
                        tamano++;
                    }
                    if (contador >= 1)
                    {
                        nuevo = item;
                        contador++;
                        tamano++;
                    }
                    contador++;
                    salto = tamano / 2;
                }
                int o = int.Parse(original);
                int n = int.Parse(nuevo);
                foreach (var fichita in t.tablero)
                {
                    if (fichita.ficha[1] == o)
                    {
                        colorjugador = fichita.ficha[0];
                    }
                    if (fichita.ficha[1] == n)
                    {
                    }
                }
                for (int i = 0; i < realjugadores; i++)
                {
                    if (listaJugadores[i].turno == true)
                    {
                        if (listaJugadores[i].color == colorjugador)
                        {
                            EsTurno = true;
                        }
                        else
                        {
                            EsTurno = false;
                        }
                    }
                }
                if (EsTurno)
                {
                    if (jugadavalida)
                    {
                        foreach (var item in listaJugadores)
                        {
                            foreach (var itemx in t.tablero)
                            {
                                if (itemx.ficha[1] == o)
                                {
                                    itemx.ficha[1] = n;

                                    for (int i = 0; i < realjugadores; i++)
                                    {
                                        if (listaJugadores[i].turno == true)
                                        {
                                            listaJugadores[i].turno = false;
                                            if (i > realjugadores - 2)
                                            {
                                                proximoturno = 0;
                                            }
                                            else
                                            {
                                                proximoturno = i + 1;
                                            }
                                        }
                                    }
                                    listaJugadores[proximoturno].turno = true;
                                    strtablerito = JsonConvert.SerializeObject(t);
                                    byte[] data2 = Encoding.ASCII.GetBytes(strtablerito);
                                    Console.WriteLine(strtablerito);
                                    current.Send(data2);

                                    Console.WriteLine("movimiento realizado");
                                }
                            }
                        }
                    }
                }
                else
                {
                    byte[] data2 = Encoding.ASCII.GetBytes("novalido");
                    Console.WriteLine("No es turno");
                    current.Send(data2);
                }
                if (!jugadavalida)
                {
                    if (salto >= 1)
                    {
                        int s = salto;
                        foreach (var fichita in t.tablero)
                        {
                            if (fichita.ficha[s] != n)
                            {
                                jugadavalida = false;
                                byte[] data2 = Encoding.ASCII.GetBytes("novalido");
                                Console.WriteLine("Jugada invalida");
                                current.Send(data2);
                            }
                            s++;
                            if (fichita.ficha[tamano] == n)
                            {
                                jugadavalida = false;
                                byte[] data2 = Encoding.ASCII.GetBytes("novalido");
                                Console.WriteLine("Jugada invalida");
                                current.Send(data2);
                            }
                        }

                        if (jugadavalida)
                        {
                            foreach (var item in listaJugadores)
                            {
                                foreach (var itemx in t.tablero)
                                {
                                    if (itemx.ficha[1] == o)
                                    {
                                        itemx.ficha[1] = n;

                                        for (int i = 0; i < realjugadores; i++)
                                        {
                                            if (listaJugadores[i].turno == true)
                                            {
                                                listaJugadores[i].turno = false;
                                                if (i > 0)
                                                {
                                                    proximoturno = 0;
                                                }
                                                else
                                                {
                                                    proximoturno = i + 1;
                                                }
                                            }
                                        }
                                        listaJugadores[proximoturno].turno = true;
                                        strtablerito = JsonConvert.SerializeObject(t);
                                        byte[] data2 = Encoding.ASCII.GetBytes(strtablerito);
                                        Console.WriteLine(strtablerito);
                                        current.Send(data2);
                                        Console.WriteLine("movimiento realizado");
                                    }
                                }
                            }
                        }
                    }
                    else if (salto == 0)
                    {
                        byte[] data2 = Encoding.ASCII.GetBytes("novalido");
                        Console.WriteLine("Jugada invalida");
                        current.Send(data2);
                    }
                }//aqui
            }
        }
 public Ficha(Ficha ficha)
 {
     Color    = ficha.Color;
     Posicion = ficha.Posicion;
 }
Beispiel #3
0
        public static void QuieroJugar(String text, List <Jugador> listaJugadores, Socket current)
        {
            if (text.Contains("quierojugar"))
            {
                Ficha  t         = JsonConvert.DeserializeObject <Ficha>(strtablerito);
                string tablerito = JsonConvert.SerializeObject(t);
                if (tablerito.Contains(finrojo2j) && realjugadores == 2)
                {
                    byte[] data4 = Encoding.ASCII.GetBytes("fin,0");
                    current.Send(data4);
                    juegoterminado = true;
                }
                if (tablerito.Contains(finverde2j) && realjugadores == 2)
                {
                    byte[] data4 = Encoding.ASCII.GetBytes("fin,3");
                    current.Send(data4);
                    juegoterminado = true;
                }
                if (tablerito.Contains(finrojo3j) && realjugadores == 3)
                {
                    byte[] data4 = Encoding.ASCII.GetBytes("fin,0");
                    current.Send(data4);
                    juegoterminado = true;
                }
                if (tablerito.Contains(finazul3j) && realjugadores == 3)
                {
                    byte[] data4 = Encoding.ASCII.GetBytes("fin,2");
                    current.Send(data4);
                    juegoterminado = true;
                }
                if (tablerito.Contains(finamarillo3j) && realjugadores == 3)
                {
                    byte[] data4 = Encoding.ASCII.GetBytes("fin,4");
                    current.Send(data4);
                    juegoterminado = true;
                }

                if (!juegoterminado)
                {
                    string[] texto          = text.Split(',');
                    string   hashpreguntado = texto[1];
                    for (int i = 0; i < realjugadores; i++)
                    {
                        if (listaJugadores[i].turno == true)
                        {
                            Console.WriteLine(listaJugadores[i].turno.ToString());
                            byte[] data = Encoding.ASCII.GetBytes("turno," + listaJugadores[i].color.ToString());
                            current.Send(data);
                            Console.WriteLine("turno del color, " + listaJugadores[i].color.ToString());
                            if (listaJugadores[i].hash == hashpreguntado)
                            {
                                Console.WriteLine("Pidio tablero");

                                Console.WriteLine(tablerito);

                                byte[] data2 = Encoding.ASCII.GetBytes(tablerito);
                                current.Send(data2);
                            }
                            else
                            {
                                byte[] data3 = Encoding.ASCII.GetBytes("nojuega");
                                current.Send(data3);
                            }
                        }
                    }
                }  //aqui;
            }
        }
Beispiel #4
0
        public static void Registrar(int res, Socket current)
        {
            Ficha t = JsonConvert.DeserializeObject <Ficha>(strtablerito);

            byte[] recBuf = new byte[res];
            Array.Copy(Server.buffer, recBuf, res);
            string text = Encoding.ASCII.GetString(recBuf);

            Console.WriteLine("Recivido: " + text);
            Console.WriteLine(totalconectados.ToString());
            if (totalconectados < 2)
            {
                if (text == "registrarse")
                {
                    byte[] data = Encoding.ASCII.GetBytes("denegado");
                    Console.WriteLine("denegado");
                    current.Send(data);
                    text = "";
                }
            }
            if (totalconectados == 2)
            {
                if (text == "registrarse" && totaljugadores == 0)
                {
                    Console.WriteLine("Registrado 1 jugador" + current.GetHashCode().ToString());
                    totaljugadores++;
                    Hash("Registrado 1 jugador");
                    jugador1.turno = true;
                    jugador1.color = 0;
                    jugador1.hash  = hash;
                    listaJugadores.Add(jugador1);
                    byte[] data = Encoding.ASCII.GetBytes("ok,0," + hash + "," + totalconectados);
                    Console.WriteLine("ok,0," + hash);
                    current.Send(data);
                    Validar.TableroInicial(totaljugadores);
                    text = "";
                }
                if (text == "registrarse" && totaljugadores == 1)
                {
                    Console.WriteLine("Registrado 2 jugador");
                    totaljugadores++;
                    Hash("Registrado 2 jugador");
                    jugador2.turno = false;
                    jugador2.color = 3;
                    jugador2.hash  = hash;
                    listaJugadores.Add(jugador2);
                    byte[] data = Encoding.ASCII.GetBytes("ok,3," + hash + "," + totalconectados);
                    Console.WriteLine("ok,3," + hash);
                    current.Send(data);
                    Validar.TableroInicial(totaljugadores);
                    text = "";
                    Console.WriteLine("");
                }
            }
            if (totalconectados == 3)
            {
                if (text == "registrarse" && totaljugadores == 0)
                {
                    Console.WriteLine("Registrado 1 jugador" + current.GetHashCode().ToString());

                    totaljugadores++;
                    Hash("Registrado 1 jugador");
                    jugador1.turno = true;
                    jugador1.color = 0;
                    jugador1.hash  = hash;
                    listaJugadores.Add(jugador1);
                    byte[] data = Encoding.ASCII.GetBytes("ok,0," + hash + "," + totalconectados);
                    Console.WriteLine("ok,0," + hash);
                    current.Send(data);
                    Validar.TableroInicial(totaljugadores);
                    text = "";
                }
                if (text == "registrarse" && totaljugadores == 1)
                {
                    Console.WriteLine("Registrado 2 jugador");

                    totaljugadores++;
                    Hash("Registrado 2 jugador");
                    jugador2.turno = false;
                    jugador2.color = 2;
                    jugador2.hash  = hash;
                    listaJugadores.Add(jugador2);
                    byte[] data = Encoding.ASCII.GetBytes("ok,2," + hash + "," + totalconectados);
                    Console.WriteLine("ok,2," + hash);
                    current.Send(data);
                    Validar.TableroInicial(totaljugadores);
                    text = "";
                    Console.WriteLine("");
                }
                if (text == "registrarse" && totaljugadores == 2)
                {
                    Console.WriteLine("Registrado 3 jugador");

                    totaljugadores++;
                    Hash("Registrado 3 jugador");
                    jugador3.turno = false;
                    jugador3.color = 4;
                    jugador3.hash  = hash;
                    listaJugadores.Add(jugador3);
                    byte[] data = Encoding.ASCII.GetBytes("ok,4," + hash + "," + totalconectados);
                    Console.WriteLine("ok,4," + hash);
                    current.Send(data);
                    Validar.TableroInicial(totaljugadores);
                    text = "";
                    Console.WriteLine("");
                }
            }
            //if (text == "registrarse" && totaljugadores == 0)
            //{

            //    Console.WriteLine("Registrado 1 jugador" + current.GetHashCode().ToString());

            //    totaljugadores++;
            //    Hash("Registrado 1 jugador");
            //    jugador1.turno = true;
            //    jugador1.color = 0;
            //    jugador1.hash = hash;
            //    listaJugadores.Add(jugador1);
            //    byte[] data = Encoding.ASCII.GetBytes("ok,0," + hash);
            //    Console.WriteLine("ok,0," + hash);
            //    current.Send(data);
            //    Validar.TableroInicial(totaljugadores);
            //    text = "";

            //}
            //if (text == "registrarse" && totaljugadores == 1)
            //{
            //    Console.WriteLine("Registrado 2 jugador");

            //    totaljugadores++;
            //    Hash("Registrado 2 jugador");
            //    jugador2.turno = false;
            //    jugador2.color = 3;
            //    jugador2.hash = hash;
            //    listaJugadores.Add(jugador2);
            //    byte[] data = Encoding.ASCII.GetBytes("ok,3," + hash);
            //    Console.WriteLine("ok,3," + hash);
            //    current.Send(data);
            //    Validar.TableroInicial(totaljugadores);
            //    text = "";
            //    Console.WriteLine("");
            //}
            //if (text == "registrarse" && totaljugadores == 2)
            //{
            //    totaljugadores++;
            //    Hash("Registrado 3 jugador");
            //    jugador3.turno = false;
            //    jugador3.color = 2;
            //    jugador3.hash = hash;
            //    listaJugadores.Add(jugador3);
            //    byte[] data = Encoding.ASCII.GetBytes("ok,2," + hash);
            //    Console.WriteLine("ok,2," + hash);
            //    current.Send(data);
            //    Validar.TableroInicial(totaljugadores);
            //    text = "";


            //}

            //if (text == "registrarse" && totaljugadores == 3)
            //{
            //    totaljugadores++;
            //    Hash("Registrado 4 jugador");
            //    jugador4.turno = false;
            //    jugador4.color = 3;
            //    jugador4.hash = hash;
            //    listaJugadores.Add(jugador4);
            //    byte[] data = Encoding.ASCII.GetBytes("ok,3," + hash);
            //    current.Send(data);
            //    Validar.TableroInicial(totaljugadores);
            //    text = "";

            //}
            //if (text == "registrarse" && totaljugadores == 4)
            //{
            //    totaljugadores++;
            //    Hash("Registrado 5 jugador");
            //    jugador5.turno = false;
            //    jugador5.color = 3;
            //    jugador5.hash = hash;
            //    listaJugadores.Add(jugador5);
            //    byte[] data = Encoding.ASCII.GetBytes("ok,4," + hash);
            //    current.Send(data);
            //    Validar.TableroInicial(totaljugadores);
            //    text = "";

            //}
            //if (text == "registrarse" && totaljugadores == 5)
            //{
            //    totaljugadores++;
            //    Hash("Registrado 6 jugador");
            //    jugador6.turno = false;
            //    jugador6.color = 3;
            //    jugador6.hash = hash;
            //    listaJugadores.Add(jugador6);
            //    byte[] data = Encoding.ASCII.GetBytes("ok,5," + hash);
            //    current.Send(data);
            //    Validar.TableroInicial(totaljugadores);
            //    text = "";
            //}


            Validar.QuieroJugar(text, listaJugadores, current);
            Validar.Jugada(text, listaJugadores, proximoturno, current);
            Validar.EsTurno(text, listaJugadores, current);


            current.BeginReceive(Server.buffer, 0, Server.BUFFER_SIZE, SocketFlags.None, RecibirLlamada.Llamada, current);
        }