|
-
Jul 31st, 2003, 06:27 PM
#1
Thread Starter
Junior Member
Magic square in VB.NET
I did this project, but it wont work..any thing wrong in my code..could any one check for me..urgent Plz..
my project:
1 text box
1 listview
1 button
Private Sub btnCmd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCmd.Click
Dim Size, I, J, II, JJ, count As Short
Size = txtSize.Text
Dim Magic(Size, Size) As Short
I = 0
J = Size / 2
Magic(I, J) = 1
For count = 1 To (Size ^ 2)
II = I
JJ = J
I = I - 1
If (I < 0) Then
I = Size - 1
End If
J = J - 1
If (J < 0) Then
J = Size - 1
End If
If (Magic(I, J) > 0) Then
I = (II + 1)
If (I > (Size - 1)) Then
I = 0
End If
End If
count += 1
Magic(I, J) = count
Next
lvwDisplay.View = View.Details
Dim col As New ListView.ColumnHeaderCollection(lvwDisplay)
col.Clear()
J = 0
For I = 0 To (Size - 1)
col.Add("", 20, HorizontalAlignment.Right)
Next
Dim Row As ListViewItem
For I = 0 To (Size - 1)
Row = lvwDisplay.Items.Add(Magic(I, J))
For J = 1 To (Size - 1)
Row.SubItems.Add(Magic(I, J))
Next
Next
End Sub
Plz..help.
-
Aug 1st, 2003, 12:33 AM
#2
Thread Starter
Junior Member
-
Aug 2nd, 2003, 01:05 AM
#3
http://mathforum.org/alejandre/magic...er/adler4.html
I may be wrong but it looks to me like ur formual needs a little tuning up
Tips:
- Google is your friend! Search before posting!
- Name your thread appropriately... "I Need Help" doesn't cut it!
- Always post your code!!!! We can't read your mind!!! (well, at least most of us!)
- Allways Include the Name and Line of the Exception (if one is occuring!)
- If it is relevant state the version of Visual Studio/.Net Framwork you are using (2002/2003/2005)
If you think I was helpful, rate my post  IRC Contact: Rizon/xous ChakraNET/xous Freenode/xous
-
Aug 2nd, 2003, 01:43 AM
#4
Also, next time, could you please use [Highlight=VB] tags?
-
Aug 2nd, 2003, 10:27 AM
#5
well i got bored, i pulled a all nighter, and read the explaination on magic squares. Then i played started trying to make vb do it and after about 30minutes of playing around heres what i got...
VB Code:
Private Sub btnCmd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCmd.Click
CreateMagicSquare(txtSize.Text, lvwDisplay)
End Sub
Private Function CreateMagicSquare(ByVal sSize As Short, ByRef lvwList As ListView)
If IsEven(sSize) = True Then
MsgBox("Error: This Function Does NOT Support Even Numbers!!!")
Exit Function
End If
Dim asMagicSquare(sSize, sSize) As Short ' Create The Array to hold the square
Dim sCount As Short ' Hold counting place
Dim sCurX As Short ' Holds The Vertical Position in array(magic Square)
Dim sCurY As Short ' Holds the Horizontal Position in array
Dim sOldX As Short ' Holds old sCurX (Needed because of overwrite)
Dim sOldY As Short ' Holds old sCurY (Needed because of overwrite)
'Must Start In The MIDDLE of the First row so:
sCurX = 1 ' This is give does not change
sCurY = ((sSize - 1) / 2) + 1 ' Find Middle
'Write Frist Number
asMagicSquare(sCurX - 1, sCurY - 1) = 1 ' Always 1
For sCount = 2 To (sSize * sSize)
sOldX = sCurX
sOldY = sCurY
If sCurX = 1 Then
'We are In The Top Row and cant Move Up anymore
sCurX = sSize 'move to bottom row
Else
sCurX = sCurX - 1
End If
If sCurY < sSize Then
sCurY = sCurY + 1
Else
sCurY = 1
End If
'Check If we are going to overwrite
If asMagicSquare(sCurX - 1, sCurY - 1) <> Nothing Then
'We are going to overwrite
'so we must go below the last number we wrote
sCurX = sOldX + 1
sCurY = sOldY
If asMagicSquare(sCurX - 1, sCurY - 1) <> Nothing Then
MsgBox("Error Overwrite!! AGAIN!")
End If
End If
'Write sCount to position
asMagicSquare(sCurX - 1, sCurY - 1) = sCount
Next
DisplayArray(asMagicSquare, lvwList)
End Function
Private Function IsEven(ByVal lngNumber As Long) As Boolean
If lngNumber = 0 Then
Return False
End If
If (lngNumber Mod 2) = 0 Then
Return True
End If
End Function
Private Function DisplayArray(ByVal asArray(,) As Short, ByRef lvwList As ListView)
Dim x As Short
Dim y As Short
'Setup listview
lvwList.Clear() ' Just Incase there is data in the list already
lvwList.View = View.Details
Dim col As New ListView.ColumnHeaderCollection(lvwList)
col.Clear()
For x = 0 To (UBound(asArray) - 1)
'Add a Colum to the listview
col.Add("", 40, HorizontalAlignment.Center)
lvwList.Items.Add(asArray(x, 0))
For y = 1 To (UBound(asArray) - 1)
lvwList.Items(x).SubItems.Add(asArray(x, y))
Next
Next
End Function
End Class
This code sample does not work on even numbers and has not been tested extensivly but i tried a few and all of them checked out.
If you find a method for finding even magic squares and you need help. i will try to help you.
btw: if my coding isnt perfect its prolly because i am a vb 6 programmer that just started playing with VB .NET
Last edited by <ABX; Aug 2nd, 2003 at 04:16 PM.
Tips:
- Google is your friend! Search before posting!
- Name your thread appropriately... "I Need Help" doesn't cut it!
- Always post your code!!!! We can't read your mind!!! (well, at least most of us!)
- Allways Include the Name and Line of the Exception (if one is occuring!)
- If it is relevant state the version of Visual Studio/.Net Framwork you are using (2002/2003/2005)
If you think I was helpful, rate my post  IRC Contact: Rizon/xous ChakraNET/xous Freenode/xous
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|