|
-
Feb 21st, 2013, 12:09 PM
#1
[VB.Net]Connect 4
This is the source code and the .exe
Features:
- Plays connect 4
- player vs player
- player vs computer
Drawbacks:
- The ai is a little splottchy. Right now it picks a random column, if there are no available rows left then it repeats until a column and row are available.
Notes:
- It's a fun game. I use GDI to draw the coins that drop into the slots, so there is nothing advanced going on.
- I would post a screenshot, but the image is to big.
Full Project:
Connect4.zip
Source Code:
Code:
Option Strict On
Option Explicit On
Public Class Form1
Private grid(6, 6) As Panel
Private p1turn As Boolean = True
Private playing As Boolean = False
Private pvp As Boolean
Private r As New Random
#Region "New/End Game"
Private Sub NewGame()
Panel1.Controls.Clear()
For x As Integer = 0 To 6
For y As Integer = 0 To 6
Dim pnl As New Panel
With pnl
.BorderStyle = BorderStyle.FixedSingle
.Size = New Size(70, 70)
.Location = New Point(x * 71, y * 71)
.Tag = "null"
End With
grid(x, y) = pnl
Panel1.Controls.Add(pnl)
Next
Next
p1turn = True
playing = False
pvp = RadioButton2.Checked
End Sub
Private Sub CheckEnd()
Dim endgame As Boolean = False
For x As Integer = 0 To 3
For y As Integer = 0 To 6
'Horizontal - Player1
If grid(x, y).Tag.ToString = "p1" AndAlso grid(x + 1, y).Tag.ToString = "p1" AndAlso grid(x + 2, y).Tag.ToString = "p1" AndAlso grid(x + 3, y).Tag.ToString = "p1" Then
MessageBox.Show("Player1 Wins!")
endgame = True
Exit For
End If
'Horizontal - Player2
If grid(x, y).Tag.ToString = "p2" AndAlso grid(x + 1, y).Tag.ToString = "p2" AndAlso grid(x + 2, y).Tag.ToString = "p2" AndAlso grid(x + 3, y).Tag.ToString = "p2" Then
MessageBox.Show("Player2 Wins!")
endgame = True
Exit For
End If
Next
Next
If endgame = False Then
For x As Integer = 0 To 6
For y As Integer = 0 To 3
'Verticle - Player1
If grid(x, y).Tag.ToString = "p1" AndAlso grid(x, y + 1).Tag.ToString = "p1" AndAlso grid(x, y + 2).Tag.ToString = "p1" AndAlso grid(x, y + 3).Tag.ToString = "p1" Then
MessageBox.Show("Player1 Wins!")
endgame = True
Exit For
End If
'Verticle - Player2
If grid(x, y).Tag.ToString = "p2" AndAlso grid(x, y + 1).Tag.ToString = "p2" AndAlso grid(x, y + 2).Tag.ToString = "p2" AndAlso grid(x, y + 3).Tag.ToString = "p2" Then
MessageBox.Show("Player2 Wins!")
endgame = True
Exit For
End If
Next
Next
End If
If endgame = False Then
For x As Integer = 0 To 3
For y As Integer = 3 To 6
'Diagonal(/) - Player1
If grid(x, y).Tag.ToString = "p1" AndAlso grid(x + 1, y - 1).Tag.ToString = "p1" AndAlso grid(x + 2, y - 2).Tag.ToString = "p1" AndAlso grid(x + 3, y - 3).Tag.ToString = "p1" Then
MessageBox.Show("Player1 Wins!")
endgame = True
Exit For
End If
'Diagonal(/) - Player2
If grid(x, y).Tag.ToString = "p2" AndAlso grid(x + 1, y - 1).Tag.ToString = "p2" AndAlso grid(x + 2, y - 2).Tag.ToString = "p2" AndAlso grid(x + 3, y - 3).Tag.ToString = "p2" Then
MessageBox.Show("Player2 Wins!")
endgame = True
Exit For
End If
Next
Next
End If
If endgame = False Then
For x As Integer = 0 To 3
For y As Integer = 0 To 3
'Diagonal(\) - Player1
If grid(x, y).Tag.ToString = "p1" AndAlso grid(x + 1, y + 1).Tag.ToString = "p1" AndAlso grid(x + 2, y + 2).Tag.ToString = "p1" AndAlso grid(x + 3, y + 3).Tag.ToString = "p1" Then
MessageBox.Show("Player1 Wins!")
endgame = True
Exit For
End If
'Diagonal(\) - Player2
If grid(x, y).Tag.ToString = "p2" AndAlso grid(x + 1, y + 1).Tag.ToString = "p2" AndAlso grid(x + 2, y + 2).Tag.ToString = "p2" AndAlso grid(x + 3, y + 3).Tag.ToString = "p2" Then
MessageBox.Show("Player2 Wins!")
endgame = True
Exit For
End If
Next
Next
End If
If endgame Then
Call NewGame()
End If
End Sub
#End Region
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Call NewGame()
End Sub
#Region "Button Clicks"
Private Sub bnt_Col0_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bnt_Col0.Click
playing = True
Dim pnl As Panel
For i As Integer = 6 To 0 Step -1
If grid(0, i).Tag.ToString = "null" Then
pnl = grid(0, i)
Exit For
End If
Next
If IsNothing(pnl) = False Then
pnl.BackgroundImage = drawcoin()
If p1turn Then
pnl.Tag = "p1"
Else
pnl.Tag = "p2"
End If
p1turn = Not p1turn
End If
If pvp = False Then
Call Ai()
End If
Call CheckEnd()
End Sub
Private Sub btn_Col1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Col1.Click
playing = True
Dim pnl As Panel
For i As Integer = 6 To 0 Step -1
If grid(1, i).Tag.ToString = "null" Then
pnl = grid(1, i)
Exit For
End If
Next
If IsNothing(pnl) = False Then
pnl.BackgroundImage = drawcoin()
If p1turn Then
pnl.Tag = "p1"
Else
pnl.Tag = "p2"
End If
p1turn = Not p1turn
End If
If pvp = False Then
Call Ai()
End If
Call CheckEnd()
End Sub
Private Sub btn_Col2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Col2.Click
playing = True
Dim pnl As Panel
For i As Integer = 6 To 0 Step -1
If grid(2, i).Tag.ToString = "null" Then
pnl = grid(2, i)
Exit For
End If
Next
If IsNothing(pnl) = False Then
pnl.BackgroundImage = drawcoin()
If p1turn Then
pnl.Tag = "p1"
Else
pnl.Tag = "p2"
End If
p1turn = Not p1turn
End If
If pvp = False Then
Call Ai()
End If
Call CheckEnd()
End Sub
Private Sub btn_Col3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Col3.Click
playing = True
Dim pnl As Panel
For i As Integer = 6 To 0 Step -1
If grid(3, i).Tag.ToString = "null" Then
pnl = grid(3, i)
Exit For
End If
Next
If IsNothing(pnl) = False Then
pnl.BackgroundImage = drawcoin()
If p1turn Then
pnl.Tag = "p1"
Else
pnl.Tag = "p2"
End If
p1turn = Not p1turn
End If
If pvp = False Then
Call Ai()
End If
Call CheckEnd()
End Sub
Private Sub btn_Col4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Col4.Click
playing = True
Dim pnl As Panel
For i As Integer = 6 To 0 Step -1
If grid(4, i).Tag.ToString = "null" Then
pnl = grid(4, i)
Exit For
End If
Next
If IsNothing(pnl) = False Then
pnl.BackgroundImage = drawcoin()
If p1turn Then
pnl.Tag = "p1"
Else
pnl.Tag = "p2"
End If
p1turn = Not p1turn
End If
If pvp = False Then
Call Ai()
End If
Call CheckEnd()
End Sub
Private Sub btn_Col5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Col5.Click
playing = True
Dim pnl As Panel
For i As Integer = 6 To 0 Step -1
If grid(5, i).Tag.ToString = "null" Then
pnl = grid(5, i)
Exit For
End If
Next
If IsNothing(pnl) = False Then
pnl.BackgroundImage = drawcoin()
If p1turn Then
pnl.Tag = "p1"
Else
pnl.Tag = "p2"
End If
p1turn = Not p1turn
End If
If pvp = False Then
Call Ai()
End If
Call CheckEnd()
End Sub
Private Sub btn_Col6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Col6.Click
playing = True
Dim pnl As Panel
For i As Integer = 6 To 0 Step -1
If grid(6, i).Tag.ToString = "null" Then
pnl = grid(6, i)
Exit For
End If
Next
If IsNothing(pnl) = False Then
pnl.BackgroundImage = drawcoin()
If p1turn Then
pnl.Tag = "p1"
Else
pnl.Tag = "p2"
End If
p1turn = Not p1turn
End If
If pvp = False Then
Call Ai()
End If
Call CheckEnd()
End Sub
#End Region
#Region "Radiobuttons"
Private Sub RadioButton1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton1.CheckedChanged
pvp = RadioButton2.Checked
If playing Then
Call NewGame()
End If
End Sub
Private Sub RadioButton2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton2.CheckedChanged
pvp = RadioButton2.Checked
If playing Then
Call NewGame()
End If
End Sub
#End Region
Private Function drawcoin() As Bitmap
drawcoin = New Bitmap(68, 68)
Dim g As Graphics = Graphics.FromImage(drawcoin)
Dim brush As SolidBrush
If p1turn Then
brush = New SolidBrush(Color.Blue)
Else
brush = New SolidBrush(Color.Red)
End If
g.DrawEllipse(Pens.Black, New Rectangle(0, 0, 69, 69))
g.FillEllipse(brush, New Rectangle(1, 1, 67, 67))
g.Save()
Return drawcoin
End Function
Private Sub Ai()
Dim emptyspace As Boolean = False
Do Until emptyspace = True
Dim col As Integer = r.Next(0, 7)
Dim pnl As Panel
For i As Integer = 6 To 0 Step -1
If grid(col, i).Tag.ToString = "null" Then
pnl = grid(col, i)
Exit For
End If
Next
If IsNothing(pnl) = False Then
pnl.BackgroundImage = drawcoin()
If p1turn Then
pnl.Tag = "p1"
Else
pnl.Tag = "p2"
End If
p1turn = Not p1turn
emptyspace = True
End If
Loop
Call CheckEnd()
End Sub
End Class
Last edited by dday9; Feb 27th, 2013 at 11:52 AM.
Reason: Uploaded a prior version of the game. The file that's on there now is the most current one.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|