protected override void OnChessPlaced(ChessPieceInfo chessPiece, bool isWin) { base.OnChessPlaced(chessPiece, isWin); ChessPiecePlaced?.Invoke(this, new ChessPiecePlacedEventArgs { ChessPiece = chessPiece, IsWin = isWin }); if (!isWin) { PlayingPlayer.StartPlay(); } }
private void setChessPiece(ChessPieceInfo chessPieceInfo) { var chessPiece = createChessPieceShape(chessPieceInfo.Kind); chessPiece.Visibility = Visibility.Visible; previousMoveIndicator.Visibility = Visibility.Visible; double left = chessPieceInfo.X * chessboardSizePerLine + chessPieceOffset; var top = chessPieceInfo.Y * chessboardSizePerLine + chessPieceOffset; Canvas.SetLeft(chessPiece, left); Canvas.SetTop(chessPiece, top); Canvas.SetLeft(previousMoveIndicator, left); Canvas.SetTop(previousMoveIndicator, top); }
protected override void OnChessPlaced(ChessPieceInfo chessPiece, bool isWin) { base.OnChessPlaced(chessPiece, isWin); history.Add(chessPiece); if (!isWin) { updateAvailablePositions(chessPiece); } else { //using (StreamWriter writer = new StreamWriter("PlayOutLog.txt", true)) //{ // writer.WriteLine(string.Join(" ",history.Select(c => $"{c.Kind} {c.X},{c.Y};"))); //} } }
private bool calcYResult(ChessPieceInfo chess) { int chessInARowCount = 1; int currentX = chess.X, currentY = chess.Y + 1; while (currentY < LineNum && positions[currentX, currentY] == chess.Kind) { chessInARowCount++; currentY++; } currentY = chess.Y - 1; while (currentY >= 0 && positions[currentX, currentY] == chess.Kind) { chessInARowCount++; currentY--; } return(chessInARowCount >= 5); }
private void updateAvailablePositions(ChessPieceInfo chessPiece) { AvailablePositions.Remove(new Position(chessPiece.X, chessPiece.Y)); int minX = chessPiece.X <= searchRadius ? 0 : chessPiece.X - searchRadius; int minY = chessPiece.Y <= searchRadius ? 0 : chessPiece.Y - searchRadius; int maxX = chessPiece.X >= LineNum - searchRadius - 1 ? LineNum - 1 : chessPiece.X + searchRadius; int maxY = chessPiece.Y >= LineNum - searchRadius - 1 ? LineNum - 1 : chessPiece.Y + searchRadius; for (int i = minX; i <= maxX; i++) { for (int j = minY; j <= maxY; j++) { if (this[i, j] == ChessPieceKind.Empty) { Position p = new Position(i, j); AvailablePositions.Add(p); } } } }
public bool PlaceChessPiece(int x, int y) { if (!CanPlaceChessPiece(x, y)) { throw new InvalidOperationException("非法落子"); } positions[x, y] = NextMoveKind; ChessPieceInfo chessPiece = new ChessPieceInfo { X = x, Y = y, Kind = NextMoveKind }; previousMove = chessPiece; bool isWin = calcResult(chessPiece); if (isWin) { Result = previousMove.Kind; } NextMoveKind = NextMoveKind == ChessPieceKind.BlackChessPiece ? ChessPieceKind.WriteChessPiece : ChessPieceKind.BlackChessPiece; OnChessPlaced(chessPiece, isWin); return(isWin); }
private bool calcResult(ChessPieceInfo chess) { return(calcXResult(chess) || calcYResult(chess) || calcXY1Result(chess) || calcXY2Result(chess)); }
protected virtual void OnChessPlaced(ChessPieceInfo chessPiece, bool isWin) { }