- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Net.Sockets;
- using System.Net;
- using System.Collections;
- using System.Threading;
- namespace ConsoleApplication1
- {
- class Program
- {
- public static int gamestate = 0;
- public static string ToBin(int value, int len)
- {
- return (len > 1 ? ToBin(value >> 1, len - 1) : null) + "01"[value & 1];
- }
- public static string[] DecodePacket(byte[] c)
- {
- string h = "";
- string[] a = new string[4];
- for (int i = 0; i < c.Length; i++)
- {
- h = h + ToBin(c[i], 8);
- }
- for (int i = 0; i < h.Length; i++)
- {
- if (i <= 5)
- a[0] = a[0] + h[i];
- if (i > 5 && i <= 9)
- a[1] = a[1] + h[i];
- if (i > 9 && i <= 17)
- a[2] = a[2] + h[i];
- if (i > 17)
- a[3] = a[3] + h[i];
- }
- return a;
- }
- public static BitArray Append(BitArray current, BitArray after)
- {
- var bools = new bool[current.Count + after.Count];
- current.CopyTo(bools, 0);
- after.CopyTo(bools, current.Count);
- return new BitArray(bools);
- }
- static byte[] ResponseToPacket(byte[] c, ref string h)
- {
- byte[] bytes = new byte[3];
- BitArray HGH = new BitArray(0);
- string[] packet = new string[4];
- packet = DecodePacket(c);
- switch (packet[0])
- {
- case "100000":
- {
- h = packet[2];
- break;
- }
- case "110000":
- {
- Console.Write("Brawo wygrales\n");
- gamestate++;
- break;
- }
- case "001000":
- {
- Console.Write("To nie byla poprawna liczba\n");
- break;
- }
- case "100100":
- {
- gamestate++;
- Console.Write("Game has begun!\n");
- break;
- }
- case "111000":
- {
- Thread.Sleep(100);
- if (gamestate == 1)
- {
- if (packet[1] == "0000")
- {
- Console.WriteLine("Timeout, no one managed to guess the number");
- }
- if (packet[1] == "1111")
- {
- Console.WriteLine("You lost, second player managed to guess the number");
- }
- gamestate++;
- }
- break;
- }
- case "101000":
- {
- string time = packet[1] + packet[3];
- int czas = Convert.ToInt32(time, 2);
- Console.Write("Pozostaly czas to: ");
- Console.Write(czas);
- Console.Write("s\n");
- break;
- }
- }
- return bytes;
- }
- static byte[] ProtocolBuilder(string operation, string response, string idref)
- {
- byte[] data = new byte[3];
- int pom1 = 0, pom2 = 0, pom3 = 0;
- BitArray c = new BitArray(24);
- BitArray g = new BitArray(24);
- for (int i = 0; i < c.Length; i++)
- {
- if (i <= 5)
- {
- if (operation[pom1] == '0')
- c[i] = false;
- else
- c[i] = true;
- pom1++;
- }
- if (i > 5 && i <= 9)
- {
- if (response[pom2] == '0')
- c[i] = false;
- else
- c[i] = true;
- pom2++;
- }
- if (i > 9 && i <= 17)
- {
- if (idref[pom3] == '0')
- c[i] = false;
- else
- c[i] = true;
- pom3++;
- }
- if (i > 17)
- c[i] = false;
- }
- for (int i = 0; i < c.Length; i++)
- {
- g[g.Length - i - 1] = c[i];
- }
- g.CopyTo(data, 0);
- Array.Reverse(data, 0, data.Length);
- return data;
- }
- static void SendPacket(UdpClient klient, byte[] bytes, IPEndPoint sender)
- {
- klient.Send(bytes, bytes.Length);
- klient.Receive(ref sender);
- }
- static byte[] ReceivePacket(ref UdpClient klient, ref IPEndPoint sender, ref string h)
- {
- byte[] ACK = new byte[3];
- byte[] data = new byte[3];
- data = klient.Receive(ref sender);
- ACK = ProtocolBuilder("111111", "0000", h);
- klient.Send(ACK, ACK.Length);
- return data;
- }
- static void SecondChannel(int port, string h)
- {
- byte[] data = new byte[3];
- UdpClient udpClient = new UdpClient("127.0.0.1", port + 10);
- IPEndPoint sender = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 0);
- byte[] newArray = new byte[3];
- newArray = ProtocolBuilder("111100", "0000", h);
- SendPacket(udpClient, newArray, sender);
- while (true)
- {
- if (gamestate == 2)
- break;
- data = ReceivePacket(ref udpClient, ref sender, ref h);
- ResponseToPacket(data, ref h);
- }
- }
- static void Main(string[] args)
- {
- int port = 9050;
- string h = "00000000";
- byte[] data = new byte[3];
- byte[] newArray = ProtocolBuilder("000000"/*Pole operacji(6)*/, "0000"/*Pole odp(4)*/, h/*Pole identyfikatora(8)*/);//pole operacji, pole odpowiedzi, pole identyfikatora
- Array.Reverse(newArray, 0, newArray.Length);
- UdpClient udpClient = new UdpClient("127.0.0.1", port);
- IPEndPoint sender = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 0);
- SendPacket(udpClient, newArray, sender);
- data = ReceivePacket(ref udpClient, ref sender, ref h);
- string[] tab = new string[4];
- tab = DecodePacket(data);
- ResponseToPacket(data, ref h);
- string c;
- int a;
- Console.WriteLine("Waiting for game to begin...");
- while (true)
- {
- if (gamestate == 0)//czekanie na rozpoczecie gry przez serwer
- {
- data = ReceivePacket(ref udpClient, ref sender, ref h);
- ResponseToPacket(data, ref h);
- string c2 = h;
- Thread thread3 = new Thread(() => SecondChannel(port, c2));
- thread3.Start();
- }
- Thread.Sleep(1000);
- if (gamestate == 2)
- {
- newArray = ProtocolBuilder("110100", "0000", h);
- SendPacket(udpClient, newArray, sender);
- break;
- }
- if (gamestate == 1)
- {
- Console.WriteLine("Proboje liczbe... ");
- c = Console.ReadLine();
- if (gamestate == 2)
- {
- Console.WriteLine("Niestety gra sie skonczyla");
- continue;
- }
- a = Int32.Parse(c);
- string liczba = ToBin(a, 4);
- newArray = ProtocolBuilder("010000", liczba, h);
- SendPacket(udpClient, newArray, sender);
- data = ReceivePacket(ref udpClient, ref sender, ref h);
- ResponseToPacket(data, ref h);
- }
- }
- }
- }
- }