public OthelloImage(int x, int y, Image discImage) { this.X = x; this.Y = y; this.DiscImage = discImage; Disc = OthelloDisc.none; }
private void btnNewGame_Click(object sender, RoutedEventArgs e) { for (int x = 0; x < 8; x++) { for (int y = 0; y < 8; y++) { if ((x == 3 && y == 3) || (x == 4 && y == 4)) { imageMatrix[x, y].ChangeToWhite(); } else if ((x == 3 && y == 4) || (x == 4 && y == 3)) { imageMatrix[x, y].ChangeToBlack(); } else { imageMatrix[x, y].Disc = OthelloDisc.none; imageMatrix[x, y].DiscImage.Source = null; } } } whoseTurn = OthelloDisc.white; imgWhoseTurn.Source = bmpimgCoinWhite; lblStatus.Content = " Score: White = 2 Black = 2"; }
private void NextPlayer(bool recalc) { int nrOfWhite = 0; int nrOfBlack = 0; if (whoseTurn == OthelloDisc.white) { whoseTurn = OthelloDisc.black; imgWhoseTurn.Source = bmpimgCoinBlack; } else { whoseTurn = OthelloDisc.white; imgWhoseTurn.Source = bmpimgCoinWhite; } if (recalc == true) { for (int x = 0; x < 8; x++) { for (int y = 0; y < 8; y++) { if (imageMatrix[x, y].Disc == OthelloDisc.white) { nrOfWhite++; } else if (imageMatrix[x, y].Disc == OthelloDisc.black) { nrOfBlack++; } } } lblStatus.Content = " Score: White = " + nrOfWhite + " Black = " + nrOfBlack; if ((nrOfWhite + nrOfBlack == 64) || (nrOfWhite == 0 || nrOfBlack == 0)) { if (nrOfWhite > nrOfBlack) { MessageBox.Show("White won with the score: White = " + nrOfWhite + " Black = " + nrOfBlack + "!"); } else if (nrOfWhite < nrOfBlack) { MessageBox.Show("Black won with the score: White = " + nrOfWhite + " Black = " + nrOfBlack + "!"); } else { MessageBox.Show("It's a tie!"); } } } }
async Task PositionDiscToCell(OthelloDisc odisc, OthelloCell ocell) { ViewExtensions.CancelAnimations(odisc.View); double jumpHeight = 40; bool cancelled = false; var rect1 = odisc.View.Bounds; if (IsPortrait) { if (odisc.View.InitialColor == OthelloColor.White) { rect1.X += jumpHeight; } else { rect1.X -= jumpHeight; } } else { rect1.Y -= jumpHeight; } var finalRect = new Rectangle(); finalRect.Left = ocell.View.Bounds.Left; finalRect.Top = ocell.View.Bounds.Top; finalRect.Width = ocell.View.Bounds.Width - 0; finalRect.Height = ocell.View.Bounds.Height - 0; if (_debug) { Debug.WriteLine($"PositionDiscToCell disc={odisc.Item} cell={ocell.Item}"); } _layout.RaiseChild(odisc.View); if (!odisc.InUse) { cancelled = await odisc.View.LayoutTo(rect1, 200, Easing.Linear); } odisc.View.IsFlat = false; cancelled = await odisc.View.LayoutTo(finalRect, 600, Easing.SpringOut); if (!cancelled) { AbsoluteLayout.SetLayoutBounds(odisc.View, finalRect); odisc.InUse = true; } return; }
async Task MoveDiscToStack(OthelloDisc odisc) { if (_debug) { Debug.WriteLine($"MoveDiscToStack Started OthelloDisc={odisc}"); } odisc.View.ActualColor = odisc.View.DiscColor; await PositionDiscToStack(odisc); if (_debug) { Debug.WriteLine($"MoveDiscToStack Ended OthelloDisc={odisc}"); } }
async Task MoveDiscToCell(OthelloDisc odisc, OthelloCell ocell) { if (_debug) { Debug.WriteLine($"MoveDiscToCell Started Cell={ocell.Item} Disc={odisc.Item}"); } odisc.View.ActualColor = odisc.View.DiscColor; ocell.InUse = true; ocell.OthelloDisc = odisc; await PositionDiscToCell(odisc, ocell); if (_debug) { Debug.WriteLine($"MoveDiscToCell Finished Cell={ocell.Item} Disc={odisc.Item}"); } }
async Task FlipDisc(OthelloDisc odisc) { if (_debug) { Debug.WriteLine($"FlipDisc {odisc.Item}"); } bool cancelled = false; ViewExtensions.CancelAnimations(odisc.View); cancelled = await odisc.View.RotateYTo(90); if (!cancelled) { odisc.View.ActualColor = odisc.View.DiscColor; cancelled = await odisc.View.RotateYTo(0); } }
async Task ProcessDisc(OthelloDisc odisc) { if (_debug) { Debug.WriteLine($"ProcessDisc {odisc.Item} IsUse={odisc.InUse} Stack={odisc.StackPosition}"); } odisc.IsProcessing = true; BusyCount++; if (!odisc.InUse) { await MoveDiscToStack(odisc); } if (odisc.InUse && odisc.View.DiscColor != odisc.View.ActualColor) { await FlipDisc(odisc); } odisc.IsProcessing = false; BusyCount--; }
void BuildLayout() { if (CellItemsSource == null || DiscItemsSource == null) { return; } _layout.Children.Clear(); _layout.Children.Add(_gridBox); foreach (var item in CellItemsSource) { CellView view = new CellView(); if (view != null) { view.BindingContext = item; _layout.Children.Add(view); var cell = new OthelloCell(view, ProcessCell); cell.Item = item; _cells.Add(item, cell); view.PropertyChanged += CellView_PropertyChanged; var tap = new TapGestureRecognizer(); tap.Command = new Command(v => { CellTappedCommand?.Execute(v); return; }); tap.CommandParameter = item; view.GestureRecognizers.Add(tap); } } foreach (var item in DiscItemsSource) { var view = new DiscView(); view.BindingContext = item; _layout.Children.Add(view); var disc = new OthelloDisc(view, ProcessDisc); disc.Item = item; _discs.Add(item, disc); view.PropertyChanged += DiscView_PropertyChanged; } }
public void ChangeToBlack() { DiscImage.Source = new BitmapImage(new Uri("pack://application:,,,/Images/DiscBlack.png")); Disc = OthelloDisc.black; }
public void ChangeToWhite() { DiscImage.Source = new BitmapImage(new Uri("pack://application:,,,/Images/DiscWhite.png")); Disc = OthelloDisc.white; }
async Task PositionDiscToStack(OthelloDisc odisc) { ViewExtensions.CancelAnimations(odisc.View); Point start = new Point(); Rectangle rec; bool isDiscVertical; if (IsPortrait) { var discHeight = _cellSize - 1; var discWidth = discHeight / 5; double x; if (odisc.View.InitialColor == OthelloColor.White) { start.X = 1; start.Y = 1; x = start.X + (odisc.StackPosition * (discWidth + 2)); } else { start.X = Width - discWidth - 1; start.Y = Height - discHeight - 1; x = start.X - (odisc.StackPosition * (discWidth + 2)); } rec = new Rectangle(x, start.Y, discWidth, discHeight); isDiscVertical = true; } else { var discWidth = _cellSize - 1; var discHeight = discWidth / 5; start.Y = Height - discHeight - 1; if (odisc.View.InitialColor == OthelloColor.White) { start.X = 1; } else { start.X = Width - discWidth - 1; } double y = start.Y - (odisc.StackPosition * (discHeight + 2)); rec = new Rectangle(start.X, y, discWidth, discHeight); isDiscVertical = false; } _layout.RaiseChild(odisc.View); bool cancelled = false; if (odisc.InUse) { cancelled = await odisc.View.LayoutTo(rec, 600, Easing.SpringIn); } if (!cancelled) { AbsoluteLayout.SetLayoutBounds(odisc.View, rec); odisc.View.IsFlat = true; odisc.View.IsVertical = isDiscVertical; odisc.InUse = false; } }