Sign in to follow this  
Followers 0
soccerstar206

Visual Basic Board Game Needs Help

11 posts in this topic

Could someone help with my code? Attached is my code and a finished .exe on how my program should work. I cant get the program to start over once a player wins. There's also a few problems that you can see each time a player moves tiles, and I cannot get the player to move up or down tiles if they land on a special tile.

 

Thanks for the help

candyland.zip

Share this post


Link to post
Share on other sites

Also the only way to win is the random number must equal the last tile, if its too high then the player position stays in its spot until the right number is rolled. Right now when it goes over the last tile I get the error index was outside the bounds of array

Share this post


Link to post
Share on other sites

Please post the relevant code in your message, it's not practical to require people to download your assignment. What have you tried and what specifically isn't working?

Share this post


Link to post
Share on other sites

The main problem I am having is the player is going over the array number (35), I need the player to stay on the same tile until they roll a number that puts them on 35 so they win

Share this post


Link to post
Share on other sites


Option Strict On

Option Explicit On

Public Class frmBoard

'-----------------------------------------------------------

' Board Game

' Name

' compiled using MS Visual Studio 2010

' tested on an Intel Xeon PC running MS Windows 7

' CS 115

' Date

' Two player board game - tokens move around the board toward finish line

'-----------------------------------------------------------

Dim lblArray(35) As Label 'array of labels that make up the game board

Dim intRules(35) As Integer 'parallel array containing rules for the squares corresponding to lblArray entries

Dim blnP1Turn As Boolean = True 'switches indicating whose turn it is

Dim blnP2Turn As Boolean = False

Dim intP1Position As Integer 'position of player 1 on board

Dim intP2Position As Integer 'position of player 2 on board

Dim blnDoubleOccupancy As Boolean = False 'True if both players on same square

Dim LblP1num, lblP2num As Integer

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

'This is the size of each square (Label) in the game

Const intSQUARE_SIZE As Integer = 50

' this is the array of labels that make up the game board

'This for..Next Loop sets properties of each label in the array. The Select..Case statement

'places the labels around the edges of the form

For intCount = 0 To 35

lblArray(intCount) = New Label

lblArray(intCount).Size = New System.Drawing.Size(intSQUARE_SIZE, intSQUARE_SIZE)

lblArray(intCount).BorderStyle = BorderStyle.FixedSingle

lblArray(intCount).Name = "lbl" & intCount

lblArray(intCount).Visible = True

'The following line display the array index on each square

'You probably will want to comment it out

lblArray(intCount).Text = intCount.ToString

Select Case intCount

Case Is < 10

lblArray(intCount).Location = New System.Drawing.Point(intSQUARE_SIZE * intCount, 0)

Case 10 To 18

lblArray(intCount).Location =

New System.Drawing.Point(intSQUARE_SIZE * 9, intSQUARE_SIZE * (intCount - 9))

Case 19 To 27

lblArray(intCount).Location =

New System.Drawing.Point(intSQUARE_SIZE * 9 - intSQUARE_SIZE * (intCount - 18), intSQUARE_SIZE * 9)

Case 28 To 35

lblArray(intCount).Location =

New System.Drawing.Point(0, intSQUARE_SIZE * 9 - intSQUARE_SIZE * (intCount - 27))

End Select

' Set colors of the labels

Select Case intCount Mod 5

Case 0

lblArray(intCount).BackColor = Color.Aqua

Case 1

lblArray(intCount).BackColor = Color.BlueViolet

lblArray(intCount).ForeColor = Color.White

Case 2

lblArray(intCount).BackColor = Color.DarkOrchid

Case 3

lblArray(intCount).BackColor = Color.HotPink

Case 4

lblArray(intCount).BackColor = Color.Green

lblArray(intCount).ForeColor = Color.White

End Select

Controls.Add(lblArray(intCount))

Next

'Initialize special squares

SetRules()

Dim dtmSystemDate As Date 'name label with date

dtmSystemDate = Now

lblInfo.Text = CStr(("" & vbCrLf & "CS115 B" & vbCrLf & "Summer 2013") & vbCrLf & dtmSystemDate)

End Sub

Private Sub SetRules()

'Initialize special squares

lblArray(0).Text = "Start"

lblArray(lblArray.Length - 1).Text = "Finish Line"

lblArray(7).Text = "2 steps back"

intRules(7) = -2

lblArray(13).Text = "1 step forward"

intRules(13) = 1

lblArray(19).Text = "2 steps forward"

intRules(19) = 2

lblArray(32).Text = "1 step back"

intRules(32) = -1

End Sub

Private Sub btnPlayer1_Click(sender As Object, e As EventArgs) Handles btnPlayer1.Click

Dim Rnd As New Random

Dim Int1 As Integer

Int1 = Rnd.Next(1, 7)

lblArray(LblP1num).Text = CStr(LblP1num)

intP1Position += Int1

LblP1num = intP1Position

If intP1Position = 35 Then

lblArray(35).Text = "P1"

MessageBox.Show("Player One Wins!")

lblPlayer1.Text = ""

lblPlayer2.Text = ""

intP1Position = 0

intP2Position = 0

Else

If intP1Position = intP2Position Then

lblArray(intP1Position).Text = "P1 P2"

ElseIf intP1Position = 7 Then

intP1Position = 5

lblArray(intP1Position).Text = "P1"

ElseIf intP1Position = 13 Then

intP1Position = 14

lblArray(intP1Position).Text = "P1"

ElseIf intP1Position = 19 Then

intP1Position = 21

lblArray(intP1Position).Text = "P1"

ElseIf intP1Position = 32 Then

intP1Position = 31

lblArray(intP1Position).Text = "P1"

Else

lblArray(intP1Position).Text = "P1"

End If

End If

blnP1Turn = False

blnP2Turn = True

lblPlayer1.Text = CStr(Int1)

btnPlayer1.Enabled = False

btnPlayer2.Enabled = True

btnPlayer2.Focus()

End Sub

Private Sub btnPlayer2_Click(sender As Object, e As EventArgs) Handles btnPlayer2.Click

Dim Rnd As New Random

Dim Int1 As Integer

Int1 = Rnd.Next(1, 7)

lblArray(lblP2num).Text = CStr(lblP2num)

intP2Position += Int1

lblP2num = intP2Position

If intP2Position = 35 Then

lblArray(35).Text = "P2"

MessageBox.Show("Player Two Wins!")

lblPlayer1.Text = ""

lblPlayer2.Text = ""

intP1Position = 0

intP2Position = 0

Else

If intP2Position > 35 Then

lblArray(intP2Position).Text = CStr(intP2Position - Int1)

Else

If intP1Position = intP2Position Then

lblArray(intP2Position).Text = "P1 P2"

ElseIf intP2Position = 7 Then

intP2Position = 5

lblArray(intP2Position).Text = "P2"

ElseIf intP2Position = 13 Then

intP2Position = 14

lblArray(intP2Position).Text = "P2"

ElseIf intP2Position = 19 Then

intP2Position = 21

lblArray(intP2Position).Text = "P2"

ElseIf intP2Position = 32 Then

intP2Position = 31

lblArray(intP2Position).Text = "P2"

Else

lblArray(intP2Position).Text = "P2"

End If

End If

End If

blnP1Turn = True

blnP2Turn = False

lblPlayer2.Text = CStr(Int1)

btnPlayer1.Enabled = True

btnPlayer2.Enabled = False

btnPlayer1.Focus()

End Sub

Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click

Me.Close()

End Sub

End Class

Share this post


Link to post
Share on other sites

The main problem I am having is the player is going over the array number (35), I need the player to stay on the same tile until they roll a number that puts them on 35 so they win

I'm sure If statements would solve this.

 

Just after dice has been "rolled": If current tile + dice value is greater than 35, stay in current position, else if less than 35 move to position, else (if 35) player wins!

Share this post


Link to post
Share on other sites

I have thought about that, I don't know how to make it stay in current position. I've tried subtracting the dice value (int1) by itself. But it keeps going to:

lblArray(LblP1num).Text = CStr(LblP1num)

saying Index was outside the bounds of the array.

Share this post


Link to post
Share on other sites

I have thought about that, I don't know how to make it stay in current position. I've tried subtracting the dice value (int1) by itself. But it keeps going to:

lblArray(LblP1num).Text = CStr(LblP1num)

saying Index was outside the bounds of the array.

There wouldn't have to be any action performed for an object to stay in it's current position. In that case, you could just ignore declaring that particular "If". You have to also think about the next line of code the program goes to after exiting the If statement.

1 person likes this

Share this post


Link to post
Share on other sites

There wouldn't have to be any action performed for an object to stay in it's current position. In that case, you could just ignore declaring that particular "If". You have to also think about the next line of code the program goes to after exiting the If statement.

Got it working. Thanks for the help

Share this post


Link to post
Share on other sites

I would look at SELECT CASE rather than ElseIf.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0

  • Recently Browsing   0 members

    No registered users viewing this page.