質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.47%
Visual Basic .NET

Microsoft Visual Basic .NET (VB.NET)とはオブジェクト志向のプログラム言語です。 Microsoft"s Visual Basic 6 のバージョンアップとしてみることができますが、Microsoft.NET Frameworktによって動かされています。

Visual Studio 2012

Microsoft Visual Studio 2012は、Microsoftによる統合開発環境(IDE)であり、多種多様なプログラミング言語に対応しています。 Visual Studio 2010の次のバージョンです

Visual Studio 2013

Microsoft Visual Studio 2013は、Microsoftによる統合開発環境(IDE)であり、多種多様なプログラミング言語に対応しています。 Visual Studio 2012の次のバージョンです

Q&A

解決済

2回答

1488閲覧

15パズルゲーム visual basic

kannnn

総合スコア3

Visual Basic .NET

Microsoft Visual Basic .NET (VB.NET)とはオブジェクト志向のプログラム言語です。 Microsoft"s Visual Basic 6 のバージョンアップとしてみることができますが、Microsoft.NET Frameworktによって動かされています。

Visual Studio 2012

Microsoft Visual Studio 2012は、Microsoftによる統合開発環境(IDE)であり、多種多様なプログラミング言語に対応しています。 Visual Studio 2010の次のバージョンです

Visual Studio 2013

Microsoft Visual Studio 2013は、Microsoftによる統合開発環境(IDE)であり、多種多様なプログラミング言語に対応しています。 Visual Studio 2012の次のバージョンです

0グッド

0クリップ

投稿2022/06/09 01:55

編集2022/06/09 03:24

15パズルのプログラムがうまく作動しません.
visual studio2019,windowsフォームアプリケーションにて作業しています。
関数number(x)はクリックしたボタンの四方に対し0のボタンを探し、自身が0であれば0、四方のボタンに0がある場合はそのボタン番号、自身・四方に0がない場合は17を返します。(ボタン番号は左から123…となります。)
ボタン1をクリックするとnumberからかえってきたボタン番号が2であった場合、配列mybutton(1))に書かれている数字をボタン2へ入力、ボタン1の数字を0にする。5の場合も同様に行う。

現在発生している問題は、隣接しているボタンを押してもそれを返さないことがあることです。
下記 プログラムになります。

Public Class Form1
'============================================================================================
'利用者がリセットボタンを押すと、Button1~Button16の表面に、0~15のランダムな数字を表示する。
'============================================================================================
Private Sub Button17_Click(sender As System.Object, e As System.EventArgs) Handles Button17.Click

'シャッフルする配列 Dim array As Integer() = New Integer() {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15} 'Fisher-Yatesアルゴリズムでシャッフルする Dim rng As New System.Random() Dim n As Integer = array.Length While n > 1 n -= 1 Dim k As Integer = rng.Next(n + 1) Dim tmp As Integer = array(k) array(k) = array(n) array(n) = tmp End While Button1.Text = array(0) Button2.Text = array(1) Button15.Text = array(14) Button16.Text = array(15) End Sub Dim mybutton(16) As Integer Private Function number(x As Integer) As Integer Dim y As Integer mybutton(1) = Button1.Text mybutton(2) = Button2.Text 省略 mybutton(15) = Button15.Text mybutton(16) = Button16.Text If mybutton(x) = 0 Then y = 0 Return y End If If x - 1 > 0 Then '(1) If mybutton(x - 1) = 0 Then y = x - 1 Return y End If End If If (x + 1) < 17 Then If mybutton(x + 1) = 0 Then y = x + 1 Return y End If End If If (x + 2) < 17 Then If mybutton(x + 2) = 0 Then y = x + 2 Return y End If End If If (x + 3) < 17 Then If mybutton(x + 3) = 0 Then y = x + 3 Return y End If End If If (x + 4) < 17 Then If mybutton(x + 4) = 0 Then y = x + 4 Return y End If End If y = 17 Return y End Function
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click Dim x As Integer = 1 Dim ans As Integer = number(x) If ans = 2 Then Button2.Text = mybutton(x) Button1.Text = 0 ElseIf ans = 5 Then Button5.Text = mybutton(x) Button1.Text = 0```ここに言語を入力 End If End Sub Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click Dim x As Integer = 2 Dim ans As Integer = number(x) If ans = 1 Then Button1.Text = mybutton(x) Button2.Text = 0 ElseIf ans = 3 Then Button3.Text = mybutton(x) Button2.Text = 0 ElseIf ans = 6 Then Button6.Text = mybutton(x) Button2.Text = 0 End If End Sub Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click Dim x As Integer = 3 Dim ans As Integer = number(x) If ans = 2 Then Button2.Text = mybutton(x) Button3.Text = 0 ElseIf ans = 4 Then Button4.Text = mybutton(x) Button3.Text = 0 ElseIf ans = 7 Then Button7.Text = mybutton(x) Button3.Text = 0 End If End Sub Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click Dim x As Integer = 4 Dim ans As Integer = number(x) If ans = 3 Then Button3.Text = mybutton(x) Button4.Text = 0 ElseIf ans = 8 Then Button8.Text = mybutton(x) Button4.Text = 0 End If End Sub Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click Dim x As Integer = 5 Dim ans As Integer = number(x) If ans = 1 Then Button1.Text = mybutton(x) Button5.Text = 0 ElseIf ans = 6 Then Button6.Text = mybutton(x) Button5.Text = 0 ElseIf ans = 9 Then Button9.Text = mybutton(x) Button5.Text = 0 End If End Sub 以降 ボタン16まで end class

補足情報(言語/FW/ツール等のバージョンなど)
visual studio2019,windowsフォームアプリケーションにて作業しています。

修正点ありましたらよろしくお願いします。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

ku__ra__ge

2022/06/09 02:06

正しくソースコードが表示されるように質問を修正してください。
kannnn

2022/06/09 04:20

修正しましたが一部繋がらない部分がありました。 よろしくお願いします。
guest

回答2

0

ベストアンサー

現在発生している問題は、隣接しているボタンを押してもそれを返さないことがあることです。

関数numberは「引数で渡されたボタン番号の四方に対し0のボタンを探し、そのボタン番号を返す」という機能を持っていません。
例えば引数に「5」を渡して、ボタン番号5の四方のボタンをチェックしたいとしましょう。
x-1~x+4の範囲だと以下のような場所のボタンがチェックされますが、これはあなたの望む動作ではないと思います。
イメージ説明

あなたが調べたいのは以下の場所ではないでしょうか?
・引数xの「左」 → x - 1  ※xが左端(1、5、9、13)ならチェックしない
・引数xの「右」 → x + 1  ※xが右端(4、8、12、16)ならチェックしない
・引数xの「上」 → x - 4  ※xが上端(1~4)ならチェックしない
・引数xの「下」 → x + 4  ※xが下端(13~16)ならチェックしない

投稿2022/06/09 14:13

ku__ra__ge

総合スコア4524

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

kannnn

2022/06/13 05:55

ありがとうございます! ちゃんと動くようになりました!
guest

0

ボタンの相対位置(0,0)~(3,3) をボタンと関連付けて管理するとプログラムがスッキリすると思います。
位置は System.Drawing.Point 構造体で示し、Dictionary を2つ作ってボタンと Point構造体を相互に変換します。

※ ボタンはコードで作成するので空のフォームに張り付けてください。

VB

1Option Explicit On 2Option Strict On 3 4Public Class Form1 5 6 Private ReadOnly PointToButton As New Dictionary(Of Point, Button) 7 Private ReadOnly ButtonToPoint As New Dictionary(Of Button, Point) 8 9 Public Sub New() 10 InitializeComponent() 11 12 Dim ButtonSize As New Size(99, 99) 'ボタンのサイズ 13 Dim ButtonSpacing As New Size(1, 1) 'ボタンの間隔 14 Dim GridSize As Size = ButtonSize + ButtonSpacing '枠のサイズ 15 16 '配列をシャッフル 17 Dim array As Integer() = New Integer() {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15} 18 array = array.OrderBy(Function() Guid.NewGuid()).ToArray() 19 20 For i As Integer = 0 To array.Length - 1 21 'ボタンを作成し、位置を割り当てる 22 Dim btn As Button = New Button() 23 Dim y As Integer = i \ 4 24 Dim x As Integer = i Mod 4 25 Dim pt As New Point(x, y) 26 PointToButton.Add(pt, btn) 27 ButtonToPoint.Add(btn, pt) 28 29 'ボタンのプロパティ設定 30 btn.Text = array(i).ToString() 31 btn.Visible = array(i) <> 0 32 btn.Location = New Point(GridSize.Width * x + ButtonSpacing.Width, 33 GridSize.Height * y + ButtonSpacing.Height) 34 btn.Size = ButtonSize 35 36 'イベントを登録 37 AddHandler btn.Click, AddressOf Button_Click 38 39 Me.Controls.Add(btn) 40 Next 41 Me.ClientSize = New Size(GridSize.Width * 4 + ButtonSpacing.Width, 42 GridSize.Height * 4 + ButtonSpacing.Height) 43 End Sub 44 45 Private Sub Button_Click(sender As Object, e As EventArgs) 46 Dim btn As Button = DirectCast(sender, Button) 47 Dim pt As Point = ButtonToPoint(btn) 48 49 Dim target As Button = Nothing 50 target = If(target, GetButtonIfZero(pt.X - 1, pt.Y)) '左 51 target = If(target, GetButtonIfZero(pt.X + 1, pt.Y)) '右 52 target = If(target, GetButtonIfZero(pt.X, pt.Y - 1)) '上 53 target = If(target, GetButtonIfZero(pt.X, pt.Y + 1)) '下 54 55 If target IsNot Nothing Then 56 ' 入れ替え 57 target.Text = btn.Text 58 target.Visible = True 59 target.Focus() 60 btn.Text = "0" 61 btn.Visible = False 62 End If 63 End Sub 64 65 ' 指定された位置にボタンがあり、Text プロパティがゼロならボタンを返す 66 Private Function GetButtonIfZero(x As Integer, y As Integer) As Button 67 Dim pt As Point = New Point(x, y) 68 Dim btn As Button = Nothing 69 If PointToButton.TryGetValue(pt, btn) AndAlso btn.Text = "0" Then 70 Return btn 71 End If 72 Return Nothing 73 End Function 74 75End Class

投稿2022/06/10 04:21

編集2022/06/10 06:55
KOZ6.0

総合スコア2639

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.47%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問