vb2010で○×ゲームの作り方教えてくださいm(_ _)m
投稿者 初心者  (学生)
投稿日時
2011/11/21 13:44:44
VBAなどの作り方は載ってるんですが、VBの作り方がわかりません。できたら、初心者なのでわかりやすく教えてくださいm(_ _)m
投稿者 shu  (社会人)
投稿日時
2011/11/21 13:55:13
ここの投稿プログラムにちょうど○×があるので参考にしてみてはどうでしょう。
投稿者 初心者  (学生)
投稿日時
2011/11/21 18:44:33
このサイトのやつが見れないんで、よかったらコードを書いてもらえませんか?よろしくお願いしますm(_ _)m
投稿者 たかくん  (社会人)
投稿日時
2011/11/22 03:43:01
初心者さん、おはようございます。
僕が初心者の時に作ったプログラムです。
Formデザイナには何もしません。
頑張って自分で読んでみてください。
文法的な事は調べてみてくださいね。
プログラムは2回に分けて掲載します。
文字数制限のため...
Public Class Form1
Const Cell As Integer = 3
Const PASS As String = Nothing
Const FIRST As Integer = 0
Const PLAYER As String = "○"
Const COM As String = "×"
Const REACHI As Integer = 2
Const BINGO As Integer = 3
Const Wid As Integer = 64
Dim Boards(Cell, Cell) As Label
Dim r As New Random
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim x, y, Cnt, Rd As Integer
Cnt = 0 : Rd = 0
For y = 0 To Cell - 1
For x = 0 To Cell - 1
Boards(x, y) = New Label
Cnt = Cnt + 1
With Boards(x, y)
.Tag = Cnt
.Size = New Size(Wid, Wid)
.Font = New Font("Time New Roman", 36, FontStyle.Bold)
.BorderStyle = BorderStyle.FixedSingle
.Text = PASS
.Location = New Point(x * Wid, y * Wid)
Me.Controls.Add(Boards(x, y))
AddHandler Boards(x, y).Click, AddressOf Man_Click
End With
If y = Cell - 1 And x = Cell - 1 Then Width = (x * Wid) + Wid + 5 : Height = (y * Wid) + Wid + 28
Next
Next
Rd = r.Next(0, 2) '人間が先手なら抜けるCPUが先手ならComputerAI()を呼ぶ
If Rd = FIRST Then Exit Sub Else ComputerAI()
End Sub
'CPUの戦術
Private Sub ComputerAI()
Dim x, y, p, c, i, ComX, ComY As Integer
For x = 0 To Cell - 1 '縦判定
p = 0 : c = 0
For y = 0 To Cell - 1
If Boards(x, y).Text = PLAYER Then p = p + 1
If Boards(x, y).Text = COM Then c = c + 1
If Boards(x, y).Text = PASS And c = 0 Then ComX = x : ComY = y
Next
If p = REACHI And c = 0 Then Boards(ComX, ComY).Text = COM : Exit Sub
Next
For y = 0 To Cell - 1 '横判定
p = 0 : c = 0
For x = 0 To Cell - 1
If Boards(x, y).Text = PLAYER Then p = p + 1
If Boards(x, y).Text = COM Then c = c + 1
If Boards(x, y).Text = PASS And c = 0 Then ComX = x : ComY = y
Next
If p = REACHI And c = 0 Then Boards(ComX, ComY).Text = COM : Exit Sub
Next
p = 0 : c = 0
For i = 0 To Cell - 1 '右下斜め判定
If Boards(i, i).Text = PLAYER Then p = p + 1
If Boards(i, i).Text = COM Then c = c + 1
If Boards(i, i).Text = PASS And c = 0 Then ComX = i : ComY = i
Next
If p = REACHI And c = 0 Then Boards(ComX, ComY).Text = COM : Exit Sub
p = 0 : c = 0
For x = Cell - 1 To 0 Step -1 '左下へ斜め探査
For y = 0 To Cell - 1
If x + y = 2 Then
If Boards(x, y).Text = PLAYER Then p = p + 1
If Boards(x, y).Text = COM Then c = c + 1
If Boards(x, y).Text = PASS And c = 0 Then ComX = x : ComY = y
End If
Next
Next
If p = REACHI And c = 0 Then Boards(ComX, ComY).Text = COM : Exit Sub
PutCom()
End Sub
僕が初心者の時に作ったプログラムです。
Formデザイナには何もしません。
頑張って自分で読んでみてください。
文法的な事は調べてみてくださいね。
プログラムは2回に分けて掲載します。
文字数制限のため...
Public Class Form1
Const Cell As Integer = 3
Const PASS As String = Nothing
Const FIRST As Integer = 0
Const PLAYER As String = "○"
Const COM As String = "×"
Const REACHI As Integer = 2
Const BINGO As Integer = 3
Const Wid As Integer = 64
Dim Boards(Cell, Cell) As Label
Dim r As New Random
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim x, y, Cnt, Rd As Integer
Cnt = 0 : Rd = 0
For y = 0 To Cell - 1
For x = 0 To Cell - 1
Boards(x, y) = New Label
Cnt = Cnt + 1
With Boards(x, y)
.Tag = Cnt
.Size = New Size(Wid, Wid)
.Font = New Font("Time New Roman", 36, FontStyle.Bold)
.BorderStyle = BorderStyle.FixedSingle
.Text = PASS
.Location = New Point(x * Wid, y * Wid)
Me.Controls.Add(Boards(x, y))
AddHandler Boards(x, y).Click, AddressOf Man_Click
End With
If y = Cell - 1 And x = Cell - 1 Then Width = (x * Wid) + Wid + 5 : Height = (y * Wid) + Wid + 28
Next
Next
Rd = r.Next(0, 2) '人間が先手なら抜けるCPUが先手ならComputerAI()を呼ぶ
If Rd = FIRST Then Exit Sub Else ComputerAI()
End Sub
'CPUの戦術
Private Sub ComputerAI()
Dim x, y, p, c, i, ComX, ComY As Integer
For x = 0 To Cell - 1 '縦判定
p = 0 : c = 0
For y = 0 To Cell - 1
If Boards(x, y).Text = PLAYER Then p = p + 1
If Boards(x, y).Text = COM Then c = c + 1
If Boards(x, y).Text = PASS And c = 0 Then ComX = x : ComY = y
Next
If p = REACHI And c = 0 Then Boards(ComX, ComY).Text = COM : Exit Sub
Next
For y = 0 To Cell - 1 '横判定
p = 0 : c = 0
For x = 0 To Cell - 1
If Boards(x, y).Text = PLAYER Then p = p + 1
If Boards(x, y).Text = COM Then c = c + 1
If Boards(x, y).Text = PASS And c = 0 Then ComX = x : ComY = y
Next
If p = REACHI And c = 0 Then Boards(ComX, ComY).Text = COM : Exit Sub
Next
p = 0 : c = 0
For i = 0 To Cell - 1 '右下斜め判定
If Boards(i, i).Text = PLAYER Then p = p + 1
If Boards(i, i).Text = COM Then c = c + 1
If Boards(i, i).Text = PASS And c = 0 Then ComX = i : ComY = i
Next
If p = REACHI And c = 0 Then Boards(ComX, ComY).Text = COM : Exit Sub
p = 0 : c = 0
For x = Cell - 1 To 0 Step -1 '左下へ斜め探査
For y = 0 To Cell - 1
If x + y = 2 Then
If Boards(x, y).Text = PLAYER Then p = p + 1
If Boards(x, y).Text = COM Then c = c + 1
If Boards(x, y).Text = PASS And c = 0 Then ComX = x : ComY = y
End If
Next
Next
If p = REACHI And c = 0 Then Boards(ComX, ComY).Text = COM : Exit Sub
PutCom()
End Sub
投稿者 たかくん  (社会人)
投稿日時
2011/11/22 03:43:52
続きです。
'人間の手番
Private Sub Man_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Dim Px, Py, x, y As Integer
For y = 0 To Cell - 1
For x = 0 To Cell - 1
If sender.tag = Boards(x, y).Tag Then Px = x : Py = y 'マウスの座標取得
Next
Next
'マークできるかどうか調べて"○"を置く
If Boards(Px, Py).Text = PASS Then Boards(Px, Py).Text = PLAYER Else Beep() : Exit Sub
ComputerAI()
If Jugement() = True Then MsgBox("PLAYERの勝ち") : Me.Close()
End Sub
Private Function Jugement() As Boolean
Dim x, y, p, c, i As Integer
For x = 0 To Cell - 1
p = 0 : c = 0
For y = 0 To Cell - 1
If Boards(x, y).Text = PLAYER Then p = p + 1
If Boards(x, y).Text = COM Then c = c + 1
If c = BINGO Then MsgBox("COMの勝ち") : Me.Close()
If p = BINGO Then Return True
Next
Next
For y = 0 To Cell - 1
p = 0 : c = 0
For x = 0 To Cell - 1
If Boards(x, y).Text = PLAYER Then p = p + 1
If Boards(x, y).Text = COM Then c = c + 1
If c = BINGO Then MsgBox("COMの勝ち") : Me.Close()
If p = BINGO Then Return True
Next
Next
p = 0 : c = 0
For i = 0 To Cell - 1
If Boards(i, i).Text = PLAYER Then p = p + 1
If Boards(i, i).Text = COM Then c = c + 1
If c = BINGO Then MsgBox("COMの勝ち") : Me.Close()
If p = BINGO Then Return True
Next
p = 0 : c = 0
For x = Cell - 1 To 0 Step -1
For y = 0 To Cell - 1
If x + y = 2 Then
If Boards(x, y).Text = PLAYER Then p = p + 1
If Boards(x, y).Text = COM Then c = c + 1
If c = BINGO Then MsgBox("COMの勝ち") : Me.Close()
If p = BINGO Then Return True
End If
Next
Next
Return False
End Function
Private Sub PutCom()
Dim Rx, Ry, x, y, Cnt As Integer
Cnt = 0
Rx = r.Next(0, Cell) : Ry = r.Next(0, Cell)
For y = 0 To Cell - 1
For x = 0 To Cell - 1
If Boards(x, y).Text <> PASS Then Cnt = Cnt + 1
Next
Next
If Cnt = Cell * Cell Then MsgBox("ドローです。") : Me.Close()
If Boards(Ry, Rx).Text = PASS Then
Boards(Ry, Rx).Text = COM
Else
PutCom()
End If
End Sub
End Class
'人間の手番
Private Sub Man_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Dim Px, Py, x, y As Integer
For y = 0 To Cell - 1
For x = 0 To Cell - 1
If sender.tag = Boards(x, y).Tag Then Px = x : Py = y 'マウスの座標取得
Next
Next
'マークできるかどうか調べて"○"を置く
If Boards(Px, Py).Text = PASS Then Boards(Px, Py).Text = PLAYER Else Beep() : Exit Sub
ComputerAI()
If Jugement() = True Then MsgBox("PLAYERの勝ち") : Me.Close()
End Sub
Private Function Jugement() As Boolean
Dim x, y, p, c, i As Integer
For x = 0 To Cell - 1
p = 0 : c = 0
For y = 0 To Cell - 1
If Boards(x, y).Text = PLAYER Then p = p + 1
If Boards(x, y).Text = COM Then c = c + 1
If c = BINGO Then MsgBox("COMの勝ち") : Me.Close()
If p = BINGO Then Return True
Next
Next
For y = 0 To Cell - 1
p = 0 : c = 0
For x = 0 To Cell - 1
If Boards(x, y).Text = PLAYER Then p = p + 1
If Boards(x, y).Text = COM Then c = c + 1
If c = BINGO Then MsgBox("COMの勝ち") : Me.Close()
If p = BINGO Then Return True
Next
Next
p = 0 : c = 0
For i = 0 To Cell - 1
If Boards(i, i).Text = PLAYER Then p = p + 1
If Boards(i, i).Text = COM Then c = c + 1
If c = BINGO Then MsgBox("COMの勝ち") : Me.Close()
If p = BINGO Then Return True
Next
p = 0 : c = 0
For x = Cell - 1 To 0 Step -1
For y = 0 To Cell - 1
If x + y = 2 Then
If Boards(x, y).Text = PLAYER Then p = p + 1
If Boards(x, y).Text = COM Then c = c + 1
If c = BINGO Then MsgBox("COMの勝ち") : Me.Close()
If p = BINGO Then Return True
End If
Next
Next
Return False
End Function
Private Sub PutCom()
Dim Rx, Ry, x, y, Cnt As Integer
Cnt = 0
Rx = r.Next(0, Cell) : Ry = r.Next(0, Cell)
For y = 0 To Cell - 1
For x = 0 To Cell - 1
If Boards(x, y).Text <> PASS Then Cnt = Cnt + 1
Next
Next
If Cnt = Cell * Cell Then MsgBox("ドローです。") : Me.Close()
If Boards(Ry, Rx).Text = PASS Then
Boards(Ry, Rx).Text = COM
Else
PutCom()
End If
End Sub
End Class
投稿者 初心者  (学生)
投稿日時
2011/11/22 18:59:43
わざわざありがとうございます!m(_ _)m
言い忘れていたんですが、コンピュータなしの三つそろったら勝ちというルールです。
その場合は、COMを抜けばいいんでしょうか(・・?
言い忘れていたんですが、コンピュータなしの三つそろったら勝ちというルールです。
その場合は、COMを抜けばいいんでしょうか(・・?
投稿者 初心者  (学生)
投稿日時
2011/11/22 19:02:36
あと、最初はDIMやBLOONEなどを使ってやれと言われました(゜o゜;)
投稿者 たかくん  (社会人)
投稿日時
2011/11/23 08:52:34
おはようございます。
人との対戦ですか?
僕ができる事はここまでです。
難しいロジックではないので読んで理解したら改造してみて下さいね。
頑張って下さい。
人との対戦ですか?
僕ができる事はここまでです。
難しいロジックではないので読んで理解したら改造してみて下さいね。
頑張って下さい。
投稿者 初心者  (学生)
投稿日時
2011/11/23 10:39:19
本当に助かりました!ありがとうございました。