/// <summary> /// handle the click of a cell (inform the logic and update the gui) /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Cell_Click(object sender, EventArgs e) { OthelloGridCell s = (OthelloGridCell)sender; int x = s.X; int y = s.Y; Tuple <int, int> pos = new Tuple <int, int>(x, y); if (currentPossibleMoves != null && currentPossibleMoves.ContainsKey(pos)) { LogicalBoard.Instance.PlayMove(x, y, LogicalBoard.Instance.CurrentPlayerTurn); NextTurn(); UpdateGui(); } }
/// <summary> /// Update the board according to the logic /// </summary> private void UpdateBoard() { currentPossibleMoves = LogicalBoard.Instance.CurrentPossibleMoves; int[,] lboard = LogicalBoard.Instance.GetBoard(); for (int x = 0; x < othelloGridCells.GetLength(0); x++) { for (int y = 0; y < othelloGridCells.GetLength(1); y++) { Tuple <int, int> pos = new Tuple <int, int>(x, y); OthelloGridCell gcell = othelloGridCells[x, y]; int lcell = lboard[x, y]; if (lcell == 1) { gcell.State = OthelloGridCell.States.Player1; } else if (lcell == 0) { gcell.State = OthelloGridCell.States.Player2; } else if (lcell == -1 && currentPossibleMoves.ContainsKey(pos)) { if (LogicalBoard.Instance.CurrentPlayerTurn) { gcell.State = OthelloGridCell.States.PreviewPlayer1; } else { gcell.State = OthelloGridCell.States.PreviewPlayer2; } } else { gcell.State = OthelloGridCell.States.Empty; } gcell.LastPlay = LogicalBoard.Instance.LastMovePosition != null && pos.Item1 == LogicalBoard.Instance.LastMovePosition.Item1 && pos.Item2 == LogicalBoard.Instance.LastMovePosition.Item2; } } }
/// <summary> /// Generate the grid with labels and cells /// </summary> private void GenerateGrid() { othelloGridCells = new OthelloGridCell[LogicalBoard.Instance.GetCol(), LogicalBoard.Instance.GetRow()]; graphicalBoard.ColumnDefinitions.Clear(); graphicalBoard.RowDefinitions.Clear(); graphicalBoard.Children.Clear(); //define columns for (int i = 0; i <= LogicalBoard.Instance.GetCol(); i++) { ColumnDefinition col = new ColumnDefinition(); graphicalBoard.ColumnDefinitions.Add(col); } //define rows for (int i = 0; i <= LogicalBoard.Instance.GetRow(); i++) { RowDefinition row = new RowDefinition(); graphicalBoard.RowDefinitions.Add(row); } //add columns labels for (int i = 1; i <= othelloGridCells.GetLength(0); i++) { //OthelloGridLabel label = new OthelloGridLabel("" + Convert.ToChar(i + 64)); OthelloGridLabel label = new OthelloGridLabel("" + (i - 1)); Grid.SetColumn(label, i); Grid.SetRow(label, 0); graphicalBoard.Children.Add(label); } //add rows labels for (int i = 1; i <= othelloGridCells.GetLength(1); i++) { //OthelloGridLabel label = new OthelloGridLabel("" + i); OthelloGridLabel label = new OthelloGridLabel("" + (i - 1)); Grid.SetColumn(label, 0); Grid.SetRow(label, i); graphicalBoard.Children.Add(label); } //add game cells for (int x = 0; x < othelloGridCells.GetLength(0); x++) { for (int y = 0; y < othelloGridCells.GetLength(1); y++) { OthelloGridCell cell = new OthelloGridCell(x, y); othelloGridCells[x, y] = cell; cell.Click += Cell_Click; Grid.SetColumn(cell, x + 1); Grid.SetRow(cell, y + 1); graphicalBoard.Children.Add(cell); } } }