Example #1
0
        public IActionResult HandleButtonClick(int index)
        {
            // set the size of the board
            int size = (int)Math.Sqrt(cells.Length);
            // selects the cell out of the cell list
            //Cell cell = cellList.ElementAt(index);
            MouseEventArgs me   = new MouseEventArgs();
            var            User = HttpContext.Session.GetInt32("_Id");

            UserID = (int)User;
            //if (cellList.ElementAt(index).visited == false)
            //{
            if (me.Button.Equals(0))
            {
                cellList.ElementAt(index).flagged = false;
                clickCount++;
                // Left click
                // check if the cell is a bomb
                if (cellList.ElementAt(index).live != true && cellList.ElementAt(index).liveNeighbors <= 1)
                {
                    // Call the Flood Fill method
                    cb.floodFill(cells, cellList.ElementAt(index).row, cellList.ElementAt(index).col, size);
                }
                else if (cellList.ElementAt(index).liveNeighbors > 1)
                {
                    cellList.ElementAt(index).visited = true;
                }
                else if (cellList.ElementAt(index).live == true)
                {
                    // set visited to true to reveal the bomb
                    cellList.ElementAt(index).visited = true;
                    lose    = true;
                    endTime = cb.stopTimer();
                    ResultData resultData = new ResultData();
                    ResultsDTO rDTO       = new ResultsDTO(UserID, 0, endTime, clickCount);
                    resultData.addResult(rDTO);
                }
            }
            // checks for win condition
            if (cb.boardCheck(cells, size) == true)
            {
                win     = true;
                endTime = cb.stopTimer();
                ResultData resultData = new ResultData();
                ResultsDTO rDTO       = new ResultsDTO(UserID, 1, endTime, clickCount);
                resultData.addResult(rDTO);
            }
            if (lose == true)
            {
                // reveals the board after a loss
                cb.revealBoard(cells, size);
                // Lose statement
                ViewBag.win = "BOOM! You set off a bomb! You Lose!";
            }
            else if (win == true)
            {
                cb.revealBoard(cells, size);
                // win statement
                ViewBag.win = "All Bomb Locations Identified! You Win!";
            }
            //}
            return(View("Board", cellList));
        }