VBで○×げーむ作ってみた
なぜかVB.NET勉強してる
覚えたてのVBでゲームっ作ってみた!
○×ゲーム。3目並べだ。
↓決して怪しいファイルじゃないですw
"プログラムファイル(.exe)"
ちなみにソースこんな感じ
Public Class Form1 Private Const CIRCLE As Integer = 3 '○ Private Const CROSS As Integer = 5 '× Dim masu(,) As Label 'ラベル用の配列 Dim buf(,) As Integer '0:なし,3:○,5:× Dim ターン As Integer '0:ゲーム中じゃない,3:○,5:× Dim count As Integer Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 'ToolStripStatusLabel1.Text = "×のターンです。" End Sub Private Sub 終了ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 終了ToolStripMenuItem.Click End End Sub Private Sub 開始ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 開始ToolStripMenuItem.Click masu = New Label(2, 2) {} masu(0, 0) = Me.Label1 masu(1, 0) = Me.Label2 masu(2, 0) = Me.Label3 masu(0, 1) = Me.Label4 masu(1, 1) = Me.Label5 masu(2, 1) = Me.Label6 masu(0, 2) = Me.Label7 masu(1, 2) = Me.Label8 masu(2, 2) = Me.Label9 Dim i As Integer Dim j As Integer For i = 0 To 2 For j = 0 To 2 masu(i, j).text = "" Next Next buf = New Integer(2, 2) {} count = 0 ターン = CIRCLE showTurn() End Sub Private Sub selectMasu(ByVal x As Integer, ByVal y As Integer) If ターン = 0 OrElse buf(x, y) <> 0 Then Exit Sub End If Dim strCharacter As String If ターン = CIRCLE Then strCharacter = "○" Else strCharacter = "×" End If masu(x, y).Text = strCharacter buf(x, y) = ターン If ターン = CIRCLE Then ターン = CROSS Else ターン = CIRCLE End If showTurn() check() count += 1 If count = 9 Then ToolStripStatusLabel1.Text = "ゲーム終了。" ターン = 0 Exit Sub End If If ターン = CROSS Then com() End If End Sub Private Sub showTurn() Select Case ターン Case 0 ToolStripStatusLabel1.Text = "ゲームを開始してください。" Case CIRCLE ToolStripStatusLabel1.Text = "○のターンです。" Case CROSS ToolStripStatusLabel1.Text = "×のターンです。" End Select End Sub Private Sub check() Dim i As Integer Dim j As Integer Dim aY As Integer Dim aX As Integer For i = 0 To 2 '縦と横のチェック aY = 0 aX = 0 For j = 0 To 2 aY += buf(i, j) aX += buf(j, i) Next If isGameOver(aY) OrElse isGameOver(aX) Then Exit Sub End If Next '斜めのチェック Dim cross As Integer cross = 0 For i = 0 To 2 cross += buf(i, i) Next If isGameOver(cross) Then Exit Sub End If cross = 0 cross += buf(0, 2) + buf(1, 1) + buf(2, 0) If isGameOver(cross) Then Exit Sub End If End Sub Private Function isGameOver(ByVal arg As Integer) As Boolean If arg = CIRCLE * 3 Then MsgBox("○の勝ちです") ターン = 0 ToolStripStatusLabel1.Text = "ゲーム終了。" Return True ElseIf arg = CROSS * 3 Then MsgBox("×の勝ちです") ToolStripStatusLabel1.Text = "ゲーム終了。" ターン = 0 Return True End If Return False End Function Private Sub Label1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label1.Click selectMasu(0, 0) End Sub Private Sub Label2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label2.Click selectMasu(1, 0) End Sub Private Sub Label3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label3.Click selectMasu(2, 0) End Sub Private Sub Label4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label4.Click selectMasu(0, 1) End Sub Private Sub Label5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label5.Click selectMasu(1, 1) End Sub Private Sub Label6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label6.Click selectMasu(2, 1) End Sub Private Sub Label7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label7.Click selectMasu(0, 2) End Sub Private Sub Label8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label8.Click selectMasu(1, 2) End Sub Private Sub Label9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label9.Click selectMasu(2, 2) End Sub ''' <summary> ''' AI(すごくテキトー) ''' </summary> ''' <remarks></remarks> Private Sub com() Dim i As Integer Dim j As Integer Dim aY As Integer Dim aX As Integer Dim cross As Integer '3つめの探索 For i = 0 To 2 '縦と横のチェック aY = 0 aX = 0 For j = 0 To 2 aY += buf(i, j) aX += buf(j, i) Next If endCheck(aY) Then 'j -= 1 searchMasu(i, 0) Exit Sub ElseIf endCheck(aX) Then 'j -= 1 searchMasu(i, 1) Exit Sub End If Next '斜めのチェック cross = 0 For i = 0 To 2 cross += buf(i, i) Next If endCheck(cross) Then searchMasu(0, 2) Exit Sub End If cross = 0 cross += buf(0, 2) + buf(1, 1) + buf(2, 0) If endCheck(cross) Then searchMasu(0, 3) Exit Sub End If '相手の阻止 For i = 0 To 2 '縦と横のチェック aY = 0 aX = 0 For j = 0 To 2 aY += buf(i, j) aX += buf(j, i) Next If counterCheck(aY) Then 'j -= 1 searchMasu(i, 0) Exit Sub ElseIf counterCheck(aX) Then 'j -= 1 searchMasu(i, 1) Exit Sub End If Next '斜めのチェック cross = 0 For i = 0 To 2 cross += buf(i, i) Next If counterCheck(cross) Then searchMasu(0, 2) Exit Sub End If cross = 0 cross += buf(0, 2) + buf(1, 1) + buf(2, 0) If counterCheck(cross) Then searchMasu(0, 3) Exit Sub End If 'あとは適当に置く selectMasu() End Sub ''' <summary> ''' 既に2個置いてある列の中で残りの1個を選択する ''' </summary> ''' <param name="num">行または列</param> ''' <param name="mode">0:列,1:行,2:\,3:/</param> ''' <remarks></remarks> Private Sub searchMasu(ByVal num As Integer, ByVal mode As Integer) Dim i As Integer If mode = 0 Then For i = 0 To 2 If buf(num, i) = 0 Then selectMasu(num, i) Exit Sub End If Next ElseIf mode = 1 Then For i = 0 To 2 If buf(i, num) = 0 Then selectMasu(i, num) Exit Sub End If Next ElseIf mode = 2 Then For i = 0 To 2 If buf(i, i) = 0 Then selectMasu(i, i) Exit Sub End If Next Else 'buf(0, 2) + buf(1, 1) + buf(2, 0) If buf(0, 2) = 0 Then selectMasu(0, 2) Exit Sub End If If buf(1, 1) = 0 Then selectMasu(1, 1) Exit Sub End If If buf(2, 0) = 0 Then selectMasu(2, 0) Exit Sub End If End If End Sub Private Function counterCheck(ByVal arg As Integer) As Boolean If arg = CIRCLE * 2 Then Return True End If Return False End Function Private Function endCheck(ByVal arg As Integer) As Boolean If arg = CROSS * 2 Then Return True End If Return False End Function Private Sub selectMasu() '適当サーチ If buf(1, 1) = 0 Then selectMasu(1, 1) Exit Sub End If If buf(0, 0) = 0 Then selectMasu(0, 0) Exit Sub End If If buf(2, 0) = 0 Then selectMasu(2, 0) Exit Sub End If If buf(0, 2) = 0 Then selectMasu(0, 2) Exit Sub End If If buf(2, 2) = 0 Then selectMasu(2, 2) Exit Sub End If Dim x As Integer Dim y As Integer For x = 0 To 2 For y = 0 To 2 If buf(x, y) = 0 Then selectMasu(x, y) Exit Sub End If Next Next End Sub End Class
適当さが滲み出てるw
でも、必勝パターンを知らないと以外とつおいぞ!