Click here to Skip to main content
15,891,607 members
Articles / Game Development

Guess My Drawing!

Rate me:
Please Sign up or sign in to vote.
4.87/5 (21 votes)
9 Jan 2013CPOL3 min read 48.1K   697   34  
A Windows Forms application to share a whiteboard with many clients with only one drawer, in a gamy way.
using System;
using System.Collections;
using System.Net;
using System.Net.Sockets;
using System.Text;


class Program
{
    static void Main(string[] args)
    {
        ArrayList Clients = new ArrayList();
        ArrayList drawing = new ArrayList();
        IPEndPoint localEP = new IPEndPoint(IPAddress.Any, 9050);
        UdpClient server = new UdpClient(localEP);
        IPEndPoint remoteEP = new IPEndPoint(IPAddress.Any, 0);
        string theAnswer = "";
        byte[] data;
        string msg;

        while (true)
        {
            data = server.Receive(ref remoteEP);
            Console.Write("Received from {0}: ", remoteEP.ToString());

            msg = Encoding.ASCII.GetString(data, 0, data.Length);
            Console.WriteLine(msg);

          
            if (msg.StartsWith("join"))
            {

                if (msg.EndsWith("drawer"))
                {
                    if (Clients.Count == 0)
                        addDrawer(Clients, localEP, server, remoteEP);
                    else
                    {
                        msg = "denied";
                        data = Encoding.ASCII.GetBytes(msg);
                        server.Send(data, data.Length, remoteEP);
                    }
                }

                if (msg.EndsWith("guesser"))
                {
                    if (Clients.Count == 0)
                    {
                        msg = "denied";
                        data = Encoding.ASCII.GetBytes(msg);
                        server.Send(data, data.Length, remoteEP);
                    }
                    else
                    {
                        addGuesser(Clients, localEP, server, remoteEP);
                    }
                }
            }

            if (msg.StartsWith("quit"))
            {
                if (msg.EndsWith("drawer"))
                    foreach (IPEndPoint endpoint in Clients)
                        {
                          data = Encoding.ASCII.GetBytes(msg);
                          server.Send(data, data.Length, remoteEP);
                          }
                Clients.Remove(remoteEP);
            }

            if (msg.StartsWith("draw"))
            {
                foreach (IPEndPoint endpoint in Clients)
                {
                    data = Encoding.ASCII.GetBytes(msg);
                    server.Send(data, data.Length, endpoint);
                }
            }

            if (msg.StartsWith("gues"))
            {
                if (msg.EndsWith(theAnswer))
                    msg += "true";
                else
                    msg += "false";
                foreach (IPEndPoint endpoint in Clients)
                {
                    data = Encoding.ASCII.GetBytes(msg);
                    server.Send(data, data.Length, endpoint);
                }
            }

            if (msg.StartsWith("submit"))
            {
                theAnswer = msg.Substring(7).ToLower();
            }

        }
    }

    public static void addDrawer(ArrayList Clients, IPEndPoint localEP, UdpClient server, IPEndPoint remoteEP)
    {
        Clients.Add(remoteEP);
        string msg = "accept";
        byte[] data = Encoding.ASCII.GetBytes(msg);
        server.Send(data, data.Length, remoteEP);
    }

    public static void addGuesser(ArrayList Clients, IPEndPoint localEP, UdpClient server, IPEndPoint remoteEP)
    {
        Clients.Add(remoteEP);
        string msg = "accept";
        byte[] data = Encoding.ASCII.GetBytes(msg);
        server.Send(data, data.Length, remoteEP);
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
United States United States
Codes > Bio

Comments and Discussions