Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 'Project Name: Noughts and Crosses Project
- 'Project Purpose: Application to run a game of Noughts and Crosses
- ' and track who wins between the O's and the X's
- 'Created/Revised By: Shannon Talbot on 23-09-2010
- Option Explicit On
- Option Strict On
- Option Infer Off
- Public Class MainForm
- #Region "Game Structure"
- Structure Game
- 'The Game's Main Structure
- 'Contains Variables for the board the current turn
- 'and the current state of the game (finished or not)
- #Region "Game Variables"
- 'Game Variables
- Public Board(,) As Integer 'Board Array to indicate what section contains a X, O or nothing
- Public Finished As Boolean 'A boolean used to determine whether the game has finished or not
- Public Player1Turn As Boolean 'A boolean used to determine whether it's currently Player 1's turn
- Public Player2Turn As Boolean 'A boolean used to determine whether it's currently Player 2's turn
- Public Player As Integer 'An integer used to keep track of what player's turn it is (just keeps the code clean)
- Public Winner As Integer 'Integer used to store the winner
- Public WinningPositions() As String 'String array used to store the 3 winning positions
- #End Region
- #Region "Constructor Procedure"
- ''' <summary>
- ''' Initilisation of the game. Sets up all the variables of the game.
- ''' </summary>
- ''' <remarks></remarks>
- Public Sub Initialise()
- 'The initialisation of the Game
- 'This procedure sets up the Game Structure and it's variables
- 'Set array size of the Board to 9
- ReDim Board(2, 2)
- 'Set all the array's items to 0 (meaning nothing occupies this square)
- For Each cell As Integer In Board
- cell = 0
- Next
- 'Set array size of the Winning Positions to 3
- ReDim WinningPositions(3)
- 'Declare the game as not finished
- Finished = False
- 'Declare winner as 0
- Winner = 0
- 'Set the player's turn to player 1
- Player1Turn = True
- Player = 1
- End Sub
- #End Region
- #Region "Update Procedure"
- ''' <summary>
- ''' The games Update function. Used to do all the updating of all the variables for the game.
- ''' </summary>
- ''' <param name="Y">The y position that was clicked.</param>
- ''' <param name="X">The x position that was clicked.</param>
- ''' <remarks></remarks>
- Public Sub Update(ByVal Y As Integer, ByVal X As Integer)
- 'The update procedure of the Game
- 'This procedure is used to update all variables
- 'Cycle through turns and check for the end of the game
- 'Set the cell's value
- SetCell(Y, X)
- 'Check if there is a winner
- If CheckForWinner() Then
- Finished = True
- Else
- 'Toggle the turn between the players
- ToggleTurn()
- End If
- End Sub
- #End Region
- #Region "SetCell Procedure"
- ''' <summary>
- ''' Sets the cell's value to the position value parsed.
- ''' </summary>
- ''' <param name="Y">The y position to be set a value to.</param>
- ''' <param name="X">The x position to be set a value to.</param>
- ''' <remarks></remarks>
- Private Sub SetCell(ByVal Y As Integer, ByVal X As Integer)
- 'Set a cell's value to the value of the current player
- Board(Y, X) = Player
- End Sub
- #End Region
- #Region "ToggleTurn Procedure"
- ''' <summary>
- ''' Toggles the current player's turn. If it was player 1's turn then it will set the current turn to player 2 and vice versa.
- ''' </summary>
- ''' <remarks></remarks>
- Private Sub ToggleTurn()
- 'Procedure used to toggle between turns
- 'If it's player 1's turn then set it to player 2's turn
- If Player1Turn Then
- Player1Turn = False
- Player2Turn = True
- Player = 2
- Exit Sub
- End If
- 'If it's player 2's turn then set it to player 1's turn
- If Player2Turn Then
- Player1Turn = True
- Player2Turn = False
- Player = 1
- Exit Sub
- End If
- End Sub
- #End Region
- #Region "SetString Function"
- ''' <summary>
- ''' Returns a string of either a X or O dependant on the current turn.
- ''' </summary>
- ''' <returns>X or O dependant on turn.</returns>
- ''' <remarks></remarks>
- Public Function SetString() As String
- 'Function used to set a X or O as the string for a button on the game grid
- If Player1Turn Then
- 'If it's player 1's turn then return an X
- Return "X"
- ElseIf Player2Turn Then
- 'If it's player 2's turn then return a O
- Return "O"
- Else
- 'If for any reason it's not any player's
- 'turn then display a Fatal Error message
- 'and close the game
- MessageBox.Show("A fatal error has occured. The game will now close.", "FATAL ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error)
- MainForm.Close()
- Return ""
- End If
- End Function
- #End Region
- #Region "GetInstruction Function"
- ''' <summary>
- ''' Gets an instruction to display to the players depending on the game state.
- ''' </summary>
- ''' <returns>An instruction for the players.</returns>
- ''' <remarks></remarks>
- Public Function GetInstruction() As String
- 'Returns an instruction depending on the current game state
- If Finished Then
- 'If the game is finished display a finished game message
- Return "Click New Game to begin a new game!"
- ElseIf Player1Turn Then
- 'If the game is not finished and it's player 1's turn give them instructions
- Return "Player 1 Click a position to place a X. Get 3 in a row to win!"
- ElseIf Player2Turn Then
- 'If the game is not finished and it's player 2's turn give them instructions
- Return "Player 2 Click a position to place a O. Get 3 in a row to win!"
- Else
- MessageBox.Show("A fatal error has occured. The game will now close.", "FATAL ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error)
- MainForm.Close()
- Return ""
- End If
- End Function
- #End Region
- #Region "CheckForWinner Function"
- ''' <summary>
- ''' Checks for a winner or draw.
- ''' </summary>
- ''' <returns>Returns whether the game has finished. True = Finished, False = Not Finished</returns>
- ''' <remarks></remarks>
- Private Function CheckForWinner() As Boolean
- 'Checks if there is a winner or a draw has occured
- 'Declare all variables
- Dim fullBoard As Boolean = True
- 'Check if any of the cells are empty
- For Each cell As Integer In Board
- If cell = 0 Then
- 'If a cell is empty then set the variable
- 'fullboard to false and exit the loop
- fullBoard = False
- Exit For
- End If
- Next
- 'Check if the board has a line up of the same values
- 'Make that position 0 is not equal to 0
- If Board(0, 0) <> 0 Then
- 'If position 0 1 and 2 are equal set the winner to it's value
- 'And return true as the game has finished
- If Board(0, 0) = Board(0, 1) AndAlso Board(0, 0) = Board(0, 2) Then
- Winner = Board(0, 0)
- WinningPositions(0) = "0,0"
- WinningPositions(1) = "0,1"
- WinningPositions(2) = "0,2"
- Return True
- 'If position 0 3 and 6 are equal set the winner to it's value
- 'and return true as the game has finished
- ElseIf Board(0, 0) = Board(1, 0) AndAlso Board(0, 0) = Board(2, 0) Then
- Winner = Board(0, 0)
- WinningPositions(0) = "0,0"
- WinningPositions(1) = "1,0"
- WinningPositions(2) = "2,0"
- Return True
- ElseIf Board(0, 0) = Board(1, 1) AndAlso Board(0, 0) = Board(2, 2) Then
- Winner = Board(0, 0)
- WinningPositions(0) = "0,0"
- WinningPositions(1) = "1,1"
- WinningPositions(2) = "2,2"
- Return True
- End If
- End If
- 'Make sure that position 8 is not equal to 0
- If Board(2, 2) <> 0 Then
- 'if position 8 5 and 2 are equal set the winner to it's value
- 'and return true as the game has finished
- If Board(2, 2) = Board(1, 2) AndAlso Board(2, 2) = Board(0, 2) Then
- Winner = Board(2, 2)
- WinningPositions(0) = "2,2"
- WinningPositions(1) = "1,2"
- WinningPositions(2) = "0,2"
- Return True
- 'If position 8 7 and 6 are equal set the winner to it's value
- 'and return true as the game has finished
- ElseIf Board(2, 2) = Board(2, 1) AndAlso Board(2, 2) = Board(2, 0) Then
- Winner = Board(2, 2)
- WinningPositions(0) = "2,2"
- WinningPositions(1) = "2,1"
- WinningPositions(2) = "2,0"
- Return True
- End If
- End If
- 'Make sure that position 4 is not equal to 0
- If Board(1, 1) <> 0 Then
- 'if position 1 4 and 7 are equal set the winner to it's value
- 'and return true as the game has finished
- If Board(1, 1) = Board(0, 1) AndAlso Board(1, 1) = Board(2, 1) Then
- Winner = Board(1, 1)
- WinningPositions(0) = "1,1"
- WinningPositions(1) = "0,1"
- WinningPositions(2) = "2,1"
- Return True
- 'if position 3 4 and 5 are equal set the winner to it's value
- 'and return true as the game has finished
- ElseIf Board(1, 1) = Board(1, 0) AndAlso Board(1, 1) = Board(1, 2) Then
- Winner = Board(1, 1)
- WinningPositions(0) = "1,1"
- WinningPositions(1) = "1,0"
- WinningPositions(2) = "1,2"
- Return True
- 'if position 2 4 and 6 are equal set the winner to it's value
- 'and return true as the game has finished
- ElseIf Board(1, 1) = Board(0, 2) AndAlso Board(1, 1) = Board(2, 0) Then
- Winner = Board(1, 1)
- WinningPositions(0) = "1,1"
- WinningPositions(1) = "0,2"
- WinningPositions(2) = "2,0"
- Return True
- End If
- End If
- 'If the function has reached this far then there is no
- 'winner there can still be a draw if the board is full
- 'if the board is full a true value is returned and
- 'a draw value is placed in the winner variable (3)
- If fullBoard Then
- Winner = 3
- Return True
- Else
- Return False
- End If
- End Function
- #End Region
- End Structure
- #End Region
- Private NoughtsAndCrosses As Game
- Private WinnerMessage() As String = {"Player 1 has won!", "Player 2 has won!", "The game has resulted in a draw!"}
- Private ButtonArray(,) As Button
- Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
- 'Closes the game
- Me.Close()
- End Sub
- Private Sub btnPosition_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPosition1.Click, btnPosition2.Click, btnPosition3.Click, btnPosition4.Click, btnPosition5.Click, btnPosition6.Click, btnPosition7.Click, btnPosition8.Click, btnPosition9.Click
- 'Determines what button was clicked and assigns the
- 'button a text value and the board's corresponding cell
- 'a player value depending on the current turn
- 'Declare variables
- Dim button As Button 'Used to parse the button that was just clicked
- Dim y As Integer 'Used to store what y position was clicked
- Dim x As Integer 'Used to store what x position was clicked
- Dim position As Integer 'Used to store what position was clicked (this is to keep the code clean)
- 'Parse the button that was clicked to the button variable
- button = TryCast(sender, Button)
- 'Set the text of the button to the player's mark (X or O)
- button.Text = NoughtsAndCrosses.SetString()
- 'Disable the current button from being used again
- button.Enabled = False
- 'Determine the position that has been clicked
- position = Convert.ToInt32(button.Name.Substring(button.Name.Length - 1)) - 1
- y = Convert.ToInt32(Math.Floor(position / 3))
- x = position - (y * 3)
- 'Update the Game Structure
- NoughtsAndCrosses.Update(y, x)
- 'Set the instruction for the player
- lblIntructionMessage.Text = NoughtsAndCrosses.GetInstruction
- 'Check a winner has been determined
- If NoughtsAndCrosses.Finished Then
- 'Display the winning message
- lblWinnerMessage.Text = WinnerMessage(NoughtsAndCrosses.Winner - 1)
- 'Disable all buttons except the new game button and the exit button
- btnPosition1.Enabled = False
- btnPosition2.Enabled = False
- btnPosition3.Enabled = False
- btnPosition4.Enabled = False
- btnPosition5.Enabled = False
- btnPosition6.Enabled = False
- btnPosition7.Enabled = False
- btnPosition8.Enabled = False
- btnPosition9.Enabled = False
- 'Enable the new game button
- btnNewGame.Enabled = True
- 'Display the new instruction for the players
- lblIntructionMessage.Text = NoughtsAndCrosses.GetInstruction
- 'If the game was not a draw
- 'Change the colour of the winning positions
- If NoughtsAndCrosses.Winner <> 3 Then
- y = Convert.ToInt32(NoughtsAndCrosses.WinningPositions(0).Substring(0, 1))
- x = Convert.ToInt32(NoughtsAndCrosses.WinningPositions(0).Substring(2))
- ButtonArray(y, x).BackColor = Color.Green
- y = Convert.ToInt32(NoughtsAndCrosses.WinningPositions(1).Substring(0, 1))
- x = Convert.ToInt32(NoughtsAndCrosses.WinningPositions(1).Substring(2))
- ButtonArray(y, x).BackColor = Color.Green
- y = Convert.ToInt32(NoughtsAndCrosses.WinningPositions(2).Substring(0, 1))
- x = Convert.ToInt32(NoughtsAndCrosses.WinningPositions(2).Substring(2))
- ButtonArray(y, x).BackColor = Color.Green
- End If
- End If
- End Sub
- Private Sub MainForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load, btnNewGame.Click
- 'Begins the game
- 'Resets the Game Variable
- NoughtsAndCrosses.Initialise()
- 'Resets all the Game Controls
- 'Set the winner message to an empty string
- lblWinnerMessage.Text = String.Empty
- 'Set the information message to an valid instruction
- lblIntructionMessage.Text = NoughtsAndCrosses.GetInstruction()
- 'Reset all the buttons
- btnPosition1.Enabled = True
- btnPosition1.Text = String.Empty
- btnPosition1.BackColor = Color.Gray
- btnPosition2.Enabled = True
- btnPosition2.Text = String.Empty
- btnPosition2.BackColor = Color.Gray
- btnPosition3.Enabled = True
- btnPosition3.Text = String.Empty
- btnPosition3.BackColor = Color.Gray
- btnPosition4.Enabled = True
- btnPosition4.Text = String.Empty
- btnPosition4.BackColor = Color.Gray
- btnPosition5.Enabled = True
- btnPosition5.Text = String.Empty
- btnPosition5.BackColor = Color.Gray
- btnPosition6.Enabled = True
- btnPosition6.Text = String.Empty
- btnPosition6.BackColor = Color.Gray
- btnPosition7.Enabled = True
- btnPosition7.Text = String.Empty
- btnPosition7.BackColor = Color.Gray
- btnPosition8.Enabled = True
- btnPosition8.Text = String.Empty
- btnPosition8.BackColor = Color.Gray
- btnPosition9.Enabled = True
- btnPosition9.Text = String.Empty
- btnPosition9.BackColor = Color.Gray
- 'Disable the New Game button
- btnNewGame.Enabled = False
- 'Setup all the buttons in the button array
- ReDim ButtonArray(2, 2)
- ButtonArray(0, 0) = TryCast(btnPosition1, Button)
- ButtonArray(0, 1) = TryCast(btnPosition2, Button)
- ButtonArray(0, 2) = TryCast(btnPosition3, Button)
- ButtonArray(1, 0) = TryCast(btnPosition4, Button)
- ButtonArray(1, 1) = TryCast(btnPosition5, Button)
- ButtonArray(1, 2) = TryCast(btnPosition6, Button)
- ButtonArray(2, 0) = TryCast(btnPosition7, Button)
- ButtonArray(2, 1) = TryCast(btnPosition8, Button)
- ButtonArray(2, 2) = TryCast(btnPosition9, Button)
- End Sub
- End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement