Click here to Skip to main content
13,866,642 members
Click here to Skip to main content
Add your own
alternative version


48 bookmarked
Posted 5 May 2004

Speech Tic-Tac-Toe

, 5 May 2004
Rate this:
Please Sign up or sign in to vote.
A speech recognition demonstration program.

Click here to play the demo (User need to install Microsoft Internet Explorer Speech Add-in 1.0 to play the demo).


This demonstration program enables you to use the Microsoft Speech Application SDK to create a voice recognition-enabled web application. I used a tic-tac-toe game (modified from a JavaScript version). This game can be played by using speech controls and a mike, without keyboard and mouse input. This is a very simple program, but it will give you an idea on how to let your web applications interact with speech controls.

How to play:

  1. User plays the X's and the computer plays the O's.
  2. User selects a square by saying the number-name of the square into the mike.

How it works:

This is a multimodal application (a web application that uses speech in conjunction with a graphical interface to interact with the user). In the program, I use three kinds of Speech objects. They are grammar, prompt and listen:

Speech ObjectsObject names
Grammar objects
  1. toplevel
  2. position
Prompt objects
  1. welcomePrompt
  2. tiedPrompt
  3. losePrompt
  4. winPrompt
Listen objects
  1. askPostionListen

Speech associations:

  1. When the program opens, welcomePrompt plays the instructions, using the text-to-speech engine. (The prompt object is associated with the TTS engine.)
  2. When the text reading is complete, askPositionListen turns on the speech recording function. (The listenprompt object is associated with the speech recognition engine.)
  3. When a word is recorded and it matches a rule of the grammar, the program will call a function yourChoice() such as OnClientReco=yourChoice(), and displays an "X" into the selected square. (In the original JavaScript version of tic-tac-toe, the function yourChoice() is an onClick function such as onClick = yourChoice().)

The following picture shows how an object prompt is associated with the speech recognition engine:

Using the code

Included in the program files are: Microsoft.Speech.Web.dll and source JavaScript file Function.js. All the functions that are needed to play the game are in the files. The following namespace and schemas will be added to the program automatically if you are using Microsoft SASDK.

<%@ Register TagPrefix="speech" Namespace="Microsoft.Speech.Web.UI" 

        Assembly="Microsoft.Speech.Web, Version=1.0.3200.0, Culture=neutral, 
        PublicKeyToken=31bf3856ad364e35" %>
<body onload="OnLoad()" xmlns:speech=""


First, let's create the rules of the grammar.

Rule 1 - toplevel:

Add a RuleRef and name it Position as seen above and set its script tag. Use the example image below.

Rule 2 - position

Add a list and some phrases and name them. The following chart shows the tic-tac-toe script tags. The one...nine represents the name of the squares. Also, select the Constant and type the name in the "Enter value" text box for each Script tag.

The code will be automatically generated, and will look like the box below.

We can find the rules for toplevel and Position in the code.

  • The toplevel rule uses the ruleref element to reference the Position rule.
  • The <item> represents a phrase. Each phrase can only have one word in this grammar.
  • The script expression, $._value= "xx", contained in the tag element, is executed when the speech recognizer follows a path through the grammar and finds the words or phrases that the tag element follows.
  • The <one-of> represents a word list, or dictionary. When a user speaks one of the words in the list, the recognition engine recognizes that word.
<rule id="toplevel" scope="public">
    <ruleref uri="#Position" type="application/srgs+xml"/>
    <tag>$.Position = $$</tag>
<rule id="Position" scope="public">
            <tag>$._value = "one"</tag>
            <tag>$._value = "two"</tag>

The second step is to add a listen element and set its properties like this:

The code will be generated as in the box below.

The listen element specifies possible speech inputs and controls the speech recognition processes and results. Grammar is one of the main elements of the listen object. In the following, OnClientReco= "yourChoice" means that if the speech is successfully recorded, the function yourChoice will be called. The function is the same as onClick="yourChoice" in the original JavaScript program. If no recording, or silence, or random speech is detected, the listen will start again. The codes are: OnClientSilence= "ListenStart", OnClientNoReco= "ListenStart" or OnClientSpeechDetected= "ListenStart".

<speech:listen id= "AskPositionListen" runat="server" OnClientSilence=  
      "ListenStart" OnClientNoReco= "ListenStart" 

      OnClientSpeechDetected= "ListenStart" 

      OnClientReco= "yourChoice" MaxTimeout="15000" 

      EndSilence=  "1000" InitialTimeout= "2000">
        <speech:Grammar Src="Grammars/SpeechTicTacToe.grxml#toplevel" 

            ID= "AskPositionListen_Grammar1">  

For example, once the user speaks a word and it activates the speech recognition engine, yourChoice will be called. The code: event.srcElement.recoResult.selectSingleNode("Position"); will return the value of the word.

function yourChoice()
    //the theNode catches the result of the speech
    var theNode = event.srcElement.recoResult.selectSingleNode("Position");

    //the chName catches the result of the speech 
    //in a string format. The string should be 
    //one of the square's name.
    var chName;
    chName = theNode.text;

    //display the X image in the chName square.
    document.images[chName].src = "x.jpg";

Now, add the prompt and set its properties:

<speech:Prompt id="welcomePrompt" runat= "server">
      Welcome to Speech  Tic-Tac-Toe! You play as theX's and the computer is the O's. 
      Select the square you want to put your X into by saying them.Good Luck!

The following shows how to use the prompt and listen objects in JavaScript.

function OnLoad(){
    //text-to-speech engine start the welcome prompt

    //called the listen function

function ListenStart(){
    //speech recognization engine start to listen speech

User requirement

Users need to install Microsoft Internet Explorer Speech Add-in 1.0 in their computer in order to play the game.

Developers need to install Microsoft Speech Application SDK v1.0 Beta 3 in order to use the speech objects.


  • 5/6/2004 - first post.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Kwong-Leung kong
Web Developer
United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralRegarding Speech add-in for Windows 7 and Windows Vista Pin
Member 415557728-Jun-10 21:55
memberMember 415557728-Jun-10 21:55 
GeneralHosting IVR application Pin
Priyanka_Gupta10-Mar-09 3:01
memberPriyanka_Gupta10-Mar-09 3:01 
Questionmultilanguage support of speech api how?? Pin
29181195-Mar-09 2:45
member29181195-Mar-09 2:45 
Generaljavascript Pin
ffw_me26-Oct-06 9:37
memberffw_me26-Oct-06 9:37 
GeneralGetting error on Tic-Tac-Toe Pin
venkatttttttttttt22-Aug-06 20:12
membervenkatttttttttttt22-Aug-06 20:12 
GeneralError regarding TicTacToe Pin
venkatttttttttttt22-Aug-06 2:47
membervenkatttttttttttt22-Aug-06 2:47 
GeneralProgram Works, but.... Pin
OldYgg10-Nov-05 18:50
memberOldYgg10-Nov-05 18:50 
GeneralRe: Program Works, but.... Pin
OldYgg10-Nov-05 19:12
memberOldYgg10-Nov-05 19:12 
GeneralAbout recognition Pin
sinmorn24-May-04 17:55
membersinmorn24-May-04 17:55 
GeneralYour Demo Pin
Albert Pascual7-May-04 13:18
sitebuilderAlbert Pascual7-May-04 13:18 
GeneralRe: Your Demo Pin
leppie7-May-04 14:41
memberleppie7-May-04 14:41 
GeneralRe: Your Demo Pin
Albert Pascual7-May-04 14:48
sitebuilderAlbert Pascual7-May-04 14:48 
GeneralRe: Your Demo Pin
Kwong-Leung kong7-May-04 18:43
memberKwong-Leung kong7-May-04 18:43 
GeneralRe: Mine works Pin
Member 10820477-May-04 19:05
memberMember 10820477-May-04 19:05 
GeneralRe: Your Demo Pin
Syed Murtaza Hussain Rizvi23-Dec-06 4:59
memberSyed Murtaza Hussain Rizvi23-Dec-06 4:59 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web02 | 2.8.190214.1 | Last Updated 6 May 2004
Article Copyright 2004 by Kwong-Leung kong
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid