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