Exemple #1
0
 public async Task SendCrossword(string name, CrossWordTimes json)
 {
     await Clients
     // Do not send to Caller:
     .AllExcept(new[] { Context.ConnectionId })
     // Send to all connected clients:
     .SendAsync("SendCrossword", name, json);
 }
Exemple #2
0
        public static CrossWordTimes GetCrossWordModelFromUrl(string url)
        {
            CrossWordTimes model = null;

            using (WebClient httpClient = new WebClient())
            {
                if (url.ToLower().Equals("http-random"))
                {
                    var start = new DateTime(1976, 01, 01);
                    var end   = new DateTime(2017, 05, 29);

                    string    jsonData  = null;
                    int       timeoutMs = 4000;
                    Stopwatch sw        = new Stopwatch();
                    sw.Start();
                    while (true)
                    {
                        var randomDateString = RandomDateString(start, end);

                        string nytBaseUrl = @"https://raw.githubusercontent.com/doshea/nyt_crosswords/master";
                        var    nytUrl     = string.Format("{0}/{1}.json", nytBaseUrl, randomDateString);

                        try
                        {
                            jsonData = httpClient.DownloadString(nytUrl);
                            break;
                        }
                        catch (WebException)
                        {
                            // could not find an url, just try again
                        }

                        if (sw.ElapsedMilliseconds > timeoutMs)
                        {
                            break;
                        }
                    }
                    sw.Stop();

                    model = CrossWordTimes.FromJson(jsonData);
                }
                else
                {
                    try
                    {
                        // url = "https://raw.githubusercontent.com/doshea/nyt_crosswords/master/1997/03/13.json";
                        var jsonData = httpClient.DownloadString(url);
                        model = CrossWordTimes.FromJson(jsonData);
                    }
                    catch (WebException e)
                    {
                        // could not find the url
                        throw e;
                    }
                }
            }
            return(model);
        }
Exemple #3
0
        public CrossWordTimes ToCrossWordModel(ICrossDictionary dictionary)
        {
            var model = new CrossWordTimes();

            var board = new char[_sizeX, _sizeY];

            foreach (var sw in _startWords)
            {
                board[sw.StartX, sw.StartY] = '-';
            }

            var patterns     = new List <CrossPattern>();
            var stringWriter = new StringWriter();

            // across = horizontal
            foreach (var p in _horizontalPatterns)
            {
                patterns.Add(p);

                for (int x = p.StartX; x < p.StartX + p.Length; x++)
                {
                    if (p.Pattern != null)
                    {
                        board[x, p.StartY] = p.Pattern[x - p.StartX];
                    }
                    else
                    {
                        board[x, p.StartY] = '.';
                    }
                }

                // stringWriter.WriteLine("{0}<br>", p);
            }

            // down = vertical
            foreach (var p in _verticalPatterns)
            {
                patterns.Add(p);

                for (int y = p.StartY; y < p.StartY + p.Length; y++)
                {
                    if (p.Pattern != null)
                    {
                        var c = p.Pattern[y - p.StartY];
                        if (c != '.')
                        {
                            board[p.StartX, y] = c;
                        }
                    }
                }

                // stringWriter.WriteLine("{0}<br>", p);
            }

            // calculate grid numbers and build answer and clues lists
            var acrossAnswerList = new List <String>();
            var downAnswerList   = new List <String>();
            var acrossClueList   = new List <String>();
            var downClueList     = new List <String>();

            model.Gridnums = new long[_sizeX * _sizeY];
            model.Circles  = new long[_sizeX * _sizeY];
            var          sortedPatterns = patterns.OrderBy(s => s.StartY).ThenBy(s => s.StartX);
            int          gridNumber     = 0;
            CrossPattern lastPattern    = null;
            var          coordinateMap  = new Dictionary <CrossPattern, Coordinate>();

            // when using a database - read in all descriptions once
            dictionary.AddAllDescriptions(patterns.Select(a => a.GetWord()).ToList());

            foreach (var p in sortedPatterns)
            {
                if (lastPattern != null && lastPattern.StartX == p.StartX && lastPattern.StartY == p.StartY)
                {
                    // patterns starts at same index
                }
                else
                {
                    // pattern start at new index, increment
                    gridNumber++;
                }

                // store grid number as a part of the coordinate
                coordinateMap.Add(p, new Coordinate(p.StartX, p.StartY, gridNumber));

                // and store the clues
                var word        = p.GetWord();
                var description = p.IsPuzzle ? "[PUZZLE]" : GetDescription(dictionary, word);

                string clue = string.Format("{0}. {1}", gridNumber, description);
                if (p.IsHorizontal)
                {
                    acrossAnswerList.Add(word);
                    acrossClueList.Add(clue);
                }
                else
                {
                    downAnswerList.Add(word);
                    downClueList.Add(clue);
                }

                // save last pattern to compare with
                lastPattern = p;
            }

            // build output
            var grid = new List <String>();

            for (int y = 0; y < _sizeY; y++)
            {
                string row = "";
                for (int x = 0; x < _sizeX; x++)
                {
                    row += board[x, y] + " ";

                    // set grid but replace - with .
                    grid.Add(board[x, y].ToString().Replace('-', '.'));

                    if (board[x, y] != '-')
                    {
                        var coordinate = new Coordinate(x, y);
                        if (coordinateMap.ContainsValue(coordinate))
                        {
                            Coordinate foundCoordinate = null;
                            var        coordinates     = coordinateMap.Where(v => v.Value == coordinate);
                            if (coordinates.Count(a => a.Key.IsPuzzle) > 0)
                            {
                                var hit          = coordinates.Where(a => a.Key.IsPuzzle).First();
                                var IsHorizontal = hit.Key.IsHorizontal;
                                var letterCount  = hit.Key.Length;

                                // highlight all cells covered by the word
                                foundCoordinate = hit.Value;
                                if (IsHorizontal)
                                {
                                    for (int i = 0; i < letterCount; i++)
                                    {
                                        model.Circles[(y * _sizeX) + x + i] = 1;
                                    }
                                }
                                else
                                {
                                    for (int i = 0; i < letterCount; i++)
                                    {
                                        model.Circles[((y * _sizeX) + x) + (i * _sizeX)] = 1;
                                    }
                                }
                            }
                            else
                            {
                                foundCoordinate = coordinates.First().Value;
                            }
                            model.Gridnums[(y * _sizeX) + x] = foundCoordinate.GridNumber;
                        }
                    }
                }

                // stringWriter.WriteLine("{0:00}: {1} <br>", y, row);
            }

            model.Title     = "Generated Crossword";
            model.Author    = "the amazing crossword generator";
            model.Copyright = "Crossword Generator";
            model.Size      = new Size {
                Cols = _sizeX, Rows = _sizeY
            };
            // model.Notepad = "<br>" + stringWriter.ToString();
            model.Grid  = grid.ToArray();
            model.Clues = new Answers()
            {
                Across = acrossClueList.ToArray(), Down = downClueList.ToArray()
            };
            model.Answers = new Answers()
            {
                Across = acrossAnswerList.ToArray(), Down = downAnswerList.ToArray()
            };
            model.Shadecircles = false;

            return(model);
        }