SlideShare a Scribd company logo
1 of 12
Download to read offline
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 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 = ""
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
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
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
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
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()
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
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
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
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()
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

More Related Content

More from fortmdu

Disneys Expedition EverestOne of the newest thrill rides to open.pdf
Disneys Expedition EverestOne of the newest thrill rides to open.pdfDisneys Expedition EverestOne of the newest thrill rides to open.pdf
Disneys Expedition EverestOne of the newest thrill rides to open.pdf
fortmdu
 
Discuss ONE risk that a company faces when trying to diversify inte.pdf
Discuss ONE risk that a company faces when trying to diversify inte.pdfDiscuss ONE risk that a company faces when trying to diversify inte.pdf
Discuss ONE risk that a company faces when trying to diversify inte.pdf
fortmdu
 
CASE 2-1 BUILDING UP OUR ASSETS DHR CONSTRUCTIONIn August 2011, w.pdf
CASE 2-1 BUILDING UP OUR ASSETS DHR CONSTRUCTIONIn August 2011, w.pdfCASE 2-1 BUILDING UP OUR ASSETS DHR CONSTRUCTIONIn August 2011, w.pdf
CASE 2-1 BUILDING UP OUR ASSETS DHR CONSTRUCTIONIn August 2011, w.pdf
fortmdu
 
Assignment isPage 349-350 #4 and #5 Use the Linked List lab.pdf
Assignment isPage 349-350 #4 and #5 Use the Linked List lab.pdfAssignment isPage 349-350 #4 and #5 Use the Linked List lab.pdf
Assignment isPage 349-350 #4 and #5 Use the Linked List lab.pdf
fortmdu
 
You are to write an efficient program that will read a dictionary of.pdf
You are to write an efficient program that will read a dictionary of.pdfYou are to write an efficient program that will read a dictionary of.pdf
You are to write an efficient program that will read a dictionary of.pdf
fortmdu
 
The last major idea in population dynamics is the idea of a minimum .pdf
The last major idea in population dynamics is the idea of a minimum .pdfThe last major idea in population dynamics is the idea of a minimum .pdf
The last major idea in population dynamics is the idea of a minimum .pdf
fortmdu
 
Python ProgramDo not use import please. Problem 1 Rolodex (Note .pdf
Python ProgramDo not use import please. Problem 1 Rolodex (Note .pdfPython ProgramDo not use import please. Problem 1 Rolodex (Note .pdf
Python ProgramDo not use import please. Problem 1 Rolodex (Note .pdf
fortmdu
 

More from fortmdu (20)

Disneys Expedition EverestOne of the newest thrill rides to open.pdf
Disneys Expedition EverestOne of the newest thrill rides to open.pdfDisneys Expedition EverestOne of the newest thrill rides to open.pdf
Disneys Expedition EverestOne of the newest thrill rides to open.pdf
 
Discuss ONE risk that a company faces when trying to diversify inte.pdf
Discuss ONE risk that a company faces when trying to diversify inte.pdfDiscuss ONE risk that a company faces when trying to diversify inte.pdf
Discuss ONE risk that a company faces when trying to diversify inte.pdf
 
Describe at least one reason why transitioning from PVST+ to Rapid P.pdf
Describe at least one reason why transitioning from PVST+ to Rapid P.pdfDescribe at least one reason why transitioning from PVST+ to Rapid P.pdf
Describe at least one reason why transitioning from PVST+ to Rapid P.pdf
 
CASE 2-1 BUILDING UP OUR ASSETS DHR CONSTRUCTIONIn August 2011, w.pdf
CASE 2-1 BUILDING UP OUR ASSETS DHR CONSTRUCTIONIn August 2011, w.pdfCASE 2-1 BUILDING UP OUR ASSETS DHR CONSTRUCTIONIn August 2011, w.pdf
CASE 2-1 BUILDING UP OUR ASSETS DHR CONSTRUCTIONIn August 2011, w.pdf
 
C++ Write a function that takes two numbers are parameters and retu.pdf
C++ Write a function that takes two numbers are parameters and retu.pdfC++ Write a function that takes two numbers are parameters and retu.pdf
C++ Write a function that takes two numbers are parameters and retu.pdf
 
B.1 Reaction of a Hydrate Addition of Water (2) Appearance Heating (1.pdf
B.1 Reaction of a Hydrate Addition of Water (2) Appearance Heating (1.pdfB.1 Reaction of a Hydrate Addition of Water (2) Appearance Heating (1.pdf
B.1 Reaction of a Hydrate Addition of Water (2) Appearance Heating (1.pdf
 
Assignment isPage 349-350 #4 and #5 Use the Linked List lab.pdf
Assignment isPage 349-350 #4 and #5 Use the Linked List lab.pdfAssignment isPage 349-350 #4 and #5 Use the Linked List lab.pdf
Assignment isPage 349-350 #4 and #5 Use the Linked List lab.pdf
 
You maintain several virtual machines (VMs) in an offline state. How.pdf
You maintain several virtual machines (VMs) in an offline state. How.pdfYou maintain several virtual machines (VMs) in an offline state. How.pdf
You maintain several virtual machines (VMs) in an offline state. How.pdf
 
Which of the following statements is not TRUE1)For a 64-bit compute.pdf
Which of the following statements is not TRUE1)For a 64-bit compute.pdfWhich of the following statements is not TRUE1)For a 64-bit compute.pdf
Which of the following statements is not TRUE1)For a 64-bit compute.pdf
 
You are running an ELISA on a sample to test for the presence of .pdf
You are running an ELISA on a sample to test for the presence of .pdfYou are running an ELISA on a sample to test for the presence of .pdf
You are running an ELISA on a sample to test for the presence of .pdf
 
You are to write an efficient program that will read a dictionary of.pdf
You are to write an efficient program that will read a dictionary of.pdfYou are to write an efficient program that will read a dictionary of.pdf
You are to write an efficient program that will read a dictionary of.pdf
 
X = C B - B C D; Use Accumulator Register-Register (LoadSt.pdf
X = C B - B  C  D; Use Accumulator  Register-Register (LoadSt.pdfX = C B - B  C  D; Use Accumulator  Register-Register (LoadSt.pdf
X = C B - B C D; Use Accumulator Register-Register (LoadSt.pdf
 
Which substance has ionic bondsLiIH2ONH3Br2Which sub.pdf
Which substance has ionic bondsLiIH2ONH3Br2Which sub.pdfWhich substance has ionic bondsLiIH2ONH3Br2Which sub.pdf
Which substance has ionic bondsLiIH2ONH3Br2Which sub.pdf
 
What is a culture A subculture Give an example of each.Solutio.pdf
What is a culture A subculture Give an example of each.Solutio.pdfWhat is a culture A subculture Give an example of each.Solutio.pdf
What is a culture A subculture Give an example of each.Solutio.pdf
 
The last major idea in population dynamics is the idea of a minimum .pdf
The last major idea in population dynamics is the idea of a minimum .pdfThe last major idea in population dynamics is the idea of a minimum .pdf
The last major idea in population dynamics is the idea of a minimum .pdf
 
Python ProgramDo not use import please. Problem 1 Rolodex (Note .pdf
Python ProgramDo not use import please. Problem 1 Rolodex (Note .pdfPython ProgramDo not use import please. Problem 1 Rolodex (Note .pdf
Python ProgramDo not use import please. Problem 1 Rolodex (Note .pdf
 
The partial pressure of oxygen in the alveolar gas is affected by cer.pdf
The partial pressure of oxygen in the alveolar gas is affected by cer.pdfThe partial pressure of oxygen in the alveolar gas is affected by cer.pdf
The partial pressure of oxygen in the alveolar gas is affected by cer.pdf
 
QUESTION 8____ are considered to be product or service market stak.pdf
QUESTION 8____ are considered to be product or service market stak.pdfQUESTION 8____ are considered to be product or service market stak.pdf
QUESTION 8____ are considered to be product or service market stak.pdf
 
A student had a set of solutions labeled A-E which contained in no o.pdf
A student had a set of solutions labeled A-E which contained in no o.pdfA student had a set of solutions labeled A-E which contained in no o.pdf
A student had a set of solutions labeled A-E which contained in no o.pdf
 
A small insect viewed through a convex lens is 1.2 cm from the lens a.pdf
A small insect viewed through a convex lens is 1.2 cm from the lens a.pdfA small insect viewed through a convex lens is 1.2 cm from the lens a.pdf
A small insect viewed through a convex lens is 1.2 cm from the lens a.pdf
 

Recently uploaded

Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
AnaAcapella
 

Recently uploaded (20)

SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docx
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
Asian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptxAsian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptx
 
Third Battle of Panipat detailed notes.pptx
Third Battle of Panipat detailed notes.pptxThird Battle of Panipat detailed notes.pptx
Third Battle of Panipat detailed notes.pptx
 
Spatium Project Simulation student brief
Spatium Project Simulation student briefSpatium Project Simulation student brief
Spatium Project Simulation student brief
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Magic bus Group work1and 2 (Team 3).pptx
Magic bus Group work1and 2 (Team 3).pptxMagic bus Group work1and 2 (Team 3).pptx
Magic bus Group work1and 2 (Team 3).pptx
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 

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