Wednesday, August 4, 2010

answers to final 1

Sub Btn_Click(...)
    Dim T(3) aS Integer = { 1, 4, 6, 8}
    Mystery(T, 3)
End Sub

Sub NumbersInSequence(ByVal I as Integer, _
            ByVal J as Integer)
    For T = I to J
        lstBox.Items.Add(T)
    Next
End Sub

Sub PrintUntil7(ByVal A() As Integer)
    For Each x in A
        Debug.Print(x)
        if x = 7 then exit for
    Next
End Sub

Sub PrintUntil7(ByVal A() As Integer)
    For I = 0 to UBound(A)
        Debug.Print(A(I))
        if A(I) = 7 then exit for
    Next
End Sub

Sub PrintUntil7(ByVal A() As Integer)
    I = 0
    Do
        Debug.Print(A(I))
    While A(I) <> 7
End Sub

letter = txtBox.Text
Select Case letter.ToUpper
    Case "A"
        Debug.Print("apple")
    Case "B"
        Debug.Print("baseball")
    Case "C"
        Debug.Print("cow")
    Case "D"
        Debug.Print("debug")
End Select

Tuesday, August 3, 2010

latest tetris -- more functionality, but more bugs

Public Class Form1
    Dim board(9, 9) As Boolean
    Dim drawnBoard(9, 9) As PictureBox
    Dim sq As Square
    Sub undraw()
        For I = 0 To sq.data.GetUpperBound(0)
            For J = 0 To sq.data.GetUpperBound(1)
                If sq.data(I, J) = True Then
                    drawnBoard(sq.Y + I, sq.X + J).Visible = False
                End If
            Next
        Next
    End Sub
    Sub moveDown()
        If isOverLap(0, +1) Then Return
        undraw()
        sq.Y = sq.Y + 1
    End Sub
    Private Sub Form1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp
        If e.KeyCode = Keys.Down Then
            moveDown()
        ElseIf e.KeyCode = Keys.Left Then
            If isOverLap(-1, 0) Then Return
            undraw()
            If sq.X <> 0 Then
                sq.X = sq.X - 1
            End If
        ElseIf e.KeyCode = Keys.Right Then
            If isOverLap(+1, 0) Then Return
            undraw()
            If sq.X + sq.data.GetUpperBound(1) <> 10 Then
                sq.X = sq.X + 1
            End If
        ElseIf e.KeyCode = Keys.Up Then
            undraw()
            sq.Rotate()
        End If
    End Sub

    Function isOverLap(ByVal XOffset As Integer, ByVal YOffset As Integer) As Boolean
        For I = 0 To sq.data.GetUpperBound(0)
            For J = 0 To sq.data.GetUpperBound(1)
                If sq.data(I, J) = True And board(I + sq.Y + YOffset, J + sq.X + XOffset) = True Then
                    Return True
                End If
            Next
        Next
        Return False
    End Function

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim pb As PictureBox
        For I = 0 To 9
            For J = 0 To 9
                pb = New PictureBox
                pb.ImageLocation = "C:\Users\joshwaxman.CSDEPT.000\Pictures\box.png"
                pb.Location = New Point(J * 16, I * 16)
                pb.Size = New Point(16, 16)
                pb.SizeMode = PictureBoxSizeMode.StretchImage
                pb.Visible = False
                Controls.Add(pb)
                drawnBoard(I, J) = pb
            Next J
        Next I

        board(6, 3) = True
        drawnBoard(6, 3).Visible = True

        sq = New Square()

        Timer1.Interval = 100
        Timer1.Enabled = True
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick

        ' draw the piece
        For I = 0 To sq.data.GetUpperBound(0)
            For J = 0 To sq.data.GetUpperBound(1)
                If sq.data(I, J) = True Then
                    drawnBoard(sq.Y + I, sq.X + J).Visible = True
                Else
                    drawnBoard(sq.Y + I, sq.X + J).Visible = False
                End If
            Next
        Next
    End Sub

    Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
        moveDown()
    End Sub
End Class

Monday, August 2, 2010

Sample Tests

A midterm 2
A final
Another final
Another midterm 2

Tetris step 1

Public Class Form1
    Dim board(9, 9) As Boolean
    Dim drawnBoard(9, 9) As PictureBox
    Dim sq As Square
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim pb As PictureBox
        For I = 0 To 9
            For J = 0 To 9
                pb = New PictureBox
                pb.ImageLocation = "C:\Users\joshwaxman.CSDEPT.000\Pictures\box.png"
                pb.Location = New Point(J * 16, I * 16)
                pb.Size = New Point(16, 16)
                pb.SizeMode = PictureBoxSizeMode.StretchImage
                pb.Visible = False
                Controls.Add(pb)
                drawnBoard(I, J) = pb
            Next J
        Next I
        sq = New Square()
        sq.Construct()
        Timer1.Interval = 1000
        Timer1.Enabled = True
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        ' draw the piece
        For I = 0 To sq.data.GetUpperBound(0)
            For J = 0 To sq.data.GetUpperBound(1)
                If sq.data(I, J) = True Then
                    drawnBoard(sq.Y + I, sq.X + J).Visible = True
                Else
                    drawnBoard(sq.Y + I, sq.X + J).Visible = False
                End If
            Next
        Next
    End Sub
End Class

Public Class Square
    Public data(1, 1) As Boolean
    Public X As Integer
    Public Y As Integer
    Public Sub Construct()
        data(0, 0) = True
        data(0, 1) = True
        data(1, 0) = True
        data(1, 1) = True
    End Sub
End Class

Thursday, July 29, 2010

Public Class Form1
    Declare Auto Function PlaySound Lib "winmm.dll" (ByVal name _
      As String, ByVal hmod As Integer, ByVal flags As Integer) As Integer

    Declare Auto Function PlaySound Lib "winmm.dll" (ByVal name _
      As Byte(), ByVal hmod As Integer, ByVal flags As Integer) As Integer

    Dim apples As New List(Of PictureBox)
    'Dim apple As PictureBox

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Dim txt As TextBox
        'For i = 0 To 9
        '    txt = New TextBox
        '    txt.BackColor = Color.AliceBlue
        '    txt.Location = New Point(100, i * 50)
        '    Me.Controls.Add(txt)
        '    txtboxes(i) = txt
        'Next
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick

        For Each apple In apples
            ' check if apple hit the ground
            If apple.Location.Y + apple.Size.Height >= theGround.Location.Y Then
                ' play a sound

                ' remove the item and penalize the player
                removeAnApple(apple)
                Label1.Text = Label1.Text - 500
                Exit For
            Else
                ' animate it
                apple.Location = New Point(apple.Location.X, apple.Location.Y + 4)
            End If
        Next
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        PlaySound("C:\Users\Public\Music\Sample Music\Kalimba.mp3", 1, 0)

        addAnApple()
        Timer1.Enabled = True

    End Sub

    Sub removeAnApple(ByVal apple As PictureBox)
        Me.Controls.Remove(apple)
        apples.Remove(apple)
    End Sub


    Private Sub apple_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        removeAnApple(sender)

        Label1.Text = Label1.Text + 100
        addAnApple()
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        addAnApple()
    End Sub

    Sub addAnApple()
        Dim r As New Random

        Dim apple As PictureBox
        apple = New PictureBox
        ' Me.Width(-90)
        apple.Location = New Point(r.Next(0, Me.Width - 90), 12)
        apple.ImageLocation = "C:\Users\joshwaxman.CSDEPT.000\Pictures\APPLE.jpg"
        apple.SizeMode = PictureBoxSizeMode.StretchImage
        apple.Size = New Point(90, 90)
        AddHandler apple.Click, AddressOf apple_Click

        Me.Controls.Add(apple)
        apples.Add(apple)
    End Sub
End Class

Wednesday, July 28, 2010

Public Class Form1
    Dim txtboxes(9) As TextBox
    Dim apple As PictureBox

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Dim txt As TextBox
        'For i = 0 To 9
        '    txt = New TextBox
        '    txt.BackColor = Color.AliceBlue
        '    txt.Location = New Point(100, i * 50)
        '    Me.Controls.Add(txt)
        '    txtboxes(i) = txt
        'Next
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        ' animate!
        If apple IsNot Nothing Then
            apple.Location = New Point(apple.Location.X, apple.Location.Y + 4)
        End If
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        addAnApple()
        Timer1.Enabled = True

    End Sub



    Private Sub apple_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Me.Controls.Remove(apple)
        apple = Nothing

        Label1.Text = Label1.Text + 100
        addAnApple()
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        addAnApple()
    End Sub

    Sub addAnApple()
        Dim r As New Random

        apple = New PictureBox
        ' Me.Width(-90)
        apple.Location = New Point(r.Next(0, Me.Width - 90), 12)
        apple.ImageLocation = "C:\Users\joshwaxman.CSDEPT.000\Pictures\APPLE.jpg"
        apple.SizeMode = PictureBoxSizeMode.StretchImage
        apple.Size = New Point(90, 90)
        AddHandler apple.Click, AddressOf apple_Click

        Me.Controls.Add(apple)
    End Sub
End Class

insertion sort

for pos = 0 to UBound(A)
    val = A(pos)
    for j = pos - 1 to 0 Step -1
        if val < A(j) then
            A(j+1) = A(j)
        else
            exit for
        endif
    next j
    A(j+1) = val
next pos