// go through the model board, find those with gui needs to be // updated set to true, and use the refs array to obtain the gui tile // finally set the model back to guineedsupdate=false // used only for initially adding the pieces private void renderAllPieces(Board modelBoard) { // use gui=true tiles from modelBoard.internalBoard // find the corresponding panels in guiTileRefs // ADD OR REMOVE THE CONTAINING PIECE ACCORDINGLY // mark the modelBoard tiles as gui=false int size = modelBoard.Size; for (int row = 0; row < size; row ++) { Tile[] rowOfTiles = modelBoard.InternalBoard[row]; for (int col = 0; col < size; col ++) { Tile tile = rowOfTiles[col]; try { System.Windows.Forms.Panel guiTile = guiTileRefs[row][col]; if (tile.IsOccupied) { string player = tile.OccupyingPiece.Player; CirclePanel guiPiece = new CirclePanel(player); guiPiece.BackColor = Color.Transparent; // this set in the current main thread for visual priority guiPiece.Size = new System.Drawing.Size(20, 20); guiPiece.Location = new System.Drawing.Point(10, 10); guiPiece.Click += (sender, eventArgs) => { tileClickedHandler(sender, tile.TileCoord); }; guiPiece.Name = "guiPiece"; // guiTile.Controls.Add(guiPiece); } else // remove the corresponding gui piece // since this means guineedsupdating=true, and there are NO pieces on the logic tile { // so remove the guiPiece which should currently be on PiecePanel guiTile.Controls.Clear(); } // finally mark the tile as gui=false, this is LOGIC CODE } catch (NullReferenceException e) { System.Console.Error.WriteLine("resetting board before its created"); // it is better practise to not have this type of error handled here // it is better to have the option to click reset disabled while the // board has not been created yet } } } }
/* A general function to replace the repeated calls to the render pieces function at the end of every turn. * Takes a list of coords of tiles which are required to be updated on the display, rather than checking every one * and updating those required. Guitiles will need to be updated when they are marked for highlighting or when * a piece has moved to or from them. Makes use of the guiTileRefs array */ private void updateGuiTiles(List<Coord> tilesToUpdate) { foreach (Coord c in tilesToUpdate) { Tile modelTile = board.getTile(c); Panel guiTile = guiTileRefs[c.Y][c.X]; // update the properties // highlighting (add a highlight panel to the tile) if (modelTile.IsHighlighted) { //System.Console.WriteLine("highlight something"); //guiTile.BackColor = (modelTile.IsHighlighted) ? Color.DarkBlue : guiTile.BackColor; Panel highlightTile = new Panel(); highlightTile.BackColor = Color.Blue; highlightTile.Size = new System.Drawing.Size(20, 20); highlightTile.Location = new System.Drawing.Point(0, 0); highlightTile.Click += (sender, eventArgs) => { tileClickedHandler(sender, modelTile.TileCoord); }; highlightTile.Name = "highlightTile"; guiTile.Controls.Add(highlightTile); } if (!modelTile.IsHighlighted) { guiTile.Controls.RemoveByKey("highlightTile"); //System.Console.WriteLine("HIGHLIGHT SHOULD BE REMOVED HERE"); } // piece (add a piece panel to the tile) if (modelTile.IsOccupied) { string player = modelTile.OccupyingPiece.Player; CirclePanel guiPiece = new CirclePanel(player); guiPiece.BackColor = Color.Transparent; guiPiece.Size = new System.Drawing.Size(20, 20); guiPiece.Location = new System.Drawing.Point(10, 10); guiPiece.Click += (sender, eventArgs) => { tileClickedHandler(sender, modelTile.TileCoord); }; guiPiece.Name = "guiPiece"; // guiTile.Controls.Add(guiPiece); } if (!modelTile.IsOccupied) { /* The greater issue being that a given tile should only have one piece on it anyway. * need to figure out why there are duplicates. * seems to be related to those tiles which start with a piece on them * maybe when it is clicked, a 2nd piece is being redrawn * For now, this should remove all 'pieces' from the tile */ while (guiTile.Controls.ContainsKey("guiPiece")) { guiTile.Controls.RemoveByKey("guiPiece"); } //System.Console.WriteLine("GUIPIECE SHOULD BE REMOVED HERE"); } } }