Advertisement
Guest User

Untitled

a guest
Jun 25th, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 18.03 KB | None | 0 0
  1. 'Project Name:          Noughts and Crosses Project
  2. 'Project Purpose:       Application to run a game of Noughts and Crosses
  3. '                       and track who wins between the O's and the X's
  4. 'Created/Revised By:    Shannon Talbot on 23-09-2010
  5.  
  6. Option Explicit On
  7. Option Strict On
  8. Option Infer Off
  9.  
  10. Public Class MainForm
  11. #Region "Game Structure"
  12.     Structure Game
  13.         'The Game's Main Structure
  14.         'Contains Variables for the board the current turn
  15.         'and the current state of the game (finished or not)
  16.  
  17. #Region "Game Variables"
  18.         'Game Variables
  19.         Public Board(,) As Integer              'Board Array to indicate what section contains a X, O or nothing
  20.         Public Finished As Boolean              'A boolean used to determine whether the game has finished or not
  21.         Public Player1Turn As Boolean           'A boolean used to determine whether it's currently Player 1's turn
  22.         Public Player2Turn As Boolean           'A boolean used to determine whether it's currently Player 2's turn
  23.         Public Player As Integer                'An integer used to keep track of what player's turn it is (just keeps the code clean)
  24.         Public Winner As Integer                'Integer used to store the winner
  25.         Public WinningPositions() As String     'String array used to store the 3 winning positions
  26. #End Region
  27.  
  28. #Region "Constructor Procedure"
  29.  
  30.  
  31.         ''' <summary>
  32.         ''' Initilisation of the game. Sets up all the variables of the game.
  33.         ''' </summary>
  34.         ''' <remarks></remarks>
  35.         Public Sub Initialise()
  36.             'The initialisation of the Game
  37.             'This procedure sets up the Game Structure and it's variables
  38.  
  39.             'Set array size of the Board to 9
  40.             ReDim Board(2, 2)
  41.             'Set all the array's items to 0 (meaning nothing occupies this square)
  42.             For Each cell As Integer In Board
  43.                 cell = 0
  44.             Next
  45.  
  46.             'Set array size of the Winning Positions to 3
  47.             ReDim WinningPositions(3)
  48.  
  49.             'Declare the game as not finished
  50.             Finished = False
  51.             'Declare winner as 0
  52.             Winner = 0
  53.  
  54.             'Set the player's turn to player 1
  55.             Player1Turn = True
  56.             Player = 1
  57.         End Sub
  58. #End Region
  59.  
  60. #Region "Update Procedure"
  61.         ''' <summary>
  62.         ''' The games Update function. Used to do all the updating of all the variables for the game.
  63.         ''' </summary>
  64.         ''' <param name="Y">The y position that was clicked.</param>
  65.         ''' <param name="X">The x position that was clicked.</param>
  66.         ''' <remarks></remarks>
  67.         Public Sub Update(ByVal Y As Integer, ByVal X As Integer)
  68.             'The update procedure of the Game
  69.             'This procedure is used to update all variables
  70.             'Cycle through turns and check for the end of the game
  71.  
  72.             'Set the cell's value
  73.             SetCell(Y, X)
  74.  
  75.             'Check if there is a winner
  76.             If CheckForWinner() Then
  77.                 Finished = True
  78.  
  79.             Else
  80.                 'Toggle the turn between the players
  81.                 ToggleTurn()
  82.             End If
  83.         End Sub
  84. #End Region
  85.  
  86. #Region "SetCell Procedure"
  87.         ''' <summary>
  88.         ''' Sets the cell's value to the position value parsed.
  89.         ''' </summary>
  90.         ''' <param name="Y">The y position to be set a value to.</param>
  91.         ''' <param name="X">The x position to be set a value to.</param>
  92.         ''' <remarks></remarks>
  93.         Private Sub SetCell(ByVal Y As Integer, ByVal X As Integer)
  94.             'Set a cell's value to the value of the current player
  95.             Board(Y, X) = Player
  96.         End Sub
  97. #End Region
  98.  
  99. #Region "ToggleTurn Procedure"
  100.         ''' <summary>
  101.         ''' 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.
  102.         ''' </summary>
  103.         ''' <remarks></remarks>
  104.         Private Sub ToggleTurn()
  105.             'Procedure used to toggle between turns
  106.  
  107.             'If it's player 1's turn then set it to player 2's turn
  108.             If Player1Turn Then
  109.                 Player1Turn = False
  110.                 Player2Turn = True
  111.                 Player = 2
  112.                 Exit Sub
  113.             End If
  114.  
  115.             'If it's player 2's turn then set it to player 1's turn
  116.             If Player2Turn Then
  117.                 Player1Turn = True
  118.                 Player2Turn = False
  119.                 Player = 1
  120.                 Exit Sub
  121.             End If
  122.         End Sub
  123. #End Region
  124.  
  125. #Region "SetString Function"
  126.         ''' <summary>
  127.         ''' Returns a string of either a X or O dependant on the current turn.
  128.         ''' </summary>
  129.         ''' <returns>X or O dependant on turn.</returns>
  130.         ''' <remarks></remarks>
  131.         Public Function SetString() As String
  132.             'Function used to set a X or O as the string for a button on the game grid
  133.             If Player1Turn Then
  134.                 'If it's player 1's turn then return an X
  135.                 Return "X"
  136.             ElseIf Player2Turn Then
  137.                 'If it's player 2's turn then return a O
  138.                 Return "O"
  139.             Else
  140.                 'If for any reason it's not any player's
  141.                 'turn then display a Fatal Error message
  142.                 'and close the game
  143.                 MessageBox.Show("A fatal error has occured. The game will now close.", "FATAL ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error)
  144.                 MainForm.Close()
  145.                 Return ""
  146.             End If
  147.         End Function
  148. #End Region
  149.  
  150. #Region "GetInstruction Function"
  151.         ''' <summary>
  152.         ''' Gets an instruction to display to the players depending on the game state.
  153.         ''' </summary>
  154.         ''' <returns>An instruction for the players.</returns>
  155.         ''' <remarks></remarks>
  156.         Public Function GetInstruction() As String
  157.             'Returns an instruction depending on the current game state
  158.  
  159.  
  160.             If Finished Then
  161.                 'If the game is finished display a finished game message
  162.                 Return "Click New Game to begin a new game!"
  163.             ElseIf Player1Turn Then
  164.                 'If the game is not finished and it's player 1's turn give them instructions
  165.                 Return "Player 1 Click a position to place a X. Get 3 in a row to win!"
  166.             ElseIf Player2Turn Then
  167.                 'If the game is not finished and it's player 2's turn give them instructions
  168.                 Return "Player 2 Click a position to place a O. Get 3 in a row to win!"
  169.             Else
  170.                 MessageBox.Show("A fatal error has occured. The game will now close.", "FATAL ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error)
  171.                 MainForm.Close()
  172.                 Return ""
  173.             End If
  174.         End Function
  175. #End Region
  176.  
  177. #Region "CheckForWinner Function"
  178.         ''' <summary>
  179.         ''' Checks for a winner or draw.
  180.         ''' </summary>
  181.         ''' <returns>Returns whether the game has finished. True = Finished, False = Not Finished</returns>
  182.         ''' <remarks></remarks>
  183.         Private Function CheckForWinner() As Boolean
  184.             'Checks if there is a winner or a draw has occured
  185.  
  186.             'Declare all variables
  187.             Dim fullBoard As Boolean = True
  188.  
  189.             'Check if any of the cells are empty
  190.             For Each cell As Integer In Board
  191.                 If cell = 0 Then
  192.                     'If a cell is empty then set the variable
  193.                     'fullboard to false and exit the loop
  194.                     fullBoard = False
  195.                     Exit For
  196.                 End If
  197.             Next
  198.  
  199.             'Check if the board has a line up of the same values
  200.             'Make that position 0 is not equal to 0
  201.             If Board(0, 0) <> 0 Then
  202.                 'If position 0 1 and 2 are equal set the winner to it's value
  203.                 'And return true as the game has finished
  204.                 If Board(0, 0) = Board(0, 1) AndAlso Board(0, 0) = Board(0, 2) Then
  205.                     Winner = Board(0, 0)
  206.                     WinningPositions(0) = "0,0"
  207.                     WinningPositions(1) = "0,1"
  208.                     WinningPositions(2) = "0,2"
  209.                     Return True
  210.  
  211.                     'If position 0 3 and 6 are equal set the winner to it's value
  212.                     'and return true as the game has finished
  213.                 ElseIf Board(0, 0) = Board(1, 0) AndAlso Board(0, 0) = Board(2, 0) Then
  214.                     Winner = Board(0, 0)
  215.                     WinningPositions(0) = "0,0"
  216.                     WinningPositions(1) = "1,0"
  217.                     WinningPositions(2) = "2,0"
  218.                     Return True
  219.  
  220.                 ElseIf Board(0, 0) = Board(1, 1) AndAlso Board(0, 0) = Board(2, 2) Then
  221.                     Winner = Board(0, 0)
  222.                     WinningPositions(0) = "0,0"
  223.                     WinningPositions(1) = "1,1"
  224.                     WinningPositions(2) = "2,2"
  225.                     Return True
  226.                 End If
  227.             End If
  228.  
  229.             'Make sure that position 8 is not equal to 0
  230.             If Board(2, 2) <> 0 Then
  231.                 'if position 8 5 and 2 are equal set the winner to it's value
  232.                 'and return true as the game has finished
  233.                 If Board(2, 2) = Board(1, 2) AndAlso Board(2, 2) = Board(0, 2) Then
  234.                     Winner = Board(2, 2)
  235.                     WinningPositions(0) = "2,2"
  236.                     WinningPositions(1) = "1,2"
  237.                     WinningPositions(2) = "0,2"
  238.                     Return True
  239.  
  240.                     'If position 8 7 and 6 are equal set the winner to it's value
  241.                     'and return true as the game has finished
  242.                 ElseIf Board(2, 2) = Board(2, 1) AndAlso Board(2, 2) = Board(2, 0) Then
  243.                     Winner = Board(2, 2)
  244.                     WinningPositions(0) = "2,2"
  245.                     WinningPositions(1) = "2,1"
  246.                     WinningPositions(2) = "2,0"
  247.                     Return True
  248.                 End If
  249.             End If
  250.  
  251.             'Make sure that position 4 is not equal to 0
  252.             If Board(1, 1) <> 0 Then
  253.                 'if position 1 4 and 7 are equal set the winner to it's value
  254.                 'and return true as the game has finished
  255.                 If Board(1, 1) = Board(0, 1) AndAlso Board(1, 1) = Board(2, 1) Then
  256.                     Winner = Board(1, 1)
  257.                     WinningPositions(0) = "1,1"
  258.                     WinningPositions(1) = "0,1"
  259.                     WinningPositions(2) = "2,1"
  260.                     Return True
  261.  
  262.                     'if position 3 4 and 5 are equal set the winner to it's value
  263.                     'and return true as the game has finished
  264.                 ElseIf Board(1, 1) = Board(1, 0) AndAlso Board(1, 1) = Board(1, 2) Then
  265.                     Winner = Board(1, 1)
  266.                     WinningPositions(0) = "1,1"
  267.                     WinningPositions(1) = "1,0"
  268.                     WinningPositions(2) = "1,2"
  269.                     Return True
  270.  
  271.                     'if position 2 4 and 6 are equal set the winner to it's value
  272.                     'and return true as the game has finished
  273.                 ElseIf Board(1, 1) = Board(0, 2) AndAlso Board(1, 1) = Board(2, 0) Then
  274.                     Winner = Board(1, 1)
  275.                     WinningPositions(0) = "1,1"
  276.                     WinningPositions(1) = "0,2"
  277.                     WinningPositions(2) = "2,0"
  278.                     Return True
  279.                 End If
  280.             End If
  281.  
  282.             'If the function has reached this far then there is no
  283.             'winner there can still be a draw if the board is full
  284.             'if the board is full a true value is returned and
  285.             'a draw value is placed in the winner variable (3)
  286.             If fullBoard Then
  287.                 Winner = 3
  288.                 Return True
  289.             Else
  290.                 Return False
  291.             End If
  292.         End Function
  293. #End Region
  294.     End Structure
  295. #End Region
  296.  
  297.     Private NoughtsAndCrosses As Game
  298.     Private WinnerMessage() As String = {"Player 1 has won!", "Player 2 has won!", "The game has resulted in a draw!"}
  299.     Private ButtonArray(,) As Button
  300.  
  301.     Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
  302.         'Closes the game
  303.         Me.Close()
  304.     End Sub
  305.  
  306.     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
  307.         'Determines what button was clicked and assigns the
  308.         'button a text value and the board's corresponding cell
  309.         'a player value depending on the current turn
  310.  
  311.         'Declare variables
  312.         Dim button As Button    'Used to parse the button that was just clicked
  313.         Dim y As Integer        'Used to store what y position was clicked
  314.         Dim x As Integer        'Used to store what x position was clicked
  315.         Dim position As Integer 'Used to store what position was clicked (this is to keep the code clean)
  316.  
  317.         'Parse the button that was clicked to the button variable
  318.         button = TryCast(sender, Button)
  319.  
  320.         'Set the text of the button to the player's mark (X or O)
  321.         button.Text = NoughtsAndCrosses.SetString()
  322.         'Disable the current button from being used again
  323.         button.Enabled = False
  324.  
  325.         'Determine the position that has been clicked
  326.         position = Convert.ToInt32(button.Name.Substring(button.Name.Length - 1)) - 1
  327.         y = Convert.ToInt32(Math.Floor(position / 3))
  328.         x = position - (y * 3)
  329.  
  330.         'Update the Game Structure
  331.         NoughtsAndCrosses.Update(y, x)
  332.  
  333.         'Set the instruction for the player
  334.         lblIntructionMessage.Text = NoughtsAndCrosses.GetInstruction
  335.  
  336.         'Check a winner has been determined
  337.         If NoughtsAndCrosses.Finished Then
  338.             'Display the winning message
  339.             lblWinnerMessage.Text = WinnerMessage(NoughtsAndCrosses.Winner - 1)
  340.             'Disable all buttons except the new game button and the exit button
  341.             btnPosition1.Enabled = False
  342.             btnPosition2.Enabled = False
  343.             btnPosition3.Enabled = False
  344.             btnPosition4.Enabled = False
  345.             btnPosition5.Enabled = False
  346.             btnPosition6.Enabled = False
  347.             btnPosition7.Enabled = False
  348.             btnPosition8.Enabled = False
  349.             btnPosition9.Enabled = False
  350.             'Enable the new game button
  351.             btnNewGame.Enabled = True
  352.             'Display the new instruction for the players
  353.             lblIntructionMessage.Text = NoughtsAndCrosses.GetInstruction
  354.             'If the game was not a draw
  355.             'Change the colour of the winning positions
  356.             If NoughtsAndCrosses.Winner <> 3 Then
  357.                 y = Convert.ToInt32(NoughtsAndCrosses.WinningPositions(0).Substring(0, 1))
  358.                 x = Convert.ToInt32(NoughtsAndCrosses.WinningPositions(0).Substring(2))
  359.                 ButtonArray(y, x).BackColor = Color.Green
  360.                 y = Convert.ToInt32(NoughtsAndCrosses.WinningPositions(1).Substring(0, 1))
  361.                 x = Convert.ToInt32(NoughtsAndCrosses.WinningPositions(1).Substring(2))
  362.                 ButtonArray(y, x).BackColor = Color.Green
  363.                 y = Convert.ToInt32(NoughtsAndCrosses.WinningPositions(2).Substring(0, 1))
  364.                 x = Convert.ToInt32(NoughtsAndCrosses.WinningPositions(2).Substring(2))
  365.                 ButtonArray(y, x).BackColor = Color.Green
  366.             End If
  367.         End If
  368.     End Sub
  369.  
  370.     Private Sub MainForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load, btnNewGame.Click
  371.         'Begins the game
  372.  
  373.         'Resets the Game Variable
  374.         NoughtsAndCrosses.Initialise()
  375.  
  376.         'Resets all the Game Controls
  377.  
  378.         'Set the winner message to an empty string
  379.         lblWinnerMessage.Text = String.Empty
  380.         'Set the information message to an valid instruction
  381.         lblIntructionMessage.Text = NoughtsAndCrosses.GetInstruction()
  382.         'Reset all the buttons
  383.         btnPosition1.Enabled = True
  384.         btnPosition1.Text = String.Empty
  385.         btnPosition1.BackColor = Color.Gray
  386.         btnPosition2.Enabled = True
  387.         btnPosition2.Text = String.Empty
  388.         btnPosition2.BackColor = Color.Gray
  389.         btnPosition3.Enabled = True
  390.         btnPosition3.Text = String.Empty
  391.         btnPosition3.BackColor = Color.Gray
  392.         btnPosition4.Enabled = True
  393.         btnPosition4.Text = String.Empty
  394.         btnPosition4.BackColor = Color.Gray
  395.         btnPosition5.Enabled = True
  396.         btnPosition5.Text = String.Empty
  397.         btnPosition5.BackColor = Color.Gray
  398.         btnPosition6.Enabled = True
  399.         btnPosition6.Text = String.Empty
  400.         btnPosition6.BackColor = Color.Gray
  401.         btnPosition7.Enabled = True
  402.         btnPosition7.Text = String.Empty
  403.         btnPosition7.BackColor = Color.Gray
  404.         btnPosition8.Enabled = True
  405.         btnPosition8.Text = String.Empty
  406.         btnPosition8.BackColor = Color.Gray
  407.         btnPosition9.Enabled = True
  408.         btnPosition9.Text = String.Empty
  409.         btnPosition9.BackColor = Color.Gray
  410.         'Disable the New Game button
  411.         btnNewGame.Enabled = False
  412.  
  413.         'Setup all the buttons in the button array
  414.         ReDim ButtonArray(2, 2)
  415.         ButtonArray(0, 0) = TryCast(btnPosition1, Button)
  416.         ButtonArray(0, 1) = TryCast(btnPosition2, Button)
  417.         ButtonArray(0, 2) = TryCast(btnPosition3, Button)
  418.         ButtonArray(1, 0) = TryCast(btnPosition4, Button)
  419.         ButtonArray(1, 1) = TryCast(btnPosition5, Button)
  420.         ButtonArray(1, 2) = TryCast(btnPosition6, Button)
  421.         ButtonArray(2, 0) = TryCast(btnPosition7, Button)
  422.         ButtonArray(2, 1) = TryCast(btnPosition8, Button)
  423.         ButtonArray(2, 2) = TryCast(btnPosition9, Button)
  424.     End Sub
  425. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement