Mezillinu
asked on
Using simple GDI + with vb.net
Hi, can anybody show me how to create a grid, which is built with a data table, divided into 10 columns and 10 rows, and I start at example (0,0) with the address highlighted were I am. Then when I enter forward in a textbox and press a button, it goes one block forward.
If I enter left or right, the highlighted block, which displays me, and where i am, moves one block at a time to right or left.
How can this be achieved?
All help is HIGHLY appreciated, code snippets are preferred but links are also appreciated.
Regards
If I enter left or right, the highlighted block, which displays me, and where i am, moves one block at a time to right or left.
How can this be achieved?
All help is HIGHLY appreciated, code snippets are preferred but links are also appreciated.
Regards
ASKER
no, I want the blocks to be drawn all of them,
no images attached - i guess this is the most suitable thing to do in my situation.
thanks for replying to my post :-)
no images attached - i guess this is the most suitable thing to do in my situation.
thanks for replying to my post :-)
ASKER
I had in mind a panel, with the grid displayed. sort of a checkers grid, and it will be of as much as columns and rows, for as much as I tell it to be.
for example i have a selection on the right hand of the form, and i select how many rows and columns there is to be. but that is for the near future.
right now i have got to focus on how am i to draw the board, and how to move a position on the board since i have never done this and i am looking on the internet but it is quite difficult to find suitable posts of other people.
thanks for your help!
for example i have a selection on the right hand of the form, and i select how many rows and columns there is to be. but that is for the near future.
right now i have got to focus on how am i to draw the board, and how to move a position on the board since i have never done this and i am looking on the internet but it is quite difficult to find suitable posts of other people.
thanks for your help!
Alrighty...I'll post something up later today then. (sorry...have to run some errands)
ASKER
Ok - thanks! :-)
ASKER
Anything so far?
Sorry...ended up running around everywhere with the family yesterday. I'll see what I can do today...
ASKER
No problem about that
If you are going to post a code snippet, could you please write some comments so I could follow up what are you doing? I have never used GDI +, that's why.
I had in mind creating a form, just 1, which has space that a user could create a grid with the specified rows and columns given from textboxes, or drop down lists or anything. And then, the grid comes up.
The grid that comes up will have a destination point, and a starting point.
The destination point always has to be on the top of the grid, and shown in red. The starting point would be generated at random - and shown in green.
That is what I had in mind - if you show me how to create the grid, and how to make addresses and how to move the cells, like from the starting point to another location on the provided grid, I guess that is basically enough, and then leave the rest to me.
Links are also fine where I could find adequate information, as I could find non relevant of what I have to make :s
Thanks for your help, really appreciated!
If you are going to post a code snippet, could you please write some comments so I could follow up what are you doing? I have never used GDI +, that's why.
I had in mind creating a form, just 1, which has space that a user could create a grid with the specified rows and columns given from textboxes, or drop down lists or anything. And then, the grid comes up.
The grid that comes up will have a destination point, and a starting point.
The destination point always has to be on the top of the grid, and shown in red. The starting point would be generated at random - and shown in green.
That is what I had in mind - if you show me how to create the grid, and how to make addresses and how to move the cells, like from the starting point to another location on the provided grid, I guess that is basically enough, and then leave the rest to me.
Links are also fine where I could find adequate information, as I could find non relevant of what I have to make :s
Thanks for your help, really appreciated!
If you are new at GDI+, I will recommend the following link:
http://www.bobpowell.net/beginnersgdi.htm
For creating a grid control for your specific needs, you must create a user control and override the following methods to implement the basic functions in a grid:
'To handle the grid mouse events
Protected Overrides Sub OnMouseDown(ByVal mevent As System.Windows.Forms.Mouse EventArgs)
'...
End Sub
'To handle the grid keyboard events
Protected Overrides Sub OnKeyDown(ByVal kevent As System.Windows.Forms.KeyEv entArgs)
'...
End Sub
'To draw the control
Protected Overrides Sub OnPaint(ByVal pevent As System.Windows.Forms.Paint EventArgs)
'Start drawing lines and continue adding your own styles
Dim rowHeight As Integer = 16
Dim rowCount As Integer = 10
Dim columnWidth As Integer = 100
Dim columnCount As Integer = 10
For i As Integer = 0 To rowCount
pevent.Graphics.DrawLine(P ens.Black, 0, rowHeight, columnCount * columnWidth, rowHeight)
Next
For j As Integer = 0 To columnCount
pevent.Graphics.DrawLine(P ens.Black, (j * columnWidth), 0, (j * columnWidth), rowCount * rowHeight)
Next
'TIP: To draw system controls use the ControlPaint class:
' ControlPaint.DrawButton(pe vent.Graph ics, MyBase.ClientRectangle, ButtonState.Normal)
'If you are using VS2005 or later, you can also use:
' System.Windows.Forms.Butto nRenderer. DrawButton (...)
End Sub
If you don't want to create a user control, the best control for drawing is a Windows.Forms.Panel
Simply use the Paint, MouseDown and KeyDown events.
http://www.bobpowell.net/beginnersgdi.htm
For creating a grid control for your specific needs, you must create a user control and override the following methods to implement the basic functions in a grid:
'To handle the grid mouse events
Protected Overrides Sub OnMouseDown(ByVal mevent As System.Windows.Forms.Mouse
'...
End Sub
'To handle the grid keyboard events
Protected Overrides Sub OnKeyDown(ByVal kevent As System.Windows.Forms.KeyEv
'...
End Sub
'To draw the control
Protected Overrides Sub OnPaint(ByVal pevent As System.Windows.Forms.Paint
'Start drawing lines and continue adding your own styles
Dim rowHeight As Integer = 16
Dim rowCount As Integer = 10
Dim columnWidth As Integer = 100
Dim columnCount As Integer = 10
For i As Integer = 0 To rowCount
pevent.Graphics.DrawLine(P
Next
For j As Integer = 0 To columnCount
pevent.Graphics.DrawLine(P
Next
'TIP: To draw system controls use the ControlPaint class:
' ControlPaint.DrawButton(pe
'If you are using VS2005 or later, you can also use:
' System.Windows.Forms.Butto
End Sub
If you don't want to create a user control, the best control for drawing is a Windows.Forms.Panel
Simply use the Paint, MouseDown and KeyDown events.
ASKER
if you have a more front example could you change its extension to .doc, and attach it here please?
I have no idea of GDI + :s, its all new.
Thanks, I am trying to implement what you have shown me in the previous post!
Regards
I have no idea of GDI + :s, its all new.
Thanks, I am trying to implement what you have shown me in the previous post!
Regards
ASKER
and in your function what parameter do i pass here,
System.Windows.Forms.Paint EventArgs
?
I prefer not to use a control and to leave it as a form and everything in it
thanks for your help
System.Windows.Forms.Paint
?
I prefer not to use a control and to leave it as a form and everything in it
thanks for your help
ASKER
Ok, I used to code below to draw a grid :-)
that was not so hard.
now how do i make a starting point, be generated at random on the grid, and for it to be highlighted in a colour?
that was not so hard.
now how do i make a starting point, be generated at random on the grid, and for it to be highlighted in a colour?
Private Sub Panel1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Panel1.Paint
'Start drawing lines and continue adding your own styles
Dim rowHeight As Integer = 16
Dim rowCount As Integer = 100
Dim columnWidth As Integer = 10
Dim columnCount As Integer = 50
For i As Integer = 0 To rowCount
e.Graphics.DrawLine(Pens.Black, 0, i * rowHeight, columnCount * columnWidth, i * rowHeight)
Next
For j As Integer = 0 To columnCount
e.Graphics.DrawLine(Pens.Black, (j * columnWidth), 0, (j * columnWidth), rowCount * rowHeight)
Next
End Sub
ASKER
Ahh btw, and you had a small bug here in the code
fix is shown below.
you were writing how many rows you have, on the same one. therefore, always one row was showing.
fix is shown below.
you were writing how many rows you have, on the same one. therefore, always one row was showing.
For i As Integer = 0 To rowCount
e.Graphics.DrawLine(Pens.Black, 0, i * rowHeight, columnCount * columnWidth, i * rowHeight)
Next
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks!!!!
now how do I make the colored cell move? what is the best approach?
i had in mind creating a textbox, and if i example enter F1 the cell moves north 1 cell, E1, east 1 etc.
i know i have to validate the text inserted in the textbox first, but that will not give me any problems.
to be sure im on the right track, and keeping up with your code, the co ordinates for that highlighted cell is (4,3). so if I am to move that cell, i just add to 4, or 3, right?
it should be easy now :-) hmmmmm
now how do I make the colored cell move? what is the best approach?
i had in mind creating a textbox, and if i example enter F1 the cell moves north 1 cell, E1, east 1 etc.
i know i have to validate the text inserted in the textbox first, but that will not give me any problems.
to be sure im on the right track, and keeping up with your code, the co ordinates for that highlighted cell is (4,3). so if I am to move that cell, i just add to 4, or 3, right?
it should be easy now :-) hmmmmm
I haven't studied the posted code closely but basically you need to move all of the "controlling" variables OUT of the Paint() event and into the Form itself. Then you can just update the values from somewhere and force a repaint of the PictureBox by calling its Refresh() method:
If highlightedRow > 0 Then
highlightedRow = highlightedRow - 1
PictureBox1.Refresh()
End If
With the form looking something like this:
If highlightedRow > 0 Then
highlightedRow = highlightedRow - 1
PictureBox1.Refresh()
End If
With the form looking something like this:
Public Class Form1
Private highlightedColumn As Integer = 4
Private highlightedRow As Integer = 3
Private rowHeight As Integer = 16
Private rowCount As Integer = 10
Private columnWidth As Integer = 100
Private columnCount As Integer = 10
Private rowSelectorWidth As Integer = 24
Private Sub Panel1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Panel1.Paint
'Start drawing lines and continue adding your own styles
For i As Integer = 0 To rowCount + 1
e.Graphics.DrawLine(Pens.Black, rowSelectorWidth, rowHeight * i, rowSelectorWidth + (columnCount * columnWidth), rowHeight * i)
Next
For j As Integer = 0 To columnCount
e.Graphics.DrawLine(Pens.Black, rowSelectorWidth + (j * columnWidth), 0, rowSelectorWidth + (j * columnWidth), (rowCount + 1) * rowHeight)
Next
'Draw the column headers
For i As Integer = 0 To columnCount - 1
ControlPaint.DrawButton(e.Graphics, New Rectangle(rowSelectorWidth + (i * columnWidth), 0, columnWidth, rowHeight), ButtonState.Normal)
e.Graphics.DrawString(String.Format("col {0}", i + 1), Panel1.Font, Brushes.Black, rowSelectorWidth + (i * columnWidth), 2)
Next
'Draw the row selectors
For i As Integer = 0 To rowCount
ControlPaint.DrawButton(e.Graphics, New Rectangle(0, i * rowHeight, rowSelectorWidth, rowHeight), ButtonState.Normal)
If (Not i = 0) Then e.Graphics.DrawString(i.ToString(), Panel1.Font, Brushes.Black, 2, (rowHeight * i))
Next
'Draw the highlighted cell
'Calculate the position
Dim highlightPoint As Point = New Point(rowSelectorWidth + (highlightedColumn * columnWidth), (highlightedRow + 1) * rowHeight)
'Draw the highlighted cell backcolor
e.Graphics.FillRectangle(SystemBrushes.Highlight, New Rectangle(highlightPoint.X, highlightPoint.Y, columnWidth, rowHeight))
'Draw the cell border again.
e.Graphics.DrawRectangle(Pens.Black, New Rectangle(highlightPoint.X, highlightPoint.Y, columnWidth, rowHeight))
End Sub
End Class
* Looks like you were using a Panel, so it would be:
Panel1.Refresh()
Panel1.Refresh()
ASKER
I am trying to print randomly the boxes, as white or blue in the grid that I have created, and for some reason they are always printing a blue line diagonally, can anybody help me out here please?
I had in mind that a random boolean was created, and then a cell could be printed as a white or a blue cell at random. all the cells in the grid are to be held like this.
can anybody show me were my code is wrong?
thanks
For o As Integer = 0 To columnCount
Dim point As Point = New Point(rowSelectorWidth + (o * columnWidth), (o + 1) * columnWidth)
Dim randomBoolean As Boolean
Dim randomDraw As New Random
randomBoolean = randomDraw.Next
If randomBoolean = True Then
e.Graphics.FillRectangle(B rushes.Whi te, New Rectangle(point.X, point.Y, columnWidth, rowHeight))
Else
e.Graphics.FillRectangle(B rushes.Blu e, New Rectangle(point.X, point.Y, columnWidth, rowHeight))
End If
Next
For i As Integer = 0 To rowCount
Dim point As Point = New Point(rowSelectorWidth + (i * me.rowHeight), (i + 1) * rowHeight)
Dim randomBoolean As Boolean
Dim randomDraw As New Random
randomBoolean = randomDraw.Next
If randomBoolean = True Then
e.Graphics.FillRectangle(B rushes.Blu e, New Rectangle(point.X, point.Y, rowHeight, rowHeight))
End If
Next
I had in mind that a random boolean was created, and then a cell could be printed as a white or a blue cell at random. all the cells in the grid are to be held like this.
can anybody show me were my code is wrong?
thanks
For o As Integer = 0 To columnCount
Dim point As Point = New Point(rowSelectorWidth + (o * columnWidth), (o + 1) * columnWidth)
Dim randomBoolean As Boolean
Dim randomDraw As New Random
randomBoolean = randomDraw.Next
If randomBoolean = True Then
e.Graphics.FillRectangle(B
Else
e.Graphics.FillRectangle(B
End If
Next
For i As Integer = 0 To rowCount
Dim point As Point = New Point(rowSelectorWidth + (i * me.rowHeight), (i + 1) * rowHeight)
Dim randomBoolean As Boolean
Dim randomDraw As New Random
randomBoolean = randomDraw.Next
If randomBoolean = True Then
e.Graphics.FillRectangle(B
End If
Next
Private Sub Panel1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Panel1.Paint
'Start drawing lines and continue adding your own styles
For i As Integer = 0 To rowCount + 1
e.Graphics.DrawLine(Pens.Black, rowSelectorWidth, rowHeight * i, rowSelectorWidth + (columnCount * columnWidth), rowHeight * i)
Next
For j As Integer = 0 To columnCount
e.Graphics.DrawLine(Pens.Black, rowSelectorWidth + (j * columnWidth), 0, rowSelectorWidth + (j * columnWidth), (rowCount + 1) * rowHeight)
Next
'Draw the highlighted cell
'Calculate the position
Dim RandomClass As New Random()
Dim RandomNumber1 As Integer
RandomNumber1 = RandomClass.Next(columnCount)
Dim RandomClass2 As New Random()
Dim RandomNumber2 As Integer
RandomNumber2 = RandomClass2.Next(rowCount)
highlightedColumn = RandomNumber1
highlightedRow = RandomNumber2
Dim highlightPoint As Point = New Point(rowSelectorWidth + (highlightedColumn * columnWidth), (highlightedRow + 1) * rowHeight)
'Draw the highlighted cell backcolor
e.Graphics.FillRectangle(Brushes.Green, New Rectangle(highlightPoint.X, highlightPoint.Y, columnWidth, rowHeight))
'Draw the cell border again.
e.Graphics.DrawRectangle(Pens.Black, New Rectangle(highlightPoint.X, highlightPoint.Y, columnWidth, rowHeight))
For o As Integer = 0 To columnCount
Dim point As Point = New Point(rowSelectorWidth + (o * columnWidth), (o + 1) * columnWidth)
Dim randomBoolean As Boolean
Dim randomDraw As New Random
randomBoolean = randomDraw.Next
If randomBoolean = True Then
e.Graphics.FillRectangle(Brushes.White, New Rectangle(point.X, point.Y, columnWidth, rowHeight))
Else
e.Graphics.FillRectangle(Brushes.Blue, New Rectangle(point.X, point.Y, columnWidth, rowHeight))
End If
Next
For i As Integer = 0 To rowCount
Dim point As Point = New Point(rowSelectorWidth + (i * me.rowHeight), (i + 1) * rowHeight)
Dim randomBoolean As Boolean
Dim randomDraw As New Random
randomBoolean = randomDraw.Next
If randomBoolean = True Then
e.Graphics.FillRectangle(Brushes.Blue, New Rectangle(point.X, point.Y, rowHeight, rowHeight))
End If
Next
End Sub
Try this one out...
Public Class Form1
Private highlightedColumn As Integer = 4
Private highlightedRow As Integer = 3
Private rowHeight As Integer = 25
Private rowCount As Integer = 10
Private columnWidth As Integer = 25
Private columnCount As Integer = 10
Private rowSelectorWidth As Integer = 24
Private randomDraw As New Random
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Panel1.Refresh()
End Sub
Private Sub Panel1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Panel1.Paint
' draw the blocks
For c As Integer = 0 To columnCount
For r As Integer = 0 To rowCount
Dim point As Point = New Point(rowSelectorWidth + (c * columnWidth), r * rowHeight)
If randomDraw.NextDouble < 0.5 Then
e.Graphics.FillRectangle(Brushes.White, New Rectangle(point.X, point.Y, columnWidth, rowHeight))
Else
e.Graphics.FillRectangle(Brushes.Blue, New Rectangle(point.X, point.Y, columnWidth, rowHeight))
End If
Next
Next
' draw the random highlighted block
highlightedColumn = randomDraw.Next(columnCount)
highlightedRow = randomDraw.Next(rowCount)
Dim highlightPoint As Point = New Point(rowSelectorWidth + (highlightedColumn * columnWidth), (highlightedRow + 1) * rowHeight)
'Draw the highlighted cell backcolor
e.Graphics.FillRectangle(Brushes.Green, New Rectangle(highlightPoint.X, highlightPoint.Y, columnWidth, rowHeight))
'Draw the cell border again.
e.Graphics.DrawRectangle(Pens.Black, New Rectangle(highlightPoint.X, highlightPoint.Y, columnWidth, rowHeight))
' draw the grid lines
For i As Integer = 0 To rowCount + 1
e.Graphics.DrawLine(Pens.Black, rowSelectorWidth, rowHeight * i, rowSelectorWidth + ((columnCount + 1) * columnWidth), rowHeight * i)
Next
For j As Integer = 0 To columnCount + 1
e.Graphics.DrawLine(Pens.Black, rowSelectorWidth + (j * columnWidth), 0, rowSelectorWidth + (j * columnWidth), (rowCount + 1) * rowHeight)
Next
End Sub
End Class
ASKER
Thanks!!
Now, I have a button. Which on the button event, I wish to make the highlighted cell move according to the commands that I give it.
Now, in the paint, I have the e, which I could get and fill its co ordinates by it.
But in the button click event, how can I obtain this from it?
Like when I enter F1, and press submit, I make the cell move forward one cell.
If you supply a code snippet, could you please comment, where did you increment the Y value? Or in other cases, the X value too?
thanks for your help, a life saver!
Now, I have a button. Which on the button event, I wish to make the highlighted cell move according to the commands that I give it.
Now, in the paint, I have the e, which I could get and fill its co ordinates by it.
But in the button click event, how can I obtain this from it?
Like when I enter F1, and press submit, I make the cell move forward one cell.
If you supply a code snippet, could you please comment, where did you increment the Y value? Or in other cases, the X value too?
thanks for your help, a life saver!
ASKER
Because the problem is, that when I press the button, and I want to move the cell, I have to some way or another re paint the grid.
which in this case, it refreshes it.
you can see in the button event what i actually had in mind to do, its actually testing and it did not work.
how can i make the cell in the grid move?
which in this case, it refreshes it.
you can see in the button event what i actually had in mind to do, its actually testing and it did not work.
how can i make the cell in the grid move?
Public Class LogoGame
Private highlightedColumn As Integer = 0
Private highlightedRow As Integer = 0
Private rowHeight As Integer = 0
Private rowCount As Integer = 0
Private columnWidth As Integer = 0
Private columnCount As Integer = 0
Private rowSelectorWidth As Integer = 20
Private randomDraw As New Random
Private Yincrement As Integer = Yincrement + highlightedColumn
Private Xincrement As Integer = Xincrement + highlightedRow
Private Sub Panel1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Panel1.Paint
' draw the blocks
For c As Integer = 0 To columnCount
For r As Integer = 0 To rowCount
Dim point As Point = New Point(rowSelectorWidth + (c * columnWidth), r * rowHeight)
If randomDraw.NextDouble < 0.5 Then
e.Graphics.FillRectangle(Brushes.White, New Rectangle(point.X, point.Y, columnWidth, rowHeight))
Else
e.Graphics.FillRectangle(Brushes.Blue, New Rectangle(point.X, point.Y, columnWidth, rowHeight))
End If
Next
Next
' draw the random highlighted block
Yincrement = randomDraw.Next(columnCount)
Xincrement = randomDraw.Next(rowCount)
Dim highlightPoint As Point = New Point(rowSelectorWidth + (Yincrement * columnWidth), (Xincrement + 1) * rowHeight)
'Draw the highlighted cell backcolor
e.Graphics.FillRectangle(Brushes.Green, New Rectangle(highlightPoint.X, highlightPoint.Y, columnWidth, rowHeight))
'Draw the cell border again.
e.Graphics.DrawRectangle(Pens.Black, New Rectangle(highlightPoint.X, highlightPoint.Y, columnWidth, rowHeight))
' draw the grid lines
For i As Integer = 0 To rowCount + 1
e.Graphics.DrawLine(Pens.Black, rowSelectorWidth, rowHeight * i, rowSelectorWidth + ((columnCount + 1) * columnWidth), rowHeight * i)
Next
For j As Integer = 0 To columnCount + 1
e.Graphics.DrawLine(Pens.Black, rowSelectorWidth + (j * columnWidth), 0, rowSelectorWidth + (j * columnWidth), (rowCount + 1) * rowHeight)
Next
End Sub
Private Sub trkColumns_ValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles trkColumns.ValueChanged
Me.lblColumnCounter.Text = trkColumns.Value.ToString
End Sub
Private Sub trkRows_ValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles trkRows.ValueChanged
Me.lblRowCounter.Text = trkRows.Value.ToString
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
rowHeight = Me.trkColumnHeight.Value * 10
rowCount = Me.trkRows.Value
columnWidth = Me.trkColumnWidth.Value * 10
columnCount = Me.trkColumns.Value
Panel1.Refresh()
End Sub
Public Sub createGrid(ByVal panel As Panel, ByVal rowHeight As Integer, ByVal rowcount As Integer, _
ByVal columnWidth As Integer, ByVal columnCount As Integer)
End Sub
Private Sub trkColumnWidth_ValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles trkColumnWidth.ValueChanged
Me.lblColumnWidth.Text = (trkColumnWidth.Value * 10).ToString
End Sub
Private Sub trkColumnHeight_ValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles trkColumnHeight.ValueChanged
Me.lblColumnHeight.Text = (trkColumnHeight.Value * 10).ToString
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If txtAlgo.Text = "F1" Then
Yincrement = Yincrement + 1
Panel1.Refresh()
End If
End Sub
End Class
You don't pass the "e" anywhere...
In the button event, you change the variables you want and then tell the Panel to repaint itself with the new values.
Just like the snippet I showed before:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If highlightedRow > 0 Then
highlightedRow = highlightedRow - 1
Panel1.Refresh()
End If
End Sub
BUT...you need to remove the RANDOM part from the Paint() event so the highlighted cell just changed randomly anymore:
Private Sub Panel1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.Paint EventArgs) Handles Panel1.Paint
' draw the blocks
For c As Integer = 0 To columnCount
For r As Integer = 0 To rowCount
Dim point As Point = New Point(rowSelectorWidth + (c * columnWidth), r * rowHeight)
If randomDraw.NextDouble < 0.5 Then
e.Graphics.FillRectangle(B rushes.Whi te, New Rectangle(point.X, point.Y, columnWidth, rowHeight))
Else
e.Graphics.FillRectangle(B rushes.Blu e, New Rectangle(point.X, point.Y, columnWidth, rowHeight))
End If
Next
Next
' draw the random highlighted block
Dim highlightPoint As Point = New Point(rowSelectorWidth + (highlightedColumn * columnWidth), (highlightedRow + 1) * rowHeight)
'Draw the highlighted cell backcolor
e.Graphics.FillRectangle(B rushes.Gre en, New Rectangle(highlightPoint.X , highlightPoint.Y, columnWidth, rowHeight))
'Draw the cell border again.
e.Graphics.DrawRectangle(P ens.Black, New Rectangle(highlightPoint.X , highlightPoint.Y, columnWidth, rowHeight))
' draw the grid lines
For i As Integer = 0 To rowCount + 1
e.Graphics.DrawLine(Pens.B lack, rowSelectorWidth, rowHeight * i, rowSelectorWidth + ((columnCount + 1) * columnWidth), rowHeight * i)
Next
For j As Integer = 0 To columnCount + 1
e.Graphics.DrawLine(Pens.B lack, rowSelectorWidth + (j * columnWidth), 0, rowSelectorWidth + (j * columnWidth), (rowCount + 1) * rowHeight)
Next
End Sub
In the button event, you change the variables you want and then tell the Panel to repaint itself with the new values.
Just like the snippet I showed before:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If highlightedRow > 0 Then
highlightedRow = highlightedRow - 1
Panel1.Refresh()
End If
End Sub
BUT...you need to remove the RANDOM part from the Paint() event so the highlighted cell just changed randomly anymore:
Private Sub Panel1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.Paint
' draw the blocks
For c As Integer = 0 To columnCount
For r As Integer = 0 To rowCount
Dim point As Point = New Point(rowSelectorWidth + (c * columnWidth), r * rowHeight)
If randomDraw.NextDouble < 0.5 Then
e.Graphics.FillRectangle(B
Else
e.Graphics.FillRectangle(B
End If
Next
Next
' draw the random highlighted block
Dim highlightPoint As Point = New Point(rowSelectorWidth + (highlightedColumn * columnWidth), (highlightedRow + 1) * rowHeight)
'Draw the highlighted cell backcolor
e.Graphics.FillRectangle(B
'Draw the cell border again.
e.Graphics.DrawRectangle(P
' draw the grid lines
For i As Integer = 0 To rowCount + 1
e.Graphics.DrawLine(Pens.B
Next
For j As Integer = 0 To columnCount + 1
e.Graphics.DrawLine(Pens.B
Next
End Sub
Play with this: (press the up/down/left/right arrow keys and see what happens)
Public Class Form1
Private highlightedColumn As Integer = 4
Private highlightedRow As Integer = 3
Private rowHeight As Integer = 25
Private rowCount As Integer = 10
Private columnWidth As Integer = 25
Private columnCount As Integer = 10
Private rowSelectorWidth As Integer = 24
Protected Overrides Function ProcessCmdKey(ByRef msg As System.Windows.Forms.Message, ByVal keyData As System.Windows.Forms.Keys) As Boolean
Select Case keyData
Case Keys.Up
If highlightedRow > 0 Then
highlightedRow = highlightedRow - 1
Panel1.Refresh()
End If
Case Keys.Down
If highlightedRow < columnCount Then
highlightedRow = highlightedRow + 1
Panel1.Refresh()
End If
Case Keys.Left
If highlightedColumn > 0 Then
highlightedColumn = highlightedColumn - 1
Panel1.Refresh()
End If
Case Keys.Right
If highlightedColumn < columnCount Then
highlightedColumn = highlightedColumn + 1
Panel1.Refresh()
End If
End Select
Return MyBase.ProcessCmdKey(msg, keyData)
End Function
Private Sub Panel1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Panel1.Paint
' draw the highlighted block
Dim highlightPoint As Point = New Point(rowSelectorWidth + (highlightedColumn * columnWidth), highlightedRow * rowHeight)
'Draw the highlighted cell backcolor
e.Graphics.FillRectangle(Brushes.Green, New Rectangle(highlightPoint.X, highlightPoint.Y, columnWidth, rowHeight))
'Draw the cell border again.
e.Graphics.DrawRectangle(Pens.Black, New Rectangle(highlightPoint.X, highlightPoint.Y, columnWidth, rowHeight))
' draw the grid lines
For i As Integer = 0 To rowCount + 1
e.Graphics.DrawLine(Pens.Black, rowSelectorWidth, rowHeight * i, rowSelectorWidth + ((columnCount + 1) * columnWidth), rowHeight * i)
Next
For j As Integer = 0 To columnCount + 1
e.Graphics.DrawLine(Pens.Black, rowSelectorWidth + (j * columnWidth), 0, rowSelectorWidth + (j * columnWidth), (rowCount + 1) * rowHeight)
Next
End Sub
End Class
ASKER
thanks for your help, you are being very generous
but how can i make it only generate a starting point at random, if it is only being loaded the first time then?
but how can i make it only generate a starting point at random, if it is only being loaded the first time then?
ASKER
but i still need to make it generate random blocks.
see, the scope of this game is that i make a destination point, and a starting point.
white blocks are blocks which i can pass through them, blue blocks are walls.
now to make the cell move, i need to insert algorithms, like F1, R2, L3, which are logo and turtle algorithms.
so the starting point has to be randomly generated, and the blue and white blocks have to be there to make it work.
any ideas? you seem very keen on GDI, do you use it at lot at work or something similar to that?
Regards
see, the scope of this game is that i make a destination point, and a starting point.
white blocks are blocks which i can pass through them, blue blocks are walls.
now to make the cell move, i need to insert algorithms, like F1, R2, L3, which are logo and turtle algorithms.
so the starting point has to be randomly generated, and the blue and white blocks have to be there to make it work.
any ideas? you seem very keen on GDI, do you use it at lot at work or something similar to that?
Regards
ASKER
couldnt I make a global variable - randomState as integer for example
and i declare it as null, and then in the page load i give it a value in the panel1_paint, like 1 or true if i decide to make it boolean.
then, in the panel1_paint, i make an if statement, if the variable is 0 then it means it is the first time being loaded, so randomly create them, if not, meaning it is 1, so it has already been randomly generated, so just change the co ordinates.
do you think this makes sense?
and i declare it as null, and then in the page load i give it a value in the panel1_paint, like 1 or true if i decide to make it boolean.
then, in the panel1_paint, i make an if statement, if the variable is 0 then it means it is the first time being loaded, so randomly create them, if not, meaning it is 1, so it has already been randomly generated, so just change the co ordinates.
do you think this makes sense?
ASKER
any tips on how to move the cell around, with the actual blue and white cells not changing anybody?
i cant seem to find a way out of this either :s
i cant seem to find a way out of this either :s
I understand that you want alot more out of it...just wanted to show a clean example of how to allow the user to move something around in the grid. Think you were trying to do too much at once... =)
So, just as we moved the highlightedColumn/highligh tedRow variables, out of the Paint() event, we also need to move the blue/white blocks out. The "state" of each block can be stored in a 2D array for example.
(...working...)
So, just as we moved the highlightedColumn/highligh
(...working...)
Play with this intermediate step. This does NOT prevent movement of the highlighted block...YET. =)
(...have to go cook some breakfast for the family...)
Start with a BLANK form:
(...have to go cook some breakfast for the family...)
Start with a BLANK form:
Public Class Form1
Private WithEvents gb As GameBoard
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
gb = New GameBoard
Dim rc As Rectangle = Me.ClientRectangle
rc.Inflate(-10, -10)
gb.Location = rc.Location
gb.Size = rc.Size
gb.Anchor = AnchorStyles.Left Or AnchorStyles.Top Or AnchorStyles.Right Or AnchorStyles.Bottom
gb.NewMap()
Me.Controls.Add(gb)
Me.WindowState = FormWindowState.Maximized
End Sub
Protected Overrides Function ProcessCmdKey(ByRef msg As System.Windows.Forms.Message, ByVal keyData As System.Windows.Forms.Keys) As Boolean
Select Case keyData
Case Keys.Up
If gb.highlightedRow > 0 Then
gb.highlightedRow = gb.highlightedRow - 1
gb.Refresh()
End If
Case Keys.Down
If gb.highlightedRow < gb.rowCount - 1 Then
gb.highlightedRow = gb.highlightedRow + 1
gb.Refresh()
End If
Case Keys.Left
If gb.highlightedColumn > 0 Then
gb.highlightedColumn = gb.highlightedColumn - 1
gb.Refresh()
End If
Case Keys.Right
If gb.highlightedColumn < gb.columnCount - 1 Then
gb.highlightedColumn = gb.highlightedColumn + 1
gb.Refresh()
End If
End Select
Return MyBase.ProcessCmdKey(msg, keyData)
End Function
Private Class GameBoard
Inherits PictureBox
Public Sub New()
' ..this gets rid of the flicker when we call Refresh()...
Me.SetStyle(ControlStyles.OptimizedDoubleBuffer, True)
Me.SetStyle(ControlStyles.AllPaintingInWmPaint, True)
End Sub
Public highlightedColumn As Integer = 4
Public highlightedRow As Integer = 3
Public rowHeight As Integer = 25
Public rowCount As Integer = 25
Public columnWidth As Integer = 25
Public columnCount As Integer = 25
Public LeftMargin As Integer = 20
Public Grid(,) As Boolean
Public Sub NewMap()
ReDim Grid(rowCount - 1, columnCount - 1)
Dim R As New Random
For row As Integer = 0 To rowCount - 1
For col As Integer = 0 To columnCount - 1
Grid(row, col) = (R.NextDouble < 0.5)
Next
Next
Me.Refresh()
End Sub
Private Sub GameBoard_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
' draw the blocks
Dim rc As Rectangle
For row As Integer = 0 To rowCount - 1
For col As Integer = 0 To columnCount - 1
rc = New Rectangle(New Point(LeftMargin + (col * columnWidth), row * rowHeight), New Size(columnWidth, rowHeight))
e.Graphics.FillRectangle(IIf(Grid(row, col), Brushes.White, Brushes.Blue), rc)
Next
Next
' draw the highlighted block
rc = New Rectangle(New Point(LeftMargin + (highlightedColumn * columnWidth), highlightedRow * rowHeight), New Size(columnWidth, rowHeight))
e.Graphics.FillRectangle(Brushes.Green, rc)
' draw the grid lines
For r As Integer = 0 To rowCount
e.Graphics.DrawLine(Pens.Black, LeftMargin, rowHeight * r, LeftMargin + (columnCount * columnWidth), rowHeight * r)
Next
For c As Integer = 0 To columnCount
e.Graphics.DrawLine(Pens.Black, LeftMargin + (c * columnWidth), 0, LeftMargin + (c * columnWidth), rowCount * rowHeight)
Next
End Sub
End Class
End Class
ASKER
Hi,
I am passing values through properties, as I made the gameBoard a separate class, to make things more clearer and easier to manipulate.
now, the values are passing through correctly, but I think the properties are not working properly, I seem to be missing something.
the value passes correctly, but then the property remains without value, meaning 0! So the board does not generate, I am attaching the class, and button click event.
thanks for your generous help!!!!!!!!! A++!
I am passing values through properties, as I made the gameBoard a separate class, to make things more clearer and easier to manipulate.
now, the values are passing through correctly, but I think the properties are not working properly, I seem to be missing something.
the value passes correctly, but then the property remains without value, meaning 0! So the board does not generate, I am attaching the class, and button click event.
thanks for your generous help!!!!!!!!! A++!
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim gb As New GameBoard
gb.rowHeight = Me.trkColumnHeight.Value * 10
gb.rowcount = Me.trkRows.Value
gb.columnWidth = Me.trkColumnWidth.Value * 10
gb.columnCount = Me.trkColumns.Value
gb.leftmargin = 25
Dim rc As Rectangle = Me.ClientRectangle
rc.Inflate(-10, -10)
gb.Location = rc.Location
gb.Size = rc.Size
gb.Anchor = AnchorStyles.Left Or AnchorStyles.Top Or AnchorStyles.Right Or AnchorStyles.Bottom
gb.NewMap()
Me.Controls.Add(gb)
Me.WindowState = FormWindowState.Maximized
End Sub
Public Class GameBoard
Inherits PictureBox
Public Sub New()
' ..this gets rid of the flicker when we call Refresh()...
Me.SetStyle(ControlStyles.OptimizedDoubleBuffer, True)
Me.SetStyle(ControlStyles.AllPaintingInWmPaint, True)
End Sub
Public _highlightedColumn As Integer
Public _highlightedRow As Integer
Public _rowHeight As Integer
Public _rowCount As Integer
Public _columnWidth As Integer
Public _columnCount As Integer
Public _LeftMargin As Integer
Public Property highlightedrow() As Integer
Get
Return _highlightedRow
End Get
Set(ByVal value As Integer)
value = _highlightedRow
End Set
End Property
Public Property highlightedcolumn() As Integer
Get
Return _highlightedColumn
End Get
Set(ByVal value As Integer)
value = _highlightedColumn
End Set
End Property
Public Property columnWidth() As Integer
Get
Return _columnWidth
End Get
Set(ByVal value As Integer)
_columnWidth = value
End Set
End Property
Public Property rowHeight() As Integer
Get
Return _rowHeight
End Get
Set(ByVal value As Integer)
value = _rowHeight
End Set
End Property
Public Property rowcount() As Integer
Get
Return _rowCount
End Get
Set(ByVal value As Integer)
value = _rowCount
End Set
End Property
Public Property columnCount() As Integer
Get
Return _columnCount
End Get
Set(ByVal value As Integer)
value = _columnCount
End Set
End Property
Public Property leftmargin() As Integer
Get
Return _LeftMargin
End Get
Set(ByVal value As Integer)
value = _LeftMargin
End Set
End Property
Public Grid(,) As Boolean
Public Sub NewMap()
ReDim Grid(rowCount - 1, columnCount - 1)
Dim R As New Random
For row As Integer = 0 To rowCount - 1
For col As Integer = 0 To columnCount - 1
Grid(row, col) = (R.NextDouble < 0.5)
Next
Next
Me.Refresh()
End Sub
Public Sub GameBoard_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
' draw the blocks
Dim rc As Rectangle
For row As Integer = 0 To rowCount - 1
For col As Integer = 0 To columnCount - 1
rc = New Rectangle(New Point(LeftMargin + (col * columnWidth), row * rowHeight), New Size(columnWidth, rowHeight))
e.Graphics.FillRectangle(IIf(Grid(row, col), Brushes.White, Brushes.Blue), rc)
Next
Next
' draw the highlighted block
rc = New Rectangle(New Point(LeftMargin + (highlightedColumn * columnWidth), highlightedRow * rowHeight), New Size(columnWidth, rowHeight))
e.Graphics.FillRectangle(Brushes.Green, rc)
' draw the grid lines
For r As Integer = 0 To rowCount
e.Graphics.DrawLine(Pens.Black, LeftMargin, rowHeight * r, LeftMargin + (columnCount * columnWidth), rowHeight * r)
Next
For c As Integer = 0 To columnCount
e.Graphics.DrawLine(Pens.Black, LeftMargin + (c * columnWidth), 0, LeftMargin + (c * columnWidth), rowCount * rowHeight)
Next
End Sub
End Class
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Wow!
let me study what you actually did here - it looks good though, very good!
thanks :-)
do you anything about logos? Those algorithms example like
F1R2L3
Forward 1 cell, 90 degress to the right, and 135 degrees to the left?
about the blue cells, it is meant to be actually that if the user hits one of them, he looses, and then he starts again at the same starting point he started with. I will see how to manipulate that :-)
thanks for your great generousity! I
let me study what you actually did here - it looks good though, very good!
thanks :-)
do you anything about logos? Those algorithms example like
F1R2L3
Forward 1 cell, 90 degress to the right, and 135 degrees to the left?
about the blue cells, it is meant to be actually that if the user hits one of them, he looses, and then he starts again at the same starting point he started with. I will see how to manipulate that :-)
thanks for your great generousity! I
So you can move in 8 directions? (up/down/left/right and diagonals) Each left or right turn would just adjust your current direction by 45 degrees? You would have to introduce the concept of current direction as well as current location. It would be simple enough to draw the current cell with an arrow showing the currently facing direction.
Can you only use those three commands?
F = Forwad
R = Turn Right
L = Turn Left
To make it easier on yourself, I would require the commands be seperated by a space like this:
F1 R2 L3 F4
Then it will be easier to parse.
Is the idea that the user has to give only ONE long command string to reach the destination?...or can they continue from where ever they left off as long as they haven't run into a wall? (series of smaller command strings until failure)
Can you only use those three commands?
F = Forwad
R = Turn Right
L = Turn Left
To make it easier on yourself, I would require the commands be seperated by a space like this:
F1 R2 L3 F4
Then it will be easier to parse.
Is the idea that the user has to give only ONE long command string to reach the destination?...or can they continue from where ever they left off as long as they haven't run into a wall? (series of smaller command strings until failure)
ASKER
Hi again!
I added the properties below to your class, since I want this to be generated from a button click, and the user would be able to choose how hard he wants the game to be,
here i am referring to, if you have more columns and rows, it is going to be more hard
so i am adding a couple of trackers on the form, 4 actually for coloumn count, row count, height and width of the rows and coloumns accordingly.
now in the button event, i am passing the property values.
it is not working :-(
And if i give these both random values,
Public curCol As Integer = 4
Public curRow As Integer = 3
would it remain ok? or these random numbers have to be in the range of (columns x rows) ?
I added the properties below to your class, since I want this to be generated from a button click, and the user would be able to choose how hard he wants the game to be,
here i am referring to, if you have more columns and rows, it is going to be more hard
so i am adding a couple of trackers on the form, 4 actually for coloumn count, row count, height and width of the rows and coloumns accordingly.
now in the button event, i am passing the property values.
it is not working :-(
And if i give these both random values,
Public curCol As Integer = 4
Public curRow As Integer = 3
would it remain ok? or these random numbers have to be in the range of (columns x rows) ?
IN THE CLASS
Public curCol As Integer = 4
Public curRow As Integer = 3
Public destCol As Integer
Public destRow As Integer
Public _rowHeight As Integer
Public _rowCount As Integer
Public _columnWidth As Integer
Public _columnCount As Integer
Public _LeftMargin As Integer
Public Property columnWidth() As Integer
Get
Return _columnWidth
End Get
Set(ByVal value As Integer)
_columnWidth = value
End Set
End Property
Public Property rowHeight() As Integer
Get
Return _rowHeight
End Get
Set(ByVal value As Integer)
_rowHeight = value
End Set
End Property
Public Property rowcount() As Integer
Get
Return _rowCount
End Get
Set(ByVal value As Integer)
_rowCount = value
End Set
End Property
Public Property columnCount() As Integer
Get
Return _columnCount
End Get
Set(ByVal value As Integer)
_columnCount = value
End Set
End Property
Public Property leftmargin() As Integer
Get
Return _LeftMargin
End Get
Set(ByVal value As Integer)
_LeftMargin = value
End Set
End Property
IN THE BUTTON CLICK EVENT
gb.rowHeight = Me.trkColumnHeight.Value * 10
gb.rowcount = Me.trkRows.Value
gb.columnWidth = Me.trkColumnWidth.Value * 10
gb.columnCount = Me.trkColumns.Value
gb.leftmargin = 25
gb.NewMap()
gb.Refresh()
Me.Panel1.Controls.Add(gb)
Me.WindowState = FormWindowState.Maximized
ASKER
no, the user will have to insert one command to reach the destination only. if he hits a wall, he will loose and restart the game.
and yes i may only use the
F, R and L
And yes I guess that arrow showing the direction would be useful, I actually had that in mind and was going to do some type of research on it.
thanks for your help! :-)
and yes i may only use the
F, R and L
And yes I guess that arrow showing the direction would be useful, I actually had that in mind and was going to do some type of research on it.
thanks for your help! :-)
ASKER
do you work with GDI + by any change? You seem to be familiar with GDI a lot!!!!
I don't actually "work" with anything these days...haven't coded professionally (translate that as "I haven't been PAID to code") since 2002...and the company I worked for back then was still using VB6. I'm a self-taught programmer so essentially all of my experience with VB.Net has been from personal projects and helping people here on EE. =)
With regard to your problem of the board not reflecting changes in size...it's working fine for me. =\
Show me your complete code and I'm sure we can spot the problem.
Do you want more help implementing changes (directions/arrows/etc) or do you want to work on it yourself. I can spit code out till I fall asleep at the keyboard but I'm sure my vision isn't the same as yours... ;)
With regard to your problem of the board not reflecting changes in size...it's working fine for me. =\
Show me your complete code and I'm sure we can spot the problem.
Do you want more help implementing changes (directions/arrows/etc) or do you want to work on it yourself. I can spit code out till I fall asleep at the keyboard but I'm sure my vision isn't the same as yours... ;)
ASKER
Hi, about the algorithm thing, if you know how to implement that, I would sure be happy as I have no idea yet, how to do that! If you need more information about it, I could give you as many as you want.
the user has to input an algorithm, in a logo format, like R1L3F1, and then I have to parse it I guess, but that I could do myself, all I need to know how to do is how to make the highlighted cell, move slowly so that the user could see his actions being done in the algorithm.
and when executing the algorithm, and arrow changes by each move in it, like example when executing R3, the arrow first moves 45 degrees, then another 45 degrees, and then another 45 degrees.
I am going to paste my full code here, about that problem I told you before.
in the form i have 4 trackers, a textbox to input the algo, and two buttons which actually do the same thing, (one button does your code, and the other button passes properties of the tracker values)
thanks for your help!
the user has to input an algorithm, in a logo format, like R1L3F1, and then I have to parse it I guess, but that I could do myself, all I need to know how to do is how to make the highlighted cell, move slowly so that the user could see his actions being done in the algorithm.
and when executing the algorithm, and arrow changes by each move in it, like example when executing R3, the arrow first moves 45 degrees, then another 45 degrees, and then another 45 degrees.
I am going to paste my full code here, about that problem I told you before.
in the form i have 4 trackers, a textbox to input the algo, and two buttons which actually do the same thing, (one button does your code, and the other button passes properties of the tracker values)
thanks for your help!
Public Class Form1
Private WithEvents gb As GameBoard
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
gb.NewMap()
gb.Refresh()
End Sub
Protected Overrides Function ProcessCmdKey(ByRef msg As System.Windows.Forms.Message, ByVal keyData As System.Windows.Forms.Keys) As Boolean
Select Case keyData
Case Keys.Up
gb.MoveUp()
Case Keys.Down
gb.MoveDown()
Case Keys.Left
gb.MoveLeft()
Case Keys.Right
gb.MoveRight()
End Select
Return MyBase.ProcessCmdKey(msg, keyData)
End Function
Private Sub gb_DestinationReached(ByVal sender As GameBoard) Handles gb.DestinationReached
MessageBox.Show("Good job!", "Destination Reached", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
End Sub
Private Class GameBoard
Inherits PictureBox
Public Enum CellType
None
Path
PathB
Wall
End Enum
Public Event DestinationReached(ByVal sender As GameBoard)
Public Sub New()
' ..this gets rid of the flicker when we call Refresh()...
Me.SetStyle(ControlStyles.OptimizedDoubleBuffer, True)
Me.SetStyle(ControlStyles.AllPaintingInWmPaint, True)
Me.Dock = DockStyle.Fill
Me.NewMap()
End Sub
Public curCol As Integer = 4
Public curRow As Integer = 3
Public destCol As Integer
Public destRow As Integer
Public _rowHeight As Integer
Public _rowCount As Integer
Public _columnWidth As Integer
Public _columnCount As Integer
Public _LeftMargin As Integer
Public Property columnWidth() As Integer
Get
Return _columnWidth
End Get
Set(ByVal value As Integer)
_columnWidth = value
End Set
End Property
Public Property rowHeight() As Integer
Get
Return _rowHeight
End Get
Set(ByVal value As Integer)
_rowHeight = value
End Set
End Property
Public Property rowcount() As Integer
Get
Return _rowCount
End Get
Set(ByVal value As Integer)
_rowCount = value
End Set
End Property
Public Property columnCount() As Integer
Get
Return _columnCount
End Get
Set(ByVal value As Integer)
_columnCount = value
End Set
End Property
Public Property leftmargin() As Integer
Get
Return _LeftMargin
End Get
Set(ByVal value As Integer)
_LeftMargin = value
End Set
End Property
Public Grid(,) As GameBoard.CellType
Public Sub NewMap()
Dim R As New Random
ReDim Grid(rowCount - 1, columnCount - 1)
' start with ALL walls
For row As Integer = 0 To rowCount - 1
For col As Integer = 0 To columnCount - 1
Grid(row, col) = CellType.Wall
Next
Next
' pick starting and destination cells
curCol = R.Next(0, columnCount)
curRow = R.Next(0, rowCount)
Grid(curRow, curCol) = CellType.Path
Do
destCol = R.Next(0, columnCount)
destRow = R.Next(0, rowCount)
Loop While destCol = curCol AndAlso destRow = curRow
Grid(destRow, destCol) = CellType.PathB
' make sure there is a path from start to finish
' use the "water" method to flow out from both start and finish
' at the same time until they meet
Dim tmpCurCol As Integer = curCol
Dim tmpCurRow As Integer = curRow
Dim tmpDestCol As Integer = destCol
Dim tmpDestRow As Integer = destRow
Dim dir As Integer
Dim connected As Boolean = False
While Not connected
dir = R.Next(0, 4)
Select Case dir
Case 0 ' left
If tmpCurCol > 0 Then
tmpCurCol = tmpCurCol - 1
If Grid(tmpCurRow, tmpCurCol) = CellType.PathB Then
connected = True
Else
Grid(tmpCurRow, tmpCurCol) = CellType.Path
End If
End If
Case 1 ' right
If tmpCurCol < columnCount - 1 Then
tmpCurCol = tmpCurCol + 1
If Grid(tmpCurRow, tmpCurCol) = CellType.PathB Then
connected = True
Else
Grid(tmpCurRow, tmpCurCol) = CellType.Path
End If
End If
Case 2 ' up
If tmpCurRow > 0 Then
tmpCurRow = tmpCurRow - 1
If Grid(tmpCurRow, tmpCurCol) = CellType.PathB Then
connected = True
Else
Grid(tmpCurRow, tmpCurCol) = CellType.Path
End If
End If
Case 3 ' down
If tmpCurRow < rowCount - 1 Then
tmpCurRow = tmpCurRow + 1
If Grid(tmpCurRow, tmpCurCol) = CellType.PathB Then
connected = True
Else
Grid(tmpCurRow, tmpCurCol) = CellType.Path
End If
End If
End Select
If Not connected Then
dir = R.Next(0, 4)
Select Case dir
Case 0 ' left
If tmpDestCol > 0 Then
tmpDestCol = tmpDestCol - 1
If Grid(tmpDestRow, tmpDestCol) = CellType.Path Then
connected = True
Else
Grid(tmpDestRow, tmpDestCol) = CellType.PathB
End If
End If
Case 1 ' right
If tmpDestCol < columnCount - 1 Then
tmpDestCol = tmpDestCol + 1
If Grid(tmpDestRow, tmpDestCol) = CellType.Path Then
connected = True
Else
Grid(tmpDestRow, tmpDestCol) = CellType.PathB
End If
End If
Case 2 ' up
If tmpDestRow > 0 Then
tmpDestRow = tmpDestRow - 1
If Grid(tmpDestRow, tmpDestCol) = CellType.Path Then
connected = True
Else
Grid(tmpDestRow, tmpDestCol) = CellType.PathB
End If
End If
Case 3 ' down
If tmpDestRow < rowCount - 1 Then
tmpDestRow = tmpDestRow + 1
If Grid(tmpDestRow, tmpDestCol) = CellType.Path Then
connected = True
Else
Grid(tmpDestRow, tmpDestCol) = CellType.PathB
End If
End If
End Select
End If
End While
' add random paths
For row As Integer = 0 To rowCount - 1
For col As Integer = 0 To columnCount - 1
If Grid(row, col) = CellType.Wall Then
If R.NextDouble < 0.5 Then
Grid(row, col) = CellType.Path
End If
End If
Next
Next
End Sub
Private Sub GameBoard_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
' draw the blocks
Dim rc As Rectangle
For row As Integer = 0 To rowCount - 1
For col As Integer = 0 To columnCount - 1
rc = New Rectangle(New Point(LeftMargin + (col * columnWidth), row * rowHeight), New Size(columnWidth, rowHeight))
e.Graphics.FillRectangle(IIf(Grid(row, col) = CellType.Wall, Brushes.Blue, Brushes.White), rc)
Next
Next
' draw the destination block
rc = New Rectangle(New Point(LeftMargin + (destCol * columnWidth), destRow * rowHeight), New Size(columnWidth, rowHeight))
e.Graphics.FillRectangle(Brushes.Red, rc)
' draw the highlighted block
rc = New Rectangle(New Point(LeftMargin + (curCol * columnWidth), curRow * rowHeight), New Size(columnWidth, rowHeight))
e.Graphics.FillRectangle(Brushes.Green, rc)
' draw the grid lines
For r As Integer = 0 To rowCount
e.Graphics.DrawLine(Pens.Black, LeftMargin, rowHeight * r, LeftMargin + (columnCount * columnWidth), rowHeight * r)
Next
For c As Integer = 0 To columnCount
e.Graphics.DrawLine(Pens.Black, LeftMargin + (c * columnWidth), 0, LeftMargin + (c * columnWidth), rowCount * rowHeight)
Next
End Sub
Public Function MoveUp() As Boolean
If Me.curRow > 0 Then
If Me.Grid(Me.curRow - 1, Me.curCol) <> GameBoard.CellType.Wall Then
Me.curRow = Me.curRow - 1
Me.Refresh()
If Me.curRow = Me.destRow AndAlso Me.curCol = Me.destCol Then
RaiseEvent DestinationReached(Me)
End If
Return True
Else
MessageBox.Show("You have lost!", "Please wait till game restarts!", MessageBoxButtons.OK)
Dim gb As New GameBoard
gb.NewMap()
gb.Refresh()
End If
End If
Return False
End Function
Public Function MoveDown() As Boolean
If Me.curRow < Me.rowCount - 1 Then
If Me.Grid(Me.curRow + 1, Me.curCol) <> GameBoard.CellType.Wall Then
Me.curRow = Me.curRow + 1
Me.Refresh()
If Me.curRow = Me.destRow AndAlso Me.curCol = Me.destCol Then
RaiseEvent DestinationReached(Me)
End If
Return True
Else
MessageBox.Show("You have lost!", "Please wait till game restarts!", MessageBoxButtons.OK)
Dim gb As New GameBoard
gb.NewMap()
gb.Refresh()
End If
End If
Return False
End Function
Public Function MoveLeft() As Boolean
If Me.curCol > 0 Then
If Me.Grid(Me.curRow, Me.curCol - 1) <> GameBoard.CellType.Wall Then
Me.curCol = Me.curCol - 1
Me.Refresh()
If Me.curRow = Me.destRow AndAlso Me.curCol = Me.destCol Then
RaiseEvent DestinationReached(Me)
End If
Return True
Else
MessageBox.Show("You have lost!", "Please wait till game restarts!", MessageBoxButtons.OK)
Dim gb As New GameBoard
gb.NewMap()
gb.Refresh()
End If
End If
Return False
End Function
Public Function MoveRight() As Boolean
If Me.curCol < Me.columnCount - 1 Then
If Me.Grid(Me.curRow, Me.curCol + 1) <> GameBoard.CellType.Wall Then
Me.curCol = Me.curCol + 1
Me.Refresh()
If Me.curRow = Me.destRow AndAlso Me.curCol = Me.destCol Then
RaiseEvent DestinationReached(Me)
End If
Return True
Else
MessageBox.Show("You have lost!", "Please wait till game restarts!", MessageBoxButtons.OK)
Dim gb As New GameBoard
gb.NewMap()
gb.Refresh()
End If
End If
Return False
End Function
End Class
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
gb.rowHeight = Me.trkColumnHeight.Value * 10
gb.rowcount = Me.trkRows.Value
gb.columnWidth = Me.trkColumnWidth.Value * 10
gb.columnCount = Me.trkColumns.Value
gb.leftmargin = 25
gb.NewMap()
gb.Refresh()
Me.Panel1.Controls.Add(gb)
Me.WindowState = FormWindowState.Maximized
End Sub
End Class
ASKER
any process? :-)
Good morning Mezillinu...hopefully I'll be able to play with this sometime today. If not, I'll definitely have time to work with it tomorrow morning.
Hi Mezillinu, I see that you were working hard on sunday with Idle Mind.
The panel1 paint event is good for learning because it is easier. But the best practice is to create a user control, a good software design should implement it, because you can override all the properties and methods to fit your design requirements.
It will be easier to make changes and add new features in the future and you will have a more organized your source code.
I think I have answered your main question, and you have the basics to start with GDI+ and user controls. You will have to practice a lot because GDI+ is beautifull and very extensive, and you can create amazing UI if you know how to use it correctly.
You will have to learn about double buffer, working with bitmap and other features.
Don't forget to split the points, and if you have more questions, you can check the KB that is full of GDI+ questions and answers, and if you don't find your answer, you can post a new question.
Sorry for not giving you a more complete example, but my work is very demanding and I don't have much time to write samples, but I will give you two usefull links:
The first one is a Microsoft Download where you can find sample vb projects with user controls:
http://msdn.microsoft.com/en-us/vbasic/ms789075.aspx
The second one was very usefull for me when I was learning GDI+ a few years ago:
http://www.bobpowell.net/beginnersgdi.htm
I hope that helps
The panel1 paint event is good for learning because it is easier. But the best practice is to create a user control, a good software design should implement it, because you can override all the properties and methods to fit your design requirements.
It will be easier to make changes and add new features in the future and you will have a more organized your source code.
I think I have answered your main question, and you have the basics to start with GDI+ and user controls. You will have to practice a lot because GDI+ is beautifull and very extensive, and you can create amazing UI if you know how to use it correctly.
You will have to learn about double buffer, working with bitmap and other features.
Don't forget to split the points, and if you have more questions, you can check the KB that is full of GDI+ questions and answers, and if you don't find your answer, you can post a new question.
Sorry for not giving you a more complete example, but my work is very demanding and I don't have much time to write samples, but I will give you two usefull links:
The first one is a Microsoft Download where you can find sample vb projects with user controls:
http://msdn.microsoft.com/en-us/vbasic/ms789075.aspx
The second one was very usefull for me when I was learning GDI+ a few years ago:
http://www.bobpowell.net/beginnersgdi.htm
I hope that helps
ASKER
thanks for your help :-)
idle mind - no problem - i am playing around with the samples you gave me last ;-)
idle mind - no problem - i am playing around with the samples you gave me last ;-)
I would use Regular Expressions to parse the command sequence:
http://www.regular-expressions.info/index.html
...and here is a quick example of RegExs in action:
http://www.regular-expressions.info/index.html
...and here is a quick example of RegExs in action:
Imports System.Text.RegularExpressions
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim sequence As String = "F10R999L3"
Dim value As Integer
For Each m As Match In Regex.Matches(sequence, "[fFrRlL]\d*")
If Integer.TryParse(m.ToString.Substring(1), value) Then
Select Case m.ToString.Substring(0, 1).ToUpper
Case "F"
Debug.Print("Forward " & value)
Case "R"
Debug.Print("Right " & value)
Case "L"
Debug.Print("Left " & value)
End Select
Else
MessageBox.Show(m.ToString, "Invalid Number", MessageBoxButtons.OK, MessageBoxIcon.Error)
End If
Next
End Sub
End Class
ASKER
Hmm, I was actually going to validate that in a class, I am not sure what to use, but regular expressions is a very good idea... I will probably use them.
About the algorithm, with the arrow changing on the parsed pieces of string, do you by any change have a similar code snippet about that? I explained in 21644903, about the arrow - I have no idea how to do that, and I was looking for how to, in the links that pigtor gave me, but still couldnt find anything related to that :s
About the algorithm, with the arrow changing on the parsed pieces of string, do you by any change have a similar code snippet about that? I explained in 21644903, about the arrow - I have no idea how to do that, and I was looking for how to, in the links that pigtor gave me, but still couldnt find anything related to that :s
I'll post some more tomorrow... =)
ASKER
thanks :)
*** DO NOT FORGET TO SPLIT POINTS WITH PIGTOR ***
I used NumericUpDown controls for number of rows/columns and then row/column sizes.
Here is the Form code: (GameBoard class is posted down below)
Public Class Form1
Private WithEvents gb As GameBoard
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
gb = New GameBoard
Me.trkColumnHeight.Value = gb.rowHeight / 10
Me.trkRows.Value = gb.rowcount
Me.trkColumnWidth.Value = gb.columnWidth / 10
Me.trkColumns.Value = gb.columnCount
Me.Panel1.Controls.Add(gb)
Me.WindowState = FormWindowState.Maximized
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
gb.rowHeight = Me.trkColumnHeight.Value * 10
gb.rowcount = Me.trkRows.Value
gb.columnWidth = Me.trkColumnWidth.Value * 10
gb.columnCount = Me.trkColumns.Value
gb.leftmargin = 25
gb.NewMap()
gb.Refresh()
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
TextBox1.Enabled = False
Button2.Enabled = False
gb.ExecuteCommandString(Te xtBox1.Tex t)
End Sub
Private Sub gb_BadCommandString(ByVal sender As GameBoard) Handles gb.BadCommandString
MessageBox.Show(TextBox1.T ext, "Bad Command String", MessageBoxButtons.OK, MessageBoxIcon.Error)
TextBox1.Enabled = True
Button2.Enabled = True
TextBox1.Focus()
End Sub
Private Sub gb_Crashed(ByVal sender As GameBoard) Handles gb.Crashed
MessageBox.Show("You have lost!" & vbCrLf & "Try again...", "Hit Wall or Edge of Map", MessageBoxButtons.OK)
gb.RestartMap()
TextBox1.Enabled = True
Button2.Enabled = True
TextBox1.Focus()
End Sub
Private Sub gb_Failed(ByVal sender As GameBoard) Handles gb.Failed
MessageBox.Show("You have lost!" & vbCrLf & "Try again...", "Failed to reach destination", MessageBoxButtons.OK)
gb.RestartMap()
TextBox1.Enabled = True
Button2.Enabled = True
TextBox1.Focus()
End Sub
Private Sub gb_DestinationReached(ByVa l sender As GameBoard) Handles gb.DestinationReached
MessageBox.Show("Good job!", "Destination Reached", MessageBoxButtons.OK, MessageBoxIcon.Exclamation )
gb.NewMap()
gb.Refresh()
TextBox1.Enabled = True
Button2.Enabled = True
TextBox1.Clear()
TextBox1.Focus()
End Sub
End Class
...and here is the GameBoard Class:
I used NumericUpDown controls for number of rows/columns and then row/column sizes.
Here is the Form code: (GameBoard class is posted down below)
Public Class Form1
Private WithEvents gb As GameBoard
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
gb = New GameBoard
Me.trkColumnHeight.Value = gb.rowHeight / 10
Me.trkRows.Value = gb.rowcount
Me.trkColumnWidth.Value = gb.columnWidth / 10
Me.trkColumns.Value = gb.columnCount
Me.Panel1.Controls.Add(gb)
Me.WindowState = FormWindowState.Maximized
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
gb.rowHeight = Me.trkColumnHeight.Value * 10
gb.rowcount = Me.trkRows.Value
gb.columnWidth = Me.trkColumnWidth.Value * 10
gb.columnCount = Me.trkColumns.Value
gb.leftmargin = 25
gb.NewMap()
gb.Refresh()
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
TextBox1.Enabled = False
Button2.Enabled = False
gb.ExecuteCommandString(Te
End Sub
Private Sub gb_BadCommandString(ByVal sender As GameBoard) Handles gb.BadCommandString
MessageBox.Show(TextBox1.T
TextBox1.Enabled = True
Button2.Enabled = True
TextBox1.Focus()
End Sub
Private Sub gb_Crashed(ByVal sender As GameBoard) Handles gb.Crashed
MessageBox.Show("You have lost!" & vbCrLf & "Try again...", "Hit Wall or Edge of Map", MessageBoxButtons.OK)
gb.RestartMap()
TextBox1.Enabled = True
Button2.Enabled = True
TextBox1.Focus()
End Sub
Private Sub gb_Failed(ByVal sender As GameBoard) Handles gb.Failed
MessageBox.Show("You have lost!" & vbCrLf & "Try again...", "Failed to reach destination", MessageBoxButtons.OK)
gb.RestartMap()
TextBox1.Enabled = True
Button2.Enabled = True
TextBox1.Focus()
End Sub
Private Sub gb_DestinationReached(ByVa
MessageBox.Show("Good job!", "Destination Reached", MessageBoxButtons.OK, MessageBoxIcon.Exclamation
gb.NewMap()
gb.Refresh()
TextBox1.Enabled = True
Button2.Enabled = True
TextBox1.Clear()
TextBox1.Focus()
End Sub
End Class
...and here is the GameBoard Class:
Imports System.Text.RegularExpressions
Public Class GameBoard
Inherits PictureBox
Public Enum CellType
None
Path
PathB
Wall
End Enum
Public Enum Direction
North
NorthEast
East
SouthEast
South
SouthWest
West
NorthWest
End Enum
Public Event BadCommandString(ByVal sender As GameBoard)
Public Event Failed(ByVal sender As GameBoard)
Public Event Crashed(ByVal sender As GameBoard)
Public Event DestinationReached(ByVal sender As GameBoard)
Public Sub New()
' ..this gets rid of the flicker when we call Refresh()...
Me.SetStyle(ControlStyles.OptimizedDoubleBuffer, True)
Me.SetStyle(ControlStyles.AllPaintingInWmPaint, True)
Me.Dock = DockStyle.Fill
Me.NewMap()
tmr.Enabled = False
tmr.Interval = 1000
End Sub
Public curCol As Integer
Public curRow As Integer
Public destCol As Integer
Public destRow As Integer
Public curDir As GameBoard.Direction
Private startCol, startRow As Integer
Private startDir As GameBoard.Direction
Public _rowHeight As Integer = 30
Public _rowCount As Integer = 10
Public _columnWidth As Integer = 50
Public _columnCount As Integer = 10
Public _LeftMargin As Integer = 25
Private WithEvents tmr As New Timer
Private cmdQueue As Queue(Of String)
Public Property columnWidth() As Integer
Get
Return _columnWidth
End Get
Set(ByVal value As Integer)
_columnWidth = value
End Set
End Property
Public Property rowHeight() As Integer
Get
Return _rowHeight
End Get
Set(ByVal value As Integer)
_rowHeight = value
End Set
End Property
Public Property rowcount() As Integer
Get
Return _rowCount
End Get
Set(ByVal value As Integer)
_rowCount = value
End Set
End Property
Public Property columnCount() As Integer
Get
Return _columnCount
End Get
Set(ByVal value As Integer)
_columnCount = value
End Set
End Property
Public Property leftmargin() As Integer
Get
Return _LeftMargin
End Get
Set(ByVal value As Integer)
_LeftMargin = value
End Set
End Property
Public Grid(,) As GameBoard.CellType
Public Sub NewMap()
Dim R As New Random
ReDim Grid(rowcount - 1, columnCount - 1)
' start with ALL walls
For row As Integer = 0 To rowcount - 1
For col As Integer = 0 To columnCount - 1
Grid(row, col) = CellType.Wall
Next
Next
' pick starting and destination cells along with direction
curDir = R.Next(0, 8)
curCol = R.Next(0, columnCount)
curRow = R.Next(0, rowcount)
Grid(curRow, curCol) = CellType.Path
Do
destCol = R.Next(0, columnCount)
destRow = R.Next(0, rowcount)
Loop While destCol = curCol AndAlso destRow = curRow
Grid(destRow, destCol) = CellType.PathB
startRow = curRow
startCol = curCol
startDir = curDir
' make sure there is a path from start to finish
' use the "water" method to flow out from both start and finish
' at the same time until they meet
Dim tmpCurCol As Integer = curCol
Dim tmpCurRow As Integer = curRow
Dim tmpDestCol As Integer = destCol
Dim tmpDestRow As Integer = destRow
Dim dir As Integer
Dim connected As Boolean = False
While Not connected
dir = R.Next(0, 4)
Select Case dir
Case 0 ' left
If tmpCurCol > 0 Then
tmpCurCol = tmpCurCol - 1
If Grid(tmpCurRow, tmpCurCol) = CellType.PathB Then
connected = True
Else
Grid(tmpCurRow, tmpCurCol) = CellType.Path
End If
End If
Case 1 ' right
If tmpCurCol < columnCount - 1 Then
tmpCurCol = tmpCurCol + 1
If Grid(tmpCurRow, tmpCurCol) = CellType.PathB Then
connected = True
Else
Grid(tmpCurRow, tmpCurCol) = CellType.Path
End If
End If
Case 2 ' up
If tmpCurRow > 0 Then
tmpCurRow = tmpCurRow - 1
If Grid(tmpCurRow, tmpCurCol) = CellType.PathB Then
connected = True
Else
Grid(tmpCurRow, tmpCurCol) = CellType.Path
End If
End If
Case 3 ' down
If tmpCurRow < rowcount - 1 Then
tmpCurRow = tmpCurRow + 1
If Grid(tmpCurRow, tmpCurCol) = CellType.PathB Then
connected = True
Else
Grid(tmpCurRow, tmpCurCol) = CellType.Path
End If
End If
End Select
If Not connected Then
dir = R.Next(0, 4)
Select Case dir
Case 0 ' left
If tmpDestCol > 0 Then
tmpDestCol = tmpDestCol - 1
If Grid(tmpDestRow, tmpDestCol) = CellType.Path Then
connected = True
Else
Grid(tmpDestRow, tmpDestCol) = CellType.PathB
End If
End If
Case 1 ' right
If tmpDestCol < columnCount - 1 Then
tmpDestCol = tmpDestCol + 1
If Grid(tmpDestRow, tmpDestCol) = CellType.Path Then
connected = True
Else
Grid(tmpDestRow, tmpDestCol) = CellType.PathB
End If
End If
Case 2 ' up
If tmpDestRow > 0 Then
tmpDestRow = tmpDestRow - 1
If Grid(tmpDestRow, tmpDestCol) = CellType.Path Then
connected = True
Else
Grid(tmpDestRow, tmpDestCol) = CellType.PathB
End If
End If
Case 3 ' down
If tmpDestRow < rowcount - 1 Then
tmpDestRow = tmpDestRow + 1
If Grid(tmpDestRow, tmpDestCol) = CellType.Path Then
connected = True
Else
Grid(tmpDestRow, tmpDestCol) = CellType.PathB
End If
End If
End Select
End If
End While
' add random paths
For row As Integer = 0 To rowcount - 1
For col As Integer = 0 To columnCount - 1
If Grid(row, col) = CellType.Wall Then
If R.NextDouble < 0.5 Then
Grid(row, col) = CellType.Path
End If
End If
Next
Next
End Sub
Public Sub RestartMap()
curCol = startCol
curRow = startRow
curDir = startDir
Me.Refresh()
End Sub
Public Sub ExecuteCommandString(ByVal cmds As String)
cmdQueue = New Queue(Of String)
Dim value As Integer
For Each m As Match In Regex.Matches(cmds.ToUpper, "[FRL]\d*")
If Integer.TryParse(m.ToString.Substring(1), value) Then
For i As Integer = 1 To value
cmdQueue.Enqueue(m.ToString.Substring(0, 1))
Next
Else
MessageBox.Show(m.ToString, "Invalid Number", MessageBoxButtons.OK, MessageBoxIcon.Error)
RaiseEvent BadCommandString(Me)
Exit Sub
End If
Next
tmr.Start()
End Sub
Private Sub GameBoard_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
Const arm_length As Integer = 5
' draw the blocks
Dim rc As Rectangle
For row As Integer = 0 To rowcount - 1
For col As Integer = 0 To columnCount - 1
rc = New Rectangle(New Point(leftmargin + (col * columnWidth), row * rowHeight), New Size(columnWidth, rowHeight))
e.Graphics.FillRectangle(IIf(Grid(row, col) = CellType.Wall, Brushes.Blue, Brushes.White), rc)
Next
Next
' draw the destination block
rc = New Rectangle(New Point(leftmargin + (destCol * columnWidth), destRow * rowHeight), New Size(columnWidth, rowHeight))
e.Graphics.FillRectangle(Brushes.Red, rc)
' draw the highlighted block with an arrow in it
rc = New Rectangle(New Point(leftmargin + (curCol * columnWidth), curRow * rowHeight), New Size(columnWidth, rowHeight))
e.Graphics.FillRectangle(Brushes.Green, rc)
rc.Inflate(-5, -5)
Dim x1, y1, x2, y2, dx, dy, length As Integer
Select Case curDir
Case Direction.North
x1 = rc.Left + rc.Width / 2
y1 = rc.Bottom
x2 = x1
y2 = rc.Top
Case Direction.NorthEast
x1 = rc.Left
y1 = rc.Bottom
x2 = rc.Right
y2 = rc.Top
Case Direction.East
x1 = rc.Left
y1 = rc.Top + rc.Height / 2
x2 = rc.Right
y2 = y1
Case Direction.SouthEast
x1 = rc.Left
y1 = rc.Top
x2 = rc.Right
y2 = rc.Bottom
Case Direction.South
x1 = rc.Left + rc.Width / 2
y1 = rc.Top
x2 = x1
y2 = rc.Bottom
Case Direction.SouthWest
x1 = rc.Right
y1 = rc.Top
x2 = rc.Left
y2 = rc.Bottom
Case Direction.West
x1 = rc.Right
y1 = rc.Top + rc.Height / 2
x2 = rc.Left
y2 = y1
Case Direction.NorthWest
x1 = rc.Right
y1 = rc.Bottom
x2 = rc.Left
y2 = rc.Top
End Select
dx = x2 - x1
dy = y2 - y1
length = Math.Sqrt(dx ^ 2 + dy ^ 2)
dx = dx / length * arm_length
dy = dy / length * arm_length
e.Graphics.DrawLine(Pens.Black, x1, y1, x2, y2)
e.Graphics.DrawLine(Pens.Black, x2, y2, x2 - dx - dy, y2 - dy + dx)
e.Graphics.DrawLine(Pens.Black, x2, y2, x2 - dx + dy, y2 - dy - dx)
' draw the grid lines
For r As Integer = 0 To rowcount
e.Graphics.DrawLine(Pens.Black, leftmargin, rowHeight * r, leftmargin + (columnCount * columnWidth), rowHeight * r)
Next
For c As Integer = 0 To columnCount
e.Graphics.DrawLine(Pens.Black, leftmargin + (c * columnWidth), 0, leftmargin + (c * columnWidth), rowcount * rowHeight)
Next
End Sub
Private Sub MoveForward()
Dim deltaRow, deltaCol As Integer
Select Case curDir
Case Direction.North
If Me.curRow > 0 Then
deltaRow = -1
deltaCol = 0
Else
tmr.Stop()
RaiseEvent Crashed(Me)
Exit Sub
End If
Case Direction.NorthEast
If Me.curRow > 0 AndAlso Me.curCol < Me.columnCount - 1 Then
deltaRow = -1
deltaCol = +1
Else
tmr.Stop()
RaiseEvent Crashed(Me)
Exit Sub
End If
Case Direction.East
If Me.curCol < Me.columnCount - 1 Then
deltaRow = 0
deltaCol = +1
Else
tmr.Stop()
RaiseEvent Crashed(Me)
Exit Sub
End If
Case Direction.SouthEast
If Me.curRow < Me.rowcount - 1 AndAlso Me.curCol < Me.columnCount - 1 Then
deltaRow = +1
deltaCol = +1
Else
tmr.Stop()
RaiseEvent Crashed(Me)
Exit Sub
End If
Case Direction.South
If Me.curRow < Me.rowcount - 1 Then
deltaRow = +1
deltaCol = 0
Else
tmr.Stop()
RaiseEvent Crashed(Me)
Exit Sub
End If
Case Direction.SouthWest
If Me.curRow < Me.rowcount - 1 AndAlso Me.curCol > 0 Then
deltaRow = +1
deltaCol = -1
Else
tmr.Stop()
RaiseEvent Crashed(Me)
Exit Sub
End If
Case Direction.West
If Me.curCol > 0 Then
deltaRow = 0
deltaCol = -1
Else
tmr.Stop()
RaiseEvent Crashed(Me)
Exit Sub
End If
Case Direction.NorthWest
If Me.curCol > 0 AndAlso Me.curCol > 0 Then
deltaRow = -1
deltaCol = -1
Else
tmr.Stop()
RaiseEvent Crashed(Me)
Exit Sub
End If
End Select
Me.curRow = Me.curRow + deltaRow
Me.curCol = Me.curCol + deltaCol
Me.Refresh()
If Me.curRow = Me.destRow AndAlso Me.curCol = Me.destCol Then
tmr.Stop()
RaiseEvent DestinationReached(Me)
ElseIf Me.Grid(Me.curRow, Me.curCol) = GameBoard.CellType.Wall Then
tmr.Stop()
RaiseEvent Crashed(Me)
End If
End Sub
Private Sub TurnRight()
If curDir = Direction.NorthWest Then
curDir = Direction.North
Else
curDir = curDir + 1
End If
Me.Refresh()
End Sub
Private Sub TurnLeft()
If curDir = Direction.North Then
curDir = Direction.NorthWest
Else
curDir = curDir - 1
End If
Me.Refresh()
End Sub
Private Sub tmr_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles tmr.Tick
If cmdQueue.Count > 0 Then
Select Case cmdQueue.Dequeue
Case "F"
Me.MoveForward()
Case "R"
Me.TurnRight()
Case "L"
Me.TurnLeft()
End Select
Else
tmr.Stop()
RaiseEvent Failed(Me)
End If
End Sub
End Class
LogoGame.jpg
ASKER
thanks for the help guys
"thanks for the help guys"
Not a problem! I hope you have learned ~SOMETHING~ from this question. Thanks for remembering to split the points with pigtor.
I enjoyed writing the code and will be using the project to help introduce my 10 year old daughter to the world of programming. I myself started out at her age so long ago...
All I've done is help someone answer a programming related question on a PUBLIC forum.
On another completely unrelated note, if this was identified in the problem description as homework, I would have approached it with a completely different tact...in "teacher" mode as many of us at EE do.
Of course, that has nothing to do with this question...so I'll leave it at that.
Have a great day!
Not a problem! I hope you have learned ~SOMETHING~ from this question. Thanks for remembering to split the points with pigtor.
I enjoyed writing the code and will be using the project to help introduce my 10 year old daughter to the world of programming. I myself started out at her age so long ago...
All I've done is help someone answer a programming related question on a PUBLIC forum.
On another completely unrelated note, if this was identified in the problem description as homework, I would have approached it with a completely different tact...in "teacher" mode as many of us at EE do.
Of course, that has nothing to do with this question...so I'll leave it at that.
Have a great day!
ASKER
Hi Idle_Mind,
could i ask you one more question?
could i ask you one more question?
Fire away...
ASKER
I have a couple of few issues on this thread which i will figure out no problems :-) Thanks a lot for your help !!
One thing, have you ever worked on differentiating from errors such as run time exception, sql exception and so on? like all the errors possible out there?
is there a way which .net framework could distinguish between them?
One thing, have you ever worked on differentiating from errors such as run time exception, sql exception and so on? like all the errors possible out there?
is there a way which .net framework could distinguish between them?
ASKER
I'll post another thread? better i think even for assigning points
ASKER
Hi Idle_mind, Could you point me out, just give me an idea - I could do it myself :-)
But if I am to like do levels of the game, for example
a beginner has a 5 x 5 grid
an intermediate has 10 x 10 grid
hard has 20 x 20 grid
And these are static boards, they never change, not with the random.
how do I reach this?
thanks for your help
But if I am to like do levels of the game, for example
a beginner has a 5 x 5 grid
an intermediate has 10 x 10 grid
hard has 20 x 20 grid
And these are static boards, they never change, not with the random.
how do I reach this?
thanks for your help
You need to make a design decision then...where is the information for the layout of the board to be stored?
You could "hard code" it...meaning the information is stored INSIDE the executable.
...or you could store that information in external files in a format of your choosing. This could easily be done using plain text files (which would make it easy for the end user to make their own boards and import them) or it could be done using Binary Serialization or even XML Serialization. Serialization involves storing the data in a CUSTOM CLASS and then telling the .Net framework to serialize it (write out the data).
You are dealing with three types of data:
(1) Cell Data --> Either "on" or "off" (wall or pathway)
(2) Start Position
(3) End Position
Thoughts?
You could "hard code" it...meaning the information is stored INSIDE the executable.
...or you could store that information in external files in a format of your choosing. This could easily be done using plain text files (which would make it easy for the end user to make their own boards and import them) or it could be done using Binary Serialization or even XML Serialization. Serialization involves storing the data in a CUSTOM CLASS and then telling the .Net framework to serialize it (write out the data).
You are dealing with three types of data:
(1) Cell Data --> Either "on" or "off" (wall or pathway)
(2) Start Position
(3) End Position
Thoughts?
ASKER
I was going to make three of the sub shown below. The one below is the one you done for me all I did was change the name since it makes a random map.
I as going to do another method that will generate a difficulty level, like easy, intermediate or hard. And they are always the same! like instead of randomly choosing the cell types.
i had in mind instead of a random, making three types of loops, one which would give me a wall every 3 times in a loop and the others will give me more or less. and then for example in easy level, i will give the loop 10 times, which will give me 3 types of cell A, and 3 types of cell B and 3 of Cell type C for each row. How does that sound?
And If I am to add a level, instead of 10, I give it 20 or more. But this will generate three types of maps, I was thinking like 3 of level easy, 3 of level intermediate and 3 of level type hard that are static.
Other than this, he can then choose from the bars I created for him/her to choose the columns and rows.
What do you think about the idea? or is it redundant?
I as going to do another method that will generate a difficulty level, like easy, intermediate or hard. And they are always the same! like instead of randomly choosing the cell types.
i had in mind instead of a random, making three types of loops, one which would give me a wall every 3 times in a loop and the others will give me more or less. and then for example in easy level, i will give the loop 10 times, which will give me 3 types of cell A, and 3 types of cell B and 3 of Cell type C for each row. How does that sound?
And If I am to add a level, instead of 10, I give it 20 or more. But this will generate three types of maps, I was thinking like 3 of level easy, 3 of level intermediate and 3 of level type hard that are static.
Other than this, he can then choose from the bars I created for him/her to choose the columns and rows.
What do you think about the idea? or is it redundant?
Public Sub Random()
Try
Dim R As New Random
ReDim Grid(rowcount - 1, columnCount - 1)
' start with ALL walls
For row As Integer = 0 To rowcount - 1
For col As Integer = 0 To columnCount - 1
Grid(row, col) = CellType.Wall
Next
Next
' pick starting and destination cells along with direction
curDir = R.Next(0, 8)
curCol = R.Next(0, columnCount)
curRow = R.Next(0, rowcount)
Grid(curRow, curCol) = CellType.Path
Do
destCol = R.Next(0, columnCount)
destRow = R.Next(0, rowcount)
Loop While destCol = curCol AndAlso destRow = curRow
Grid(destRow, destCol) = CellType.PathB
startRow = curRow
startCol = curCol
startDir = curDir
' make sure there is a path from start to finish
' use the "water" method to flow out from both start and finish
' at the same time until they meet
Dim tmpCurCol As Integer = curCol
Dim tmpCurRow As Integer = curRow
Dim tmpDestCol As Integer = destCol
Dim tmpDestRow As Integer = destRow
Dim dir As Integer
Dim connected As Boolean = False
While Not connected
dir = R.Next(0, 4)
Select Case dir
Case 0 ' left
If tmpCurCol > 0 Then
tmpCurCol = tmpCurCol - 1
If Grid(tmpCurRow, tmpCurCol) = CellType.PathB Then
connected = True
Else
Grid(tmpCurRow, tmpCurCol) = CellType.Path
End If
End If
Case 1 ' right
If tmpCurCol < columnCount - 1 Then
tmpCurCol = tmpCurCol + 1
If Grid(tmpCurRow, tmpCurCol) = CellType.PathB Then
connected = True
Else
Grid(tmpCurRow, tmpCurCol) = CellType.Path
End If
End If
Case 2 ' up
If tmpCurRow > 0 Then
tmpCurRow = tmpCurRow - 1
If Grid(tmpCurRow, tmpCurCol) = CellType.PathB Then
connected = True
Else
Grid(tmpCurRow, tmpCurCol) = CellType.Path
End If
End If
Case 3 ' down
If tmpCurRow < rowcount - 1 Then
tmpCurRow = tmpCurRow + 1
If Grid(tmpCurRow, tmpCurCol) = CellType.PathB Then
connected = True
Else
Grid(tmpCurRow, tmpCurCol) = CellType.Path
End If
End If
End Select
If Not connected Then
dir = R.Next(0, 4)
Select Case dir
Case 0 ' left
If tmpDestCol > 0 Then
tmpDestCol = tmpDestCol - 1
If Grid(tmpDestRow, tmpDestCol) = CellType.Path Then
connected = True
Else
Grid(tmpDestRow, tmpDestCol) = CellType.PathB
End If
End If
Case 1 ' right
If tmpDestCol < columnCount - 1 Then
tmpDestCol = tmpDestCol + 1
If Grid(tmpDestRow, tmpDestCol) = CellType.Path Then
connected = True
Else
Grid(tmpDestRow, tmpDestCol) = CellType.PathB
End If
End If
Case 2 ' up
If tmpDestRow > 0 Then
tmpDestRow = tmpDestRow - 1
If Grid(tmpDestRow, tmpDestCol) = CellType.Path Then
connected = True
Else
Grid(tmpDestRow, tmpDestCol) = CellType.PathB
End If
End If
Case 3 ' down
If tmpDestRow < rowcount - 1 Then
tmpDestRow = tmpDestRow + 1
If Grid(tmpDestRow, tmpDestCol) = CellType.Path Then
connected = True
Else
Grid(tmpDestRow, tmpDestCol) = CellType.PathB
End If
End If
End Select
End If
End While
' add random paths
For row As Integer = 0 To rowcount - 1
For col As Integer = 0 To columnCount - 1
If Grid(row, col) = CellType.Wall Then
If R.NextDouble < 0.5 Then
Grid(row, col) = CellType.Path
End If
End If
Next
Next
Catch ex As Exception
Throw ex
End Try
End Sub
ASKER
The information about the layer of the board is to be stored in a database i was thinking like a table having
Map Name, Map Difficulty, Map Type
Map Name = the name of the map
Map Difficulty = possibility of 3, easy medium and hard
Map Type = a range 3 types. Example type 1 makes the first cell a wall, second path, third path 2. The second one makes a wall every 5 blocks, and so on...
Map Name, Map Difficulty, Map Type
Map Name = the name of the map
Map Difficulty = possibility of 3, easy medium and hard
Map Type = a range 3 types. Example type 1 makes the first cell a wall, second path, third path 2. The second one makes a wall every 5 blocks, and so on...
ASKER
I am going to add a folder in my application and in it an SQL database, with the tables in them. The executable will then have the map information as stated above. There is no need for maps to be imported at the moment so the concern is only getting static maps available for the game user
what do you think about my idea? or is it a bit under rated?
what do you think about my idea? or is it a bit under rated?
"i had in mind instead of a random, making three types of loops, one which would give me a wall every 3 times in a loop and the others will give me more or less. and then for example in easy level, i will give the loop 10 times, which will give me 3 types of cell A, and 3 types of cell B and 3 of Cell type C for each row."
Sorry...having a hard time understanding what you had envisioned here. Could you explain in more words?
The biggest issue you are going to face is ensuring that your map is SOLVEABLE. You can't simply change the probability of walls...you may end up with more or less bricks...but can the user actually get from "start" to "finish"?...
Sorry...having a hard time understanding what you had envisioned here. Could you explain in more words?
The biggest issue you are going to face is ensuring that your map is SOLVEABLE. You can't simply change the probability of walls...you may end up with more or less bricks...but can the user actually get from "start" to "finish"?...
ASKER
no the map does not have to be always solvable :-) it can be sometimes not solvable so that helps in this case
I had in mind for example, using a number, like 10 for easy, 20 for intermediate, and 30 for hard,
Example
Dim mynum As Double = 10/20/30
If mynum Mod 2 = 0 Then
'"Even"
ElseIf mynum Mod 2 = 1 Then
'"Odd"
End If
If the number is even, make the cell a wall, if not path. Can this come up with a solution to 9 static maps? that are always the same?
I had in mind for example, using a number, like 10 for easy, 20 for intermediate, and 30 for hard,
Example
Dim mynum As Double = 10/20/30
If mynum Mod 2 = 0 Then
'"Even"
ElseIf mynum Mod 2 = 1 Then
'"Odd"
End If
If the number is even, make the cell a wall, if not path. Can this come up with a solution to 9 static maps? that are always the same?
If you want the walls to "vary by distance" then you could do something like:
(pseudo code)
Dim frequency As Integer = 2/3/4/5
For i As Integer = 1 To 10/20/30
If i Mod frequency = 0 Then
' turn wall ON
Else
' leave cell OFF
End If
Next i
(pseudo code)
Dim frequency As Integer = 2/3/4/5
For i As Integer = 1 To 10/20/30
If i Mod frequency = 0 Then
' turn wall ON
Else
' leave cell OFF
End If
Next i
ASKER
Would this do the trick?
For row As Integer = 0 To rowcount - 1
For col As Integer = 0 To columnCount - 1
If Grid(row, col) = CellType.Wall Then
Dim frequency As Integer = 2 / 3 / 4 / 5
For i As Integer = 1 To 10 / 20 / 30
If i Mod frequency = 0 Then
' turn wall ON
Grid(row, col) = CellType.Path
Else
' leave cell OFF
Grid(row, col) = CellType.None
End If
Next i
End If
Next
Next
ASKER
Hi Idle_mind, I came up with this solution and would like a suggestion if it is OK the way it is?
I call it within the method to create the map
'create the paths
createPaths(rowcount, columnCount, 2, 10)
I call it within the method to create the map
'create the paths
createPaths(rowcount, columnCount, 2, 10)
Public Sub createPaths(ByVal rowcount As Integer, ByVal columncount As Integer, _
ByVal frequency As Integer, ByVal baseNumber As Integer)
For row As Integer = 0 To rowcount - 1
For col As Integer = 0 To columncount - 1
If Grid(row, col) = CellType.Wall Then
'Dim frequency As Integer = 2 / 3 / 4 / 5
'basenumber 10/20/30
For i As Integer = 1 To baseNumber
If i Mod frequency = 0 Then
' turn wall ON
Grid(row, col) = CellType.Path
Else
' leave cell OFF
Grid(row, col) = CellType.None
End If
Next i
End If
Next
Next
End Sub
Sure...it shold create a fixed pattern that has the specified "density".
ASKER
Hi Idle mind, another question... wouldnt it be better if the game board would be mapped onto an array?
Huh?...
It's already an Array:
Public Grid(,) As GameBoard.CellType
That is a two dimensional array of type GameBoard.CellType.
It's already an Array:
Public Grid(,) As GameBoard.CellType
That is a two dimensional array of type GameBoard.CellType.
ASKER
Could you please explain a bit further please because I'm finding a bit hard understanding the code that was inputted to build public grid(,) as gameboard.celltype
thanks for still helping me out here :-)
highly appreciated!
thanks for still helping me out here :-)
highly appreciated!
GameBoard.CellType is a simple Enum:
Public Enum CellType
None
Path
PathB
Wall
End Enum
Where by default (since I didn't specify any explicit numbering scheme):
None = 0
Path = 1
PathB = 2
Wall =3
So you could think of the "Grid" array of being a simple Integer array where the only possible values are 0, 1, 2 and 3.
Does that help conceptualize it?
Public Enum CellType
None
Path
PathB
Wall
End Enum
Where by default (since I didn't specify any explicit numbering scheme):
None = 0
Path = 1
PathB = 2
Wall =3
So you could think of the "Grid" array of being a simple Integer array where the only possible values are 0, 1, 2 and 3.
Does that help conceptualize it?
By making it an Enum, the code is easier to read because the Enum values are descriptive.
Consider this:
If Me.Grid(Me.curRow, Me.curCol) = GameBoard.CellType.Wall Then
Compared to:
If Me.Grid(Me.curRow, Me.curCol) = 3 Then
Which conveys more information?
Consider this:
If Me.Grid(Me.curRow, Me.curCol) = GameBoard.CellType.Wall Then
Compared to:
If Me.Grid(Me.curRow, Me.curCol) = 3 Then
Which conveys more information?
ASKER
Hi Idle mind, I have an error, could you please help me out?
I have got a function below that returns from a database what i give it as a filter string. I am calling the maps from the database attached. I added a combobox, that will show the name of the map, and when an item is selected i give my new method called levelmap(), the attributes of the map which are stored in the database.
Like below
If Me.cmbMaps.Text <> "Choose Map" Then
Dim tools As New Tools
Dim gameCollection As DataSet = tools.Return_Access("selec t * from tbl_gameboards where name = '" & Me.cmbMaps.SelectedItem.To String & "'", "C:\Documents and Settings\Database_Path\alg eek.mdb")
Dim MaxBaseNumber As Integer = gameCollection.Tables(0).R ows(0).Ite m(3)
Dim frequency As Integer = gameCollection.Tables(0).R ows(0).Ite m(4)
Dim rows As Integer = gameCollection.Tables(0).R ows(0).Ite m(5)
Dim columns As Integer = gameCollection.Tables(0).R ows(0).Ite m(6)
Dim cellWidth As Integer = gameCollection.Tables(0).R ows(0).Ite m(7)
Dim cellHeight As Integer = gameCollection.Tables(0).R ows(0).Ite m(8)
gb.baseNumber = MaxBaseNumber
gb.frequency = frequency
gb.rowcount = rows
gb.rowHeight = cellHeight
gb.columnWidth = cellWidth
gb.columnCount = columns
gb.LevelMap()
Else
MessageBox.Show("Please choose map from the drop down list!")
End If
The problem is that it is giving me an arithmetic overflow here
dx = x2 - x1
dy = y2 - y1
length = Math.Sqrt(dx ^ 2 + dy ^ 2)
dx = dx / length * arm_length
dy = dy / length * arm_length
e.Graphics.DrawLine(Pens.B lack, x1, y1, x2, y2)
e.Graphics.DrawLine(Pens.B lack, x2, y2, x2 - dx - dy, y2 - dy + dx)
e.Graphics.DrawLine(Pens.B lack, x2, y2, x2 - dx + dy, y2 - dy - dx)
Somethings to add are, that maybe you do not know about, is that I added to more properties to the class which are the base number and the frequency. The base number is the maximum number it goes through in the loop to return walls out of a number, example in a base number of 10, and with a frequency of 2, 5 walls are returned. In this case, we have 1 path, 1 wall, 1 path, 1 wall, 1 path, 1 wall and so on till 10.
How can I get around this arithmetic overflow?
I have got a function below that returns from a database what i give it as a filter string. I am calling the maps from the database attached. I added a combobox, that will show the name of the map, and when an item is selected i give my new method called levelmap(), the attributes of the map which are stored in the database.
Like below
If Me.cmbMaps.Text <> "Choose Map" Then
Dim tools As New Tools
Dim gameCollection As DataSet = tools.Return_Access("selec
Dim MaxBaseNumber As Integer = gameCollection.Tables(0).R
Dim frequency As Integer = gameCollection.Tables(0).R
Dim rows As Integer = gameCollection.Tables(0).R
Dim columns As Integer = gameCollection.Tables(0).R
Dim cellWidth As Integer = gameCollection.Tables(0).R
Dim cellHeight As Integer = gameCollection.Tables(0).R
gb.baseNumber = MaxBaseNumber
gb.frequency = frequency
gb.rowcount = rows
gb.rowHeight = cellHeight
gb.columnWidth = cellWidth
gb.columnCount = columns
gb.LevelMap()
Else
MessageBox.Show("Please choose map from the drop down list!")
End If
The problem is that it is giving me an arithmetic overflow here
dx = x2 - x1
dy = y2 - y1
length = Math.Sqrt(dx ^ 2 + dy ^ 2)
dx = dx / length * arm_length
dy = dy / length * arm_length
e.Graphics.DrawLine(Pens.B
e.Graphics.DrawLine(Pens.B
e.Graphics.DrawLine(Pens.B
Somethings to add are, that maybe you do not know about, is that I added to more properties to the class which are the base number and the frequency. The base number is the maximum number it goes through in the loop to return walls out of a number, example in a base number of 10, and with a frequency of 2, 5 walls are returned. In this case, we have 1 path, 1 wall, 1 path, 1 wall, 1 path, 1 wall and so on till 10.
How can I get around this arithmetic overflow?
Public Function Return_Access(ByVal myQuery As String, ByVal myPath As String) As DataSet
Try
Dim myConnStr As String = "Provider=Microsoft.jet.OLEDB.4.0;Data Source=" + myPath
Dim myConn As New OleDb.OleDbConnection(myConnStr)
Dim myAdapt As New OleDb.OleDbDataAdapter(myQuery, myConn)
myConn.Open()
Dim myReader As New DataSet
myAdapt.Fill(myReader)
myConn.Close()
Return myReader
Catch ex As Exception
Throw ex
End Try
End Function
Public Sub LevelMap()
Try
Dim R As New Random
ReDim Grid(rowcount - 1, columnCount - 1)
' start with ALL walls
For row As Integer = 0 To rowcount - 1
For col As Integer = 0 To columnCount - 1
Grid(row, col) = CellType.Wall
Next
Next
' pick starting and destination cells along with direction
curDir = R.Next(0, 8)
curCol = R.Next(0, columnCount)
curRow = R.Next(0, rowcount)
Grid(curRow, curCol) = CellType.Path
Do
destCol = R.Next(0, columnCount)
destRow = R.Next(0, rowcount)
Loop While destCol = curCol AndAlso destRow = curRow
Grid(destRow, destCol) = CellType.PathB
startRow = curRow
startCol = curCol
startDir = curDir
' make sure there is a path from start to finish
' use the "water" method to flow out from both start and finish
' at the same time until they meet
Dim tmpCurCol As Integer = curCol
Dim tmpCurRow As Integer = curRow
Dim tmpDestCol As Integer = destCol
Dim tmpDestRow As Integer = destRow
Dim dir As Integer
Dim connected As Boolean = False
While Not connected
dir = R.Next(0, 4)
Select Case dir
Case 0 ' left
If tmpCurCol > 0 Then
tmpCurCol = tmpCurCol - 1
If Grid(tmpCurRow, tmpCurCol) = CellType.PathB Then
connected = True
Else
Grid(tmpCurRow, tmpCurCol) = CellType.Path
End If
End If
Case 1 ' right
If tmpCurCol < columnCount - 1 Then
tmpCurCol = tmpCurCol + 1
If Grid(tmpCurRow, tmpCurCol) = CellType.PathB Then
connected = True
Else
Grid(tmpCurRow, tmpCurCol) = CellType.Path
End If
End If
Case 2 ' up
If tmpCurRow > 0 Then
tmpCurRow = tmpCurRow - 1
If Grid(tmpCurRow, tmpCurCol) = CellType.PathB Then
connected = True
Else
Grid(tmpCurRow, tmpCurCol) = CellType.Path
End If
End If
Case 3 ' down
If tmpCurRow < rowcount - 1 Then
tmpCurRow = tmpCurRow + 1
If Grid(tmpCurRow, tmpCurCol) = CellType.PathB Then
connected = True
Else
Grid(tmpCurRow, tmpCurCol) = CellType.Path
End If
End If
End Select
If Not connected Then
dir = R.Next(0, 4)
Select Case dir
Case 0 ' left
If tmpDestCol > 0 Then
tmpDestCol = tmpDestCol - 1
If Grid(tmpDestRow, tmpDestCol) = CellType.Path Then
connected = True
Else
Grid(tmpDestRow, tmpDestCol) = CellType.PathB
End If
End If
Case 1 ' right
If tmpDestCol < columnCount - 1 Then
tmpDestCol = tmpDestCol + 1
If Grid(tmpDestRow, tmpDestCol) = CellType.Path Then
connected = True
Else
Grid(tmpDestRow, tmpDestCol) = CellType.PathB
End If
End If
Case 2 ' up
If tmpDestRow > 0 Then
tmpDestRow = tmpDestRow - 1
If Grid(tmpDestRow, tmpDestCol) = CellType.Path Then
connected = True
Else
Grid(tmpDestRow, tmpDestCol) = CellType.PathB
End If
End If
Case 3 ' down
If tmpDestRow < rowcount - 1 Then
tmpDestRow = tmpDestRow + 1
If Grid(tmpDestRow, tmpDestCol) = CellType.Path Then
connected = True
Else
Grid(tmpDestRow, tmpDestCol) = CellType.PathB
End If
End If
End Select
End If
End While
'create the paths
createPaths(rowcount, columnCount, frequency, baseNumber)
Catch ex As Exception
Throw ex
End Try
End Sub
Public Sub createPaths(ByVal rowcount As Integer, ByVal columncount As Integer, _
ByVal frequency As Integer, ByVal baseNumber As Integer)
For row As Integer = 0 To rowcount - 1
For col As Integer = 0 To columncount - 1
If Grid(row, col) = CellType.Wall Then
'Dim frequency As Integer = 2 / 3 / 4 / 5
'basenumber 10/20/30
For i As Integer = 1 To baseNumber
If i Mod frequency = 0 Then
' turn wall ON
Grid(row, col) = CellType.Path
Else
' leave cell OFF
Grid(row, col) = CellType.None
End If
Next i
End If
Next
Next
End Sub
AlGeek.mdb
ASKER
Yes, that makes it a bit easier to understand
so in this case the array is then generated upon the properties that I am passing through the class.
right?
so in this case the array is then generated upon the properties that I am passing through the class.
right?
The arithmetic overflow would occur if you were using a REALLY big cell size. If the cells are too wide or tall then this line would be the likely culprit:
length = Math.Sqrt(dx ^ 2 + dy ^ 2)
We are taknig the square of dx and dy which could possibly be exceeding the maximum range of the Integer data type.
Check the values of dx and dy by printing them out to the Immediate window.
length = Math.Sqrt(dx ^ 2 + dy ^ 2)
We are taknig the square of dx and dy which could possibly be exceeding the maximum range of the Integer data type.
Check the values of dx and dy by printing them out to the Immediate window.
ASKER
The cell sizes are being extracted out of the database, which are
Cell Width = 10
Cell Height = 10
Are there any limitations for this? And how can I avoid this apart from putting a simple if statement?
Cell Width = 10
Cell Height = 10
Are there any limitations for this? And how can I avoid this apart from putting a simple if statement?
Then it would be my guess that the values are either being stored incorrectly or coming back out of your database incorrectly making those numbers become huge.
ASKER
Hi Idle_Mind
I managed to fix that problem. I have done that part, but I wanted the maps to be static, and for some reason, when i load different maps they come up different all the time :s
this does not make sense as i am giving it always the same values, can you help me out here?
under the button click event to load the map i have this code
If Me.cmbMaps.Text <> "Choose Map" Then
Dim tools As New Tools
Dim gameCollection As DataSet = tools.Return_Access("selec t * from tbl_gameboards where name = '" & Me.cmbMaps.SelectedItem.To String & "'", "C:\Documents and Settings\Stephen Borg\My Documents\Visual Studio 2005\Projects\VB.NET IADCS Assignment\VB.NET IADCS Assignment\Database\algeek .mdb")
Dim MaxBaseNumber As Integer = gameCollection.Tables(0).R ows(0).Ite m(3)
Dim frequency As Integer = gameCollection.Tables(0).R ows(0).Ite m(4)
Dim rows As Integer = gameCollection.Tables(0).R ows(0).Ite m(5)
Dim columns As Integer = gameCollection.Tables(0).R ows(0).Ite m(6)
Dim cellWidth As Integer = gameCollection.Tables(0).R ows(0).Ite m(7)
Dim cellHeight As Integer = gameCollection.Tables(0).R ows(0).Ite m(8)
gb.baseNumber = MaxBaseNumber
gb.frequency = frequency
gb.rowcount = rows
gb.rowHeight = cellHeight
gb.columnWidth = cellWidth
gb.columnCount = columns
gb.LevelMap()
Else
MessageBox.Show("Please choose map from the drop down list!")
End If
and level map is this code
Public Sub LevelMap()
Try
Dim R As New Random
ReDim Grid(rowcount - 1, columnCount - 1)
' start with ALL walls
For row As Integer = 0 To rowcount - 1
For col As Integer = 0 To columnCount - 1
Grid(row, col) = CellType.Wall
Next
Next
' pick starting and destination cells along with direction
curDir = R.Next(0, 8)
curCol = R.Next(0, columnCount)
curRow = R.Next(0, rowcount)
Grid(curRow, curCol) = CellType.Path
Do
destCol = R.Next(0, columnCount)
destRow = R.Next(0, rowcount)
Loop While destCol = curCol AndAlso destRow = curRow
Grid(destRow, destCol) = CellType.PathB
startRow = curRow
startCol = curCol
startDir = curDir
' make sure there is a path from start to finish
' use the "water" method to flow out from both start and finish
' at the same time until they meet
Dim tmpCurCol As Integer = curCol
Dim tmpCurRow As Integer = curRow
Dim tmpDestCol As Integer = destCol
Dim tmpDestRow As Integer = destRow
Dim dir As Integer
Dim connected As Boolean = False
While Not connected
dir = R.Next(0, 4)
Select Case dir
Case 0 ' left
If tmpCurCol > 0 Then
tmpCurCol = tmpCurCol - 1
If Grid(tmpCurRow, tmpCurCol) = CellType.PathB Then
connected = True
Else
Grid(tmpCurRow, tmpCurCol) = CellType.Path
End If
End If
Case 1 ' right
If tmpCurCol < columnCount - 1 Then
tmpCurCol = tmpCurCol + 1
If Grid(tmpCurRow, tmpCurCol) = CellType.PathB Then
connected = True
Else
Grid(tmpCurRow, tmpCurCol) = CellType.Path
End If
End If
Case 2 ' up
If tmpCurRow > 0 Then
tmpCurRow = tmpCurRow - 1
If Grid(tmpCurRow, tmpCurCol) = CellType.PathB Then
connected = True
Else
Grid(tmpCurRow, tmpCurCol) = CellType.Path
End If
End If
Case 3 ' down
If tmpCurRow < rowcount - 1 Then
tmpCurRow = tmpCurRow + 1
If Grid(tmpCurRow, tmpCurCol) = CellType.PathB Then
connected = True
Else
Grid(tmpCurRow, tmpCurCol) = CellType.Path
End If
End If
End Select
If Not connected Then
dir = R.Next(0, 4)
Select Case dir
Case 0 ' left
If tmpDestCol > 0 Then
tmpDestCol = tmpDestCol - 1
If Grid(tmpDestRow, tmpDestCol) = CellType.Path Then
connected = True
Else
Grid(tmpDestRow, tmpDestCol) = CellType.PathB
End If
End If
Case 1 ' right
If tmpDestCol < columnCount - 1 Then
tmpDestCol = tmpDestCol + 1
If Grid(tmpDestRow, tmpDestCol) = CellType.Path Then
connected = True
Else
Grid(tmpDestRow, tmpDestCol) = CellType.PathB
End If
End If
Case 2 ' up
If tmpDestRow > 0 Then
tmpDestRow = tmpDestRow - 1
If Grid(tmpDestRow, tmpDestCol) = CellType.Path Then
connected = True
Else
Grid(tmpDestRow, tmpDestCol) = CellType.PathB
End If
End If
Case 3 ' down
If tmpDestRow < rowcount - 1 Then
tmpDestRow = tmpDestRow + 1
If Grid(tmpDestRow, tmpDestCol) = CellType.Path Then
connected = True
Else
Grid(tmpDestRow, tmpDestCol) = CellType.PathB
End If
End If
End Select
End If
End While
'create the paths
createPaths(rowcount, columnCount, frequency, baseNumber)
Catch ex As Exception
Throw ex
End Try
End Sub
and create paths is this code
Public Sub createPaths(ByVal rowcount As Integer, ByVal columncount As Integer, _
ByVal frequency As Integer, ByVal baseNumber As Integer)
For row As Integer = 0 To rowcount - 1
For col As Integer = 0 To columncount - 1
If Grid(row, col) = CellType.Wall Then
'Dim frequency As Integer = 2 / 3 / 4 / 5
'basenumber 10/20/30
For i As Integer = 1 To baseNumber
If i Mod frequency = 0 Then
' turn wall ON
Grid(row, col) = CellType.Path
Else
' leave cell OFF
Grid(row, col) = CellType.Wall
End If
Next i
End If
Next
Next
End Sub
the information stored in the database, is always static, and i am expecting to get always from what is loaded from the database, to be same maps.
is there something that im missing?
thanks for your help!
I managed to fix that problem. I have done that part, but I wanted the maps to be static, and for some reason, when i load different maps they come up different all the time :s
this does not make sense as i am giving it always the same values, can you help me out here?
under the button click event to load the map i have this code
If Me.cmbMaps.Text <> "Choose Map" Then
Dim tools As New Tools
Dim gameCollection As DataSet = tools.Return_Access("selec
Dim MaxBaseNumber As Integer = gameCollection.Tables(0).R
Dim frequency As Integer = gameCollection.Tables(0).R
Dim rows As Integer = gameCollection.Tables(0).R
Dim columns As Integer = gameCollection.Tables(0).R
Dim cellWidth As Integer = gameCollection.Tables(0).R
Dim cellHeight As Integer = gameCollection.Tables(0).R
gb.baseNumber = MaxBaseNumber
gb.frequency = frequency
gb.rowcount = rows
gb.rowHeight = cellHeight
gb.columnWidth = cellWidth
gb.columnCount = columns
gb.LevelMap()
Else
MessageBox.Show("Please choose map from the drop down list!")
End If
and level map is this code
Public Sub LevelMap()
Try
Dim R As New Random
ReDim Grid(rowcount - 1, columnCount - 1)
' start with ALL walls
For row As Integer = 0 To rowcount - 1
For col As Integer = 0 To columnCount - 1
Grid(row, col) = CellType.Wall
Next
Next
' pick starting and destination cells along with direction
curDir = R.Next(0, 8)
curCol = R.Next(0, columnCount)
curRow = R.Next(0, rowcount)
Grid(curRow, curCol) = CellType.Path
Do
destCol = R.Next(0, columnCount)
destRow = R.Next(0, rowcount)
Loop While destCol = curCol AndAlso destRow = curRow
Grid(destRow, destCol) = CellType.PathB
startRow = curRow
startCol = curCol
startDir = curDir
' make sure there is a path from start to finish
' use the "water" method to flow out from both start and finish
' at the same time until they meet
Dim tmpCurCol As Integer = curCol
Dim tmpCurRow As Integer = curRow
Dim tmpDestCol As Integer = destCol
Dim tmpDestRow As Integer = destRow
Dim dir As Integer
Dim connected As Boolean = False
While Not connected
dir = R.Next(0, 4)
Select Case dir
Case 0 ' left
If tmpCurCol > 0 Then
tmpCurCol = tmpCurCol - 1
If Grid(tmpCurRow, tmpCurCol) = CellType.PathB Then
connected = True
Else
Grid(tmpCurRow, tmpCurCol) = CellType.Path
End If
End If
Case 1 ' right
If tmpCurCol < columnCount - 1 Then
tmpCurCol = tmpCurCol + 1
If Grid(tmpCurRow, tmpCurCol) = CellType.PathB Then
connected = True
Else
Grid(tmpCurRow, tmpCurCol) = CellType.Path
End If
End If
Case 2 ' up
If tmpCurRow > 0 Then
tmpCurRow = tmpCurRow - 1
If Grid(tmpCurRow, tmpCurCol) = CellType.PathB Then
connected = True
Else
Grid(tmpCurRow, tmpCurCol) = CellType.Path
End If
End If
Case 3 ' down
If tmpCurRow < rowcount - 1 Then
tmpCurRow = tmpCurRow + 1
If Grid(tmpCurRow, tmpCurCol) = CellType.PathB Then
connected = True
Else
Grid(tmpCurRow, tmpCurCol) = CellType.Path
End If
End If
End Select
If Not connected Then
dir = R.Next(0, 4)
Select Case dir
Case 0 ' left
If tmpDestCol > 0 Then
tmpDestCol = tmpDestCol - 1
If Grid(tmpDestRow, tmpDestCol) = CellType.Path Then
connected = True
Else
Grid(tmpDestRow, tmpDestCol) = CellType.PathB
End If
End If
Case 1 ' right
If tmpDestCol < columnCount - 1 Then
tmpDestCol = tmpDestCol + 1
If Grid(tmpDestRow, tmpDestCol) = CellType.Path Then
connected = True
Else
Grid(tmpDestRow, tmpDestCol) = CellType.PathB
End If
End If
Case 2 ' up
If tmpDestRow > 0 Then
tmpDestRow = tmpDestRow - 1
If Grid(tmpDestRow, tmpDestCol) = CellType.Path Then
connected = True
Else
Grid(tmpDestRow, tmpDestCol) = CellType.PathB
End If
End If
Case 3 ' down
If tmpDestRow < rowcount - 1 Then
tmpDestRow = tmpDestRow + 1
If Grid(tmpDestRow, tmpDestCol) = CellType.Path Then
connected = True
Else
Grid(tmpDestRow, tmpDestCol) = CellType.PathB
End If
End If
End Select
End If
End While
'create the paths
createPaths(rowcount, columnCount, frequency, baseNumber)
Catch ex As Exception
Throw ex
End Try
End Sub
and create paths is this code
Public Sub createPaths(ByVal rowcount As Integer, ByVal columncount As Integer, _
ByVal frequency As Integer, ByVal baseNumber As Integer)
For row As Integer = 0 To rowcount - 1
For col As Integer = 0 To columncount - 1
If Grid(row, col) = CellType.Wall Then
'Dim frequency As Integer = 2 / 3 / 4 / 5
'basenumber 10/20/30
For i As Integer = 1 To baseNumber
If i Mod frequency = 0 Then
' turn wall ON
Grid(row, col) = CellType.Path
Else
' leave cell OFF
Grid(row, col) = CellType.Wall
End If
Next i
End If
Next
Next
End Sub
the information stored in the database, is always static, and i am expecting to get always from what is loaded from the database, to be same maps.
is there something that im missing?
thanks for your help!
ASKER
Hi Idle_mind, I can see that the problem is of me creating a random number which will set each cell randomly - how can i make it static? what do i use instead of the random number?
thanks for helping me out
thanks for helping me out
ASKER
hey idle_mind - can you help me out on this please?
Sorry...been busy...I'll take a look later today.
ASKER
Thanks :-)
Do you want to draw EVERYTHING using the drawing classes?
...or represent the blocks using controls (could be a custom UserControl using GDI to draw inside it) laid out in a grid?