打つときの誤った手を打てないように
| @@ -59,11 +59,11 @@ | ||
| 59 | 59 | '''</summary> |
| 60 | 60 | <TestMethod()> _ |
| 61 | 61 | Public Sub PromateKomaValueTest() |
| 62 | - Assert.AreEqual(SENTE_TOKIN, KomaUtil.PromateKomaValue(SENTE_FU)) | |
| 63 | - Assert.AreEqual(SENTE_NARI_KYO, KomaUtil.PromateKomaValue(SENTE_KYO)) | |
| 64 | - Assert.AreEqual(GOTE_NARI_GIN, KomaUtil.PromateKomaValue(GOTE_GIN)) | |
| 65 | - Assert.AreEqual(GOTE_UMA, KomaUtil.PromateKomaValue(GOTE_KAKU)) | |
| 66 | - Assert.AreEqual(GOTE_RYU, KomaUtil.PromateKomaValue(GOTE_HISYA)) | |
| 62 | + Assert.AreEqual(SENTE_TOKIN, KomaUtil.PromoteKomaValue(SENTE_FU)) | |
| 63 | + Assert.AreEqual(SENTE_NARI_KYO, KomaUtil.PromoteKomaValue(SENTE_KYO)) | |
| 64 | + Assert.AreEqual(GOTE_NARI_GIN, KomaUtil.PromoteKomaValue(GOTE_GIN)) | |
| 65 | + Assert.AreEqual(GOTE_UMA, KomaUtil.PromoteKomaValue(GOTE_KAKU)) | |
| 66 | + Assert.AreEqual(GOTE_RYU, KomaUtil.PromoteKomaValue(GOTE_HISYA)) | |
| 67 | 67 | |
| 68 | 68 | End Sub |
| 69 | 69 |
| @@ -47,7 +47,10 @@ | ||
| 47 | 47 | If Not e.MovingValue.ToLocation.IsMasu Then |
| 48 | 48 | e.Cancel = True |
| 49 | 49 | End If |
| 50 | - 'todo:動ける場所以外はCancel | |
| 50 | + | |
| 51 | + If Not Board.KomaMoveValidater.GetInstance.IsValidate(Viewer.kyokumen, e.MovingValue) Then | |
| 52 | + e.Cancel = True | |
| 53 | + End If | |
| 51 | 54 | If e.MouseRightClicked Then |
| 52 | 55 | e.Cancel = True |
| 53 | 56 | End If |
| @@ -432,6 +432,9 @@ | ||
| 432 | 432 | End If |
| 433 | 433 | Dim editingeventArgs As New EditingEventArgs |
| 434 | 434 | editingeventArgs.MovingValue = te |
| 435 | + If te.MustPromote Then | |
| 436 | + te.Promote = True | |
| 437 | + End If | |
| 435 | 438 | RaiseEvent Editing(sender, editingeventArgs) |
| 436 | 439 | |
| 437 | 440 | If editingeventArgs.Cancel Then |
| @@ -469,13 +472,18 @@ | ||
| 469 | 472 | Dim koma As Koma = kyokumen.GetMasu(l) |
| 470 | 473 | Dim toggleKoma As Koma = New Koma(koma.Value) |
| 471 | 474 | toggleKoma.Toggle() |
| 472 | - Dim te As Te = New Te(l, l, toggleKoma.Value, False) | |
| 475 | + | |
| 476 | + Dim te As Te = New Te(l, l, toggleKoma.Value, toggleKoma.IsPromoted) | |
| 473 | 477 | If te.MustPromote Then |
| 474 | 478 | te.Piece.Toggle() |
| 479 | + te.Promote = te.Piece.IsPromoted | |
| 475 | 480 | End If |
| 476 | 481 | |
| 477 | 482 | Dim eventArgs As New EditingEventArgs |
| 478 | 483 | eventArgs.MovingValue = te |
| 484 | + If te.MustPromote Then | |
| 485 | + te.Promote = True | |
| 486 | + End If | |
| 479 | 487 | eventArgs.MouseRightClicked = True |
| 480 | 488 | RaiseEvent Editing(sender, eventArgs) |
| 481 | 489 |
| @@ -3,20 +3,8 @@ | ||
| 3 | 3 | Public Class EditingEventArgs |
| 4 | 4 | Inherits System.ComponentModel.CancelEventArgs |
| 5 | 5 | |
| 6 | - Private _movingValue As ShogiNextMove.Board.Te | |
| 7 | 6 | Public Property MovingValue As ShogiNextMove.Board.Te |
| 8 | - Get | |
| 9 | - Return _movingValue | |
| 10 | - End Get | |
| 11 | - Set(value As ShogiNextMove.Board.Te) | |
| 12 | - If value.MustPromote Then | |
| 13 | - value.Piece.Promate() | |
| 14 | - End If | |
| 15 | - _movingValue = value | |
| 16 | 7 | |
| 17 | - End Set | |
| 18 | - End Property | |
| 19 | - | |
| 20 | 8 | Public Property MouseRightClicked As Boolean = False |
| 21 | 9 | |
| 22 | 10 | Public Property IsMove As Boolean = False |
| @@ -14,7 +14,7 @@ | ||
| 14 | 14 | |
| 15 | 15 | Public Sub New(normalKomaValue As Byte, x As Integer, y As Integer) |
| 16 | 16 | Me.InitializeComponent() |
| 17 | - Dim promoteKomaValue As Byte = Board.KomaUtil.PromateKomaValue(normalKomaValue) | |
| 17 | + Dim promoteKomaValue As Byte = Board.KomaUtil.PromoteKomaValue(normalKomaValue) | |
| 18 | 18 | |
| 19 | 19 | Me.boardBitmap = _imageLoader.BoardBitmap |
| 20 | 20 | Me.promoteKomaBitmap = _imageLoader.GetKomaImage(promoteKomaValue) |
| @@ -60,18 +60,27 @@ | ||
| 60 | 60 | ''' <returns>True:二歩 false:正常</returns> |
| 61 | 61 | ''' <remarks></remarks> |
| 62 | 62 | Private Shared Function IsNifuInCol(ByVal kyokumen As Kyokumen, col As Integer, which As Byte) As Boolean |
| 63 | - Dim sentefuExist As Boolean = False | |
| 63 | + Return 2 <= CountFuInCol(kyokumen, col, which) | |
| 64 | + End Function | |
| 65 | + | |
| 66 | + ''' <summary> | |
| 67 | + ''' 一列に歩が何枚あるか? | |
| 68 | + ''' </summary> | |
| 69 | + ''' <param name="kyokumen"></param> | |
| 70 | + ''' <param name="col"></param> | |
| 71 | + ''' <param name="which"></param> | |
| 72 | + ''' <returns></returns> | |
| 73 | + ''' <remarks></remarks> | |
| 74 | + Public Shared Function CountFuInCol(ByVal kyokumen As Kyokumen, col As Integer, which As Byte) As Integer | |
| 75 | + Dim count As Integer = 0 | |
| 64 | 76 | For row As Integer = 1 To 9 |
| 65 | 77 | Dim l As New KomaLocation(col, row) |
| 66 | 78 | Dim koma As Koma = kyokumen.GetMasu(l) |
| 67 | 79 | If koma.Value = which Then |
| 68 | - If sentefuExist Then | |
| 69 | - Return True | |
| 70 | - End If | |
| 71 | - sentefuExist = True | |
| 80 | + count += 1 | |
| 72 | 81 | End If |
| 73 | 82 | Next |
| 74 | - Return False | |
| 83 | + Return count | |
| 75 | 84 | End Function |
| 76 | 85 | |
| 77 | 86 | End Class |
| @@ -71,7 +71,7 @@ | ||
| 71 | 71 | End If |
| 72 | 72 | End Function |
| 73 | 73 | |
| 74 | - Public Shared Function PromateKomaValue(ByVal komaValue As Byte) As Byte | |
| 74 | + Public Shared Function PromoteKomaValue(ByVal komaValue As Byte) As Byte | |
| 75 | 75 | Debug.Assert(SENTE_FU <= komaValue AndAlso komaValue <= promoteKoma.Length - 1) |
| 76 | 76 | Dim pvalue As Byte = promoteKoma(komaValue) |
| 77 | 77 | Debug.Assert(pvalue <> 0) |
| @@ -85,8 +85,8 @@ | ||
| 85 | 85 | Return KomaUtil.CanPromote(Value) |
| 86 | 86 | End Function |
| 87 | 87 | |
| 88 | - Public Sub Promate() | |
| 89 | - Me.Value = KomaUtil.PromateKomaValue(Value) | |
| 88 | + Public Sub Promote() | |
| 89 | + Me.Value = KomaUtil.PromoteKomaValue(Value) | |
| 90 | 90 | End Sub |
| 91 | 91 | |
| 92 | 92 | Public Function IsSente() As Boolean |
| @@ -120,7 +120,7 @@ | ||
| 120 | 120 | Public Sub Toggle() |
| 121 | 121 | |
| 122 | 122 | If Not IsPromoted() AndAlso CanPromote() Then |
| 123 | - Promate() | |
| 123 | + Promote() | |
| 124 | 124 | Else |
| 125 | 125 | Value = CapturedKomaKind() + KomaUtil.OppositeKoma(Which) |
| 126 | 126 | End If |
| @@ -15,8 +15,8 @@ | ||
| 15 | 15 | End Get |
| 16 | 16 | Set(value As Boolean) |
| 17 | 17 | _promote = value |
| 18 | - If value = True Then | |
| 19 | - Piece.Promate() | |
| 18 | + If value = True AndAlso Not Piece.IsPromoted Then | |
| 19 | + Piece.Promote() | |
| 20 | 20 | End If |
| 21 | 21 | End Set |
| 22 | 22 | End Property |
| @@ -0,0 +1,41 @@ | ||
| 1 | +Namespace Board | |
| 2 | + | |
| 3 | + Public Class KomaMoveValidater | |
| 4 | + | |
| 5 | + Private Shared _instance As New KomaMoveValidater | |
| 6 | + Public Shared Function GetInstance() As KomaMoveValidater | |
| 7 | + Return _instance | |
| 8 | + End Function | |
| 9 | + | |
| 10 | + Private Sub New() | |
| 11 | + End Sub | |
| 12 | + | |
| 13 | + Public Function IsValidate(ByVal kyokumen As Kyokumen, ByVal te As Te) As Boolean | |
| 14 | + If te.FromLocation.IsMochigoma Then | |
| 15 | + Return IsValidatePut(kyokumen, te) | |
| 16 | + End If | |
| 17 | + Return True | |
| 18 | + End Function | |
| 19 | + | |
| 20 | + Private Function IsValidatePut(ByVal kyokumen As Kyokumen, ByVal te As Te) As Boolean | |
| 21 | + Dim tolocation As KomaLocation = te.ToLocation | |
| 22 | + If Not kyokumen.GetMasu(tolocation).IsEmpty Then | |
| 23 | + Return False | |
| 24 | + End If | |
| 25 | + If te.MustPromote Then | |
| 26 | + Return False | |
| 27 | + End If | |
| 28 | + If te.Promote Then | |
| 29 | + Return False | |
| 30 | + End If | |
| 31 | + If te.KomaKind = Koma.C_FU Then | |
| 32 | + If 1 <= KyokumenUtil.CountFuInCol(kyokumen, tolocation.Col, te.Piece.Value) Then | |
| 33 | + Return False | |
| 34 | + End If | |
| 35 | + End If | |
| 36 | + Return True | |
| 37 | + End Function | |
| 38 | + | |
| 39 | + End Class | |
| 40 | + | |
| 41 | +End Namespace |