Tic-Tac-Toe Simulator [C# Visual Basic]
Create an application that simulates a game of tic-tac-toe. Figure 7-45 shows an ex-
ample of the application’s form. The form shown in the figure uses eight large Label
controls to display the Xs and Os.
The application should use a two-dimensional int array to simulate the game board
in memory. When the user clicks the New Game button, the application should step
through the array, storing a random number in the range of 0 through 1 in each ele-
ment. The number 0 represents the letter O, and the number 1 represents the letter
X. The form should then be updated to display the game board. The application
should display a message indicating whether player X won, player Y won, or the
game was a tie. Tic-Tac-Toe X O O X ins New Game
Solution
Answer:-
Tic-Tac-Toe Game :
The application should begin with a form that contains the following elements:-
1) A label saying \"Welcome to Tic-Tac-Toe\"
2) Two option buttons enclosed in a frame. The captions for the option buttons should be \"X\"
and \"O\". The caption for the frame should be \"Select \'X\' or \'O\' and click OK\".
3) A command button with the caption \"OK\".
4) When the user clicks OK, exit this form and display the main form (the game board).
5) At the start of each game, the application should select who should go first at random, and
display an appropriate message to the user in a message box (either \"You go first this time.\" or
\"This time, I will go first.\")
6) The game board should be set up with an array of label controls indexed 0 through 8. When a
player clicks one of the available labels, an \"X\" or an \"O\" (depending on the user\'s initial
selection) should appear in that label. Then the computer should make its move.
7) When the program detects a win, a line should be drawn through the \"three in a row\". If the
player wins, the message \"YOU WIN !!!\" should flash across the game board; if the computer
wins, the message \"YOU LOSE!!!\" (or \"I WIN!!!\" or \"COMPUTER WINS!!!\") should flash
across the game board. If the game ends in a tie, the message \"IT\'S A TIE!!!\" should flash
across the game board. (Hint: To get the flashing message, use a label in conjunction with a
timer and toggle the label\'s Visible property on and off.)
8) The program should provide options to play a new game and to quit.
9) The program should provide keep four counts: games played, games won, games lost, and
games tied. These counts should be displayed on the form.
10) The overall appearance of the application should be tasteful and suggest \"fun\". Experiment
with various colors and fonts until you get it the way you want it.
The Computer\'s AI Algorithm
Your program should implement the following algorithm to try to make the computer win:
(1) Examine the board for a winning move, and if you find one, make that move – otherwise
proceed to step (2).
(2) Examine the board to see if your opponent has a winning move, and if so, select that square
to blo.
Tic-Tac-Toe Simulator [C# Visual Basic] Create an application th.pdf
1. Tic-Tac-Toe Simulator [C# Visual Basic]
Create an application that simulates a game of tic-tac-toe. Figure 7-45 shows an ex-
ample of the application’s form. The form shown in the figure uses eight large Label
controls to display the Xs and Os.
The application should use a two-dimensional int array to simulate the game board
in memory. When the user clicks the New Game button, the application should step
through the array, storing a random number in the range of 0 through 1 in each ele-
ment. The number 0 represents the letter O, and the number 1 represents the letter
X. The form should then be updated to display the game board. The application
should display a message indicating whether player X won, player Y won, or the
game was a tie. Tic-Tac-Toe X O O X ins New Game
Solution
Answer:-
Tic-Tac-Toe Game :
The application should begin with a form that contains the following elements:-
1) A label saying "Welcome to Tic-Tac-Toe"
2) Two option buttons enclosed in a frame. The captions for the option buttons should be "X"
and "O". The caption for the frame should be "Select 'X' or 'O' and click OK".
3) A command button with the caption "OK".
4) When the user clicks OK, exit this form and display the main form (the game board).
5) At the start of each game, the application should select who should go first at random, and
display an appropriate message to the user in a message box (either "You go first this time." or
"This time, I will go first.")
6) The game board should be set up with an array of label controls indexed 0 through 8. When a
player clicks one of the available labels, an "X" or an "O" (depending on the user's initial
selection) should appear in that label. Then the computer should make its move.
7) When the program detects a win, a line should be drawn through the "three in a row". If the
player wins, the message "YOU WIN !!!" should flash across the game board; if the computer
wins, the message "YOU LOSE!!!" (or "I WIN!!!" or "COMPUTER WINS!!!") should flash
across the game board. If the game ends in a tie, the message "IT'S A TIE!!!" should flash
across the game board. (Hint: To get the flashing message, use a label in conjunction with a
timer and toggle the label's Visible property on and off.)
2. 8) The program should provide options to play a new game and to quit.
9) The program should provide keep four counts: games played, games won, games lost, and
games tied. These counts should be displayed on the form.
10) The overall appearance of the application should be tasteful and suggest "fun". Experiment
with various colors and fonts until you get it the way you want it.
The Computer's AI Algorithm
Your program should implement the following algorithm to try to make the computer win:
(1) Examine the board for a winning move, and if you find one, make that move – otherwise
proceed to step (2).
(2) Examine the board to see if your opponent has a winning move, and if so, select that square
to block, otherwise proceed to step (3).
(3) If the center square is available, select it, otherwise proceed to step (4).
(4) If you are already occupying the center square, and a side square is available, take the side
square, otherwise proceed to step (5).
(5) If a corner square is available, then take it, otherwise, take the next available square.
code :-
Private Sub win()
lbl1.Caption = ""
lbl2.Caption = ""
lbl3.Caption = ""
lbl4.Caption = ""
lbl5.Caption = ""
lbl6.Caption = ""
lbl7.Caption = ""
lbl8.Caption = ""
lbl9.Caption = ""
lblwinsno.Caption = Val(lblwinsno.Caption) + 1
hardwinform.lblwin.Caption = "You win!"
playform.Visible = False
hardwinform.Visible = True
End Sub
Private Sub lose()
lbl1.Caption = ""
lbl2.Caption = ""
lbl3.Caption = ""
lbl4.Caption = ""
3. lbl5.Caption = ""
lbl6.Caption = ""
lbl7.Caption = ""
lbl8.Caption = ""
lbl9.Caption = ""
lbllosesno.Caption = Val(lbllosesno.Caption) + 1
hardwinform.lblwin.Caption = "You Lose!"
playform.Visible = False
hardwinform.Visible = True
End Sub
Private Sub tie()
lbl1.Caption = ""
lbl2.Caption = ""
lbl3.Caption = ""
lbl4.Caption = ""
lbl5.Caption = ""
lbl6.Caption = ""
lbl7.Caption = ""
lbl8.Caption = ""
lbl9.Caption = ""
lbltiesno.Caption = Val(lbltiesno.Caption) + 1
hardwinform.lblwin.Caption = "Tie!"
playform.Visible = False
hardwinform.Visible = True
End Sub
Private Sub check()
If lbl3.Caption = "X" And lbl5.Caption = "X" And lbl7.Caption = "X" Then
Call win
ElseIf lbl3.Caption = "O" And lbl5.Caption = "O" And lbl7.Caption = "O" Then
Call lose
ElseIf lbl1.Caption = "X" And lbl5.Caption = "X" And lbl9.Caption = "X" Then
Call win
ElseIf lbl1.Caption = "O" And lbl5.Caption = "O" And lbl9.Caption = "O" Then
Call lose
ElseIf lbl1.Caption = "X" And lbl2.Caption = "X" And lbl3.Caption = "X" Then
Call win
4. ElseIf lbl1.Caption = "O" And lbl2.Caption = "O" And lbl3.Caption = "O" Then
Call lose
ElseIf lbl4.Caption = "X" And lbl5.Caption = "X" And lbl6.Caption = "X" Then
Call win
ElseIf lbl4.Caption = "O" And lbl5.Caption = "O" And lbl6.Caption = "O" Then
Call lose
ElseIf lbl7.Caption = "X" And lbl8.Caption = "X" And lbl9.Caption = "X" Then
Call win
ElseIf lbl7.Caption = "O" And lbl8.Caption = "O" And lbl9.Caption = "O" Then
Call lose
ElseIf lbl1.Caption = "X" And lbl4.Caption = "X" And lbl7.Caption = "X" Then
Call win
ElseIf lbl1.Caption = "O" And lbl4.Caption = "O" And lbl7.Caption = "O" Then
Call lose
ElseIf lbl3.Caption = "X" And lbl6.Caption = "X" And lbl9.Caption = "X" Then
Call win
ElseIf lbl3.Caption = "O" And lbl6.Caption = "O" And lbl9.Caption = "O" Then
Call lose
ElseIf lbl2.Caption = "X" And lbl5.Caption = "X" And lbl8.Caption = "X" Then
Call win
ElseIf lbl2.Caption = "O" And lbl5.Caption = "O" And lbl8.Caption = "O" Then
Call lose
End If
If lbl1.Caption <> "" And lbl2.Caption <> "" And lbl3.Caption <> "" And lbl4.Caption <>
"" And lbl5.Caption <> "" And lbl6.Caption <> "" And lbl7.Caption <> "" And
lbl8.Caption <> "" And lbl9.Caption <> "" Then
Call tie
End If
End Sub
Private Sub priority5()
If lbl2.Caption = "" Then
lbl2.Caption = "O"
Call check
ElseIf lbl4.Caption = "" Then
lbl4.Caption = "O"
Call check
5. ElseIf lbl6.Caption = "" Then
lbl6.Caption = "O"
Call check
ElseIf lbl8.Caption = "" Then
lbl8.Caption = "O"
Call check
End If
End Sub
Private Sub priority3()
If lbl1.Caption = "" Then
lbl1.Caption = "O"
Call check
ElseIf lbl3.Caption = "" Then
lbl3.Caption = "O"
Call check
ElseIf lbl7.Caption = "" Then
lbl7.Caption = "O"
Call check
ElseIf lbl9.Caption = "" Then
lbl9.Caption = "O"
Call check
Else: Call priority5
End If
End Sub
Private Sub priority4()
If lbl5.Caption = "" Then
lbl5.Caption = "O"
Call check
Else: Call priority4
End If
End Sub
Private Sub priority2()
If lbl3.Caption = "X" And lbl5.Caption = "X" And lbl7.Caption = "" Then
lbl7.Caption = "O"
Call check
ElseIf lbl5.Caption = "X" And lbl3.Caption = "" And lbl7.Caption = "X" Then
6. lbl3.Caption = "O"
Call check
ElseIf lbl5.Caption = "" And lbl3.Caption = "X" And lbl7.Caption = "X" Then
lbl5.Caption = "O"
Call check
ElseIf lbl1.Caption = "X" And lbl5.Caption = "X" And lbl9.Caption = "" Then
lbl9.Caption = "O"
Call check
ElseIf lbl1.Caption = "X" And lbl5.Caption = "" And lbl9.Caption = "X" Then
lbl5.Caption = "O"
Call check
ElseIf lbl1.Caption = "" And lbl5.Caption = "X" And lbl9.Caption = "X" Then
lbl1.Caption = "O"
Call check
ElseIf lbl1.Caption = "X" And lbl2.Caption = "X" And lbl3.Caption = "" Then
lbl3.Caption = "O"
Call check
ElseIf lbl1.Caption = "X" And lbl2.Caption = "" And lbl3.Caption = "X" Then
lbl2.Caption = "O"
Call check
ElseIf lbl1.Caption = "" And lbl2.Caption = "X" And lbl3.Caption = "X" Then
lbl1.Caption = "O"
Call check
ElseIf lbl4.Caption = "X" And lbl5.Caption = "X" And lbl6.Caption = "" Then
lbl6.Caption = "O"
Call check
ElseIf lbl4.Caption = "X" And lbl5.Caption = "" And lbl6.Caption = "X" Then
lbl5.Caption = "O"
Call check
ElseIf lbl4.Caption = "" And lbl5.Caption = "X" And lbl6.Caption = "X" Then
lbl4.Caption = "O"
Call check
ElseIf lbl7.Caption = "X" And lbl8.Caption = "X" And lbl9.Caption = "" Then
lbl9.Caption = "O"
Call check
ElseIf lbl7.Caption = "X" And lbl8.Caption = "" And lbl9.Caption = "X" Then
7. lbl8.Caption = "O"
Call check
ElseIf lbl7.Caption = "" And lbl8.Caption = "X" And lbl9.Caption = "X" Then
lbl7.Caption = "O"
Call check
ElseIf lbl1.Caption = "X" And lbl4.Caption = "X" And lbl7.Caption = "" Then
lbl7.Caption = "O"
Call check
ElseIf lbl1.Caption = "X" And lbl4.Caption = "" And lbl7.Caption = "X" Then
lbl4.Caption = "O"
Call check
ElseIf lbl1.Caption = "" And lbl4.Caption = "X" And lbl7.Caption = "X" Then
lbl1.Caption = "O"
Call check
ElseIf lbl3.Caption = "X" And lbl6.Caption = "X" And lbl9.Caption = "" Then
lbl9.Caption = "O"
Call check
ElseIf lbl3.Caption = "X" And lbl6.Caption = "" And lbl9.Caption = "X" Then
lbl6.Caption = "O"
Call check
ElseIf lbl3.Caption = "" And lbl6.Caption = "X" And lbl9.Caption = "X" Then
lbl3.Caption = "O"
Call check
ElseIf lbl2.Caption = "X" And lbl5.Caption = "X" And lbl8.Caption = "" Then
lbl8.Caption = "O"
Call check
ElseIf lbl2.Caption = "X" And lbl5.Caption = "" And lbl8.Caption = "X" Then
lbl5.Caption = "O"
Call check
ElseIf lbl2.Caption = "" And lbl5.Caption = "X" And lbl8.Caption = "X" Then
lbl2.Caption = "O"
Call check
Else: Call priority3
End If
End Sub
Private Sub priority1()
8. If lbl3.Caption = "O" And lbl5.Caption = "O" And lbl7.Caption = "" Then
lbl7.Caption = "O"
Call check
ElseIf lbl5.Caption = "O" And lbl3.Caption = "" And lbl7.Caption = "O" Then
lbl3.Caption = "O"
Call check
ElseIf lbl5.Caption = "" And lbl3.Caption = "O" And lbl7.Caption = "O" Then
lbl5.Caption = "O"
Call check
ElseIf lbl1.Caption = "O" And lbl5.Caption = "O" And lbl9.Caption = "" Then
lbl9.Caption = "O"
Call check
ElseIf lbl1.Caption = "O" And lbl5.Caption = "" And lbl9.Caption = "O" Then
lbl5.Caption = "O"
Call check
ElseIf lbl1.Caption = "" And lbl5.Caption = "O" And lbl9.Caption = "O" Then
lbl1.Caption = "O"
Call check
ElseIf lbl1.Caption = "O" And lbl2.Caption = "O" And lbl3.Caption = "" Then
lbl3.Caption = "O"
Call check
ElseIf lbl1.Caption = "O" And lbl2.Caption = "" And lbl3.Caption = "O" Then
lbl2.Caption = "O"
Call check
ElseIf lbl1.Caption = "" And lbl2.Caption = "O" And lbl3.Caption = "O" Then
lbl1.Caption = "O"
Call check
ElseIf lbl4.Caption = "O" And lbl5.Caption = "O" And lbl6.Caption = "" Then
lbl6.Caption = "O"
Call check
ElseIf lbl4.Caption = "O" And lbl5.Caption = "" And lbl6.Caption = "O" Then
lbl5.Caption = "O"
Call check
ElseIf lbl4.Caption = "" And lbl5.Caption = "O" And lbl6.Caption = "O" Then
lbl4.Caption = "O"
Call check
9. ElseIf lbl7.Caption = "O" And lbl8.Caption = "O" And lbl9.Caption = "" Then
lbl9.Caption = "O"
Call check
ElseIf lbl7.Caption = "O" And lbl8.Caption = "" And lbl9.Caption = "O" Then
lbl8.Caption = "O"
Call check
ElseIf lbl7.Caption = "" And lbl8.Caption = "O" And lbl9.Caption = "O" Then
lbl7.Caption = "O"
Call check
ElseIf lbl1.Caption = "O" And lbl4.Caption = "O" And lbl7.Caption = "" Then
lbl7.Caption = "O"
Call check
ElseIf lbl1.Caption = "O" And lbl4.Caption = "" And lbl7.Caption = "O" Then
lbl4.Caption = "O"
Call check
ElseIf lbl1.Caption = "" And lbl4.Caption = "O" And lbl7.Caption = "O" Then
lbl1.Caption = "O"
Call check
ElseIf lbl3.Caption = "O" And lbl6.Caption = "O" And lbl9.Caption = "" Then
lbl9.Caption = "O"
Call check
ElseIf lbl3.Caption = "O" And lbl6.Caption = "" And lbl9.Caption = "O" Then
lbl6.Caption = "O"
Call check
ElseIf lbl3.Caption = "" And lbl6.Caption = "O" And lbl9.Caption = "O" Then
lbl3.Caption = "O"
Call check
ElseIf lbl2.Caption = "O" And lbl5.Caption = "O" And lbl8.Caption = "" Then
lbl8.Caption = "O"
Call check
ElseIf lbl2.Caption = "O" And lbl5.Caption = "" And lbl8.Caption = "O" Then
lbl5.Caption = "O"
Call check
ElseIf lbl2.Caption = "" And lbl5.Caption = "O" And lbl8.Caption = "O" Then
lbl2.Caption = "O"
Call check
10. ElseIf lbl1.Caption = "X" And lbl5.Caption = "" Then
lbl5.Caption = "O"
ElseIf lbl9.Caption = "X" And lbl5.Caption = "" Then
lbl5.Caption = "O"
ElseIf lbl1.Caption = "X" And lbl9.Caption = "X" And lbl8.Caption = "" Then
lbl8.Caption = "O"
ElseIf lbl1.Caption = "X" And lbl9.Caption = "X" And lbl2.Caption = "" Then
lbl2.Caption = "O"
ElseIf lbl3.Caption = "X" And lbl5.Caption = "" Then
lbl5.Caption = "O"
ElseIf lbl7.Caption = "X" And lbl5.Caption = "" Then
lbl5.Caption = "O"
ElseIf lbl3.Caption = "X" And lbl7.Caption = "X" And lbl8.Caption = "" Then
lbl8.Caption = "O"
ElseIf lbl3.Caption = "X" And lbl7.Caption = "X" And lbl2.Caption = "" Then
lbl2.Caption = "O"
Else: Call priority2
End If
End Sub
Private Sub CPU()
Call priority1
End Sub
Private Sub Label1_Click()
End Sub
Private Sub cmdhelp_Click()
helpform.cmdplay.Caption = "Continue - Hard"
hardform.Visible = False
helpform.Visible = True
End Sub
Private Sub Form_Load()
End Sub
Private Sub lbl1_Click()
If lbl1.Caption = "" Then
lbl1.Caption = "X"
Call check
Call CPU
11. Else: MsgBox "Invalid move"
End If
End Sub
Private Sub lbl2_Click()
If lbl2.Caption = "" Then
lbl2.Caption = "X"
Call check
Call CPU
Else: MsgBox "Invalid move"
End If
End Sub
Private Sub lbl3_Click()
If lbl3.Caption = "" Then
lbl3.Caption = "X"
Call check
Call CPU
Else: MsgBox "Invalid move"
End If
End Sub
Private Sub lbl4_Click()
If lbl4.Caption = "" Then
lbl4.Caption = "X"
Call check
Call CPU
Else: MsgBox "Invalid move"
End If
End Sub
Private Sub lbl5_Click()
If lbl5.Caption = "" Then
lbl5.Caption = "X"
Call check
Call CPU
Else: MsgBox "Invalid move"
End If
End Sub
Private Sub lbl6_Click()
12. If lbl6.Caption = "" Then
lbl6.Caption = "X"
Call check
Call CPU
Else: MsgBox "Invalid move"
End If
End Sub
Private Sub lbl7_Click()
If lbl7.Caption = "" Then
lbl7.Caption = "X"
Call check
Call CPU
Else: MsgBox "Invalid move"
End If
End Sub
Private Sub lbl8_Click()
If lbl8.Caption = "" Then
lbl8.Caption = "X"
Call check
Call CPU
Else: MsgBox "Invalid move"
End If
End Sub
Private Sub lbl9_Click()
If lbl9.Caption = "" Then
lbl9.Caption = "X"
Call check
Call CPU
Else: MsgBox "Invalid move"
End If
End Sub