Page 1 of 1

Introduction To Basic AI (Artificial Inteligence) Rate Topic: ***** 1 Votes

#1 aj32  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 30
  • View blog
  • Posts: 577
  • Joined: 30-August 07

Post icon  Posted 15 March 2008 - 05:19 PM

Title: Introduction To AI
SubT: (Artificial Inteligence)

Body:

In this tutorial I will show you how to set up a basic AI program, written in Visual C++. The language I will be using is "Managed C++", using the .NET framework.

To begin, we will create the project. First, create a Windows Forms Project, and open the Form1.h code view.

Delete all the coding in Form1.h, and insert the following code into the Form:
#pragma once
#include <cstdlib>

namespace Tutorialnumber3IntroductiontoAI {

	using namespace System;
	using namespace System::ComponentModel;
	using namespace System::Collections;
	using namespace System::Windows::Forms;
	using namespace System::Data;
	using namespace System::Drawing;
	using namespace std;

	/// <summary>
	/// Summary for Form1
	///
	/// WARNING: If you change the name of this class, you will need to change the
	///		  'Resource File Name' property for the managed resource compiler tool
	///		  associated with all .resx files this class depends on.  Otherwise,
	///		  the designers will not be able to interact properly with localized
	///		  resources associated with this form.
	/// </summary>
	public ref class Form1 : public System::Windows::Forms::Form
	{
	public:
		Form1(void)
		{
			InitializeComponent();
			//
			//TODO: Add the constructor code here
			//
		}

	protected:
		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		~Form1()
		{
			if (components)
			{
				delete components;
			}
		}
	private: System::Windows::Forms::ListBox^  Disc;
	protected: 
	private: System::Windows::Forms::Label^  label1;
	private: System::Windows::Forms::Button^  button1;
	private: System::Windows::Forms::TextBox^  Talk;

	private:
		/// <summary>
		/// Required designer variable.
		/// </summary>
		System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		void InitializeComponent(void)
		{
			this->Disc = (gcnew System::Windows::Forms::ListBox());
			this->label1 = (gcnew System::Windows::Forms::Label());
			this->button1 = (gcnew System::Windows::Forms::Button());
			this->Talk = (gcnew System::Windows::Forms::TextBox());
			this->SuspendLayout();
			// 
			// Disc
			// 
			this->Disc->Dock = System::Windows::Forms::DockStyle::Top;
			this->Disc->FormattingEnabled = true;
			this->Disc->Location = System::Drawing::Point(0, 0);
			this->Disc->Name = L"Disc";
			this->Disc->Size = System::Drawing::Size(583, 264);
			this->Disc->TabIndex = 0;
			// 
			// label1
			// 
			this->label1->AutoSize = true;
			this->label1->Location = System::Drawing::Point(0, 271);
			this->label1->Name = L"label1";
			this->label1->Size = System::Drawing::Size(31, 13);
			this->label1->TabIndex = 1;
			this->label1->Text = L"Talk:";
			// 
			// button1
			// 
			this->button1->Location = System::Drawing::Point(280, 266);
			this->button1->Name = L"button1";
			this->button1->Size = System::Drawing::Size(75, 23);
			this->button1->TabIndex = 2;
			this->button1->Text = L"Submit";
			this->button1->UseVisualStyleBackColor = true;
			this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);
			// 
			// Talk
			// 
			this->Talk->Location = System::Drawing::Point(29, 268);
			this->Talk->Name = L"Talk";
			this->Talk->Size = System::Drawing::Size(245, 20);
			this->Talk->TabIndex = 3;
			// 
			// Form1
			// 
			this->AcceptButton = this->button1;
			this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
			this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
			this->ClientSize = System::Drawing::Size(583, 291);
			this->Controls->Add(this->Talk);
			this->Controls->Add(this->button1);
			this->Controls->Add(this->label1);
			this->Controls->Add(this->Disc);
			this->FormBorderStyle = System::Windows::Forms::FormBorderStyle::FixedSingle;
			this->MaximizeBox = false;
			this->Name = L"Form1";
			this->ShowIcon = false;
			this->StartPosition = System::Windows::Forms::FormStartPosition::CenterScreen;
			this->Text = L"</dream.in.code> - Tutorial - Introduction to AI";
			this->ResumeLayout(false);
			this->PerformLayout();

		}
#pragma endregion
	private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
			 }
};
}



Now, if you open design view, you will see the designed form.

Next (THIS IS VERY IMPORTANT!!) open "yourprojectname.cpp" under source files in solution explorer, replace all the coding it that file with:
// Tutorial Number 3 - test.cpp : main project file.

#include "stdafx.h"
#include "Form1.h"

using namespace Tutorialnumber3IntroductiontoAI;

[STAThreadAttribute]
int main(array<System::String ^> ^args)
{
	// Enabling Windows XP visual effects before any controls are created
	Application::EnableVisualStyles();
	Application::SetCompatibleTextRenderingDefault(false); 

	// Create the main window and run it
	Application::Run(gcnew Form1());
	return 0;
}





Next, we will add some basic AI inot our "Form1.h" file, under button1_Click(), type in (or copy and paste) the following code into the program:

Talk->Text->ToLower();
if(Talk->Text->Contains("hi") || Talk->Text->Contains("hello") || Talk->Text->Contains("howdy"))
				 {
					 if(Talk->Text->Contains("how are you"))
					 {
						 Disc->Items->AddRange(gcnew cli::array< System::Object^  >(1) {"Hello there! I'm feeling fine, how are you?"});
					 }
					 else
					 {
						 Disc->Items->AddRange(gcnew cli::array< System::Object^  >(1) {"Hi there!"});
					 }
				 }
				 else
				 {
					  Disc->Items->AddRange(gcnew cli::array< System::Object^  >(1) {"That is rather interesting!! But I don't think I know how to respond to that..."});
				 }
				 Talk->Text = "";


Now, if you run the program, we have a simple AI set up!! If you say "Hi there, how are you?" or "hi, how are you feeling?" or something like that, the program will respond, if the program cannot figure out what you said, it will return the "smart error message"! See where the code goes Talk->Text->ToLower(); - this is changing the text in the textbox to lower case, thus making it easier to work with!

Our program is rather simple, wouldn't you say? Lets make it a little more interesting!" Change the code we just added, to this:
				 Disc->Items->AddRange(gcnew cli::array< System::Object^  >(1) {Talk->Text}); 
				 Talk->Text->ToLower();
				 if(Talk->Text->Contains("hi") || Talk->Text->Contains("hello") || Talk->Text->Contains("howdy"))
				 {
					 if(Talk->Text->Contains("how are you"))
					 {
						 int RAND = rand() % 3 + 1;
						 if(RAND == 1)
						 {
							 Disc->Items->AddRange(gcnew cli::array< System::Object^  >(1) {"Hello there! I'm feeling fine, how are you?"});
						 }
						 else if(RAND == 2)
						 {
							 Disc->Items->AddRange(gcnew cli::array< System::Object^  >(1) {"Hello, I'm feeling ok, how are you today?"});
						 }
						 else
						 {
							 Disc->Items->AddRange(gcnew cli::array< System::Object^  >(1) {"Hi! I'm ok, and you?"});
						 }
							 
					 }
					 else
					 {
						 Disc->Items->AddRange(gcnew cli::array< System::Object^  >(1) {"Hi there!"});
					 }
				 }
				 else
				 {
					  Disc->Items->AddRange(gcnew cli::array< System::Object^  >(1) {"That is rather interesting!! But I don't think I know how to respond to that..."});
				 }
				 Talk->Text = "";



Now, our program will generate a random number, this is done with the rand() % x + n function, where x = highest integer and n = lowest integer that the random value is selected from. The program then uses the number generated by random to output one of three messages in response to you saying "hi", and asking"how are you"!

That took a while to type out, all those Disc->Items->Addrange(...); values, didn't it? Let's make it a little easier on ourselves! Type in the code below before the button1_Click function:

		private: System::Void O (String^ data)
				 {
					 Disc->Items->AddRange(gcnew cli::array< System::Object^  >(1) {"Mr. Bot: " + data});	 
				 }
		private: System::Void IO (String^ data)
				 {
					 Disc->Items->AddRange(gcnew cli::array< System::Object^  >(1) {"You: " + data});	 
				 }



Those will be our output functions, 'O' will be used for outputing what the 'bot' or program 'says', and IO will be used for outputting what you say. Now, change the button1_Click Function code to:

IO(Talk->Text); 
				 Talk->Text->ToLower();
				 if(Talk->Text->Contains("hi") || Talk->Text->Contains("hello") || Talk->Text->Contains("howdy"))
				 {
					 if(Talk->Text->Contains("how are you"))
					 {
						 int RAND = rand() % 3 + 1;
						 if(RAND == 1)
						 {
							 O("Hello there! I'm feeling fine, how are you?");
						 }
						 else if(RAND == 2)
						 {
							 O("Hello, I'm feeling ok, how are you today?");
						 }
						 else
						 {
							 O("Hi! I'm ok, and you?");
						 }
							 
					 }
					 else
					 {
						 O("Hi there!");
					 }
				 }
				 else
				 {
					  O("That is rather interesting!! But I don't think I know how to respond to that...");
				 }
				 Talk->Text = "";


Now, you will notice that I changed all the [ disc->items->... ] coding to [ O("SOMETHING"); ], that should make our job a little easier, now all we have to type is O("BLAHBLAHBLAH");! Now let's add some more functions to our program:

in [button1_Click()] before:
else
{
O("That is rather interesting!! But I don't think I know how to respond to that...");
}
Talk->Text = "";:

Add this code:
else if(Talk->Text->Contains("hate you") || (Talk->Text->Contains("annoying") && Talk->Text->Contains("you") && Talk->Text->Contains("are")))
				 {
					 	int RAND = rand() % 3 + 1;
						 if(RAND == 1)
						 {
							 O("Why would you say something like that?");
						 }
						 else if(RAND == 2)
						 {
							 O("Really? I haven't noticed!");
						 }
						 else
						 {
							 O("Well, I think the same about you!");
						 }
				 }
				 else if(Talk->Text->Contains("I like you") || (Talk->Text->Contains("you") && Talk->Text->Contains("nice") && Talk->Text->Contains("great")))
				 {
					 	int RAND = rand() % 3 + 1;
						 if(RAND == 1)
						 {
							 O("Why, thank you!");
						 }
						 else if(RAND == 2)
						 {
							 O("I like you too!");
						 }
						 else
						 {
							 O("Of course!! Everyone likes me!");
						 }
				 }
				 else if(Talk->Text->Contains("dream") && (Talk->Text->Contains("in") && Talk->Text->Contains("code")) && (Talk->Text->Contains("hate") || Talk->Text->Contains("stupid") || Talk->Text->Contains("annoying")))
				 {
					 	 int RAND = rand() % 3 + 1;
						 if(RAND == 1)
						 {
							 O("Shut up, dreamincode.Net is the best website out there!");
						 }
						 else if(RAND == 2)
						 {
							 O("Are you crazy??? </d.i.c.> is the best site out there!");
						 }
						 else
						 {
							 O("If you don't like </d.i.c.>, Go away!!");
						 }
				 }
 				 else if(Talk->Text->Contains("dream") && (Talk->Text->Contains("in") && Talk->Text->Contains("code")) && (Talk->Text->Contains("like") || Talk->Text->Contains("good") || Talk->Text->Contains("great")))
				 {
					 	 int RAND = rand() % 3 + 1;
						 if(RAND == 1)
						 {
							 O("Yes, </dream.in.code>.Net is the best website out there!");
						 }
						 else if(RAND == 2)
						 {
							 O("I must aggree with you!");
						 }
						 else
						 {
							 O("I like </dream.in.code>, it's a great site!");
						 }
				 }



Now that we have added some more if() functions into our AI program, it will be more able to 'talk' with the user! As you will see in my code, I have split items up into if() groups, such as instead of
if(Talk->Text->Contains("dream.in.code")
,
if(Talk->Text->Contains("dream") && (Talk->Text->Contains("in") && Talk->Text->Contains("code"))
is used, this enables the the program to "Read" the user input better, so the user doesn't have to type in "I like dream.in.code", he can have more options, such as typing "I like dreamincode".

Now lets add a few global variables to our program, where you will see code such as private: System::Windows::Forms::TextBox^ Talk;, enter the following:
	private: String^ likedic;
			 String^ likeme;
			 String^ hi;



Then add this under the if() statement that will see if the user doesn't like </d.i.c.>:
if(likedic == "NO")
					 {
						 O("You must be crazy, first you talk about </d.i.c.> Being a bad site, now you say it's great!");
					 }
					 else
					 {	 


and close the else {} tag after else, so that the code looks like:
else if(Talk->Text->Contains("dream") && (Talk->Text->Contains("in") && Talk->Text->Contains("code")) && (Talk->Text->Contains("like") || Talk->Text->Contains("good") || Talk->Text->Contains("great")))
				 {
					 if(likedic == "NO")
					 {
						 O("You must be crazy, first you talk about </d.i.c.> Being a bad site, now you say it's great!");
					 }
					 else
					 {	 
						 int RAND = rand() % 3 + 1;
						 if(RAND == 1)
						 {
							 O("Yes, </dream.in.code>.Net is the best website out there!");
						 }
						 else if(RAND == 2)
						 {
							 O("I must aggree with you!");
						 }
						 else
						 {
							 O("I like </dream.in.code>, it's a great site!");
						 }
						 
					 }
				 }



Next, add a function that will set the String value 'likedic' to "YES", if you user says he likes </d.i.c.>!
int RAND = rand() % 3 + 1;
						 if(RAND == 1)
						 {
							 O("Yes, </dream.in.code>.Net is the best website out there!");
						 }
						 else if(RAND == 2)
						 {
							 O("I must aggree with you!");
						 }
						 else
						 {
							 O("I like </dream.in.code>, it's a great site!");
						 }
						 
					 }
					/*******************/ likedic = "YES";  /********************/



and the one that will switch it to "NO" if the user says he doesn't like </d.i.c.>:
					 	 int RAND = rand() % 3 + 1;
						 if(RAND == 1)
						 {
							 O("Shut up, dreamincode.Net is the best website out there!");
						 }
						 else if(RAND == 2)
						 {
							 O("Are you crazy??? </d.i.c.> is the best site out there!");
						 }
						 else
						 {
							O("If you don't like </d.i.c.>, Go away!!");
						 }
						 
					 }
					 likedic = "NO";



Now, our code should look like this:


[code]
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
IO(Talk->Text);
Talk->Text->ToLower();
if(Talk->Text->Contains("hi") || Talk->Text->Contains("hello") || Talk->Text->Contains("howdy"))
{
if(Talk->Text->Contains("how are you"))
{
int RAND = rand() % 3 + 1;
if(RAND == 1)
{
O("Hello there! I'm feeling fine, how are you?");
}
else if(RAND == 2)
{
O("Hello, I'm feeling ok, how are you today?");
}
else
{
O("Hi! I'm ok, and you?");
}

}
else
{
O("Hi there!");
}
}
else if(Talk->Text->Contains("hate you") || (Talk->Text->Contains("annoying") && Talk->Text->Contains("you") && Talk->Text->Contains("are")))
{
int RAND = rand() % 3 + 1;
if(RAND == 1)
{
O("Why would you say something like that?");
}
else if(RAND == 2)
{
O("Really? I haven't noticed!");
}
else
{
O("Well, I think the same about you!");
}
}
else if(Talk->Text->Contains("I like you") || (Talk->Text->Contains("you") && Talk->Text->Contains("nice") && Talk->Text->Contains("great")))
{
int RAND = rand() % 3 + 1;
if(RAND == 1)
{
O("Why, thank you!");
}
else if(RAND == 2)
{
&nbs

Is This A Good Question/Topic? 0
  • +

Replies To: Introduction To Basic AI

#2 proud2bmyself  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 19-March 09

Posted 02 April 2009 - 06:33 AM

hey man im a total noob lol and when you say, now add this code, that starts with private, before the button click function. where exactly are you talking about because i tried adding it in a few places by the button function and when i build it, it always has an error?? before i add this code it builds fine but when i debug it, it just outputs something like " im not sure i understand" and when i try to type hello or something and click submit, it never replies back?? Thanks!!
Oh if you could just paste the complete code for each page, like form1.h and the complete for the (projectname).cpp that would really help??
Peace!
Was This Post Helpful? 0
  • +
  • -

#3 khaotic  Icon User is offline

  • New D.I.C Head

Reputation: -4
  • View blog
  • Posts: 6
  • Joined: 27-June 09

Posted 11 December 2010 - 11:47 PM

if only i was a windows user...linux ftw
Was This Post Helpful? -4
  • +
  • -

Page 1 of 1