Example #1
0
        public IActionResult Board(int size, int difficulty, string btnVal)
        {
            //Login Validation to see if user is logged in and setting the session id
            var User = HttpContext.Session.GetInt32("_Id");

            if (User.HasValue)
            {
                UserID = (int)User;
            }
            else
            {
                return(RedirectToAction("Index", "Player"));
            }

            //from button index, get btnval.
            if (btnVal.Equals("Load Game"))
            {
                GameData gd = new GameData();
                if (gd.CheckSave(UserID, "") == true) //if user has a game stored in the database that we should resume.
                {
                    //Code to load in game.
                    List <GameDTO> game      = gd.LoadGame(UserID);
                    int            boardSize = (int)Math.Sqrt(game.Count);
                    cells    = new Cell[boardSize, boardSize];
                    cellList = new List <Cell>();
                    bool gameover = false;
                    // Theres a bug with game sql inserting not in order before this so we use linq to fix it :)
                    List <GameDTO> fixIdOrder = new List <GameDTO>();
                    int            counter    = 0;
                    while (counter < game.Count)
                    {
                        //while we re-add board count starting from 0 (going in order)
                        foreach (GameDTO gdto in game)
                        { //find id of counter so we can add that gamedto and cell data in order.
                            if (gdto.Id == counter)
                            {
                                fixIdOrder.Add(gdto);
                                counter++;
                            }
                        }
                    }
                    //need to set cells otherwise a nullable object error comes up.
                    for (int row = 0; row < boardSize; row++)
                    {
                        for (int col = 0; col < boardSize; col++)
                        {
                            foreach (GameDTO gdto in game)
                            {
                                if (gdto.Id == row + col)
                                {
                                    Cell imp = new Cell(gdto.row, gdto.col);
                                    imp.Id            = gdto.Id;
                                    imp.visited       = gdto.visited;
                                    imp.live          = gdto.live;
                                    imp.flagged       = gdto.flagged;
                                    imp.liveNeighbors = gdto.liveNeighbors;
                                    cells[row, col]   = imp;
                                }
                            }
                        }
                    }
                    //take out the cell of our gamedto and add it to cell list.
                    foreach (GameDTO gdto in fixIdOrder)
                    {
                        Cell imp = new Cell(gdto.row, gdto.col);
                        imp.Id            = gdto.Id;
                        imp.visited       = gdto.visited;
                        imp.live          = gdto.live;
                        imp.flagged       = gdto.flagged;
                        imp.liveNeighbors = gdto.liveNeighbors;
                        cellList.Add(imp);
                        cb.addElapsedTime(TimeSpan.Parse(gdto.Time));
                        clickCount = gdto.Clicks;
                        //a quick calculation if game is already over. may change logic later.
                        if (imp.visited == true && imp.live)
                        {
                            gameover = true;
                        }
                    }
                    if (gameover == true)
                    {
                        cb.revealBoard(cells, size);
                        ViewBag.win = "You loaded a game that is completed.";
                    }
                }
                else
                {
                    //if there is no game saved, we create a new game.
                    btnVal = "New Game";
                }
            }
            if (btnVal.Equals("New Game"))
            {
                // Creates a 2d array of cells
                cells    = new Cell[size, size];
                cellList = new List <Cell>();
                int k = 0;
                for (int row = 0; row < size; row++)
                {
                    for (int col = 0; col < size; col++)
                    {
                        cells[row, col]    = new Cell(row, col);
                        cells[row, col].Id = k;
                        k++;
                    }
                }
                // Sets the bomb cells to active
                cb.setLiveNeighbors(cells, size, difficulty);
                // Calculates how many neighboring cells are bombs
                cb.calculateLiveNeighbors(cells, size);
                // Adds cells to cellList
                for (int i = 0; i < size; i++)
                {
                    for (int j = 0; j < size; j++)
                    {
                        cellList.Add(cells[i, j]);
                    }
                }
                clickCount = 0;
                cb.resetTimer();
            }
            cb.startTimer();

            return(View("Board", cellList));
        }