/// <summary> /// 当用户按下鼠标按钮时执行人工下棋操作 /// </summary> private void PieceControl_MouseDown(object sender, MouseButtonEventArgs e) { if (!reversiGame.IsBusy) { if (e.LeftButton == MouseButtonState.Pressed) { PieceControl pc = (PieceControl)sender; MovePiece(UidToPosition(pc.Uid)); } else if (IsRegretPieceEnabled && e.RightButton == MouseButtonState.Pressed) { RegretMovePiece(); } } }
private void PieceControl_MouseMove(object sender, MouseEventArgs e) { if (isInitialized && !reversiGame.IsBusy) { foreach (ReversiPiecePosition rpp in reversiGame.GetEnabledPositions()) { pieceControls[rpp.X, rpp.Y].SetPreview = false; } PieceControl pc = (PieceControl)sender; if (reversiGame.IspieceEnabled(UidToPosition(pc.Uid))) { pc.SetPreview = true; lastEnablePiece = pc; } } }
/// <summary> /// 悔棋 /// </summary> private void RegretMovePiece() { try { changeWaitingState(WaitingState.WaitingRegretPieceCompleted); reversiGame.SetRegretPieceArgs(RegretPiece_Finished); reversiGame.RegretPieceMove(true); UpdateWindow(); ShowNextEnabledPieces(); lastMovedPiece.IsLastPiece = false; lastMovedPiece = pieceControls[reversiGame.LastPosition.X, reversiGame.LastPosition.Y]; lastMovedPiece.IsLastPiece = true; } catch { UpdateWindow(); } }
/// <summary> /// 当时钟到达时执行 /// </summary> private void timer_Tick(object sender, EventArgs e) { // 如果需要停止下棋过程 (通常是因为下棋失败) if (MovePieceNeedStop) { MovePieceNeedStop = false; timer.Stop(); changeWaitingState(WaitingState.NotWaiting); return; } // 当当前未等待任何事时 if (waitingState == WaitingState.NotWaiting) { changeWaitingState(WaitingState.NotWaiting); } // 当当前正在等待下棋确认时 else if (waitingState == WaitingState.WaitingMovePieceConfirmed) { // 如果下棋已被确认 if (IsMovePieceConfirmed) { timer.Stop(); // 确保只有到玩家下棋时才会显示下一步可下的棋子 if (reversiGame.CurrentPiece != HumanPieceType) { UpdateWindow(); ClearAllNextPieces(); if (lastMovedPiece != null) { lastMovedPiece.IsLastPiece = false; } lastMovedPiece = pieceControls[reversiGame.LastPosition.X, reversiGame.LastPosition.Y]; lastMovedPiece.IsLastPiece = true; } IsMovePieceConfirmed = false; changeWaitingState(WaitingState.WaitingMovePieceCompleted); } } // 当当前正在等待下棋完成时 else if (waitingState == WaitingState.WaitingMovePieceCompleted || waitingState == WaitingState.StillWaitingMovePieceCompleted) { if (!reversiGame.IsBusy && IsMovePieceCompleted) { timer.Stop(); // 当游戏结束时, 通知父容器 if (reversiGame.IsGameOver) { changeWaitingState(WaitingState.NotWaiting); UpdateWindow(); EndGame(); } else { // 确保当玩家下棋时能显示上一步已下棋子和下一步可下棋子 if (reversiGame.CurrentPiece == HumanPieceType) { UpdateWindow(); if (lastMovedPiece != null) { lastMovedPiece.IsLastPiece = false; } lastMovedPiece = pieceControls[reversiGame.LastPosition.X, reversiGame.LastPosition.Y]; lastMovedPiece.IsLastPiece = true; } // 如果下完棋后不是玩家下棋, 则自动调用AI再下一步 if (reversiGame.CurrentPiece == ReversiGame.GetReversepiece(HumanPieceType)) { UpdateWindow(); MovePiece(); changeWaitingState(WaitingState.WaitingMovePieceConfirmed); } // 如果下完棋后轮到玩家下棋, 则显示下一步棋子 else { ShowNextEnabledPieces(); IsMovePieceCompleted = false; changeWaitingState(WaitingState.NotWaiting); } } } // 如果规定时间内未完成下棋, 则使界面转到长时间等待状态 else { if (waitingState != WaitingState.StillWaitingMovePieceCompleted) { changeWaitingState(WaitingState.StillWaitingMovePieceCompleted); } } } // 当当前正在等待悔棋完成时 else if (waitingState == WaitingState.WaitingRegretPieceCompleted) { changeWaitingState(WaitingState.NotWaiting); } }