Initial commit
This commit is contained in:
commit
c42d5896ea
39 changed files with 6278 additions and 0 deletions
995
StudentForms/ConnectFour/frmConnect4Hotseat.vb
Normal file
995
StudentForms/ConnectFour/frmConnect4Hotseat.vb
Normal file
|
@ -0,0 +1,995 @@
|
|||
Imports MySql.Data.MySqlClient
|
||||
|
||||
Public Class frmConnect4Hotseat
|
||||
|
||||
'Declares the variable used to determine the horizontal coordinate of the square being used
|
||||
Dim x As Integer = 1
|
||||
'Declares the variable used to determine the vertical coordinate of the square being used
|
||||
Dim y As Integer = 1
|
||||
'Declares the array containing the coordinates of all the pictureboxes that make up the grid
|
||||
Dim Group(7, 6) As PictureBox
|
||||
'Declares the array containing the coordinates of the squares of the grid and what condition they are currently in
|
||||
'0 = Empty
|
||||
'1 = Red
|
||||
'2= Yellow
|
||||
'3 = Terminator
|
||||
Dim theGrid(7, 7) As Integer
|
||||
'Declares the array containing the number of the buttons for dropping counters
|
||||
Dim Buttons(7) As Button
|
||||
|
||||
'Declares the class used for both players
|
||||
Public Class C4Play
|
||||
'Declares the variables used for storing the details of the Connect Four player
|
||||
Public Colour, Username, Fname, lname As String
|
||||
End Class
|
||||
'Declares the class used for both players' scores
|
||||
Public Class Score
|
||||
'Declares the variable used for storing score of the player
|
||||
Public ScoreNum As Integer
|
||||
'This subroutine runs when a player wins a game
|
||||
Public Sub Increase()
|
||||
'Increases the score by 1
|
||||
ScoreNum = ScoreNum + 1
|
||||
End Sub
|
||||
End Class
|
||||
'Creates two objects of the Score class, one for each player
|
||||
Dim RedScore As New Score
|
||||
Dim YellowScore As New Score
|
||||
'Creates two objects of the C4Play class, one for each player
|
||||
Dim RedPlayer As New C4Play
|
||||
Dim YellowPlayer As New C4Play
|
||||
|
||||
'Declares the variable used to store the primary key of the question record in the database
|
||||
Dim QuestionID As Integer
|
||||
'Declares the y-coords for moving the current player label
|
||||
Dim StudentCurrLocationY As Integer = 35
|
||||
Dim OppStudentCurrLocationY As Integer = 140
|
||||
'Declares the variable used for detecting wins along the y-axis
|
||||
Dim Why As Integer
|
||||
'Declares the variable used to keep track of how many reds in a row there are
|
||||
Dim RedAddUp As Integer = 0
|
||||
'Declares the variable used to keep track of how many yellow in a row there are
|
||||
Dim YellowAddUp As Integer = 0
|
||||
'Declares the variable used to determine if a question was answered correctly
|
||||
Dim QCorrect As Boolean = False
|
||||
'Declares the variables used to store whether there is a winner or the game is a draw
|
||||
Dim Won As Boolean = False
|
||||
Dim Draw As Boolean = False
|
||||
|
||||
'Subroutine runs on form load
|
||||
Private Sub frmConnect4Hotseat_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
|
||||
'Runs the OpenDB subroutine
|
||||
OpenDB()
|
||||
|
||||
'If the logged-in student is red...
|
||||
If LoggedInStudent.C4Player = "Red" Then
|
||||
'Populate the RedPlayer object with the logged-in student's details
|
||||
With RedPlayer
|
||||
.Colour = "Red"
|
||||
.Username = LoggedInStudent.Username
|
||||
.Fname = LoggedInStudent.Fname
|
||||
.lname = LoggedInStudent.Lname
|
||||
End With
|
||||
|
||||
'Populate the YellowPlayer object with the opponent student's details
|
||||
With YellowPlayer
|
||||
.Colour = "Yellow"
|
||||
.Username = OppStudent.Username
|
||||
.Fname = OppStudent.Fname
|
||||
.lname = OppStudent.Lname
|
||||
End With
|
||||
'But if the logged-in student is yellow...
|
||||
Else
|
||||
'Populate the RedPlayer object with the logged-in student's details
|
||||
With RedPlayer
|
||||
.Colour = "Red"
|
||||
.Username = OppStudent.Username
|
||||
.Fname = OppStudent.Fname
|
||||
.lname = OppStudent.Lname
|
||||
End With
|
||||
|
||||
'Populate the YellowPlayer object with the opponent student's details
|
||||
With YellowPlayer
|
||||
.Colour = "Yellow"
|
||||
.Username = LoggedInStudent.Username
|
||||
.Fname = LoggedInStudent.Fname
|
||||
.lname = LoggedInStudent.Lname
|
||||
End With
|
||||
End If
|
||||
|
||||
'Runs the AccountSection subroutine
|
||||
AccountSection()
|
||||
'Runs the MakeGrid subroutine
|
||||
MakeGrid()
|
||||
'Runs the Terminators subroutine
|
||||
Terminators()
|
||||
|
||||
'Declares the variables used to store the current horizontal and vertical coordinates of the grid
|
||||
Dim GridHor, GridVer As Integer
|
||||
|
||||
'For each column of the grid...
|
||||
For GridHor = 1 To 7
|
||||
'For each square in that column...
|
||||
For GridVer = 1 To 6
|
||||
'Set the value to empty
|
||||
theGrid(GridHor, GridVer) = 0
|
||||
Next
|
||||
Next
|
||||
|
||||
'Sets the scores to the defaults
|
||||
RedScore.ScoreNum = 0
|
||||
YellowScore.ScoreNum = 0
|
||||
|
||||
'Sets the current player, runs the ChangePlayer and CurrPlayer subroutines
|
||||
C4Player = "Yellow"
|
||||
ChangePlayer()
|
||||
CurrPlayer()
|
||||
End Sub
|
||||
|
||||
'Subroutine runs when called in DetectWinner subroutine
|
||||
Sub DatabaseDetails()
|
||||
'Declares the StreamWriter used to write to the game breakdown text files
|
||||
Dim writer As System.IO.StreamWriter
|
||||
Dim sql As String
|
||||
Dim dbcomm As MySqlCommand
|
||||
Dim dbread As MySqlDataReader
|
||||
|
||||
'If the red player is the winner...
|
||||
If RedScore.ScoreNum = 1 Then
|
||||
If RedPlayer.Username = LoggedInStudent.Username Then
|
||||
'Builds SQL query to execute
|
||||
sql = "UPDATE `tblstudents` SET `Wins`=`Wins`+1 WHERE `Username`='" & RedPlayer.Username & "';"
|
||||
|
||||
dbcomm = New MySqlCommand(sql, DBConn)
|
||||
dbread = dbcomm.ExecuteReader()
|
||||
dbread.Close()
|
||||
|
||||
'Sets the path to where the log shall be generated, and the filename
|
||||
writer = My.Computer.FileSystem.OpenTextFileWriter(RedPlayer.Username & ".txt", True)
|
||||
|
||||
'Writes the relevant data to the log
|
||||
writer.WriteLine(RedPlayer.Fname & " " & RedPlayer.lname & " beat " & YellowPlayer.Fname & " " & YellowPlayer.lname & " in Connect Four - " & TimeOfDay & " " & DateValue(Now))
|
||||
'Saves the log file
|
||||
writer.Close()
|
||||
ElseIf YellowPlayer.Username = LoggedInStudent.Username Then
|
||||
'Builds SQL query to execute
|
||||
sql = "UPDATE `tblstudents` SET `Losses`=`Losses`+1 WHERE `Username`='" & YellowPlayer.Username & "';"
|
||||
|
||||
dbcomm = New MySqlCommand(sql, DBConn)
|
||||
dbread = dbcomm.ExecuteReader()
|
||||
dbread.Close()
|
||||
|
||||
'Sets the path to where the log shall be generated, and the filename
|
||||
writer = My.Computer.FileSystem.OpenTextFileWriter(YellowPlayer.Username & ".txt", True)
|
||||
|
||||
'Writes the relevant data to the log
|
||||
writer.WriteLine(YellowPlayer.Fname & " " & YellowPlayer.lname & " was beaten by " & RedPlayer.Fname & " " & RedPlayer.lname & " in Connect Four - " & TimeOfDay & " " & DateValue(Now))
|
||||
'Saves the log file
|
||||
writer.Close()
|
||||
End If
|
||||
|
||||
'Resets the red score back to its default
|
||||
RedScore.ScoreNum = 0
|
||||
End If
|
||||
'However, if the yellow player is the winner...
|
||||
If YellowScore.ScoreNum = 1 Then
|
||||
If YellowPlayer.Username = LoggedInStudent.Username Then
|
||||
'Builds SQL query to execute
|
||||
sql = "UPDATE `tblstudents` SET `Wins`=`Wins`+1 WHERE `Username`='" & YellowPlayer.Username & "';"
|
||||
|
||||
dbcomm = New MySqlCommand(sql, DBConn)
|
||||
dbread = dbcomm.ExecuteReader()
|
||||
dbread.Close()
|
||||
|
||||
'Sets the path to where the log shall be generated, and the filename
|
||||
writer = My.Computer.FileSystem.OpenTextFileWriter(YellowPlayer.Username & ".txt", True)
|
||||
|
||||
'Writes the relevant data to the log
|
||||
writer.WriteLine(YellowPlayer.Fname & " " & YellowPlayer.lname & " beat " & RedPlayer.Fname & " " & RedPlayer.lname & " in Connect Four - " & TimeOfDay & " " & DateValue(Now))
|
||||
'Saves the log file
|
||||
writer.Close()
|
||||
ElseIf RedPlayer.Username = LoggedInStudent.Username Then
|
||||
'Builds SQL query to execute
|
||||
sql = "UPDATE `tblstudents` SET `Losses`=`Losses`+1 WHERE `Username`='" & RedPlayer.Username & "';"
|
||||
|
||||
dbcomm = New MySqlCommand(sql, DBConn)
|
||||
dbread = dbcomm.ExecuteReader()
|
||||
dbread.Close()
|
||||
|
||||
'Sets the path to where the log shall be generated, and the filename
|
||||
writer = My.Computer.FileSystem.OpenTextFileWriter(RedPlayer.Username & ".txt", True)
|
||||
|
||||
'Writes the relevant data to the log
|
||||
writer.WriteLine(RedPlayer.Fname & " " & RedPlayer.lname & " was beaten by " & YellowPlayer.Fname & " " & YellowPlayer.lname & " in Connect Four - " & TimeOfDay & " " & DateValue(Now))
|
||||
'Saves the log file
|
||||
writer.Close()
|
||||
End If
|
||||
'Resets the red score back to its default
|
||||
YellowScore.ScoreNum = 0
|
||||
End If
|
||||
'However, if the game is a draw...
|
||||
If Draw = True Then
|
||||
If RedPlayer.Username = LoggedInStudent.Username Then
|
||||
'Builds SQL query to execute
|
||||
sql = "UPDATE `tblstudents` SET `Draws`=`Draws`+1 WHERE `Username`='" & RedPlayer.Username & "';"
|
||||
|
||||
dbcomm = New MySqlCommand(sql, DBConn)
|
||||
dbread = dbcomm.ExecuteReader()
|
||||
dbread.Close()
|
||||
|
||||
'Sets the path to where the log shall be generated, and the filename
|
||||
writer = My.Computer.FileSystem.OpenTextFileWriter(RedPlayer.Username & ".txt", True)
|
||||
|
||||
'Writes the relevant data to the log
|
||||
writer.WriteLine(RedPlayer.Fname & " " & RedPlayer.lname & " drew with " & YellowPlayer.Fname & " " & YellowPlayer.lname & " in Connect Four - " & TimeOfDay & " " & DateValue(Now))
|
||||
'Saves the log file
|
||||
writer.Close()
|
||||
ElseIf YellowPlayer.Username = LoggedInStudent.Username Then
|
||||
'Builds SQL query to execute
|
||||
sql = "UPDATE `tblstudents` SET `Draws`=`Draws`+1 WHERE `Username`='" & YellowPlayer.Username & "';"
|
||||
|
||||
dbcomm = New MySqlCommand(sql, DBConn)
|
||||
dbread = dbcomm.ExecuteReader()
|
||||
dbread.Close()
|
||||
|
||||
'Sets the path to where the log shall be generated, and the filename
|
||||
writer = My.Computer.FileSystem.OpenTextFileWriter(YellowPlayer.Username & ".txt", True)
|
||||
|
||||
'Writes the relevant data to the log
|
||||
writer.WriteLine(YellowPlayer.Fname & " " & YellowPlayer.lname & " drew with " & RedPlayer.Fname & " " & RedPlayer.lname & " in Connect Four - " & TimeOfDay & " " & DateValue(Now))
|
||||
'Saves the log file
|
||||
writer.Close()
|
||||
End If
|
||||
End If
|
||||
End Sub
|
||||
'Subroutine runs when called in the ChangePlayer subroutine
|
||||
Sub CurrPlayer()
|
||||
'If the logged-in player is the same as the current player, which is red...
|
||||
If LoggedInStudent.C4Player = "Red" Then
|
||||
If C4Player = "Red" Then
|
||||
'Places the current player label pointing to the logged-in student
|
||||
lblCurrPlayer.Top = StudentCurrLocationY
|
||||
'However if the current player is yellow...
|
||||
Else
|
||||
'Places the current player label pointing to the opponent student
|
||||
lblCurrPlayer.Top = OppStudentCurrLocationY
|
||||
End If
|
||||
'However, if the opponent student is the same as the current player, which is yellow...
|
||||
Else
|
||||
If C4Player = "Yellow" Then
|
||||
'Places the current player label pointing to the logged-in student
|
||||
lblCurrPlayer.Top = StudentCurrLocationY
|
||||
'However if the current player is red...
|
||||
Else
|
||||
'Places the current player label pointing to the opponent student
|
||||
lblCurrPlayer.Top = OppStudentCurrLocationY
|
||||
End If
|
||||
End If
|
||||
End Sub
|
||||
|
||||
'Subroutine runs when called in form load subroutine
|
||||
Sub AccountSection()
|
||||
'Populates the player name labels with data and get a picture of each player
|
||||
lblStudentName.Text = LoggedInStudent.Fname & " " & LoggedInStudent.Lname
|
||||
picStudent.ImageLocation = RedPlayer.Username & ".jpg"
|
||||
|
||||
lblOppStudentName.Text = OppStudent.Fname & " " & OppStudent.Lname
|
||||
picOppStudentPic.ImageLocation = YellowPlayer.Username & ".jpg"
|
||||
End Sub
|
||||
|
||||
'Subroutine runs when called in form load subroutine
|
||||
Sub MakeGrid()
|
||||
'Ties the Buttons array with the button controls for dropping counters
|
||||
Buttons(1) = btn1
|
||||
Buttons(2) = btn2
|
||||
Buttons(3) = btn3
|
||||
Buttons(4) = btn4
|
||||
Buttons(5) = btn5
|
||||
Buttons(6) = btn6
|
||||
Buttons(7) = btn7
|
||||
|
||||
'Ties the Group coordinates with the pictureboxes on the form
|
||||
Group(1, 1) = pb1dash1
|
||||
Group(1, 2) = pb1dash2
|
||||
Group(1, 3) = pb1dash3
|
||||
Group(1, 4) = pb1dash4
|
||||
Group(1, 5) = pb1dash5
|
||||
Group(1, 6) = pb1dash6
|
||||
|
||||
Group(2, 1) = pb2dash1
|
||||
Group(2, 2) = pb2dash2
|
||||
Group(2, 3) = pb2dash3
|
||||
Group(2, 4) = pb2dash4
|
||||
Group(2, 5) = pb2dash5
|
||||
Group(2, 6) = pb2dash6
|
||||
|
||||
Group(3, 1) = pb3dash1
|
||||
Group(3, 2) = pb3dash2
|
||||
Group(3, 3) = pb3dash3
|
||||
Group(3, 4) = pb3dash4
|
||||
Group(3, 5) = pb3dash5
|
||||
Group(3, 6) = pb3dash6
|
||||
|
||||
Group(4, 1) = pb4dash1
|
||||
Group(4, 2) = pb4dash2
|
||||
Group(4, 3) = pb4dash3
|
||||
Group(4, 4) = pb4dash4
|
||||
Group(4, 5) = pb4dash5
|
||||
Group(4, 6) = pb4dash6
|
||||
|
||||
Group(5, 1) = pb5dash1
|
||||
Group(5, 2) = pb5dash2
|
||||
Group(5, 3) = pb5dash3
|
||||
Group(5, 4) = pb5dash4
|
||||
Group(5, 5) = pb5dash5
|
||||
Group(5, 6) = pb5dash6
|
||||
|
||||
Group(6, 1) = pb6dash1
|
||||
Group(6, 2) = pb6dash2
|
||||
Group(6, 3) = pb6dash3
|
||||
Group(6, 4) = pb6dash4
|
||||
Group(6, 5) = pb6dash5
|
||||
Group(6, 6) = pb6dash6
|
||||
|
||||
Group(7, 1) = pb7dash1
|
||||
Group(7, 2) = pb7dash2
|
||||
Group(7, 3) = pb7dash3
|
||||
Group(7, 4) = pb7dash4
|
||||
Group(7, 5) = pb7dash5
|
||||
Group(7, 6) = pb7dash6
|
||||
End Sub
|
||||
|
||||
'Subroutine runs when called in form load and btnReset_Click subroutines
|
||||
Sub Terminators()
|
||||
'Sets the extra seventh horizontal line of squares in theGrid to terminators
|
||||
While y <> 8
|
||||
theGrid(y, 7) = 3
|
||||
y = y + 1
|
||||
End While
|
||||
|
||||
'Sets x back to 1 for use later
|
||||
y = 1
|
||||
End Sub
|
||||
|
||||
'Subroutine runs when called in button click subroutines
|
||||
Sub CounterPlace()
|
||||
'Runs down the column to find the next blank space
|
||||
While theGrid(x, y) = 0
|
||||
y = y + 1
|
||||
End While
|
||||
|
||||
'Changes the next blank space into the current player's marker and claims the square for them
|
||||
If theGrid(x, 2) = 0 Then
|
||||
y = y - 1
|
||||
|
||||
If C4Player = "Red" Then
|
||||
theGrid(x, y) = 1
|
||||
Group(x, y).Image = My.Resources.red
|
||||
Else
|
||||
theGrid(x, y) = 2
|
||||
Group(x, y).Image = My.Resources.yellow
|
||||
End If
|
||||
Else
|
||||
'If the only square left in the current row is the topmost one, the button disables after being pressed to seal off the column
|
||||
y = y - 1
|
||||
If C4Player = "Red" Then
|
||||
theGrid(x, y) = 1
|
||||
Group(x, y).Image = My.Resources.red
|
||||
Else
|
||||
theGrid(x, y) = 2
|
||||
Group(x, y).Image = My.Resources.yellow
|
||||
End If
|
||||
Buttons(x).Enabled = False
|
||||
End If
|
||||
|
||||
'Detects whether there is a winner
|
||||
DetectWinner()
|
||||
|
||||
'If the game is still on, runs the ChangePlayer subroutine
|
||||
If (Won = False) And (Draw = False) Then
|
||||
ChangePlayer()
|
||||
End If
|
||||
End Sub
|
||||
|
||||
'Subroutine runs when called in CounterPlace subroutine
|
||||
Sub DetectWinner()
|
||||
'Declares the variable used to determine number of spaces to the left of the last-placed counter
|
||||
Dim SpacestoLeft As Integer
|
||||
'Declares the variable used to determine number of spaces to the right of the last-placed counter
|
||||
Dim SpacestoRight As Integer
|
||||
'Declares the variable used to determine number of spaces above the last-placed counter
|
||||
Dim SpacesAbove As Integer
|
||||
'Declares the variable used to determine number of spaces below the last-placed counter
|
||||
Dim SpacesBelow As Integer
|
||||
|
||||
'///WIN CONDITIONS\\\
|
||||
|
||||
'/HORIZONTAL\
|
||||
|
||||
'Determines no. of spaces to left and right of last-placed coutner
|
||||
SpacestoLeft = x - 1
|
||||
SpacestoRight = 7 - x
|
||||
|
||||
'Runs along the row of the last-placed counter to see if there are four red or four yellows in a row
|
||||
For HorizSquare As Integer = x - SpacestoLeft To x + SpacestoRight
|
||||
If theGrid(HorizSquare, y) = 1 Then
|
||||
YellowAddUp = 0
|
||||
RedAddUp = RedAddUp + 1
|
||||
'If there are four reds in a row horizontally...
|
||||
If RedAddUp = 4 Then
|
||||
'Gives the red player a win, pops up a messagebox, makes changes to the database and sets up the game to be reset
|
||||
Won = True
|
||||
RedScore.Increase()
|
||||
MsgBox("Red wins")
|
||||
DatabaseDetails()
|
||||
btn1.Enabled = False
|
||||
btn2.Enabled = False
|
||||
btn3.Enabled = False
|
||||
btn4.Enabled = False
|
||||
btn5.Enabled = False
|
||||
btn6.Enabled = False
|
||||
btn7.Enabled = False
|
||||
lblCurrPlayer.Visible = False
|
||||
btnReset.Visible = True
|
||||
End If
|
||||
End If
|
||||
If theGrid(HorizSquare, y) = 2 Then
|
||||
RedAddUp = 0
|
||||
YellowAddUp = YellowAddUp + 1
|
||||
'Of there are four yellows in a row horizontally...
|
||||
If YellowAddUp = 4 Then
|
||||
'Gives the yellow player a win, pops up a messagebox, makes changes to the database and sets up the game to be reset
|
||||
Won = True
|
||||
YellowScore.Increase()
|
||||
MsgBox("Yellow wins")
|
||||
DatabaseDetails()
|
||||
btn1.Enabled = False
|
||||
btn2.Enabled = False
|
||||
btn3.Enabled = False
|
||||
btn4.Enabled = False
|
||||
btn5.Enabled = False
|
||||
btn6.Enabled = False
|
||||
btn7.Enabled = False
|
||||
lblCurrPlayer.Visible = False
|
||||
btnReset.Visible = True
|
||||
End If
|
||||
End If
|
||||
If theGrid(HorizSquare, y) = 0 Then
|
||||
RedAddUp = 0
|
||||
YellowAddUp = 0
|
||||
End If
|
||||
Next
|
||||
|
||||
'/VERTICAL\
|
||||
|
||||
'Determines no. of spaces above and below the last-placed coutner
|
||||
SpacesAbove = y - 1
|
||||
SpacesBelow = 7 - y
|
||||
|
||||
'Runs down the column of the last-placed counter to see if there are four red or four yellows in a row
|
||||
For VertSquare As Integer = y - SpacesAbove To y + SpacesBelow
|
||||
If theGrid(x, VertSquare) = 1 Then
|
||||
YellowAddUp = 0
|
||||
RedAddUp = RedAddUp + 1
|
||||
'If there are four reds in a row vertically...
|
||||
If RedAddUp = 4 Then
|
||||
'Gives the red player a win, pops up a messagebox, makes changes to the database and sets up the game to be reset
|
||||
Won = True
|
||||
RedScore.Increase()
|
||||
MsgBox("Red wins")
|
||||
DatabaseDetails()
|
||||
btn1.Enabled = False
|
||||
btn2.Enabled = False
|
||||
btn3.Enabled = False
|
||||
btn4.Enabled = False
|
||||
btn5.Enabled = False
|
||||
btn6.Enabled = False
|
||||
btn7.Enabled = False
|
||||
lblCurrPlayer.Visible = False
|
||||
btnReset.Visible = True
|
||||
End If
|
||||
End If
|
||||
If theGrid(x, VertSquare) = 2 Then
|
||||
RedAddUp = 0
|
||||
YellowAddUp = YellowAddUp + 1
|
||||
'If there are four yellow in a row vertically...
|
||||
If RedAddUp = 4 Then
|
||||
'Gives the yellow player a win, pops up a messagebox, makes changes to the database and sets up the game to be reset
|
||||
Won = True
|
||||
YellowScore.Increase()
|
||||
MsgBox("Yellow wins")
|
||||
DatabaseDetails()
|
||||
btn1.Enabled = False
|
||||
btn2.Enabled = False
|
||||
btn3.Enabled = False
|
||||
btn4.Enabled = False
|
||||
btn5.Enabled = False
|
||||
btn6.Enabled = False
|
||||
btn7.Enabled = False
|
||||
lblCurrPlayer.Visible = False
|
||||
btnReset.Visible = True
|
||||
End If
|
||||
End If
|
||||
If theGrid(x, VertSquare) = 0 Then
|
||||
RedAddUp = 0
|
||||
YellowAddUp = 0
|
||||
End If
|
||||
Next
|
||||
|
||||
'If a diagonal win in possible with the position of the last-placed counter...
|
||||
If x < 5 Then
|
||||
|
||||
'/DIAGONAL BOTTOM-UP\
|
||||
|
||||
'Runs the Diag (Bottom-Up) Win Detection subroutine
|
||||
DiagBUWin()
|
||||
|
||||
End If
|
||||
|
||||
'If a diagonal win in possible with the position of the last-placed counter...
|
||||
If x > 3 Then
|
||||
|
||||
'/DIAGONAL TOP-DOWN\
|
||||
|
||||
'Runs the Diag (Top-Down) Win Detection subroutine
|
||||
DiagTDWin()
|
||||
|
||||
End If
|
||||
|
||||
'If there is no winner...
|
||||
If Won <> True Then
|
||||
'Declares the variable used to keep track of how many blank squares there are left
|
||||
Dim Blanks As Integer = 42
|
||||
'Goes through the grid row-by-row, column-by-column, decrementing the blanks value when a non-blank square in encountered
|
||||
For Why = 1 To 6
|
||||
For x = 1 To 7
|
||||
If theGrid(x, Why) <> 0 Then
|
||||
Blanks = Blanks - 1
|
||||
End If
|
||||
Next
|
||||
Next
|
||||
'If there are no blanks left...
|
||||
If Blanks = 0 Then
|
||||
'Declares the game a draw, amends the database, sets the form up to reset
|
||||
Draw = True
|
||||
MsgBox("No-one wins, it's a draw")
|
||||
DatabaseDetails()
|
||||
lblCurrPlayer.Visible = False
|
||||
btnReset.Visible = True
|
||||
End If
|
||||
End If
|
||||
End Sub
|
||||
|
||||
'Subroutine runs when called in DetectWinner subroutine
|
||||
Sub DiagTDWin()
|
||||
'Declares the varables used to run through the diagonals
|
||||
Dim v, z, a, w As Integer
|
||||
|
||||
'Sets the variables to their defaults
|
||||
x = 1
|
||||
v = x
|
||||
a = x
|
||||
z = x + 3
|
||||
y = 1
|
||||
|
||||
'Runs through the diagonals to determine if there are four red or yellow counters in a row
|
||||
For Why = 1 To 3
|
||||
For x = 1 To 4
|
||||
w = x
|
||||
y = Why
|
||||
If theGrid(x, y) = 1 Then
|
||||
RedAddUp = 1
|
||||
For v = a To z
|
||||
x = x + 1
|
||||
y = y + 1
|
||||
If x < 8 Then
|
||||
If theGrid(x, y) = 1 Then
|
||||
RedAddUp = RedAddUp + 1
|
||||
End If
|
||||
'If there are four reds in a row diagonally...
|
||||
If RedAddUp = 4 Then
|
||||
'Gives the red player a win, pops up a messagebox, makes changes to the database and sets up the game to be reset
|
||||
Won = True
|
||||
RedScore.Increase()
|
||||
MsgBox("Red wins")
|
||||
DatabaseDetails()
|
||||
btn1.Enabled = False
|
||||
btn2.Enabled = False
|
||||
btn3.Enabled = False
|
||||
btn4.Enabled = False
|
||||
btn5.Enabled = False
|
||||
btn6.Enabled = False
|
||||
btn7.Enabled = False
|
||||
lblCurrPlayer.Visible = False
|
||||
btnReset.Visible = True
|
||||
Exit Sub
|
||||
End If
|
||||
End If
|
||||
Next
|
||||
End If
|
||||
|
||||
If theGrid(w, y) = 2 Then
|
||||
YellowAddUp = 1
|
||||
For v = a To z
|
||||
w = w + 1
|
||||
y = y + 1
|
||||
If w < 8 Then
|
||||
If theGrid(w, y) = 1 Then
|
||||
YellowAddUp = YellowAddUp + 1
|
||||
End If
|
||||
'If there are four yellow in a row diagonally...
|
||||
If YellowAddUp = 4 Then
|
||||
'Gives the yellow player a win, pops up a messagebox, makes changes to the database and sets up the game to be reset
|
||||
Won = True
|
||||
YellowScore.Increase()
|
||||
MsgBox("Yellow wins")
|
||||
DatabaseDetails()
|
||||
btn1.Enabled = False
|
||||
btn2.Enabled = False
|
||||
btn3.Enabled = False
|
||||
btn4.Enabled = False
|
||||
btn5.Enabled = False
|
||||
btn6.Enabled = False
|
||||
btn7.Enabled = False
|
||||
lblCurrPlayer.Visible = False
|
||||
btnReset.Visible = True
|
||||
Exit Sub
|
||||
End If
|
||||
End If
|
||||
Next
|
||||
End If
|
||||
Next
|
||||
Next
|
||||
|
||||
'Resets the values to their defaults
|
||||
RedAddUp = 0
|
||||
YellowAddUp = 0
|
||||
End Sub
|
||||
|
||||
'Subroutine runs when called in DetectWinner subroutine
|
||||
Sub DiagBUWin()
|
||||
'Declares the varables used to run through the diagonals
|
||||
Dim v, z, a, w As Integer
|
||||
|
||||
'Sets the variables to their defaults
|
||||
x = 1
|
||||
v = x
|
||||
a = x
|
||||
z = x + 3
|
||||
y = 6
|
||||
|
||||
'Runs through the diagonals to determine if there are four red or yellow counters in a row
|
||||
For Why = 4 To 6
|
||||
For x = 1 To 4
|
||||
w = x
|
||||
y = Why
|
||||
If theGrid(x, y) = 1 Then
|
||||
RedAddUp = 1
|
||||
For v = a To z
|
||||
x = x + 1
|
||||
y = y - 1
|
||||
If x < 8 Then
|
||||
If theGrid(x, y) = 1 Then
|
||||
RedAddUp = RedAddUp + 1
|
||||
End If
|
||||
'If there are four reds in a row diagonally...
|
||||
If RedAddUp = 4 Then
|
||||
'Gives the red player a win, pops up a messagebox, makes changes to the database and sets up the game to be reset
|
||||
Won = True
|
||||
RedScore.Increase()
|
||||
MsgBox("Red wins")
|
||||
DatabaseDetails()
|
||||
btn1.Enabled = False
|
||||
btn2.Enabled = False
|
||||
btn3.Enabled = False
|
||||
btn4.Enabled = False
|
||||
btn5.Enabled = False
|
||||
btn6.Enabled = False
|
||||
btn7.Enabled = False
|
||||
lblCurrPlayer.Visible = False
|
||||
btnReset.Visible = True
|
||||
Exit Sub
|
||||
End If
|
||||
End If
|
||||
Next
|
||||
End If
|
||||
|
||||
If theGrid(w, y) = 2 Then
|
||||
YellowAddUp = 1
|
||||
For v = a To z
|
||||
w = w + 1
|
||||
y = y - 1
|
||||
If w < 8 Then
|
||||
If theGrid(w, y) = 1 Then
|
||||
YellowAddUp = YellowAddUp + 1
|
||||
End If
|
||||
'If there are four yellows in a row diagonally...
|
||||
If YellowAddUp = 4 Then
|
||||
'Gives the yellow player a win, pops up a messagebox, makes changes to the database and sets up the game to be reset
|
||||
Won = True
|
||||
YellowScore.Increase()
|
||||
MsgBox("Yellow wins")
|
||||
DatabaseDetails()
|
||||
btn1.Enabled = False
|
||||
btn2.Enabled = False
|
||||
btn3.Enabled = False
|
||||
btn4.Enabled = False
|
||||
btn5.Enabled = False
|
||||
btn6.Enabled = False
|
||||
btn7.Enabled = False
|
||||
lblCurrPlayer.Visible = False
|
||||
btnReset.Visible = True
|
||||
Exit Sub
|
||||
End If
|
||||
End If
|
||||
Next
|
||||
End If
|
||||
Next
|
||||
Next
|
||||
|
||||
'Resets the values to their defaults
|
||||
RedAddUp = 0
|
||||
YellowAddUp = 0
|
||||
End Sub
|
||||
|
||||
'Subroutine runs when reset button is clicked
|
||||
Private Sub btnReset_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReset.Click
|
||||
'Resets the values to their defaults
|
||||
RedAddUp = 0
|
||||
YellowAddUp = 0
|
||||
|
||||
'Resets the grid to its original blank state
|
||||
For y = 1 To 7
|
||||
For x = 1 To 7
|
||||
theGrid(x, y) = 0
|
||||
If y < 7 Then
|
||||
Group(x, y).Image = Nothing
|
||||
End If
|
||||
Next
|
||||
Next
|
||||
|
||||
'Resets the game state variables
|
||||
Won = False
|
||||
Draw = False
|
||||
|
||||
'Resets the x and y coords of the grid
|
||||
x = 1
|
||||
y = 1
|
||||
|
||||
'Runs the Terminators subroutine
|
||||
Terminators()
|
||||
|
||||
'Resets the current player
|
||||
C4Player = "Red"
|
||||
CurrPlayer()
|
||||
|
||||
'Disables the counter drop buttons
|
||||
btn1.Visible = False
|
||||
btn2.Visible = False
|
||||
btn3.Visible = False
|
||||
btn4.Visible = False
|
||||
btn5.Visible = False
|
||||
btn6.Visible = False
|
||||
btn7.Visible = False
|
||||
'Enables them for later
|
||||
btn1.Enabled = True
|
||||
btn2.Enabled = True
|
||||
btn3.Enabled = True
|
||||
btn4.Enabled = True
|
||||
btn5.Enabled = True
|
||||
btn6.Enabled = True
|
||||
btn7.Enabled = True
|
||||
|
||||
'Runs the Question subroutine
|
||||
Question()
|
||||
|
||||
'Hides the reset button
|
||||
lblCurrPlayer.Visible = True
|
||||
btnReset.Visible = False
|
||||
End Sub
|
||||
|
||||
'Subroutines run when their respective buttons are clicked
|
||||
Private Sub btn1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn1.Click
|
||||
'Sets x to the x-coord of the selected column
|
||||
x = 1
|
||||
'Runs the CounterPlace subroutine
|
||||
CounterPlace()
|
||||
End Sub
|
||||
Private Sub btn2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn2.Click
|
||||
x = 2
|
||||
CounterPlace()
|
||||
End Sub
|
||||
Private Sub btn3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn3.Click
|
||||
x = 3
|
||||
CounterPlace()
|
||||
End Sub
|
||||
Private Sub btn4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn4.Click
|
||||
x = 4
|
||||
CounterPlace()
|
||||
End Sub
|
||||
Private Sub btn5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn5.Click
|
||||
x = 5
|
||||
CounterPlace()
|
||||
End Sub
|
||||
Private Sub btn6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn6.Click
|
||||
x = 6
|
||||
CounterPlace()
|
||||
End Sub
|
||||
Private Sub btn7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn7.Click
|
||||
x = 7
|
||||
CounterPlace()
|
||||
End Sub
|
||||
|
||||
'Subroutine runs when back button is clicked
|
||||
Private Sub btnBack_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBack.Click
|
||||
'Shows the Connect Four menu
|
||||
frmConnect4Menu.Show()
|
||||
'Closes this form
|
||||
Me.Close()
|
||||
End Sub
|
||||
|
||||
'Subroutine runs when called in ChangePlayer and btnReset_Click subroutines
|
||||
Sub Question()
|
||||
Dim sql As String
|
||||
Dim dbcomm As MySqlCommand
|
||||
Dim dbread As MySqlDataReader
|
||||
|
||||
sql = "SELECT * FROM tblquestions WHERE SubjectID='" & C4HSubject & "' AND Difficulty='" & C4HDifficulty & "' AND Topic='" & C4HTopic & "' ORDER BY RAND() LIMIT 1;"
|
||||
dbcomm = New MySqlCommand(sql, DBConn)
|
||||
dbread = dbcomm.ExecuteReader()
|
||||
|
||||
While dbread.Read
|
||||
'Sets the QuestionID variable to that of the selected question
|
||||
QuestionID = dbread("QuestionID")
|
||||
|
||||
'Makes the question controls visible
|
||||
grpQuestion.Visible = True
|
||||
lblQuestion.Visible = True
|
||||
txtAnswer.Visible = True
|
||||
btnSubmit.Visible = True
|
||||
|
||||
'Displays the selected question
|
||||
lblQuestion.Text = dbread("Question")
|
||||
End While
|
||||
'Closes the recordset
|
||||
dbread.Close()
|
||||
End Sub
|
||||
|
||||
'Subroutine runs when called in form load and CounterPlace subroutines
|
||||
Sub ChangePlayer()
|
||||
'Sets x & y back to 1 for use later
|
||||
x = 1
|
||||
y = 1
|
||||
|
||||
'Changes the current player
|
||||
If C4Player = "Red" Then
|
||||
C4Player = "Yellow"
|
||||
CurrPlayer()
|
||||
Else
|
||||
C4Player = "Red"
|
||||
CurrPlayer()
|
||||
End If
|
||||
|
||||
'Hides the counter placing buttons
|
||||
btn1.Visible = False
|
||||
btn2.Visible = False
|
||||
btn3.Visible = False
|
||||
btn4.Visible = False
|
||||
btn5.Visible = False
|
||||
btn6.Visible = False
|
||||
btn7.Visible = False
|
||||
|
||||
'Runs the Question subroutine
|
||||
Question()
|
||||
End Sub
|
||||
|
||||
'Subroutine runs then answer submit button is clicked
|
||||
Private Sub btnSubmit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
|
||||
Dim sql As String
|
||||
Dim dbcomm As MySqlCommand
|
||||
Dim dbread As MySqlDataReader
|
||||
Dim foob As Integer = 0
|
||||
'Hides the question controls
|
||||
grpQuestion.Visible = False
|
||||
lblQuestion.Visible = False
|
||||
txtAnswer.Visible = False
|
||||
btnSubmit.Visible = False
|
||||
|
||||
sql = "SELECT * FROM tblquestions WHERE QuestionID='" & QuestionID & "';"
|
||||
dbcomm = New MySqlCommand(sql, DBConn)
|
||||
dbread = dbcomm.ExecuteReader()
|
||||
While dbread.Read
|
||||
'If the answer is correct...
|
||||
If txtAnswer.Text = dbread("Answer") Then
|
||||
'Display a message box
|
||||
MsgBox("Correct!")
|
||||
|
||||
'Sets the question correct flag to true
|
||||
QCorrect = True
|
||||
|
||||
'Makes the counter placement buttons visible
|
||||
btn1.Visible = True
|
||||
btn2.Visible = True
|
||||
btn3.Visible = True
|
||||
btn4.Visible = True
|
||||
btn5.Visible = True
|
||||
btn6.Visible = True
|
||||
btn7.Visible = True
|
||||
|
||||
'However if the answer if incorrect...
|
||||
Else
|
||||
'Display a message box
|
||||
MsgBox("Incorrect!")
|
||||
|
||||
'Sets the question correct flag to false
|
||||
QCorrect = False
|
||||
|
||||
foob = 1
|
||||
End If
|
||||
End While
|
||||
|
||||
dbread.Close()
|
||||
|
||||
'Runs the QDatabase subroutine
|
||||
QDatabase()
|
||||
|
||||
If foob = 1 Then
|
||||
'Runs the ChangePlayer subroutine
|
||||
ChangePlayer()
|
||||
End If
|
||||
|
||||
'Blanks the answer textbox for the next question
|
||||
txtAnswer.Text = ""
|
||||
End Sub
|
||||
|
||||
'Subroutine runs when called in btnSubmit_Click subroutine
|
||||
Sub QDatabase()
|
||||
Dim sql As String
|
||||
Dim dbcomm As MySqlCommand
|
||||
Dim dbread As MySqlDataReader
|
||||
|
||||
If C4Player = "Red" Then
|
||||
If RedPlayer.Username = LoggedInStudent.Username Then
|
||||
sql = "INSERT INTO `cl51-ben`.`tblattempted` (`SubjectID`, `QuestionID`, `StudentID`, `When`, `Correct`) VALUES ('" & C4HSubject & "', '" & QuestionID & "', '" & LoggedInStudent.StudentID & "', '" & TimeOfDay & " " & DateValue(Now) & "', "
|
||||
Else
|
||||
sql = "INSERT INTO `cl51-ben`.`tblattempted` (`SubjectID`, `QuestionID`, `StudentID`, `When`, `Correct`) VALUES ('" & C4HSubject & "', '" & QuestionID & "', '" & OppStudent.StudentID & "', '" & TimeOfDay & " " & DateValue(Now) & "', "
|
||||
End If
|
||||
Else
|
||||
If YellowPlayer.Username = LoggedInStudent.Username Then
|
||||
sql = "INSERT INTO `cl51-ben`.`tblattempted` (`SubjectID`, `QuestionID`, `StudentID`, `When`, `Correct`) VALUES ('" & C4HSubject & "', '" & QuestionID & "', '" & LoggedInStudent.StudentID & "', '" & TimeOfDay & " " & DateValue(Now) & "', "
|
||||
Else
|
||||
sql = "INSERT INTO `cl51-ben`.`tblattempted` (`SubjectID`, `QuestionID`, `StudentID`, `When`, `Correct`) VALUES ('" & C4HSubject & "', '" & QuestionID & "', '" & OppStudent.StudentID & "', '" & TimeOfDay & " " & DateValue(Now) & "', "
|
||||
End If
|
||||
End If
|
||||
If QCorrect = True Then
|
||||
sql = sql & "'1');"
|
||||
Else
|
||||
sql = sql & "'0');"
|
||||
End If
|
||||
|
||||
dbcomm = New MySqlCommand(sql, DBConn)
|
||||
dbread = dbcomm.ExecuteReader()
|
||||
|
||||
'Resets the question correct flag
|
||||
QCorrect = False
|
||||
|
||||
dbread.Close()
|
||||
End Sub
|
||||
|
||||
'Subroutine runs when view logged-in student's profile button is clicked
|
||||
Private Sub btnViewProfile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnViewProfile.Click
|
||||
'Sets viewed profile to logged-in student's
|
||||
Viewing = 1
|
||||
'Shows the student account form
|
||||
frmStudentAccount.Show()
|
||||
End Sub
|
||||
|
||||
'Subroutine runs when view opponent student's profile button is clicked
|
||||
Private Sub btnOppViewProfile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOppViewProfile.Click
|
||||
'Sets viewed profile to opponent student's
|
||||
Viewing = 2
|
||||
'Shows the student account form
|
||||
frmStudentAccount.Show()
|
||||
End Sub
|
||||
|
||||
End Class
|
74
StudentForms/ConnectFour/frmConnect4HotseatLogin.vb
Normal file
74
StudentForms/ConnectFour/frmConnect4HotseatLogin.vb
Normal file
|
@ -0,0 +1,74 @@
|
|||
Imports MySql.Data.MySqlClient
|
||||
|
||||
Public Class frmConnect4HotseatLogin
|
||||
|
||||
'Declares the variables used to log in
|
||||
Dim EnteredUsername, EnteredPassword As String
|
||||
|
||||
'Subroutine runs when the form loads
|
||||
Private Sub frmConnect4HotseatLogin_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
|
||||
'Runs the OpenDB subroutine
|
||||
OpenDB()
|
||||
End Sub
|
||||
|
||||
'Subroutine runs when the okay button is clicked
|
||||
Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click
|
||||
'Runs the Login subroutine
|
||||
Login()
|
||||
End Sub
|
||||
|
||||
'Subroutine runs when called in btnOK_Click subroutine
|
||||
Sub Login()
|
||||
'Sets the EnteredUsername and EnteredPassword variables to the entered username and password
|
||||
EnteredUsername = txtUsername.Text
|
||||
EnteredPassword = txtPassword.Text
|
||||
|
||||
If EnteredUsername <> LoggedInStudent.Username Then
|
||||
Dim sql As String
|
||||
Dim dbcomm As MySqlCommand
|
||||
Dim dbread As MySqlDataReader
|
||||
Dim foo As Integer = 0
|
||||
sql = "SELECT * FROM tblstudents WHERE Username='" & EnteredUsername & "' AND Password='" & EnteredPassword & "';"
|
||||
dbcomm = New MySqlCommand(sql, DBConn)
|
||||
dbread = dbcomm.ExecuteReader()
|
||||
While dbread.Read
|
||||
'Fills the various properties of the OppStudent object with their respective values from the database
|
||||
OppStudent.Fname = dbread("Fname")
|
||||
OppStudent.Lname = dbread("Lname")
|
||||
OppStudent.Form = dbread("FormNum") & dbread("FormLetter")
|
||||
OppStudent.Wins = dbread("Wins")
|
||||
OppStudent.Losses = dbread("Losses")
|
||||
OppStudent.Draws = dbread("Draws")
|
||||
OppStudent.Username = dbread("Username")
|
||||
OppStudent.StudentID = dbread("StudentID")
|
||||
foo = 1
|
||||
End While
|
||||
If foo = 1 Then
|
||||
'Opens the Connect Four subject selection form
|
||||
frmConnect4HotseatSubject.Show()
|
||||
'Closes this form
|
||||
Me.Close()
|
||||
dbread.Close()
|
||||
Exit Sub
|
||||
End If
|
||||
'If the login details were invalid an error message will appear and the username and password textboxes will be blanked out
|
||||
MsgBox("Invalid: Incorrect username or password.")
|
||||
txtUsername.Text = ""
|
||||
txtPassword.Text = ""
|
||||
Else
|
||||
'If the login details where the same as those of the currently logged-in student, an error message will appear and the username and password textboxes will be blanked out
|
||||
MsgBox("Invalid: That's you.")
|
||||
txtUsername.Text = ""
|
||||
txtPassword.Text = ""
|
||||
End If
|
||||
End Sub
|
||||
|
||||
'Subroutine runs when the cancel button is clicked
|
||||
Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
|
||||
'Opens the Connect Four menu form
|
||||
frmConnect4Menu.Show()
|
||||
'Closes this form
|
||||
Me.Close()
|
||||
End Sub
|
||||
|
||||
End Class
|
89
StudentForms/ConnectFour/frmConnect4HotseatPlayerSelect.vb
Normal file
89
StudentForms/ConnectFour/frmConnect4HotseatPlayerSelect.vb
Normal file
|
@ -0,0 +1,89 @@
|
|||
Public Class frmConnect4HotseatPlayerSelect
|
||||
|
||||
'Subroutine runs on form load
|
||||
Private Sub frmConnect4HotseatPlayerSelect_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
|
||||
'Populates the player name labels with data
|
||||
lblLoggedInStudent.Text = LoggedInStudent.Fname & " " & LoggedInStudent.Lname
|
||||
lblOppStudent.Text = OppStudent.Fname & " " & OppStudent.Lname
|
||||
End Sub
|
||||
|
||||
'Subroutines run when the counter selection buttons are clicked
|
||||
Private Sub btnOSred_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOSred.Click
|
||||
'Sets the opponent student's colour to red
|
||||
OppStudent.C4Player = "Red"
|
||||
'Updates the appearance of the form
|
||||
picOSred.Image = My.Resources.red
|
||||
'Disables changing the colour and the logged-in student also picking red
|
||||
btnOSred.Enabled = False
|
||||
btnOSyellow.Enabled = False
|
||||
btnLISred.Enabled = False
|
||||
With picRed
|
||||
.Image = Nothing
|
||||
.Tag = "None"
|
||||
End With
|
||||
'Runs the CheckBoth subroutine
|
||||
CheckBoth()
|
||||
End Sub
|
||||
Private Sub btnOSyellow_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOSyellow.Click
|
||||
OppStudent.C4Player = "Yellow"
|
||||
picOSyellow.Image = My.Resources.yellow
|
||||
btnOSyellow.Enabled = False
|
||||
btnOSred.Enabled = False
|
||||
btnLISyellow.Enabled = False
|
||||
With picYellow
|
||||
.Image = Nothing
|
||||
.Tag = "None"
|
||||
End With
|
||||
CheckBoth()
|
||||
End Sub
|
||||
Private Sub btnLISred_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLISred.Click
|
||||
LoggedInStudent.C4Player = "Red"
|
||||
picLISred.Image = My.Resources.red
|
||||
btnLISred.Enabled = False
|
||||
btnLISyellow.Enabled = False
|
||||
btnOSred.Enabled = False
|
||||
With picRed
|
||||
.Image = Nothing
|
||||
.Tag = "None"
|
||||
End With
|
||||
CheckBoth()
|
||||
End Sub
|
||||
Private Sub btnLISyellow_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLISyellow.Click
|
||||
LoggedInStudent.C4Player = "Yellow"
|
||||
picLISyellow.Image = My.Resources.yellow
|
||||
btnLISyellow.Enabled = False
|
||||
btnLISred.Enabled = False
|
||||
btnOSyellow.Enabled = False
|
||||
With picYellow
|
||||
.Image = Nothing
|
||||
.Tag = "None"
|
||||
End With
|
||||
CheckBoth()
|
||||
End Sub
|
||||
|
||||
'Subroutine runs when called in the counter selection button click subroutines
|
||||
Sub CheckBoth()
|
||||
'If both players have chosen...
|
||||
If picYellow.Tag = "None" And picRed.Tag = "None" Then
|
||||
'Enables the button to leave the form
|
||||
btnContinue.Enabled = True
|
||||
End If
|
||||
End Sub
|
||||
|
||||
'Subroutine runs when the continue button is clicked
|
||||
Private Sub btnContinue_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnContinue.Click
|
||||
'Shows the Connect Four hotseat form
|
||||
frmConnect4Hotseat.Show()
|
||||
'Closes this form
|
||||
Me.Close()
|
||||
End Sub
|
||||
|
||||
'Subroutine runs when the cancel button is clicked
|
||||
Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
|
||||
'Shows the Connect Four hotseat question selection form
|
||||
frmConnect4HotseatSubject.Show()
|
||||
'Closes this form
|
||||
Me.Close()
|
||||
End Sub
|
||||
|
||||
End Class
|
92
StudentForms/ConnectFour/frmConnect4HotseatSubject.vb
Normal file
92
StudentForms/ConnectFour/frmConnect4HotseatSubject.vb
Normal file
|
@ -0,0 +1,92 @@
|
|||
Imports MySql.Data.MySqlClient
|
||||
|
||||
Public Class frmConnect4HotseatSubject
|
||||
'Declares the variable used to store the chosen SubjectID
|
||||
Dim SubjectID As Integer
|
||||
|
||||
'Subroutine runs on form load
|
||||
Private Sub frmConnect4HotseatSubject_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
|
||||
'Runs the OpenDB subroutine
|
||||
OpenDB()
|
||||
|
||||
'Runs the Populate subroutine
|
||||
Populate()
|
||||
End Sub
|
||||
|
||||
'Subroutine runs then the okay button is clicked
|
||||
Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click
|
||||
'If everything has been selected...
|
||||
If (cmboDifficulty.SelectedItem <> "") And (cmboSubject.SelectedItem <> "") And (cmboTopic.SelectedItem <> "") Then
|
||||
|
||||
'Sets the chosen subject, difficulty and topic variables
|
||||
C4HSubject = SubjectID
|
||||
C4HDifficulty = cmboDifficulty.SelectedItem
|
||||
C4HTopic = cmboTopic.SelectedItem
|
||||
|
||||
'Shows the Connect Four hotseat player selection form
|
||||
frmConnect4HotseatPlayerSelect.Show()
|
||||
'Closes this form
|
||||
Me.Close()
|
||||
Else
|
||||
'Otherwise display an error message
|
||||
MsgBox("Incorrect values")
|
||||
End If
|
||||
End Sub
|
||||
|
||||
'Subroutine runs when called at form load
|
||||
Sub Populate()
|
||||
Dim sql As String
|
||||
Dim dbcomm As MySqlCommand
|
||||
Dim dbread As MySqlDataReader
|
||||
|
||||
sql = "SELECT * FROM tblsubjects;"
|
||||
dbcomm = New MySqlCommand(sql, DBConn)
|
||||
dbread = dbcomm.ExecuteReader()
|
||||
While dbread.Read
|
||||
cmboSubject.Items.Add(dbread("Subject"))
|
||||
End While
|
||||
dbread.Close()
|
||||
End Sub
|
||||
|
||||
'Subroutine runs when the selected item of the subject combobox is changed
|
||||
Private Sub cmboSubject_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmboSubject.SelectedIndexChanged
|
||||
'Declares the StreamReader used to read the topic text file
|
||||
Dim Reader As System.IO.StreamReader
|
||||
|
||||
'Enables the topic combobox and clears it of any data
|
||||
cmboTopic.Enabled = True
|
||||
cmboTopic.Items.Clear()
|
||||
|
||||
'Sets the path to where the file is
|
||||
Reader = My.Computer.FileSystem.OpenTextFileReader(cmboSubject.SelectedItem & "Topics.txt")
|
||||
'Whilst not at the end of the text file...
|
||||
While Not Reader.EndOfStream
|
||||
'Add the topic to the combobox
|
||||
cmboTopic.Items.Add(Reader.ReadLine)
|
||||
End While
|
||||
|
||||
'Closes the streamreader
|
||||
Reader.Close()
|
||||
|
||||
Dim sql As String
|
||||
Dim dbcomm As MySqlCommand
|
||||
Dim dbread As MySqlDataReader
|
||||
|
||||
sql = "SELECT * FROM tblsubjects WHERE Subject='" & cmboSubject.SelectedItem & "';"
|
||||
dbcomm = New MySqlCommand(sql, DBConn)
|
||||
dbread = dbcomm.ExecuteReader()
|
||||
While dbread.Read
|
||||
SubjectID = dbread("SubjectID")
|
||||
End While
|
||||
dbread.Close()
|
||||
End Sub
|
||||
|
||||
'Subroutine runs when the cancel button is clicked
|
||||
Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
|
||||
'Shows the Connect Four hotseat login form
|
||||
frmConnect4HotseatLogin.Show()
|
||||
'Closes this form
|
||||
Me.Close()
|
||||
End Sub
|
||||
|
||||
End Class
|
27
StudentForms/ConnectFour/frmConnect4Menu.vb
Normal file
27
StudentForms/ConnectFour/frmConnect4Menu.vb
Normal file
|
@ -0,0 +1,27 @@
|
|||
Public Class frmConnect4Menu
|
||||
|
||||
'Subroutine runs when the hotseat game button is clicked
|
||||
Private Sub btnHotseat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnHotseat.Click
|
||||
'Shows the Connect Four hotseat login form
|
||||
frmConnect4HotseatLogin.Show()
|
||||
'Closes this form
|
||||
Me.Close()
|
||||
End Sub
|
||||
|
||||
'Subroutine runs when the network game button is clicked
|
||||
Private Sub btnNetwork_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNetwork.Click
|
||||
'Shows the Connect Four network lobby form
|
||||
frmConnect4NetworkLobby.Show()
|
||||
'Closes this form
|
||||
Me.Close()
|
||||
End Sub
|
||||
|
||||
'Subroutine runs when the back button is clicked
|
||||
Private Sub btnBack_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBack.Click
|
||||
'Shows the student home form
|
||||
frmStudentHome.Show()
|
||||
'Closes this form
|
||||
Me.Close()
|
||||
End Sub
|
||||
|
||||
End Class
|
1192
StudentForms/ConnectFour/frmConnect4Network.vb
Normal file
1192
StudentForms/ConnectFour/frmConnect4Network.vb
Normal file
File diff suppressed because it is too large
Load diff
114
StudentForms/ConnectFour/frmConnect4NetworkHostGame.vb
Normal file
114
StudentForms/ConnectFour/frmConnect4NetworkHostGame.vb
Normal file
|
@ -0,0 +1,114 @@
|
|||
Imports MySql.Data.MySqlClient
|
||||
|
||||
Public Class frmConnect4NetworkHostGame
|
||||
'Declares the variable used to store the chosen SubjectID
|
||||
Dim SubjectID As Integer
|
||||
|
||||
'Subroutine runs on form load
|
||||
Private Sub frmConnect4NetworkHostGame_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
|
||||
'Runs the OpenDB subroutine
|
||||
OpenDB()
|
||||
|
||||
'Runs the Populate subroutine
|
||||
Populate()
|
||||
End Sub
|
||||
|
||||
'Subroutine runs then the host game button is clicked
|
||||
Private Sub btnHost_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnHost.Click
|
||||
'Declares the variable used for getting the result of the message box
|
||||
Dim Result As MsgBoxResult
|
||||
'Declares the variable used for detecting invalid data entry
|
||||
Dim Errors As String = ""
|
||||
|
||||
Dim sql As String
|
||||
Dim dbcomm As MySqlCommand
|
||||
Dim dbread As MySqlDataReader
|
||||
|
||||
'Assembles an error report if any invalid data entry detected
|
||||
If txtGameName.Text = "" Then
|
||||
Errors = Errors & "No game name input" & vbCrLf
|
||||
End If
|
||||
If cmboSubject.SelectedItem = "" Then
|
||||
Errors = Errors & "No subject selected" & vbCrLf
|
||||
End If
|
||||
If cmboDifficulty.SelectedItem = "" Then
|
||||
Errors = Errors & "No difficulty selected" & vbCrLf
|
||||
End If
|
||||
If cmboTopic.SelectedItem = "" Then
|
||||
Errors = Errors & "No topic selected" & vbCrLf
|
||||
End If
|
||||
|
||||
'If no invalid data entry is detected...
|
||||
If Errors = "" Then
|
||||
'Displays a validation message box before saving the data to the database
|
||||
Result = MsgBox("Are you sure all these details are correct? Remember, spelling is vital." & vbCrLf & vbCrLf & "Details:" & vbCrLf & txtGameName.Text & vbCrLf & cmboSubject.SelectedItem & " (" & cmboDifficulty.SelectedItem & ")" & vbCrLf & cmboTopic.SelectedItem, MsgBoxStyle.YesNo)
|
||||
'If the data is approved by the user...
|
||||
If Result = MsgBoxResult.Yes Then
|
||||
'Builds SQL query to execute
|
||||
sql = "INSERT INTO `cl51-ben`.`tblconnect4` (`GameName`, `HostUsername`, `CurrentPlayer`, `C4NSubject`, `C4NDifficulty`, `C4NTopic`) VALUES ('" & txtGameName.Text & "', '" & LoggedInStudent.Username & "', 'Red', '" & SubjectID & "', '" & cmboDifficulty.SelectedItem & "', '" & cmboTopic.SelectedItem & "');"
|
||||
|
||||
dbcomm = New MySqlCommand(sql, DBConn)
|
||||
dbread = dbcomm.ExecuteReader()
|
||||
dbread.Close()
|
||||
End If
|
||||
'If any invalid data entry is detected...
|
||||
Else
|
||||
'Displays a message box with any detected invalid data entry
|
||||
MsgBox("Invalid input:" & vbCrLf & vbCrLf & Errors)
|
||||
End If
|
||||
End Sub
|
||||
|
||||
'Subroutine runs when called in form load subroutine
|
||||
Sub Populate()
|
||||
Dim sql As String
|
||||
Dim dbcomm As MySqlCommand
|
||||
Dim dbread As MySqlDataReader
|
||||
|
||||
'Builds SQL query to execute
|
||||
sql = "SELECT * FROM tblsubjects"
|
||||
|
||||
dbcomm = New MySqlCommand(sql, DBConn)
|
||||
dbread = dbcomm.ExecuteReader()
|
||||
'Fills the various properties of the LoggedInStudent object with their respective values from the database
|
||||
While dbread.Read
|
||||
cmboSubject.Items.Add(dbread("Subject"))
|
||||
End While
|
||||
dbread.Close()
|
||||
End Sub
|
||||
|
||||
'Subroutine runs when the selected item in the subject combobox is changed
|
||||
Private Sub cmboSubject_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmboSubject.SelectedIndexChanged
|
||||
'Declares the StreamReader used to read the topic text file
|
||||
Dim Reader As System.IO.StreamReader
|
||||
|
||||
'Enables the topic combobox and clears it of any data
|
||||
cmboTopic.Enabled = True
|
||||
cmboTopic.Items.Clear()
|
||||
|
||||
'Sets the path to where the file is
|
||||
Reader = My.Computer.FileSystem.OpenTextFileReader(cmboSubject.SelectedItem & "Topics.txt")
|
||||
'Whilst not at the end of the text file...
|
||||
While Not Reader.EndOfStream
|
||||
'Add the topic to the combobox
|
||||
cmboTopic.Items.Add(Reader.ReadLine)
|
||||
End While
|
||||
|
||||
'Closes the streamreader
|
||||
Reader.Close()
|
||||
|
||||
Dim sql As String
|
||||
Dim dbcomm As MySqlCommand
|
||||
Dim dbread As MySqlDataReader
|
||||
|
||||
'Builds SQL query to execute
|
||||
sql = "SELECT * FROM tblsubjects WHERE Subject='" & cmboSubject.SelectedItem & "'"
|
||||
|
||||
dbcomm = New MySqlCommand(sql, DBConn)
|
||||
dbread = dbcomm.ExecuteReader()
|
||||
'Fills the various properties of the LoggedInStudent object with their respective values from the database
|
||||
While dbread.Read
|
||||
SubjectID = dbread("SubjectID")
|
||||
End While
|
||||
dbread.Close()
|
||||
End Sub
|
||||
End Class
|
202
StudentForms/ConnectFour/frmConnect4NetworkLobby.vb
Normal file
202
StudentForms/ConnectFour/frmConnect4NetworkLobby.vb
Normal file
|
@ -0,0 +1,202 @@
|
|||
Imports MySql.Data.MySqlClient
|
||||
|
||||
Public Class frmConnect4NetworkLobby
|
||||
'Declares the variable used to determine if a game has a second player or not
|
||||
Dim NoOpp As Boolean = True
|
||||
'Declares the variable used to determine if the logged-in player is the host of a game
|
||||
Dim Host As Boolean = True
|
||||
|
||||
'Subroutine runs on form load
|
||||
Private Sub frmConnect4NetworkLobby_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
|
||||
'Runs the OpenDB subroutine
|
||||
OpenDB()
|
||||
|
||||
'Runs the UpdateList subroutine
|
||||
UpdateList()
|
||||
End Sub
|
||||
|
||||
'Subroutine runs when called on form load or btnRefresh_Click subroutines
|
||||
Sub UpdateList()
|
||||
'Clears the lobby listbox of data
|
||||
lstLobby.Items.Clear()
|
||||
|
||||
'Declares the variable used to determine if the logged-in player is currently playing a given game
|
||||
Dim CurrPlaying As String = ""
|
||||
|
||||
Dim sql As String
|
||||
Dim dbcomm As MySqlCommand
|
||||
Dim dbread As MySqlDataReader
|
||||
|
||||
'Builds SQL query to execute
|
||||
sql = "SELECT * FROM tblconnect4 WHERE OppUsername ='' OR OppUsername ='" & LoggedInStudent.Username & "' OR HostUsername ='" & LoggedInStudent.Username & "';"
|
||||
|
||||
dbcomm = New MySqlCommand(sql, DBConn)
|
||||
dbread = dbcomm.ExecuteReader()
|
||||
Dim foo As Integer = 0
|
||||
'Fills the various properties of the LoggedInStudent object with their respective values from the database
|
||||
While dbread.Read
|
||||
'If there is an opponent in the game...
|
||||
If dbread("OppUsername") <> "" Then
|
||||
'Sets CurrPlaying to positive
|
||||
CurrPlaying = " - CURRENTLY PLAYING"
|
||||
Else
|
||||
'Otherwise CurrPlaying is negative
|
||||
CurrPlaying = ""
|
||||
End If
|
||||
'Add the game details to the lobby listbox
|
||||
lstLobby.Items.Add(dbread("GameID") & " - " & dbread("GameName") & " - " & dbread("HostUsername") & CurrPlaying)
|
||||
foo = 1
|
||||
End While
|
||||
dbread.Close()
|
||||
End Sub
|
||||
|
||||
'Subroutine runs when the host game button is clicked
|
||||
Private Sub btnHostGame_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnHostGame.Click
|
||||
'Shows the Connect Four network hosting form
|
||||
frmConnect4NetworkHostGame.Show()
|
||||
End Sub
|
||||
|
||||
'Subroutine runs when the your games button is clicked
|
||||
Private Sub btnYourGames_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnYourGames.Click
|
||||
'Shows the Connect Four network your games form
|
||||
frmConnect4NetworkYourGames.Show()
|
||||
End Sub
|
||||
|
||||
'Subroutine runs when the selected item of the lobby listbox is changes
|
||||
Private Sub lstLobby_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstLobby.SelectedIndexChanged
|
||||
foob()
|
||||
End Sub
|
||||
|
||||
Sub foob()
|
||||
'Determine the GameID of the selected game
|
||||
Dim EndofGamID As Integer = InStr(1, lstLobby.SelectedItem, " ", CompareMethod.Text)
|
||||
Dim GamID As String = Mid(lstLobby.SelectedItem, 1, EndofGamID - 1)
|
||||
|
||||
'Declares the variables used to determine the usernames of both players in the selected game
|
||||
Dim HUsername As String
|
||||
Dim Username As String
|
||||
|
||||
Dim sql As String
|
||||
Dim dbcomm As MySqlCommand
|
||||
Dim dbread As MySqlDataReader
|
||||
'Builds SQL query to execute
|
||||
sql = "SELECT * FROM tblconnect4 WHERE GameID=" & GamID & ";"
|
||||
|
||||
'Sets the selected GameID
|
||||
GameID = GamID
|
||||
|
||||
dbcomm = New MySqlCommand(sql, DBConn)
|
||||
dbread = dbcomm.ExecuteReader()
|
||||
|
||||
'Fills the various properties of the LoggedInStudent object with their respective values from the database
|
||||
While dbread.Read
|
||||
'If the logged-in user isn't the host of the selected game...
|
||||
If dbread("HostUsername") <> LoggedInStudent.Username Then
|
||||
'Sets the host flag to false
|
||||
Host = False
|
||||
|
||||
'Sets the players
|
||||
LoggedInStudent.C4Player = "Yellow"
|
||||
OppStudent.C4Player = "Red"
|
||||
|
||||
'Sets the opponent as the host
|
||||
Username = dbread("HostUsername")
|
||||
|
||||
'Closes the recordset
|
||||
dbread.Close()
|
||||
|
||||
'Builds SQL query to execute
|
||||
sql = "SELECT * FROM tblstudents WHERE Username='" & Username & "';"
|
||||
|
||||
dbcomm = New MySqlCommand(sql, DBConn)
|
||||
dbread = dbcomm.ExecuteReader()
|
||||
|
||||
While dbread.Read
|
||||
'Populates the properties of the OppStudent object with the host's data
|
||||
With OppStudent
|
||||
.Fname = dbread("Fname")
|
||||
.Lname = dbread("Lname")
|
||||
.StudentID = dbread("StudentID")
|
||||
.Username = dbread("Username")
|
||||
End With
|
||||
End While
|
||||
|
||||
'Sets the no opponent flag to false
|
||||
NoOpp = False
|
||||
dbread.Close()
|
||||
Else
|
||||
'If the logged-in user is the host, sets the host flag to true
|
||||
Host = True
|
||||
'Sets the players
|
||||
LoggedInStudent.C4Player = "Red"
|
||||
OppStudent.C4Player = "Yellow"
|
||||
|
||||
'Sets the usernames of the players
|
||||
HUsername = dbread("HostUsername")
|
||||
Username = dbread("OppUsername")
|
||||
|
||||
'Closes the recordset
|
||||
dbread.Close()
|
||||
|
||||
'Builds SQL query to execute
|
||||
sql = "SELECT * FROM tblstudents WHERE Username='" & Username & "';"
|
||||
|
||||
dbcomm = New MySqlCommand(sql, DBConn)
|
||||
dbread = dbcomm.ExecuteReader()
|
||||
|
||||
While dbread.Read
|
||||
'Populates the properties of the OppStudent object with data
|
||||
With OppStudent
|
||||
.Fname = dbread("Fname")
|
||||
.Lname = dbread("Lname")
|
||||
.StudentID = dbread("StudentID")
|
||||
.Username = dbread("Username")
|
||||
End With
|
||||
'Sets the no opponent flag to false
|
||||
NoOpp = False
|
||||
dbread.Close()
|
||||
Exit Sub
|
||||
End While
|
||||
'Sets the no opponent flag to true
|
||||
NoOpp = True
|
||||
dbread.Close()
|
||||
Exit Sub
|
||||
End If
|
||||
'Closes the recordset
|
||||
dbread.Close()
|
||||
Exit Sub
|
||||
End While
|
||||
dbread.Close()
|
||||
End Sub
|
||||
|
||||
'Subroutine runs when join game button is clicked
|
||||
Private Sub btnJoinGame_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnJoinGame.Click
|
||||
Dim sql As String
|
||||
Dim dbcomm As MySqlCommand
|
||||
Dim dbread As MySqlDataReader
|
||||
|
||||
'If there is an opponent in the game...
|
||||
If NoOpp = False Then
|
||||
If Host = False Then
|
||||
'Builds the SQL query to execute
|
||||
sql = "UPDATE `tblconnect4` SET `OppUsername`='" & LoggedInStudent.Username & "' WHERE `GameID`='" & GameID & "';"
|
||||
dbcomm = New MySqlCommand(sql, DBConn)
|
||||
dbread = dbcomm.ExecuteReader()
|
||||
dbread.Close()
|
||||
End If
|
||||
|
||||
'Shows the Connect Four network game form
|
||||
frmConnect4Network.Show()
|
||||
Me.Close()
|
||||
Else
|
||||
'Displays an error message
|
||||
MsgBox("No opponent in game")
|
||||
End If
|
||||
End Sub
|
||||
|
||||
'Subroutine runs when the refresh button is clicked
|
||||
Private Sub btnRefresh_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRefresh.Click
|
||||
'Runs the UpdateList subroutine
|
||||
UpdateList()
|
||||
End Sub
|
||||
End Class
|
152
StudentForms/ConnectFour/frmConnect4NetworkYourGames.vb
Normal file
152
StudentForms/ConnectFour/frmConnect4NetworkYourGames.vb
Normal file
|
@ -0,0 +1,152 @@
|
|||
Imports MySql.Data.MySqlClient
|
||||
|
||||
Public Class frmConnect4NetworkYourGames
|
||||
|
||||
'Declares the variable used to determine if a game has a second player or not
|
||||
Dim NoOpp As Boolean = True
|
||||
|
||||
'Subroutine runs on form load
|
||||
Private Sub frmConnect4NetworkYourGames_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
|
||||
'Runs the OpenDB subroutine
|
||||
OpenDB()
|
||||
|
||||
'Runs the UpdateList subroutine
|
||||
UpdateList()
|
||||
|
||||
'Brings the host game form to the front
|
||||
frmConnect4NetworkHostGame.BringToFront()
|
||||
End Sub
|
||||
|
||||
'Subroutine runs when called on form load or btnRefresh_Click subroutines
|
||||
Sub UpdateList()
|
||||
'Clears the lobby listbox of data
|
||||
lstYourGames.Items.Clear()
|
||||
|
||||
'Declares the variable used for the result of the confirmation messagebox
|
||||
Dim Result As MsgBoxResult
|
||||
'Declares the variable used to determine if the logged-in player is currently playing a given game
|
||||
Dim CurrPlaying As String = ""
|
||||
|
||||
Dim sql As String
|
||||
Dim dbcomm As MySqlCommand
|
||||
Dim dbread As MySqlDataReader
|
||||
|
||||
'Builds SQL query to execute
|
||||
sql = "SELECT * FROM tblconnect4 WHERE HostUsername = '" & LoggedInStudent.Username & "'"
|
||||
|
||||
dbcomm = New MySqlCommand(sql, DBConn)
|
||||
dbread = dbcomm.ExecuteReader()
|
||||
'Fills the various properties of the LoggedInStudent object with their respective values from the database
|
||||
While dbread.Read
|
||||
'If there is an opponent in the game...
|
||||
If dbread("OppUsername").Value <> "" Then
|
||||
'Sets CurrPlaying to positive
|
||||
CurrPlaying = " - Opponent: " & dbread("OppUsername").Value
|
||||
Else
|
||||
'Otherwise CurrPlaying is negative
|
||||
CurrPlaying = " - No opponent"
|
||||
End If
|
||||
'Add the game details to the lobby listbox
|
||||
lstYourGames.Items.Add(dbread("GameID").Value & " - " & dbread("GameName").Value & " - " & dbread("HostUsername").Value & CurrPlaying)
|
||||
dbread.Close()
|
||||
Exit Sub
|
||||
End While
|
||||
dbread.Close()
|
||||
End Sub
|
||||
|
||||
'Subroutine runs when the host game button is clicked
|
||||
Private Sub btnHostGame_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnHostGame.Click
|
||||
'Shows the Connect Four network host game form
|
||||
frmConnect4NetworkHostGame.Show()
|
||||
End Sub
|
||||
|
||||
'Subroutine runs when the selected item of the lobby listbox is changes
|
||||
Private Sub lstYourGames_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstYourGames.SelectedIndexChanged
|
||||
'Determine the GameID of the selected game
|
||||
Dim EndofGamID As Integer = InStr(1, lstYourGames.SelectedItem, " ", CompareMethod.Text)
|
||||
Dim GamID As String = Mid(lstYourGames.SelectedItem, 1, EndofGamID)
|
||||
|
||||
'Declares the variable used to determine the username of the host
|
||||
Dim Username As String
|
||||
|
||||
Dim sql As String
|
||||
Dim dbcomm As MySqlCommand
|
||||
Dim dbread As MySqlDataReader
|
||||
|
||||
Try
|
||||
'Builds SQL query to execute
|
||||
sql = "SELECT * FROM tblConnect4 WHERE GameID='" & GamID & "'"
|
||||
|
||||
'Sets the selected GameID
|
||||
GameID = GamID
|
||||
|
||||
dbcomm = New MySqlCommand(sql, DBConn)
|
||||
dbread = dbcomm.ExecuteReader()
|
||||
'Fills the various properties of the LoggedInStudent object with their respective values from the database
|
||||
While dbread.Read
|
||||
'If there is an opponent...
|
||||
If dbread("OppUsername") <> "" Then
|
||||
'Sets the players
|
||||
LoggedInStudent.C4Player = "Red"
|
||||
OppStudent.C4Player = "Yellow"
|
||||
|
||||
'Sets the host
|
||||
Username = dbread("OppUsername")
|
||||
|
||||
'Closes the recordset
|
||||
dbread.Close()
|
||||
|
||||
'Builds the SQl query to execute
|
||||
sql = "SELECT * FROM tblstudents WHERE username='" & Username & "'"
|
||||
dbcomm = New MySqlCommand(sql, DBConn)
|
||||
dbread = dbcomm.ExecuteReader()
|
||||
|
||||
'If there are records found...
|
||||
While dbread.Read
|
||||
'Populates the properties of the OppStudent object with the host's data
|
||||
With OppStudent
|
||||
.Fname = dbread("fname")
|
||||
.Lname = dbread("lname")
|
||||
.StudentID = dbread("StudentID")
|
||||
.Username = dbread("username")
|
||||
End With
|
||||
|
||||
'Sets the no opponent flag to false
|
||||
NoOpp = False
|
||||
dbread.Close()
|
||||
Exit Sub
|
||||
End While
|
||||
'Sets the no opponent flag to true
|
||||
NoOpp = True
|
||||
dbread.Close()
|
||||
Exit Sub
|
||||
End If
|
||||
'Closes the recordset
|
||||
dbread.Close()
|
||||
Exit Sub
|
||||
End While
|
||||
dbread.Close()
|
||||
Catch
|
||||
End Try
|
||||
End Sub
|
||||
|
||||
'Subroutine runs when join game button is clicked
|
||||
Private Sub btnJoinGame_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnJoinGame.Click
|
||||
'If there is an opponent in the game...
|
||||
If NoOpp = False Then
|
||||
'Shows the Connect Four network game form
|
||||
frmConnect4Network.Show()
|
||||
'Closes this form
|
||||
Me.Close()
|
||||
Else
|
||||
'Displays an error message
|
||||
MsgBox("No opponent in game")
|
||||
End If
|
||||
End Sub
|
||||
|
||||
'Subroutine runs when the refresh button is clicked
|
||||
Private Sub btnRefresh_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRefresh.Click
|
||||
'Runs the UpdateList subroutine
|
||||
UpdateList()
|
||||
End Sub
|
||||
End Class
|
Reference in a new issue