0% found this document useful (0 votes)
30 views

Visual Basic Os and Xs

The document describes how to build a Tic Tac Toe game interface and code in Visual Studio. It includes instructions to: 1) Create a new project and form with a 3x3 table layout panel for the game board and buttons for new game and exit. 2) Declare arrays to store the label objects for each space on the board. 3) Write code to handle label clicks, alternate between marking "X" and "O", check for a winner after each turn, and reset the board for a new game. 4) The code simulates a basic Tic Tac Toe game where two players alternate turns placing "X" or "O" on the board until someone wins a row, column

Uploaded by

andy2006
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views

Visual Basic Os and Xs

The document describes how to build a Tic Tac Toe game interface and code in Visual Studio. It includes instructions to: 1) Create a new project and form with a 3x3 table layout panel for the game board and buttons for new game and exit. 2) Declare arrays to store the label objects for each space on the board. 3) Write code to handle label clicks, alternate between marking "X" and "O", check for a winner after each turn, and reset the board for a new game. 4) The code simulates a basic Tic Tac Toe game where two players alternate turns placing "X" or "O" on the board until someone wins a row, column

Uploaded by

andy2006
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Xs and 0s - Game

Open Visual Studio as usual and create a new project Application Name: Tic Tac Toe Interface: Create the Interface as shown below:

Form Properties Name: frmMainForm Text: Tic Tac Toe Table Properties Name: gameBoardTableLayoutPanel Anchor: Top, Bottom, Left, Right Number of Columns 3 Number of Rows 3 Row size 33% Column size 33% Label Properties Name: Label1, Label2, Label3, Label4, Label5, Label6, Label7, Label8 Anchor: None Autosize: False BackColor: ActiveBoarder Size: 99, 78 Text: Blank TextAlign: MiddleCenter New Game (Button Properties) Name: btnNewGame Text: New Game Exit Button Properties Name: btnExit Text: Exit

Game Code
Project Name: Tic Tac Toe Game 'Project Purpose: Simulates Xs and Os Game 'Created/revised by: Anisa Mkuwu 'Date: July 2012 Option Explicit On Option Strict On Option Infer Off Public Class MainForm 'declare 9 arrays for labels 0 - 8 Dim gameBoard(8) As Label 'the isWinner variable keeps track of whether there is a winner Dim isWinner As Boolean Private Sub TestforAWinner(ByVal sender As Object, ByVal e As System.EventArgs ) Handles Label1.Click, Label2.Click, Label3.Click, Label4.Click, Label5.Click, Label6.Click, Label7.Click, Label8.Click, Label9.Click Dim currentLabel As Label Dim subscript As Integer Dim isWinner As Boolean Static player As String = "X" Static squareColor As Color = Color.LightGreen 'access the lable that raised the click event currentLabel = TryCast(sender, Label) 'display either X or 0 in the current label currentLabel.Text = player 'set the current label's Backcolor and enabled properties currentLabel.BackColor = squareColor currentLabel.Enabled = False Select Case currentLabel.Name Case "Label1" subscript = 0 Case "Label2" subscript = 1 Case "Label3" subscript = 2 Case "Label4" subscript = 3 Case "Label5" subscript = 4 Case "Label6" subscript = 5 Case "Label7" subscript = 6 Case "Label8" subscript = 7 Case "Label9" subscript = 8 End Select gameBoard(subscript).Text = player 'determine whether there is a winner Select Case player & player & player Case gameBoard(0).Text & gameBoard(1).Text & gameBoard(2).Text isWinner = True Case gameBoard(3).Text & gameBoard(4).Text & gameBoard(5).Text isWinner = True Case gameBoard(6).Text & gameBoard(7).Text & gameBoard(8).Text isWinner = True

Case gameBoard(0).Text isWinner = True Case gameBoard(1).Text isWinner = True Case gameBoard(2).Text isWinner = True Case gameBoard(0).Text isWinner = True Case gameBoard(2).Text isWinner = True End Select

& gameBoard(3).Text & gameBoard(6).Text & gameBoard(4).Text & gameBoard(7).Text & gameBoard(5).Text & gameBoard(8).Text & gameBoard(4).Text & gameBoard(8).Text & gameBoard(4).Text & gameBoard(6).Text

If isWinner Then MessageBox.Show("Game Over! " & player & " wins.", "Tic Tac Toe", MessageBoxButtons.OK, MessageBoxIcon.Information) gameBoardTableLayoutPanel.Enabled = False Else If player = "X" Then player = "0" squareColor = Color.LightSalmon Else player = "X" squareColor = Color.LightGreen End If End If End Sub Private Sub MainForm_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load ' add labels to gameBoard array gameBoard(0) = Label1 gameBoard(1) = Label2 gameBoard(2) = Label3 gameBoard(3) = Label4 gameBoard(4) = Label5 gameBoard(5) = Label6 gameBoard(6) = Label7 gameBoard(7) = Label8 gameBoard(8) = Label9 End Sub Private Sub btnNewGame_Click(sender As System.Object, e As System.EventArgs) Handles btnNewGame.Click 'deletes the contents of the labels, then sets their 'backcolor property, and then enables them 'also enables the game board and resets the isWinner variable For Each labelElement As Label In gameBoard labelElement.Text = String.Empty labelElement.BackColor = Color.LightGray labelElement.Enabled = True Next labelElement gameBoardTableLayoutPanel.Enabled = True isWinner = False End Sub Private Sub btnExit_Click(sender As System.Object, e As System.EventArgs) Handles btnExit.Click Me.Close() End Sub End Class

Working App

X Wins

0 Wins Make sure to save your application!

You might also like