掲示板システム
ホーム
アクセス解析
カテゴリ
ログアウト
VB.NET2003で丸罰ゲーム (ID:84803)
名前
ホームページ(ブログ、Twitterなど)のURL (省略可)
本文
コンピュータ対戦のプログラムが作りたいのですが なかなかうまくいきません。 どなたか教えていただきたいのですが? public picture(2, 2) As PictureBox '3×3のピクチャボックス Public masu(2, 2) As Integer '升目の配列 Public Shared sente As Integer '先手・後手の選択 Public jyunban As Integer '順番 #Region " Windows フォーム デザイナで生成されたコード " Public Sub New() MyBase.New() ' この呼び出しは Windows フォーム デザイナで必要です。 InitializeComponent() ' InitializeComponent() 呼び出しの後に初期化を追加します。 End Sub ' Form は、コンポーネント一覧に後処理を実行するために dispose をオーバーライドします。 Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean) If disposing Then If Not (components Is Nothing) Then components.Dispose() End If End If MyBase.Dispose(disposing) End Sub ' Windows フォーム デザイナで必要です。 Private components As System.ComponentModel.IContainer ' メモ : 以下のプロシージャは、Windows フォーム デザイナで必要です。 'Windows フォーム デザイナを使って変更してください。 ' コード エディタを使って変更しないでください。 Friend WithEvents PictureBox1 As System.Windows.Forms.PictureBox Friend WithEvents PictureBox2 As System.Windows.Forms.PictureBox Friend WithEvents PictureBox3 As System.Windows.Forms.PictureBox <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent() Dim resources As System.Resources.ResourceManager = New System.Resources.ResourceManager(GetType(Form1)) Me.PictureBox1 = New System.Windows.Forms.PictureBox Me.PictureBox2 = New System.Windows.Forms.PictureBox Me.PictureBox3 = New System.Windows.Forms.PictureBox Me.SuspendLayout() ' 'PictureBox1 ' Me.PictureBox1.Image = CType(resources.GetObject("PictureBox1.Image"), System.Drawing.Image) Me.PictureBox1.Location = New System.Drawing.Point(16, 32) Me.PictureBox1.Name = "PictureBox1" Me.PictureBox1.Size = New System.Drawing.Size(64, 64) Me.PictureBox1.TabIndex = 0 Me.PictureBox1.TabStop = False Me.PictureBox1.Visible = False ' 'PictureBox2 ' Me.PictureBox2.Image = CType(resources.GetObject("PictureBox2.Image"), System.Drawing.Image) Me.PictureBox2.Location = New System.Drawing.Point(96, 32) Me.PictureBox2.Name = "PictureBox2" Me.PictureBox2.Size = New System.Drawing.Size(64, 64) Me.PictureBox2.TabIndex = 1 Me.PictureBox2.TabStop = False Me.PictureBox2.Visible = False ' 'PictureBox3 ' Me.PictureBox3.Image = CType(resources.GetObject("PictureBox3.Image"), System.Drawing.Image) Me.PictureBox3.Location = New System.Drawing.Point(176, 32) Me.PictureBox3.Name = "PictureBox3" Me.PictureBox3.Size = New System.Drawing.Size(64, 64) Me.PictureBox3.TabIndex = 2 Me.PictureBox3.TabStop = False Me.PictureBox3.Visible = False ' 'Form1 ' Me.AutoScaleBaseSize = New System.Drawing.Size(7, 15) Me.ClientSize = New System.Drawing.Size(292, 260) Me.Controls.Add(Me.PictureBox3) Me.Controls.Add(Me.PictureBox2) Me.Controls.Add(Me.PictureBox1) Me.Name = "Form1" Me.Text = "三目並べ" Me.ResumeLayout(False) End Sub #End Region 'フォームを開いた時の処理 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim x As Integer, y As Integer '添え字 Dim fm2 As Windows.Forms.Form = New Form2 'フォーム2のオブジェクト '1)初期値設定 For y = 0 To 2 '3×3の升目に空き状態(1)を入れる。 For x = 0 To 2 masu(y, x) = 1 Next Next '2)表示する升目の作成 For y = 0 To 2 For x = 0 To 2 picture(y, x) = New PictureBox 'ピクチャボックスの生成 Controls.Add(picture(y, x)) 'フォームに升目を貼り付ける。 AddHandler picture(y, x).Click, AddressOf picture_click '升目をクリックした時の処理 Next Next '3)升目の表示 Disp_masu() '4)先手・後手の選択 fm2.ShowDialog() 'フォーム2の表示 '5)順番の設定 jyunban = 2 '6)先手がコンピュータなら,コンピュータの番 If jyunban <> sente Then computer(jyunban) 'コンピュータの思考 Disp_masu() '盤の表示 jyunban = 3 End If End Sub '升目をクリックした時の処理 Private Sub picture_click(ByVal sender As System.Object, ByVal e As System.EventArgs) Dim y As Integer, x As Integer '添え字 y = sender.tabindex \ 3 'クリックした升目の位置を得る。 x = sender.tabindex Mod 3 If masu(y, x) = 1 Then '升目が空なら,○か×をおく masu(y, x) = jyunban Else MessageBox.Show("置けません") Exit Sub End If Disp_masu() '盤の表示 If owari() = True Then '終了判定 End 'ゲームは終了です。 End If If jyunban = 2 Then '順番の交代 2→3,3→2 jyunban = 3 Else jyunban = 2 End If computer(jyunban) 'コンピュータの番です。 Disp_masu() '盤の表示 If owari() = True Then '終了判定 End 'ゲームが終了です。 End If If jyunban = 2 Then '順番の交代 2→3,3→2 jyunban = 3 Else jyunban = 2 End If End Sub '升目の表示 Private Sub Disp_masu() Dim x As Integer, y As Integer '添え字 For y = 0 To 2 For x = 0 To 2 picture(y, x).Top = 20 + y * 64 '升目の左上の位置 picture(y, x).Left = 20 + x * 64 picture(y, x).Height = 64 '升目の幅と高さ picture(y, x).Width = 64 picture(y, x).TabIndex = y * 3 + x '升目のインデックス If masu(y, x) = 1 Then '升目のイメージ picture(y, x).Image = PictureBox1.Image '1=空き ElseIf masu(y, x) = 2 Then picture(y, x).Image = PictureBox2.Image '2=× Else picture(y, x).Image = PictureBox3.Image '3=○ End If picture(y, x).Visible = True Next Next End Sub '終わりの判定 Public Function owari() As Boolean Dim y As Integer, x As Integer '添え字 Dim kosu As Integer '個数 '横に3つ並んだ判定 For y = 0 To 2 If masu(y, 0) = masu(y, 1) And masu(y, 1) = masu(y, 2) And masu(y, 2) = 2 Then MessageBox.Show("×の勝ち") owari = True Exit Function End If If masu(y, 0) = masu(y, 1) And masu(y, 1) = masu(y, 2) And masu(y, 2) = 3 Then MessageBox.Show("○の勝ち") End If Next '縦に3つ並んだ判定 For y = 0 To 2 If masu(0, y) = masu(1, y) And masu(1, y) = masu(2, y) And masu(2, y) = 2 Then MessageBox.Show("×の勝ち") owari = True Exit Function End If If masu(0, y) = masu(1, y) And masu(1, y) = masu(2, y) And masu(2, y) = 3 Then MessageBox.Show("○の勝ち") owari = True Exit Function End If Next '右上がり斜めに3つ並んだ判定 If masu(2, 0) = masu(1, 1) And masu(1, 1) = masu(0, 2) And masu(0, 2) = 2 Then MessageBox.Show("×の勝ち") owari = True Exit Function End If If masu(2, 0) = masu(1, 1) And masu(1, 1) = masu(0, 2) And masu(0, 2) = 3 Then MessageBox.Show("○の勝ち") owari = True Exit Function End If '右下がり斜めに3つ並んだ判定 If masu(0, 0) = masu(1, 1) And masu(1, 1) = masu(2, 2) And masu(2, 2) = 2 Then MessageBox.Show("×の勝ち") owari = True Exit Function End If If masu(0, 0) = masu(1, 1) And masu(1, 1) = masu(2, 2) And masu(2, 2) = 3 Then MessageBox.Show("○の勝ち") owari = True Exit Function End If '引き分けの判定 kosu = 0 '空きの升目を数える。 For y = 0 To 2 For x = 0 To 2 If masu(y, x) = 1 Then kosu = kosu + 1 End If Next Next If kosu = 0 Then '空きがない MessageBox.Show("引き分けです。") owari = True Exit Functionp End If 'それ以外は,試合は続行中 owari = False End Function
←解決時は質問者本人がここをチェックしてください。
戻る
掲示板システム
Copyright 2020 Takeshi Okamoto All Rights Reserved.