EDN Admin
Well-known member
I created War, the classic card game In Visual Basic 2010 Express Edition. The game runs fine, however, it has flaws. For example, it does not have logic on keeping track of cards used already; because of this, it is possible to draw the
same cards multiple times in a game. <span style="text-decoration:underline I am a beginner and I need advice and suggestions on features that I should add and how I can improve the game. Since I didnt use the designer,
its possible to copy and paste the code and it should run.
Things you should know...
1. If you copy the code, images wont load because I called them from the resources, so you will need to comment out the code that calles images.
2. The game has multiple forms running, and not all are visible to the user, so I added a function to remove the X button at the top of each form. I did this because If the user exits the game by clicking the X button, then the forms not visible
to the user will still run in the background.
3. I was not able to add logic to handle two draws in a row, so when that happens, I did this
<pre>Call GetWinner()
If Player = Computer Then
History.Items.Add("Turn " & Y & " was a DRAW")
MsgBox("NULL!!!", MsgBoxStyle.Exclamation, "DRAW")
End If[/code]
<br/>
3. The game is boring to play because it is played by clicking on the Play button. I tried to make it to where the user has to click and drag on the cards to make it a little more fun, but it was beyond my skills.
OK, SO HERES THE CODE, FEEL FREE TO COPY AND PASTE!!!
<pre>==============================================================================
War
CopyRight (c) 2011 Brandon Jimenez
Please note that the Designer was not used
==============================================================================
Public Class War
Dim Begin, Options, Quit As New Button
Dim DeckPreview As New PictureBox
Dim EnableAudio, DisableAudio As New RadioButton
Dim InputDeckSize, InputDeckStyle As New ComboBox
Dim InputPlayerName As New TextBox
Dim Label1, Label2, Label3, Label4, Label5, Label6, Label7, Label8 As New Label
Dim CompLabel As New Label
Dim Table As New Form
Dim GameMenu As New MenuStrip
Dim DrawLabel As New Label
Dim Game, Help, NewGame, Settings, EndGame, ViewScores, About As New ToolStripMenuItem
Dim MenuSeperator1, MenuSeperator2 As New ToolStripSeparator
Dim PlayerCard1, PlayerCard2, ComputerCard1, ComputerCard2 As New PictureBox
Dim Back1, Back2, Back3, Back4, Back5, Back6 As New PictureBox
Dim Play As New Button
Dim FontStyle As New Font("Times New Roman", 10, Drawing.FontStyle.Bold)
Dim SmallFont As New Font("Times New Roman", 8, Drawing.FontStyle.Bold)
Dim Confirmation, NG As MsgBoxResult
Dim C, T, X, Y, W, L, GW, GL As Integer
Dim OutputDeckSize, OutputDeckStyle As Boolean
Dim Generator As New System.Random
Dim Player, Computer As Integer
Dim Rnd1, Rnd2 As Integer
Dim Hand, CompHand As Integer
Dim Draw As Boolean
Dim Win As Boolean
Dim DrawMode, LabelColor As New Timer
Dim hSysMenu As Long
Dim Scoreboard As New Form
Dim History As New ListBox
Dim ResumeGame As New Button
Dim Label9, label10, Label11, Label12, Label13, Label14, label15, Label16, Label17, Label18 As New Label
Dim CurrentWinner As String
Removes the X button****************************************************
Public Declare Function GetSystemMenu Lib "user32" _
(ByVal hwnd As Long, _
ByVal bRevert As Long) As Long
Public Declare Function RemoveMenu Lib "user32" _
(ByVal hMenu As Long, _
ByVal nPosition As Long, _
ByVal wFlags As Long) As Long
Public Const MF_BYPOSITION = &H400
**************************************************************************
Public Sub DisableCloseWindowButton(ByVal frm As Form)
Get the handle to the windows system menu
hSysMenu = GetSystemMenu(frm.Handle, 0)
Remove the Close menu item, this will also disable the close button
RemoveMenu(hSysMenu, 6, MF_BYPOSITION)
Remove the seperator bar
RemoveMenu(hSysMenu, 5, MF_BYPOSITION)
End Sub
Private Sub War_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
On Error Resume Next
Creats controls
Me.Controls.Add(Begin)
Me.Controls.Add(Options)
Me.Controls.Add(Quit)
Me.Controls.Add(DeckPreview)
Me.Controls.Add(EnableAudio)
Me.Controls.Add(DisableAudio)
Me.Controls.Add(InputDeckSize)
Me.Controls.Add(InputDeckStyle)
Me.Controls.Add(InputPlayerName)
Me.Controls.Add(Label1)
Me.Controls.Add(Label2)
Me.Controls.Add(Label3)
Me.Controls.Add(Label4)
Me.Controls.Add(Label5)
Table.Controls.Add(GameMenu)
Table.Controls.Add(CompLabel)
Table.Controls.Add(DrawLabel)
Table.Controls.Add(Label6)
Table.Controls.Add(Label7)
Table.Controls.Add(Label8)
Table.Controls.Add(PlayerCard1)
Table.Controls.Add(PlayerCard2)
Table.Controls.Add(ComputerCard1)
Table.Controls.Add(ComputerCard2)
Table.Controls.Add(Back1)
Table.Controls.Add(Back2)
Table.Controls.Add(Back3)
Table.Controls.Add(Back4)
Table.Controls.Add(Back5)
Table.Controls.Add(Back6)
Table.Controls.Add(Play)
Scoreboard.Controls.Add(History)
Scoreboard.Controls.Add(ResumeGame)
Scoreboard.Controls.Add(Label9)
Scoreboard.Controls.Add(label10)
Scoreboard.Controls.Add(Label11)
Scoreboard.Controls.Add(Label12)
Scoreboard.Controls.Add(Label13)
Scoreboard.Controls.Add(Label14)
Scoreboard.Controls.Add(label15)
Scoreboard.Controls.Add(Label16)
Scoreboard.Controls.Add(Label17)
Scoreboard.Controls.Add(Label18)
Add event handlers
AddHandler Quit.Click, AddressOf Quit_Click
AddHandler Options.Click, AddressOf Options_Click
AddHandler Begin.Click, AddressOf Begin_Click
AddHandler InputDeckStyle.SelectedIndexChanged, AddressOf InputDeckStyle_SelectedIndexChanged
AddHandler Table.Load, AddressOf Table_Load
AddHandler Play.Click, AddressOf Play_Click
AddHandler DrawMode.Tick, AddressOf DrawMode_Tick
AddHandler LabelColor.Tick, AddressOf LabelColor_Tick
AddHandler NewGame.Click, AddressOf NewGame_Click
AddHandler Settings.Click, AddressOf Settings_Click
AddHandler EndGame.Click, AddressOf EndGame_Click
AddHandler About.Click, AddressOf About_Click
AddHandler ViewScores.Click, AddressOf ViewScores_Click
AddHandler Scoreboard.Load, AddressOf Scoreboard_Load
AddHandler ResumeGame.Click, AddressOf ResumeGame_Click
Startup properties for the "War Settings" form
With Me
.BackgroundImage = My.Resources.Table
.BackgroundImageLayout = ImageLayout.Stretch
.CenterToScreen()
.ControlBox = False
.FormBorderStyle = Windows.Forms.FormBorderStyle.FixedSingle
.Icon = My.Resources.WarIcon
.Size = New Size(300, 150)
.Text = "War Settings"
End With
Properties for the "Begin Game" Button
With Begin
.AutoSize = True
.Text = "Begin Game"
.Location = New Point(197, 67)
.TabIndex = 3
End With
Properties for the "Options" Button
With Options
.Text = "Options"
.Location = New Point(12, 67)
.TabIndex = 2
End With
Properties for the "Exit" Button
With Quit
.Text = "Exit"
.Location = New Point(197, 12)
.TabIndex = 4
End With
Properties for the deck preview PictureBox
With DeckPreview
.BackgroundImage = My.Resources.Blue
.BackgroundImageLayout = ImageLayout.Stretch
.Location = New Point(201, 137)
.Size = New Size(71, 96)
End With
Properties for the "Enable" RadioButton
With EnableAudio
.AutoSize = True
.BackColor = Color.Transparent
.ForeColor = Color.White
.Text = "Enable"
.Location = New Point(12, 236)
.Checked = True
.TabIndex = 7
End With
Properties for the "Disable" RadioButton
With DisableAudio
.AutoSize = True
.BackColor = Color.Transparent
.ForeColor = Color.White
.Text = "Disable"
.Location = New Point(76, 236)
.TabIndex = 8
End With
Properties for the "Select deck size:" ComboBox
With InputDeckSize
.Items.Add("Small (Default)")
.Items.Add("Medium")
.Items.Add("Large")
.Location = New Point(12, 137)
.Text = ("Small (Default)")
.TabIndex = 5
End With
Properties for the "Select deck style:" ComboBox
With InputDeckStyle
.Items.Add("Black")
.Items.Add("Blue (Default)")
.Items.Add("Red")
.Location = New Point(12, 186)
.Text = "Blue (Default)"
.TabIndex = 6
End With
Properties for the TextBox
With InputPlayerName
.Location = New Point(12, 41)
.Size = New Size(260, 20)
.TabIndex = 1
End With
With Label1
.AutoSize = True
.BackColor = Color.Transparent
.ForeColor = Color.White
.Font = FontStyle
.Location = New Point(12, 12)
.Text = "Enter your name:"
End With
With Label2
.AutoSize = True
.BackColor = Color.Transparent
.ForeColor = Color.White
.Font = FontStyle
.Location = New Point(12, 121)
.Text = "Select deck size:"
End With
With Label3
.AutoSize = True
.BackColor = Color.Transparent
.ForeColor = Color.White
.Font = FontStyle
.Location = New Point(12, 170)
.Text = "Select deck style:"
End With
With Label4
.AutoSize = True
.BackColor = Color.Transparent
.ForeColor = Color.White
.Font = FontStyle
.Location = New Point(12, 220)
.Text = "Sound"
End With
With Label5
.AutoSize = True
.BackColor = Color.Transparent
.ForeColor = Color.White
.Font = FontStyle
.Location = New Point(198, 121)
.Text = "Preview"
End With
End Sub
This sub will load the default settings
Sub DefaultWarSettings()
Me.Size = New Size(300, 150)
InputPlayerName.Enabled = False
InputDeckSize.Text = "Small (Default)"
InputDeckSize.Enabled = True
InputDeckStyle.Text = "Blue (Default)"
InputDeckStyle.Enabled = True
Begin.Text = "Begin Game"
Table.BackgroundImage = My.Resources.Table
Play.Enabled = True
disable timers
DrawMode.Enabled = False
LabelColor.Enabled = False
Resets the scoreboard(does not reset games won)
History.Items.Clear()
Y = 0
W = 0
L = 0
Label16.Text = W
Label17.Text = L
End Sub
This sub will prompt the user to end the current game
Sub EndGameConfirmation()
Confirmation = MsgBox("Are you sure you want to end the game?", MsgBoxStyle.YesNo, "End Game Confirmation")
If Confirmation = MsgBoxResult.Yes Then
Scoreboard.Close()
Table.Close()
Me.Close()
End If
End Sub
This sub will prompt the user to start a new game
Sub NewGameConfirmation()
NG = MsgBox("Do you want to start a new game?", MsgBoxStyle.YesNo, "New Game Confirmation")
If NG = MsgBoxResult.Yes Then
Call DefaultWarSettings()
Scoreboard.Hide()
Table.Hide()
Me.Show()
End If
End Sub
Private Sub InputDeckStyle_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
Gets the correct image for the deck style preview
If InputDeckStyle.Text = "Black" Then
DeckPreview.BackgroundImage = My.Resources.Black
Else
If InputDeckStyle.Text = "Blue (Default)" Then
DeckPreview.BackgroundImage = My.Resources.Blue
Else
If InputDeckStyle.Text = "Red" Then
DeckPreview.BackgroundImage = My.Resources.Red
End If
End If
End If
End Sub
Private Sub Quit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Call EndGameConfirmation()
End Sub
Private Sub Options_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
C = C + 1
If C = 1 Then
Plays a sound if the radiobutton is set to enabled, and expands the form
If EnableAudio.Checked = True Then
My.Computer.Audio.Play(My.Resources.SpeechOn, AudioPlayMode.Background)
End If
Me.Size = New Size(300, 300)
ElseIf C = 2 Then
Plays a sound if the radiobutton is set to enabled, and sets form back to the original size
If EnableAudio.Checked = True Then
My.Computer.Audio.Play(My.Resources.SpeechSleep, AudioPlayMode.Background)
End If
C = 0
Me.Size = New Size(300, 150)
End If
End Sub
Private Sub Begin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Data is not yet inputed
OutputDeckSize = False
OutputDeckStyle = False
Sets the dafault player name
If InputPlayerName.Text = "" Then
InputPlayerName.Text = Environ("USERNAME")
End If
Sets the default deck size
If InputDeckSize.Text = "" Then
InputDeckSize.Text = "Small (Default)"
End If
Sets the default deck style
If InputDeckStyle.Text = "" Then
InputDeckStyle.Text = "Blue (Default)"
End If
If InputDeckSize.Text = "Small (Default)" Or InputDeckSize.Text = "Medium" Or InputDeckSize.Text = "Large" Then
OutputDeckSize = True
End If
If InputDeckStyle.Text = "Black" Or InputDeckStyle.Text = "Blue (Default)" Or InputDeckStyle.Text = "Red" Then
OutputDeckStyle = True
End If
If OutputDeckSize = False Or OutputDeckStyle = False Then
MsgBox("Please enter the correct deck size and/or deck style", MsgBoxStyle.Exclamation, "Error")
End If
Starts the game if data is inputed
If OutputDeckSize = True And OutputDeckStyle = True And Begin.Text = "Begin Game" Then
C = 0
Me.Size = New Size(300, 150)
My.Computer.Audio.Play(My.Resources.ir_inter, AudioPlayMode.Background)
Me.Hide()
Table.Show()
Table.Enabled = True
Call GameStart()
End If
If OutputDeckSize = True And OutputDeckStyle = True And Begin.Text = "Resume Game" Then
Me.Hide()
Table.Enabled = True
Table.Show()
Table.BringToFront()
End If
End Sub
=================================================================================
Start of Main
=================================================================================
Sub GameStart()
Draw = False
DrawMode.Enabled = False
DrawMode.Interval = 1000
LabelColor.Enabled = False
LabelColor.Interval = 300
Gets the deck size
If InputDeckSize.Text = "Small (Default)" Then
Hand = 13
CompHand = 13
Else
If InputDeckSize.Text = "Medium" Then
Hand = 26
CompHand = 26
Else
If InputDeckSize.Text = "Large" Then
Hand = 52
CompHand = 52
End If
End If
End If
Gets the deck style
If InputDeckStyle.Text = "Black" Then
PlayerCard1.BackgroundImage = My.Resources.Black
ComputerCard1.BackgroundImage = My.Resources.Black
Else
If InputDeckStyle.Text = "Blue (Default)" Then
PlayerCard1.BackgroundImage = My.Resources.Blue
ComputerCard1.BackgroundImage = My.Resources.Blue
Else
If InputDeckStyle.Text = "Red" Then
PlayerCard1.BackgroundImage = My.Resources.Red
ComputerCard1.BackgroundImage = My.Resources.Red
End If
End If
End If
Displays the name of the player
With Label6
.AutoSize = True
.BackColor = Color.Transparent
.ForeColor = Color.White
.Font = FontStyle
.Location = New Point(12, 28)
.Text = InputPlayerName.Text
End With
Creates the label that displays the amount of cards in the players hand
With Label8
.AutoSize = True
.BackColor = Color.Transparent
.ForeColor = Color.White
.Font = FontStyle
.Location = New Point(12, 232)
.Text = "Cards in Hand: " & Hand
End With
Creates the label that displays the amount of cards the computer has
With CompLabel
.AutoSize = True
.BackColor = Color.Transparent
.ForeColor = Color.White
.Font = SmallFont
.Location = New Point(185, 148)
.Text = "Computer has: " & CompHand
End With
End Sub
Sub GetRandomValue()
Gets random numbers
Rnd1 = Generator.Next(1, 14)
Rnd2 = Generator.Next(1, 14)
Outputs random numbers
Player = Rnd1
Computer = Rnd2
End Sub
Sub GetWinner()
Logic for a standard win
If Player > Computer And Draw = False Then
If EnableAudio.Checked = True Then
My.Computer.Audio.Play(My.Resources.chimes, AudioPlayMode.Background)
End If
Hand = Hand + 1
CompHand = CompHand - 1
Label8.Text = "Cards in Hand: " & Hand
CompLabel.Text = "Computer has: " & CompHand
History.Items.Add(InputPlayerName.Text & " won turn " & Y)
W = W + 1
Label16.Text = W
End If
Logic for a standard loose
If Player < Computer And Draw = False Then
If EnableAudio.Checked = True Then
My.Computer.Audio.Play(My.Resources.chord, AudioPlayMode.Background)
End If
Hand = Hand - 1
CompHand = CompHand + 1
Label8.Text = "Cards in Hand: " & Hand
CompLabel.Text = "Computer has: " & CompHand
History.Items.Add(InputPlayerName.Text & " lost turn " & Y)
L = L + 1
Label17.Text = L
End If
Logic for a win in draw mode
If Player > Computer And Draw = True Then
If EnableAudio.Checked = True Then
My.Computer.Audio.Play(My.Resources.tada, AudioPlayMode.Background)
End If
Hand = Hand + 4
CompHand = CompHand - 4
Label8.Text = "Cards in Hand: " & Hand
CompLabel.Text = "Computer has: " & CompHand
History.Items.Add(InputPlayerName.Text & " won turn " & Y)
W = W + 1
Label16.Text = W
End If
Logic for a loose in draw mode
If Player < Computer And Draw = True Then
If EnableAudio.Checked = True Then
My.Computer.Audio.Play(My.Resources.chord, AudioPlayMode.Background)
End If
Hand = Hand - 4
CompHand = CompHand + 4
Label8.Text = "Cards in Hand: " & Hand
CompLabel.Text = "Computer has: " & CompHand
History.Items.Add(InputPlayerName.Text & " lost turn " & Y)
L = L + 1
Label17.Text = L
End If
Starts the DrawMode timer if a draw occures
If Player = Computer And Draw = False Then
Play.Enabled = False
Draw = True
DrawMode.Enabled = True
End If
Displays the current winner in the Scoreboard
If GW > GL Then
Label18.Text = "Current Winner: " & InputPlayerName.Text
Else
If GW < GL Then
Label18.Text = "Current Winner: Computer"
Else
If GW = GL Then
CurrentWinner = "DRAW!!!"
Else
If GW = GL And W > L Then
Label18.Text = "Current Winner: " & InputPlayerName.Text
Else
If GW = GL And W < L Then
Label18.Text = "Current Winner: Computer"
End If
End If
End If
End If
End If
Logic for ending the game
If Hand < 1 Then
Win = False
Call GameOver()
Else
If Hand > 25 And InputDeckSize.Text = "Small (Default)" Then
Win = True
Call GameOver()
Else
If Hand > 51 And InputDeckSize.Text = "Medium" Then
Win = True
Call GameOver()
Else
If Hand > 103 And InputDeckSize.Text = "Large" Then
Win = True
Call GameOver()
End If
End If
End If
End If
End Sub
Sub GameOver()
If Win = False Then
GL = GL + 1
label15.Text = GL
Table.BackgroundImage = My.Resources.Skull
Call NewGameConfirmation()
Else
If Win = True Then
GW = GW + 1
Label14.Text = GW
Call NewGameConfirmation()
End If
End If
End Sub
Private Sub Table_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)
On Error Resume Next
Call DisableCloseWindowButton(Table)
Startup propoerties for the "War" form
With Table
.AcceptButton = Play
.BackgroundImage = My.Resources.Table
.BackgroundImageLayout = ImageLayout.Stretch
.FormBorderStyle = Windows.Forms.FormBorderStyle.FixedSingle
.Icon = My.Resources.WarIcon
.MaximizeBox = False
.Size = New Size(300, 300)
.Text = "War"
End With
Creates the menustrip
With GameMenu
.Location = New Point(0, 0)
.Items.Add(Game)
.Items.Add(Help)
End With
Add items to the menustrip
Game.Text = "Game"
Help.Text = "Help"
NewGame.Text = "New Game"
Settings.Text = "Settings"
ViewScores.Text = "Scoreboard"
EndGame.Text = "End Game"
About.Text = "About"
Game.DropDownItems.Add(NewGame)
Game.DropDownItems.Add(Settings)
Game.DropDownItems.Add(MenuSeperator1)
Game.DropDownItems.Add(ViewScores)
Game.DropDownItems.Add(MenuSeperator2)
Game.DropDownItems.Add(EndGame)
Help.DropDownItems.Add(About)
With Label7
.BackColor = Color.Transparent
.ForeColor = Color.White
.Font = FontStyle
.Location = New Point(198, 28)
.Text = "Computer"
End With
With DrawLabel
.BackColor = Color.Transparent
.Font = FontStyle
.Location = New Point(112, 92)
.Text = "DRAW"
.Visible = False
End With
With PlayerCard1
.BringToFront()
.Size = New Size(71, 96)
.Location = New Point(12, 49)
.BackgroundImageLayout = ImageLayout.Stretch
End With
With ComputerCard1
.BringToFront()
.Size = New Size(71, 96)
.Location = New Point(201, 49)
.BackgroundImageLayout = ImageLayout.Stretch
End With
With Back1
.BackgroundImageLayout = ImageLayout.Stretch
.Location = New Point(12, 265)
.Size = New Size(96, 71)
.Visible = False
End With
With Back2
.BackgroundImageLayout = ImageLayout.Stretch
.Location = New Point(55, 265)
.Size = New Size(96, 71)
.Visible = False
End With
With Back3
.BackgroundImageLayout = ImageLayout.Stretch
.Location = New Point(96, 265)
.Size = New Size(96, 71)
.Visible = False
End With
With Back4
.BackgroundImageLayout = ImageLayout.Stretch
.Location = New Point(270, 342)
.Size = New Size(96, 71)
.Visible = False
End With
With Back5
.BackgroundImageLayout = ImageLayout.Stretch
.Location = New Point(229, 342)
.Size = New Size(96, 71)
.Visible = False
End With
With Back6
.BackgroundImageLayout = ImageLayout.Stretch
.Location = New Point(186, 342)
.Size = New Size(96, 71)
.Visible = False
End With
With PlayerCard2
.BackgroundImageLayout = ImageLayout.Stretch
.Location = New Point(141, 265)
.Size = New Size(96, 71)
.Visible = False
End With
With ComputerCard2
.BackgroundImageLayout = ImageLayout.Stretch
.Location = New Point(141, 342)
.Size = New Size(96, 71)
.Visible = False
End With
With Play
.Text = "Play"
.Location = New Point(197, 227)
End With
End Sub
Private Sub Play_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Counter for adding data to the history
Y = Y + 1
Call GetRandomValue()
Call GetWinner()
Logic for displaying correct card
If Player = 1 Then
PlayerCard1.BackgroundImage = My.Resources.AceOfClubs
ElseIf Player = 2 Then
PlayerCard1.BackgroundImage = My.Resources.TwoOfClubs
ElseIf Player = 3 Then
PlayerCard1.BackgroundImage = My.Resources.ThreeOfClubs
ElseIf Player = 4 Then
PlayerCard1.BackgroundImage = My.Resources.FourOfClubs
ElseIf Player = 5 Then
PlayerCard1.BackgroundImage = My.Resources.FiveOfClubs
ElseIf Player = 6 Then
PlayerCard1.BackgroundImage = My.Resources.SixOfClubs
ElseIf Player = 7 Then
PlayerCard1.BackgroundImage = My.Resources.SevenOfClubs
ElseIf Player = 8 Then
PlayerCard1.BackgroundImage = My.Resources.EightOfClubs
ElseIf Player = 9 Then
PlayerCard1.BackgroundImage = My.Resources.NineOfClubs
ElseIf Player = 10 Then
PlayerCard1.BackgroundImage = My.Resources.TenOfClubs
ElseIf Player = 11 Then
PlayerCard1.BackgroundImage = My.Resources.JaxOfClubs
ElseIf Player = 12 Then
PlayerCard1.BackgroundImage = My.Resources.QueenOfClubs
ElseIf Player = 13 Then
PlayerCard1.BackgroundImage = My.Resources.KingOfClubs
End If
If Computer = 1 Then
ComputerCard1.BackgroundImage = My.Resources.AceOfHearts
ElseIf Computer = 2 Then
ComputerCard1.BackgroundImage = My.Resources.TwoOfHearts
ElseIf Computer = 3 Then
ComputerCard1.BackgroundImage = My.Resources.ThreeOfHearts
ElseIf Computer = 4 Then
ComputerCard1.BackgroundImage = My.Resources.FourOfClubs
ElseIf Computer = 5 Then
ComputerCard1.BackgroundImage = My.Resources.FiveOfHearts
ElseIf Computer = 6 Then
ComputerCard1.BackgroundImage = My.Resources.SixOfHearts
ElseIf Computer = 7 Then
ComputerCard1.BackgroundImage = My.Resources.SevenOfHearts
ElseIf Computer = 8 Then
ComputerCard1.BackgroundImage = My.Resources.EightOfHearts
ElseIf Computer = 9 Then
ComputerCard1.BackgroundImage = My.Resources.NineOfHearts
ElseIf Computer = 10 Then
ComputerCard1.BackgroundImage = My.Resources.TenOfHearts
ElseIf Computer = 11 Then
ComputerCard1.BackgroundImage = My.Resources.JaxOfHearts
ElseIf Computer = 12 Then
ComputerCard1.BackgroundImage = My.Resources.QueenOfHearts
ElseIf Computer = 13 Then
ComputerCard1.BackgroundImage = My.Resources.KingOfHearts
End If
End Sub
Private Sub LabelColor_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs)
DrawLabel.Visible = True
Logic for changing the color of the "DRAW" label
X = X + 1
If X = 1 Then
DrawLabel.ForeColor = Color.Red
ElseIf X = 2 Then
DrawLabel.ForeColor = Color.Orange
ElseIf X = 3 Then
DrawLabel.ForeColor = Color.Yellow
ElseIf X = 4 Then
DrawLabel.ForeColor = Color.LightGreen
ElseIf X = 5 Then
DrawLabel.ForeColor = Color.SkyBlue
ElseIf X = 6 Then
DrawLabel.ForeColor = Color.Purple
X = 0
End If
End Sub
Private Sub DrawMode_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs)
Enables the DRAW label
LabelColor.Enabled = True
Expands the form
Table.Size = New Size(400, 460)
Gets the correct card style
If InputDeckStyle.Text = "Black" Then
Back1.BackgroundImage = My.Resources.Black_Side
Back2.BackgroundImage = My.Resources.Black_Side
Back3.BackgroundImage = My.Resources.Black_Side
Back4.BackgroundImage = My.Resources.Black_Side
Back5.BackgroundImage = My.Resources.Black_Side
Back6.BackgroundImage = My.Resources.Black_Side
Else
If InputDeckStyle.Text = "Blue (Default)" Then
Back1.BackgroundImage = My.Resources.Blue_Side
Back2.BackgroundImage = My.Resources.Blue_Side
Back3.BackgroundImage = My.Resources.Blue_Side
Back4.BackgroundImage = My.Resources.Blue_Side
Back5.BackgroundImage = My.Resources.Blue_Side
Back6.BackgroundImage = My.Resources.Blue_Side
Else
If InputDeckStyle.Text = "Red" Then
Back1.BackgroundImage = My.Resources.Red_Side
Back2.BackgroundImage = My.Resources.Red_Side
Back3.BackgroundImage = My.Resources.Red_Side
Back4.BackgroundImage = My.Resources.Red_Side
Back5.BackgroundImage = My.Resources.Red_Side
Back6.BackgroundImage = My.Resources.Red_Side
End If
End If
End If
T = T + 1
If T = 1 Then
Back1.Visible = True
Back1.BringToFront()
Back4.Visible = True
Back4.BringToFront()
ElseIf T = 2 Then
Back2.Visible = True
Back2.BringToFront()
Back5.Visible = True
Back5.BringToFront()
ElseIf T = 3 Then
Back3.Visible = True
Back3.BringToFront()
Back6.Visible = True
Back6.BringToFront()
ElseIf T = 4 Then
PlayerCard2.Visible = True
PlayerCard2.BringToFront()
ComputerCard2.Visible = True
ComputerCard2.BringToFront()
Call GetRandomValue()
If Player = 1 Then
PlayerCard2.BackgroundImage = My.Resources.AceOfClubs_Side
ElseIf Player = 2 Then
PlayerCard2.BackgroundImage = My.Resources.TwoOfClubs_Side
ElseIf Player = 3 Then
PlayerCard2.BackgroundImage = My.Resources.ThreeOfClubs_Side
ElseIf Player = 4 Then
PlayerCard2.BackgroundImage = My.Resources.FourOfClubs_Side
ElseIf Player = 5 Then
PlayerCard2.BackgroundImage = My.Resources.FiveOfClubs_Side
ElseIf Player = 6 Then
PlayerCard2.BackgroundImage = My.Resources.SixOfClubs_Side
ElseIf Player = 7 Then
PlayerCard2.BackgroundImage = My.Resources.SevenOfClubs_Side
ElseIf Player = 8 Then
PlayerCard2.BackgroundImage = My.Resources.EightOfClubs_Side
ElseIf Player = 9 Then
PlayerCard2.BackgroundImage = My.Resources.NineOfClubs_Side
ElseIf Player = 10 Then
PlayerCard2.BackgroundImage = My.Resources.TenOfClubs_Side
ElseIf Player = 11 Then
PlayerCard2.BackgroundImage = My.Resources.JaxOfClubs_Side
ElseIf Player = 12 Then
PlayerCard2.BackgroundImage = My.Resources.QueenOfClubs_Side
ElseIf Player = 13 Then
PlayerCard2.BackgroundImage = My.Resources.KingOfClubs_Side
End If
If Computer = 1 Then
ComputerCard2.BackgroundImage = My.Resources.AceOfHearts_Side
ElseIf Computer = 2 Then
ComputerCard2.BackgroundImage = My.Resources.TwoOfHearts_Side
ElseIf Computer = 3 Then
ComputerCard2.BackgroundImage = My.Resources.ThreeOfHearts_Side
ElseIf Computer = 4 Then
ComputerCard2.BackgroundImage = My.Resources.FourOfClubs_Side
ElseIf Computer = 5 Then
ComputerCard2.BackgroundImage = My.Resources.FiveOfHearts_Side
ElseIf Computer = 6 Then
ComputerCard2.BackgroundImage = My.Resources.SixOfHearts_Side
ElseIf Computer = 7 Then
ComputerCard2.BackgroundImage = My.Resources.SevenOfHearts_Side
ElseIf Computer = 8 Then
ComputerCard2.BackgroundImage = My.Resources.EightOfHearts_Side
ElseIf Computer = 9 Then
ComputerCard2.BackgroundImage = My.Resources.NineOfHearts_Side
ElseIf Computer = 10 Then
ComputerCard2.BackgroundImage = My.Resources.TenOfHearts_Side
ElseIf Computer = 11 Then
ComputerCard2.BackgroundImage = My.Resources.JaxOfHearts_Side
ElseIf Computer = 12 Then
ComputerCard2.BackgroundImage = My.Resources.QueenOfHearts_Side
ElseIf Computer = 13 Then
ComputerCard2.BackgroundImage = My.Resources.KingOfHearts_Side
End If
Call GetWinner()
If Player = Computer Then
History.Items.Add("Turn " & Y & " was a DRAW")
MsgBox("NULL!!!", MsgBoxStyle.Exclamation, "DRAW")
End If
ElseIf T = 5 Then
DrawMode.Enabled = False
Draw = False
DrawLabel.Visible = False
LabelColor.Enabled = False
X = 0
T = 0
Play.Enabled = True
Table.Size = New Size(300, 300)
Back1.Visible = False
Back2.Visible = False
Back3.Visible = False
Back4.Visible = False
Back5.Visible = False
Back6.Visible = False
PlayerCard2.Visible = False
ComputerCard2.Visible = False
End If
End Sub
Private Sub NewGame_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Call NewGameConfirmation()
End Sub
Private Sub Settings_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Me.Size = New Size(300, 300)
Begin.Text = "Resume Game"
InputDeckSize.Enabled = False
InputPlayerName.Enabled = False
Me.Show()
Table.Enabled = False
End Sub
Private Sub ViewScores_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Table.Enabled = False
Scoreboard.Show()
End Sub
Private Sub EndGame_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Call EndGameConfirmation()
End Sub
Private Sub About_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
MsgBox(ProductName & vbCrLf & "Version: " & ProductVersion & vbCrLf & "Copyright (c) 2011 " & CompanyName, MsgBoxStyle.OkOnly, "About War")
End Sub
Private Sub Scoreboard_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)
On Error Resume Next
With Scoreboard
.BackgroundImage = My.Resources.Table
.BackgroundImageLayout = ImageLayout.Stretch
.ControlBox = False
.FormBorderStyle = Windows.Forms.FormBorderStyle.FixedSingle
.Icon = My.Resources.WarIcon
.Size = New Size(300, 300)
.Text = "Scoreboard"
End With
With History
.Location = New Point(12, 41)
.Size = New Size(175, 135)
End With
With ResumeGame
.AutoSize = True
.Location = New Point(197, 12)
.Text = "Resume Game"
End With
With Label9
.AutoSize = True
.BackColor = Color.Transparent
.ForeColor = Color.White
.Font = SmallFont
.Location = New Point(9, 9)
.Text = InputPlayerName.Text & "s" & " " & "stats"
End With
With label10
.AutoSize = True
.BackColor = Color.Transparent
.ForeColor = Color.White
.Font = SmallFont
.Location = New Point(12, 182)
.Text = "Games Won:"
End With
With Label11
.AutoSize = True
.BackColor = Color.Transparent
.ForeColor = Color.White
.Font = SmallFont
.Location = New Point(84, 182)
.Text = "Games Lost:"
End With
With Label12
.AutoSize = True
.BackColor = Color.Transparent
.ForeColor = Color.White
.Font = SmallFont
.Location = New Point(153, 182)
.Text = "Turns Won:"
End With
With Label13
.AutoSize = True
.BackColor = Color.Transparent
.ForeColor = Color.White
.Font = SmallFont
.Location = New Point(219, 182)
.Text = "Turns Lost:"
End With
With Label14
.AutoSize = True
.BackColor = Color.Transparent
.ForeColor = Color.LightGreen
.Font = SmallFont
.Location = New Point(12, 204)
End With
With label15
.AutoSize = True
.BackColor = Color.Transparent
.ForeColor = Color.Red
.Font = SmallFont
.Location = New Point(84, 204)
End With
With Label16
.AutoSize = True
.BackColor = Color.Transparent
.ForeColor = Color.LightGreen
.Font = SmallFont
.Location = New Point(153, 204)
End With
With Label17
.AutoSize = True
.BackColor = Color.Transparent
.ForeColor = Color.Red
.Font = SmallFont
.Location = New Point(219, 204)
End With
With Label18
.AutoSize = True
.BackColor = Color.Transparent
.ForeColor = Color.White
.Font = SmallFont
.Location = New Point(12, 240)
End With
End Sub
Private Sub ResumeGame_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Table.Enabled = True
Table.BringToFront()
Scoreboard.Hide()
End Sub
End Class[/code]
<hr class="sig Brandon Jimenez
View the full article
same cards multiple times in a game. <span style="text-decoration:underline I am a beginner and I need advice and suggestions on features that I should add and how I can improve the game. Since I didnt use the designer,
its possible to copy and paste the code and it should run.
Things you should know...
1. If you copy the code, images wont load because I called them from the resources, so you will need to comment out the code that calles images.
2. The game has multiple forms running, and not all are visible to the user, so I added a function to remove the X button at the top of each form. I did this because If the user exits the game by clicking the X button, then the forms not visible
to the user will still run in the background.
3. I was not able to add logic to handle two draws in a row, so when that happens, I did this
<pre>Call GetWinner()
If Player = Computer Then
History.Items.Add("Turn " & Y & " was a DRAW")
MsgBox("NULL!!!", MsgBoxStyle.Exclamation, "DRAW")
End If[/code]
<br/>
3. The game is boring to play because it is played by clicking on the Play button. I tried to make it to where the user has to click and drag on the cards to make it a little more fun, but it was beyond my skills.
OK, SO HERES THE CODE, FEEL FREE TO COPY AND PASTE!!!
<pre>==============================================================================
War
CopyRight (c) 2011 Brandon Jimenez
Please note that the Designer was not used
==============================================================================
Public Class War
Dim Begin, Options, Quit As New Button
Dim DeckPreview As New PictureBox
Dim EnableAudio, DisableAudio As New RadioButton
Dim InputDeckSize, InputDeckStyle As New ComboBox
Dim InputPlayerName As New TextBox
Dim Label1, Label2, Label3, Label4, Label5, Label6, Label7, Label8 As New Label
Dim CompLabel As New Label
Dim Table As New Form
Dim GameMenu As New MenuStrip
Dim DrawLabel As New Label
Dim Game, Help, NewGame, Settings, EndGame, ViewScores, About As New ToolStripMenuItem
Dim MenuSeperator1, MenuSeperator2 As New ToolStripSeparator
Dim PlayerCard1, PlayerCard2, ComputerCard1, ComputerCard2 As New PictureBox
Dim Back1, Back2, Back3, Back4, Back5, Back6 As New PictureBox
Dim Play As New Button
Dim FontStyle As New Font("Times New Roman", 10, Drawing.FontStyle.Bold)
Dim SmallFont As New Font("Times New Roman", 8, Drawing.FontStyle.Bold)
Dim Confirmation, NG As MsgBoxResult
Dim C, T, X, Y, W, L, GW, GL As Integer
Dim OutputDeckSize, OutputDeckStyle As Boolean
Dim Generator As New System.Random
Dim Player, Computer As Integer
Dim Rnd1, Rnd2 As Integer
Dim Hand, CompHand As Integer
Dim Draw As Boolean
Dim Win As Boolean
Dim DrawMode, LabelColor As New Timer
Dim hSysMenu As Long
Dim Scoreboard As New Form
Dim History As New ListBox
Dim ResumeGame As New Button
Dim Label9, label10, Label11, Label12, Label13, Label14, label15, Label16, Label17, Label18 As New Label
Dim CurrentWinner As String
Removes the X button****************************************************
Public Declare Function GetSystemMenu Lib "user32" _
(ByVal hwnd As Long, _
ByVal bRevert As Long) As Long
Public Declare Function RemoveMenu Lib "user32" _
(ByVal hMenu As Long, _
ByVal nPosition As Long, _
ByVal wFlags As Long) As Long
Public Const MF_BYPOSITION = &H400
**************************************************************************
Public Sub DisableCloseWindowButton(ByVal frm As Form)
Get the handle to the windows system menu
hSysMenu = GetSystemMenu(frm.Handle, 0)
Remove the Close menu item, this will also disable the close button
RemoveMenu(hSysMenu, 6, MF_BYPOSITION)
Remove the seperator bar
RemoveMenu(hSysMenu, 5, MF_BYPOSITION)
End Sub
Private Sub War_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
On Error Resume Next
Creats controls
Me.Controls.Add(Begin)
Me.Controls.Add(Options)
Me.Controls.Add(Quit)
Me.Controls.Add(DeckPreview)
Me.Controls.Add(EnableAudio)
Me.Controls.Add(DisableAudio)
Me.Controls.Add(InputDeckSize)
Me.Controls.Add(InputDeckStyle)
Me.Controls.Add(InputPlayerName)
Me.Controls.Add(Label1)
Me.Controls.Add(Label2)
Me.Controls.Add(Label3)
Me.Controls.Add(Label4)
Me.Controls.Add(Label5)
Table.Controls.Add(GameMenu)
Table.Controls.Add(CompLabel)
Table.Controls.Add(DrawLabel)
Table.Controls.Add(Label6)
Table.Controls.Add(Label7)
Table.Controls.Add(Label8)
Table.Controls.Add(PlayerCard1)
Table.Controls.Add(PlayerCard2)
Table.Controls.Add(ComputerCard1)
Table.Controls.Add(ComputerCard2)
Table.Controls.Add(Back1)
Table.Controls.Add(Back2)
Table.Controls.Add(Back3)
Table.Controls.Add(Back4)
Table.Controls.Add(Back5)
Table.Controls.Add(Back6)
Table.Controls.Add(Play)
Scoreboard.Controls.Add(History)
Scoreboard.Controls.Add(ResumeGame)
Scoreboard.Controls.Add(Label9)
Scoreboard.Controls.Add(label10)
Scoreboard.Controls.Add(Label11)
Scoreboard.Controls.Add(Label12)
Scoreboard.Controls.Add(Label13)
Scoreboard.Controls.Add(Label14)
Scoreboard.Controls.Add(label15)
Scoreboard.Controls.Add(Label16)
Scoreboard.Controls.Add(Label17)
Scoreboard.Controls.Add(Label18)
Add event handlers
AddHandler Quit.Click, AddressOf Quit_Click
AddHandler Options.Click, AddressOf Options_Click
AddHandler Begin.Click, AddressOf Begin_Click
AddHandler InputDeckStyle.SelectedIndexChanged, AddressOf InputDeckStyle_SelectedIndexChanged
AddHandler Table.Load, AddressOf Table_Load
AddHandler Play.Click, AddressOf Play_Click
AddHandler DrawMode.Tick, AddressOf DrawMode_Tick
AddHandler LabelColor.Tick, AddressOf LabelColor_Tick
AddHandler NewGame.Click, AddressOf NewGame_Click
AddHandler Settings.Click, AddressOf Settings_Click
AddHandler EndGame.Click, AddressOf EndGame_Click
AddHandler About.Click, AddressOf About_Click
AddHandler ViewScores.Click, AddressOf ViewScores_Click
AddHandler Scoreboard.Load, AddressOf Scoreboard_Load
AddHandler ResumeGame.Click, AddressOf ResumeGame_Click
Startup properties for the "War Settings" form
With Me
.BackgroundImage = My.Resources.Table
.BackgroundImageLayout = ImageLayout.Stretch
.CenterToScreen()
.ControlBox = False
.FormBorderStyle = Windows.Forms.FormBorderStyle.FixedSingle
.Icon = My.Resources.WarIcon
.Size = New Size(300, 150)
.Text = "War Settings"
End With
Properties for the "Begin Game" Button
With Begin
.AutoSize = True
.Text = "Begin Game"
.Location = New Point(197, 67)
.TabIndex = 3
End With
Properties for the "Options" Button
With Options
.Text = "Options"
.Location = New Point(12, 67)
.TabIndex = 2
End With
Properties for the "Exit" Button
With Quit
.Text = "Exit"
.Location = New Point(197, 12)
.TabIndex = 4
End With
Properties for the deck preview PictureBox
With DeckPreview
.BackgroundImage = My.Resources.Blue
.BackgroundImageLayout = ImageLayout.Stretch
.Location = New Point(201, 137)
.Size = New Size(71, 96)
End With
Properties for the "Enable" RadioButton
With EnableAudio
.AutoSize = True
.BackColor = Color.Transparent
.ForeColor = Color.White
.Text = "Enable"
.Location = New Point(12, 236)
.Checked = True
.TabIndex = 7
End With
Properties for the "Disable" RadioButton
With DisableAudio
.AutoSize = True
.BackColor = Color.Transparent
.ForeColor = Color.White
.Text = "Disable"
.Location = New Point(76, 236)
.TabIndex = 8
End With
Properties for the "Select deck size:" ComboBox
With InputDeckSize
.Items.Add("Small (Default)")
.Items.Add("Medium")
.Items.Add("Large")
.Location = New Point(12, 137)
.Text = ("Small (Default)")
.TabIndex = 5
End With
Properties for the "Select deck style:" ComboBox
With InputDeckStyle
.Items.Add("Black")
.Items.Add("Blue (Default)")
.Items.Add("Red")
.Location = New Point(12, 186)
.Text = "Blue (Default)"
.TabIndex = 6
End With
Properties for the TextBox
With InputPlayerName
.Location = New Point(12, 41)
.Size = New Size(260, 20)
.TabIndex = 1
End With
With Label1
.AutoSize = True
.BackColor = Color.Transparent
.ForeColor = Color.White
.Font = FontStyle
.Location = New Point(12, 12)
.Text = "Enter your name:"
End With
With Label2
.AutoSize = True
.BackColor = Color.Transparent
.ForeColor = Color.White
.Font = FontStyle
.Location = New Point(12, 121)
.Text = "Select deck size:"
End With
With Label3
.AutoSize = True
.BackColor = Color.Transparent
.ForeColor = Color.White
.Font = FontStyle
.Location = New Point(12, 170)
.Text = "Select deck style:"
End With
With Label4
.AutoSize = True
.BackColor = Color.Transparent
.ForeColor = Color.White
.Font = FontStyle
.Location = New Point(12, 220)
.Text = "Sound"
End With
With Label5
.AutoSize = True
.BackColor = Color.Transparent
.ForeColor = Color.White
.Font = FontStyle
.Location = New Point(198, 121)
.Text = "Preview"
End With
End Sub
This sub will load the default settings
Sub DefaultWarSettings()
Me.Size = New Size(300, 150)
InputPlayerName.Enabled = False
InputDeckSize.Text = "Small (Default)"
InputDeckSize.Enabled = True
InputDeckStyle.Text = "Blue (Default)"
InputDeckStyle.Enabled = True
Begin.Text = "Begin Game"
Table.BackgroundImage = My.Resources.Table
Play.Enabled = True
disable timers
DrawMode.Enabled = False
LabelColor.Enabled = False
Resets the scoreboard(does not reset games won)
History.Items.Clear()
Y = 0
W = 0
L = 0
Label16.Text = W
Label17.Text = L
End Sub
This sub will prompt the user to end the current game
Sub EndGameConfirmation()
Confirmation = MsgBox("Are you sure you want to end the game?", MsgBoxStyle.YesNo, "End Game Confirmation")
If Confirmation = MsgBoxResult.Yes Then
Scoreboard.Close()
Table.Close()
Me.Close()
End If
End Sub
This sub will prompt the user to start a new game
Sub NewGameConfirmation()
NG = MsgBox("Do you want to start a new game?", MsgBoxStyle.YesNo, "New Game Confirmation")
If NG = MsgBoxResult.Yes Then
Call DefaultWarSettings()
Scoreboard.Hide()
Table.Hide()
Me.Show()
End If
End Sub
Private Sub InputDeckStyle_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
Gets the correct image for the deck style preview
If InputDeckStyle.Text = "Black" Then
DeckPreview.BackgroundImage = My.Resources.Black
Else
If InputDeckStyle.Text = "Blue (Default)" Then
DeckPreview.BackgroundImage = My.Resources.Blue
Else
If InputDeckStyle.Text = "Red" Then
DeckPreview.BackgroundImage = My.Resources.Red
End If
End If
End If
End Sub
Private Sub Quit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Call EndGameConfirmation()
End Sub
Private Sub Options_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
C = C + 1
If C = 1 Then
Plays a sound if the radiobutton is set to enabled, and expands the form
If EnableAudio.Checked = True Then
My.Computer.Audio.Play(My.Resources.SpeechOn, AudioPlayMode.Background)
End If
Me.Size = New Size(300, 300)
ElseIf C = 2 Then
Plays a sound if the radiobutton is set to enabled, and sets form back to the original size
If EnableAudio.Checked = True Then
My.Computer.Audio.Play(My.Resources.SpeechSleep, AudioPlayMode.Background)
End If
C = 0
Me.Size = New Size(300, 150)
End If
End Sub
Private Sub Begin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Data is not yet inputed
OutputDeckSize = False
OutputDeckStyle = False
Sets the dafault player name
If InputPlayerName.Text = "" Then
InputPlayerName.Text = Environ("USERNAME")
End If
Sets the default deck size
If InputDeckSize.Text = "" Then
InputDeckSize.Text = "Small (Default)"
End If
Sets the default deck style
If InputDeckStyle.Text = "" Then
InputDeckStyle.Text = "Blue (Default)"
End If
If InputDeckSize.Text = "Small (Default)" Or InputDeckSize.Text = "Medium" Or InputDeckSize.Text = "Large" Then
OutputDeckSize = True
End If
If InputDeckStyle.Text = "Black" Or InputDeckStyle.Text = "Blue (Default)" Or InputDeckStyle.Text = "Red" Then
OutputDeckStyle = True
End If
If OutputDeckSize = False Or OutputDeckStyle = False Then
MsgBox("Please enter the correct deck size and/or deck style", MsgBoxStyle.Exclamation, "Error")
End If
Starts the game if data is inputed
If OutputDeckSize = True And OutputDeckStyle = True And Begin.Text = "Begin Game" Then
C = 0
Me.Size = New Size(300, 150)
My.Computer.Audio.Play(My.Resources.ir_inter, AudioPlayMode.Background)
Me.Hide()
Table.Show()
Table.Enabled = True
Call GameStart()
End If
If OutputDeckSize = True And OutputDeckStyle = True And Begin.Text = "Resume Game" Then
Me.Hide()
Table.Enabled = True
Table.Show()
Table.BringToFront()
End If
End Sub
=================================================================================
Start of Main
=================================================================================
Sub GameStart()
Draw = False
DrawMode.Enabled = False
DrawMode.Interval = 1000
LabelColor.Enabled = False
LabelColor.Interval = 300
Gets the deck size
If InputDeckSize.Text = "Small (Default)" Then
Hand = 13
CompHand = 13
Else
If InputDeckSize.Text = "Medium" Then
Hand = 26
CompHand = 26
Else
If InputDeckSize.Text = "Large" Then
Hand = 52
CompHand = 52
End If
End If
End If
Gets the deck style
If InputDeckStyle.Text = "Black" Then
PlayerCard1.BackgroundImage = My.Resources.Black
ComputerCard1.BackgroundImage = My.Resources.Black
Else
If InputDeckStyle.Text = "Blue (Default)" Then
PlayerCard1.BackgroundImage = My.Resources.Blue
ComputerCard1.BackgroundImage = My.Resources.Blue
Else
If InputDeckStyle.Text = "Red" Then
PlayerCard1.BackgroundImage = My.Resources.Red
ComputerCard1.BackgroundImage = My.Resources.Red
End If
End If
End If
Displays the name of the player
With Label6
.AutoSize = True
.BackColor = Color.Transparent
.ForeColor = Color.White
.Font = FontStyle
.Location = New Point(12, 28)
.Text = InputPlayerName.Text
End With
Creates the label that displays the amount of cards in the players hand
With Label8
.AutoSize = True
.BackColor = Color.Transparent
.ForeColor = Color.White
.Font = FontStyle
.Location = New Point(12, 232)
.Text = "Cards in Hand: " & Hand
End With
Creates the label that displays the amount of cards the computer has
With CompLabel
.AutoSize = True
.BackColor = Color.Transparent
.ForeColor = Color.White
.Font = SmallFont
.Location = New Point(185, 148)
.Text = "Computer has: " & CompHand
End With
End Sub
Sub GetRandomValue()
Gets random numbers
Rnd1 = Generator.Next(1, 14)
Rnd2 = Generator.Next(1, 14)
Outputs random numbers
Player = Rnd1
Computer = Rnd2
End Sub
Sub GetWinner()
Logic for a standard win
If Player > Computer And Draw = False Then
If EnableAudio.Checked = True Then
My.Computer.Audio.Play(My.Resources.chimes, AudioPlayMode.Background)
End If
Hand = Hand + 1
CompHand = CompHand - 1
Label8.Text = "Cards in Hand: " & Hand
CompLabel.Text = "Computer has: " & CompHand
History.Items.Add(InputPlayerName.Text & " won turn " & Y)
W = W + 1
Label16.Text = W
End If
Logic for a standard loose
If Player < Computer And Draw = False Then
If EnableAudio.Checked = True Then
My.Computer.Audio.Play(My.Resources.chord, AudioPlayMode.Background)
End If
Hand = Hand - 1
CompHand = CompHand + 1
Label8.Text = "Cards in Hand: " & Hand
CompLabel.Text = "Computer has: " & CompHand
History.Items.Add(InputPlayerName.Text & " lost turn " & Y)
L = L + 1
Label17.Text = L
End If
Logic for a win in draw mode
If Player > Computer And Draw = True Then
If EnableAudio.Checked = True Then
My.Computer.Audio.Play(My.Resources.tada, AudioPlayMode.Background)
End If
Hand = Hand + 4
CompHand = CompHand - 4
Label8.Text = "Cards in Hand: " & Hand
CompLabel.Text = "Computer has: " & CompHand
History.Items.Add(InputPlayerName.Text & " won turn " & Y)
W = W + 1
Label16.Text = W
End If
Logic for a loose in draw mode
If Player < Computer And Draw = True Then
If EnableAudio.Checked = True Then
My.Computer.Audio.Play(My.Resources.chord, AudioPlayMode.Background)
End If
Hand = Hand - 4
CompHand = CompHand + 4
Label8.Text = "Cards in Hand: " & Hand
CompLabel.Text = "Computer has: " & CompHand
History.Items.Add(InputPlayerName.Text & " lost turn " & Y)
L = L + 1
Label17.Text = L
End If
Starts the DrawMode timer if a draw occures
If Player = Computer And Draw = False Then
Play.Enabled = False
Draw = True
DrawMode.Enabled = True
End If
Displays the current winner in the Scoreboard
If GW > GL Then
Label18.Text = "Current Winner: " & InputPlayerName.Text
Else
If GW < GL Then
Label18.Text = "Current Winner: Computer"
Else
If GW = GL Then
CurrentWinner = "DRAW!!!"
Else
If GW = GL And W > L Then
Label18.Text = "Current Winner: " & InputPlayerName.Text
Else
If GW = GL And W < L Then
Label18.Text = "Current Winner: Computer"
End If
End If
End If
End If
End If
Logic for ending the game
If Hand < 1 Then
Win = False
Call GameOver()
Else
If Hand > 25 And InputDeckSize.Text = "Small (Default)" Then
Win = True
Call GameOver()
Else
If Hand > 51 And InputDeckSize.Text = "Medium" Then
Win = True
Call GameOver()
Else
If Hand > 103 And InputDeckSize.Text = "Large" Then
Win = True
Call GameOver()
End If
End If
End If
End If
End Sub
Sub GameOver()
If Win = False Then
GL = GL + 1
label15.Text = GL
Table.BackgroundImage = My.Resources.Skull
Call NewGameConfirmation()
Else
If Win = True Then
GW = GW + 1
Label14.Text = GW
Call NewGameConfirmation()
End If
End If
End Sub
Private Sub Table_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)
On Error Resume Next
Call DisableCloseWindowButton(Table)
Startup propoerties for the "War" form
With Table
.AcceptButton = Play
.BackgroundImage = My.Resources.Table
.BackgroundImageLayout = ImageLayout.Stretch
.FormBorderStyle = Windows.Forms.FormBorderStyle.FixedSingle
.Icon = My.Resources.WarIcon
.MaximizeBox = False
.Size = New Size(300, 300)
.Text = "War"
End With
Creates the menustrip
With GameMenu
.Location = New Point(0, 0)
.Items.Add(Game)
.Items.Add(Help)
End With
Add items to the menustrip
Game.Text = "Game"
Help.Text = "Help"
NewGame.Text = "New Game"
Settings.Text = "Settings"
ViewScores.Text = "Scoreboard"
EndGame.Text = "End Game"
About.Text = "About"
Game.DropDownItems.Add(NewGame)
Game.DropDownItems.Add(Settings)
Game.DropDownItems.Add(MenuSeperator1)
Game.DropDownItems.Add(ViewScores)
Game.DropDownItems.Add(MenuSeperator2)
Game.DropDownItems.Add(EndGame)
Help.DropDownItems.Add(About)
With Label7
.BackColor = Color.Transparent
.ForeColor = Color.White
.Font = FontStyle
.Location = New Point(198, 28)
.Text = "Computer"
End With
With DrawLabel
.BackColor = Color.Transparent
.Font = FontStyle
.Location = New Point(112, 92)
.Text = "DRAW"
.Visible = False
End With
With PlayerCard1
.BringToFront()
.Size = New Size(71, 96)
.Location = New Point(12, 49)
.BackgroundImageLayout = ImageLayout.Stretch
End With
With ComputerCard1
.BringToFront()
.Size = New Size(71, 96)
.Location = New Point(201, 49)
.BackgroundImageLayout = ImageLayout.Stretch
End With
With Back1
.BackgroundImageLayout = ImageLayout.Stretch
.Location = New Point(12, 265)
.Size = New Size(96, 71)
.Visible = False
End With
With Back2
.BackgroundImageLayout = ImageLayout.Stretch
.Location = New Point(55, 265)
.Size = New Size(96, 71)
.Visible = False
End With
With Back3
.BackgroundImageLayout = ImageLayout.Stretch
.Location = New Point(96, 265)
.Size = New Size(96, 71)
.Visible = False
End With
With Back4
.BackgroundImageLayout = ImageLayout.Stretch
.Location = New Point(270, 342)
.Size = New Size(96, 71)
.Visible = False
End With
With Back5
.BackgroundImageLayout = ImageLayout.Stretch
.Location = New Point(229, 342)
.Size = New Size(96, 71)
.Visible = False
End With
With Back6
.BackgroundImageLayout = ImageLayout.Stretch
.Location = New Point(186, 342)
.Size = New Size(96, 71)
.Visible = False
End With
With PlayerCard2
.BackgroundImageLayout = ImageLayout.Stretch
.Location = New Point(141, 265)
.Size = New Size(96, 71)
.Visible = False
End With
With ComputerCard2
.BackgroundImageLayout = ImageLayout.Stretch
.Location = New Point(141, 342)
.Size = New Size(96, 71)
.Visible = False
End With
With Play
.Text = "Play"
.Location = New Point(197, 227)
End With
End Sub
Private Sub Play_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Counter for adding data to the history
Y = Y + 1
Call GetRandomValue()
Call GetWinner()
Logic for displaying correct card
If Player = 1 Then
PlayerCard1.BackgroundImage = My.Resources.AceOfClubs
ElseIf Player = 2 Then
PlayerCard1.BackgroundImage = My.Resources.TwoOfClubs
ElseIf Player = 3 Then
PlayerCard1.BackgroundImage = My.Resources.ThreeOfClubs
ElseIf Player = 4 Then
PlayerCard1.BackgroundImage = My.Resources.FourOfClubs
ElseIf Player = 5 Then
PlayerCard1.BackgroundImage = My.Resources.FiveOfClubs
ElseIf Player = 6 Then
PlayerCard1.BackgroundImage = My.Resources.SixOfClubs
ElseIf Player = 7 Then
PlayerCard1.BackgroundImage = My.Resources.SevenOfClubs
ElseIf Player = 8 Then
PlayerCard1.BackgroundImage = My.Resources.EightOfClubs
ElseIf Player = 9 Then
PlayerCard1.BackgroundImage = My.Resources.NineOfClubs
ElseIf Player = 10 Then
PlayerCard1.BackgroundImage = My.Resources.TenOfClubs
ElseIf Player = 11 Then
PlayerCard1.BackgroundImage = My.Resources.JaxOfClubs
ElseIf Player = 12 Then
PlayerCard1.BackgroundImage = My.Resources.QueenOfClubs
ElseIf Player = 13 Then
PlayerCard1.BackgroundImage = My.Resources.KingOfClubs
End If
If Computer = 1 Then
ComputerCard1.BackgroundImage = My.Resources.AceOfHearts
ElseIf Computer = 2 Then
ComputerCard1.BackgroundImage = My.Resources.TwoOfHearts
ElseIf Computer = 3 Then
ComputerCard1.BackgroundImage = My.Resources.ThreeOfHearts
ElseIf Computer = 4 Then
ComputerCard1.BackgroundImage = My.Resources.FourOfClubs
ElseIf Computer = 5 Then
ComputerCard1.BackgroundImage = My.Resources.FiveOfHearts
ElseIf Computer = 6 Then
ComputerCard1.BackgroundImage = My.Resources.SixOfHearts
ElseIf Computer = 7 Then
ComputerCard1.BackgroundImage = My.Resources.SevenOfHearts
ElseIf Computer = 8 Then
ComputerCard1.BackgroundImage = My.Resources.EightOfHearts
ElseIf Computer = 9 Then
ComputerCard1.BackgroundImage = My.Resources.NineOfHearts
ElseIf Computer = 10 Then
ComputerCard1.BackgroundImage = My.Resources.TenOfHearts
ElseIf Computer = 11 Then
ComputerCard1.BackgroundImage = My.Resources.JaxOfHearts
ElseIf Computer = 12 Then
ComputerCard1.BackgroundImage = My.Resources.QueenOfHearts
ElseIf Computer = 13 Then
ComputerCard1.BackgroundImage = My.Resources.KingOfHearts
End If
End Sub
Private Sub LabelColor_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs)
DrawLabel.Visible = True
Logic for changing the color of the "DRAW" label
X = X + 1
If X = 1 Then
DrawLabel.ForeColor = Color.Red
ElseIf X = 2 Then
DrawLabel.ForeColor = Color.Orange
ElseIf X = 3 Then
DrawLabel.ForeColor = Color.Yellow
ElseIf X = 4 Then
DrawLabel.ForeColor = Color.LightGreen
ElseIf X = 5 Then
DrawLabel.ForeColor = Color.SkyBlue
ElseIf X = 6 Then
DrawLabel.ForeColor = Color.Purple
X = 0
End If
End Sub
Private Sub DrawMode_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs)
Enables the DRAW label
LabelColor.Enabled = True
Expands the form
Table.Size = New Size(400, 460)
Gets the correct card style
If InputDeckStyle.Text = "Black" Then
Back1.BackgroundImage = My.Resources.Black_Side
Back2.BackgroundImage = My.Resources.Black_Side
Back3.BackgroundImage = My.Resources.Black_Side
Back4.BackgroundImage = My.Resources.Black_Side
Back5.BackgroundImage = My.Resources.Black_Side
Back6.BackgroundImage = My.Resources.Black_Side
Else
If InputDeckStyle.Text = "Blue (Default)" Then
Back1.BackgroundImage = My.Resources.Blue_Side
Back2.BackgroundImage = My.Resources.Blue_Side
Back3.BackgroundImage = My.Resources.Blue_Side
Back4.BackgroundImage = My.Resources.Blue_Side
Back5.BackgroundImage = My.Resources.Blue_Side
Back6.BackgroundImage = My.Resources.Blue_Side
Else
If InputDeckStyle.Text = "Red" Then
Back1.BackgroundImage = My.Resources.Red_Side
Back2.BackgroundImage = My.Resources.Red_Side
Back3.BackgroundImage = My.Resources.Red_Side
Back4.BackgroundImage = My.Resources.Red_Side
Back5.BackgroundImage = My.Resources.Red_Side
Back6.BackgroundImage = My.Resources.Red_Side
End If
End If
End If
T = T + 1
If T = 1 Then
Back1.Visible = True
Back1.BringToFront()
Back4.Visible = True
Back4.BringToFront()
ElseIf T = 2 Then
Back2.Visible = True
Back2.BringToFront()
Back5.Visible = True
Back5.BringToFront()
ElseIf T = 3 Then
Back3.Visible = True
Back3.BringToFront()
Back6.Visible = True
Back6.BringToFront()
ElseIf T = 4 Then
PlayerCard2.Visible = True
PlayerCard2.BringToFront()
ComputerCard2.Visible = True
ComputerCard2.BringToFront()
Call GetRandomValue()
If Player = 1 Then
PlayerCard2.BackgroundImage = My.Resources.AceOfClubs_Side
ElseIf Player = 2 Then
PlayerCard2.BackgroundImage = My.Resources.TwoOfClubs_Side
ElseIf Player = 3 Then
PlayerCard2.BackgroundImage = My.Resources.ThreeOfClubs_Side
ElseIf Player = 4 Then
PlayerCard2.BackgroundImage = My.Resources.FourOfClubs_Side
ElseIf Player = 5 Then
PlayerCard2.BackgroundImage = My.Resources.FiveOfClubs_Side
ElseIf Player = 6 Then
PlayerCard2.BackgroundImage = My.Resources.SixOfClubs_Side
ElseIf Player = 7 Then
PlayerCard2.BackgroundImage = My.Resources.SevenOfClubs_Side
ElseIf Player = 8 Then
PlayerCard2.BackgroundImage = My.Resources.EightOfClubs_Side
ElseIf Player = 9 Then
PlayerCard2.BackgroundImage = My.Resources.NineOfClubs_Side
ElseIf Player = 10 Then
PlayerCard2.BackgroundImage = My.Resources.TenOfClubs_Side
ElseIf Player = 11 Then
PlayerCard2.BackgroundImage = My.Resources.JaxOfClubs_Side
ElseIf Player = 12 Then
PlayerCard2.BackgroundImage = My.Resources.QueenOfClubs_Side
ElseIf Player = 13 Then
PlayerCard2.BackgroundImage = My.Resources.KingOfClubs_Side
End If
If Computer = 1 Then
ComputerCard2.BackgroundImage = My.Resources.AceOfHearts_Side
ElseIf Computer = 2 Then
ComputerCard2.BackgroundImage = My.Resources.TwoOfHearts_Side
ElseIf Computer = 3 Then
ComputerCard2.BackgroundImage = My.Resources.ThreeOfHearts_Side
ElseIf Computer = 4 Then
ComputerCard2.BackgroundImage = My.Resources.FourOfClubs_Side
ElseIf Computer = 5 Then
ComputerCard2.BackgroundImage = My.Resources.FiveOfHearts_Side
ElseIf Computer = 6 Then
ComputerCard2.BackgroundImage = My.Resources.SixOfHearts_Side
ElseIf Computer = 7 Then
ComputerCard2.BackgroundImage = My.Resources.SevenOfHearts_Side
ElseIf Computer = 8 Then
ComputerCard2.BackgroundImage = My.Resources.EightOfHearts_Side
ElseIf Computer = 9 Then
ComputerCard2.BackgroundImage = My.Resources.NineOfHearts_Side
ElseIf Computer = 10 Then
ComputerCard2.BackgroundImage = My.Resources.TenOfHearts_Side
ElseIf Computer = 11 Then
ComputerCard2.BackgroundImage = My.Resources.JaxOfHearts_Side
ElseIf Computer = 12 Then
ComputerCard2.BackgroundImage = My.Resources.QueenOfHearts_Side
ElseIf Computer = 13 Then
ComputerCard2.BackgroundImage = My.Resources.KingOfHearts_Side
End If
Call GetWinner()
If Player = Computer Then
History.Items.Add("Turn " & Y & " was a DRAW")
MsgBox("NULL!!!", MsgBoxStyle.Exclamation, "DRAW")
End If
ElseIf T = 5 Then
DrawMode.Enabled = False
Draw = False
DrawLabel.Visible = False
LabelColor.Enabled = False
X = 0
T = 0
Play.Enabled = True
Table.Size = New Size(300, 300)
Back1.Visible = False
Back2.Visible = False
Back3.Visible = False
Back4.Visible = False
Back5.Visible = False
Back6.Visible = False
PlayerCard2.Visible = False
ComputerCard2.Visible = False
End If
End Sub
Private Sub NewGame_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Call NewGameConfirmation()
End Sub
Private Sub Settings_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Me.Size = New Size(300, 300)
Begin.Text = "Resume Game"
InputDeckSize.Enabled = False
InputPlayerName.Enabled = False
Me.Show()
Table.Enabled = False
End Sub
Private Sub ViewScores_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Table.Enabled = False
Scoreboard.Show()
End Sub
Private Sub EndGame_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Call EndGameConfirmation()
End Sub
Private Sub About_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
MsgBox(ProductName & vbCrLf & "Version: " & ProductVersion & vbCrLf & "Copyright (c) 2011 " & CompanyName, MsgBoxStyle.OkOnly, "About War")
End Sub
Private Sub Scoreboard_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)
On Error Resume Next
With Scoreboard
.BackgroundImage = My.Resources.Table
.BackgroundImageLayout = ImageLayout.Stretch
.ControlBox = False
.FormBorderStyle = Windows.Forms.FormBorderStyle.FixedSingle
.Icon = My.Resources.WarIcon
.Size = New Size(300, 300)
.Text = "Scoreboard"
End With
With History
.Location = New Point(12, 41)
.Size = New Size(175, 135)
End With
With ResumeGame
.AutoSize = True
.Location = New Point(197, 12)
.Text = "Resume Game"
End With
With Label9
.AutoSize = True
.BackColor = Color.Transparent
.ForeColor = Color.White
.Font = SmallFont
.Location = New Point(9, 9)
.Text = InputPlayerName.Text & "s" & " " & "stats"
End With
With label10
.AutoSize = True
.BackColor = Color.Transparent
.ForeColor = Color.White
.Font = SmallFont
.Location = New Point(12, 182)
.Text = "Games Won:"
End With
With Label11
.AutoSize = True
.BackColor = Color.Transparent
.ForeColor = Color.White
.Font = SmallFont
.Location = New Point(84, 182)
.Text = "Games Lost:"
End With
With Label12
.AutoSize = True
.BackColor = Color.Transparent
.ForeColor = Color.White
.Font = SmallFont
.Location = New Point(153, 182)
.Text = "Turns Won:"
End With
With Label13
.AutoSize = True
.BackColor = Color.Transparent
.ForeColor = Color.White
.Font = SmallFont
.Location = New Point(219, 182)
.Text = "Turns Lost:"
End With
With Label14
.AutoSize = True
.BackColor = Color.Transparent
.ForeColor = Color.LightGreen
.Font = SmallFont
.Location = New Point(12, 204)
End With
With label15
.AutoSize = True
.BackColor = Color.Transparent
.ForeColor = Color.Red
.Font = SmallFont
.Location = New Point(84, 204)
End With
With Label16
.AutoSize = True
.BackColor = Color.Transparent
.ForeColor = Color.LightGreen
.Font = SmallFont
.Location = New Point(153, 204)
End With
With Label17
.AutoSize = True
.BackColor = Color.Transparent
.ForeColor = Color.Red
.Font = SmallFont
.Location = New Point(219, 204)
End With
With Label18
.AutoSize = True
.BackColor = Color.Transparent
.ForeColor = Color.White
.Font = SmallFont
.Location = New Point(12, 240)
End With
End Sub
Private Sub ResumeGame_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Table.Enabled = True
Table.BringToFront()
Scoreboard.Hide()
End Sub
End Class[/code]
<hr class="sig Brandon Jimenez
View the full article