Articles   Members Online:
-Article/Tip Search
-News Group Search over 21 Million news group articles.
-Delphi/Pascal
-CBuilder/C++
-C#Builder/C#
-JBuilder/Java
-Kylix
Member Area
-Home
-Account Center
-Top 10 NEW!!
-Submit Article/Tip
-Forums Upgraded!!
-My Articles
-Edit Information
-Login/Logout
-Become a Member
-Why sign up!
-Newsletter
-Chat Online!
-Indexes NEW!!
Employment
-Build your resume
-Find a job
-Post a job
-Resume Search
Contacts
-Contacts
-Feedbacks
-Link to us
-Privacy/Disclaimer
Embarcadero
Visit Embarcadero
Embarcadero Community
JEDI
Links
A Simple example of Artificial Intelligence using Delphi Array Turn on/off line numbers in source code. Switch to Orginial background IDE or DSP color Comment or reply to this aritlce/tip for discussion. Bookmark this article to my favorite article(s). Print this article
22-Jun-03
Category
OO-related
Language
Delphi 2.x
Views
159
User Rating
No Votes
# Votes
0
Replies
0
Publisher:
DSP, Administrator
Reference URL:
DKB
			Author: Raghunath Dhungel

A Simple example of Artificial Intelligence using Delphi Array (Computer simulates 
learning process of human, learning by correcting mistakes !)

Answer:

Artificial Intelligence (AI) is an advance branch of science that studies the 
process of human thinking and attempts to apply the knowledge to simulate the same 
process in machines.  As computers are far ahead in the marathon of processing 
machines, AI is considered to be the branch of Computer Science than that of 
General Science. 
There have been many research and development in the field of Artificial 
Intelligence. The area of research include speech and pattern recognition, natural 
language processing, learning from previous experiences (learning by making and 
correcting mistakes!), reasoning under the situations providing limited or 
incomplete information etc. AI is practically applied in the field of computer 
games, expert systems, neural networks, robotics and many other fields of science 
and technology. 

In this article we will try to demonstrate a very simple practical example of 
artificial Intelligence programming in Delphi using Delphi arrays. I have chosen a 
Nepali game named "GATTA TIPNE KHEL" (meaning pebble picking game) for this 
purpose. We can see small children playing this game in the playground. 

(By the way, Nepal, my nation, is a small Asian country between India and China. 
Recently Nepal was in the main highlight of media for the notorious Royal Massacre 
in which the whole family of the ruling king were cruelly killed.) 

In this pebble picking game a pile of some pebbles is kept in the ground. One of 
the two players picks one, two or three pebbles at a time in his turn, leaving the 
pile for the other player to pick for his alternate turn. In this alternate picking 
process, the player who picks the last pebble(s) will be the loser and called to be 
a DOOM in Nepali.   
The main logic of the game is to leave the pile of pebbles with 13, 9, 5 or 1 
pebble(s) for the opponent to pick. 
In the program the starting number of pebbles are set to 17, 21, 25, 29 … etc. so 
that computer could win always if it does not make a mistake. But in the real play 
computer seems to be gradually learning by correcting mistakes of the previously 
played games. At last it finds all its mistakes and corrects them to become an 
unbeatable champion. It seems computer simulates the psychological learning process 
of animal, learning by correcting and not repeating the mistakes. 
A multidimensional array of elements (1..4,1..3) is chosen as the instruction book 
for the computer to pick the pebbles. The instruction book contains four pages with 
three lines of instructions to pick pebbles. The first line instructs to pick a 
single pebble, the second line instructs to pick 2 and the third line instructs to 
pick 3 pebbles. At the beginning, computer chooses a random page and a random line 
of instruction to pick the pebble. When the game finishes, if computer looses the 
game, the last instruction is red-marked (erased) and the instruction will not be 
read in the future. After playing many games, all the instructions leading to a 
lost game will be red marked and there will be left only the instructions those 
lead to a win.   

Well, it is enough for the description of the game. 
Let us jump directly to the code below: 

1   unit Unit1;
2   
3   interface
4   
5   uses
6     Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
7     StdCtrls, ExtCtrls, Buttons;
8   
9   type
10    TForm1 = class(TForm)
11      Panel1: TPanel;
12      LabelPeb1: TLabel;
13      LabelPeb2: TLabel;
14      Panel2: TPanel;
15      Panel3: TPanel;
16      Panel4: TPanel;
17      Label1: TLabel;
18      ComboBox1: TComboBox;
19      Label2: TLabel;
20      Label3: TLabel;
21      Label4: TLabel;
22      Label5: TLabel;
23      Label6: TLabel;
24      LabelIWon: TLabel;
25      LabelYouWon: TLabel;
26      LabelTotPlayed: TLabel;
27      ListBox1: TListBox;
28      GroupBox1: TGroupBox;
29      BitBtn1: TBitBtn;
30      BitBtn2: TBitBtn;
31      BitBtn3: TBitBtn;
32      BitBtn5: TBitBtn;
33      Panel5: TPanel;
34      Labelbtn: TLabel;
35      BitBtnResign: TBitBtn;
36      Label7: TLabel;
37      procedure FormCreate(Sender: TObject);
38      procedure ComboBox1Change(Sender: TObject);
39      procedure BitBtnResignClick(Sender: TObject);
40      procedure LabelbtnClick(Sender: TObject);
41      procedure BitBtn1Click(Sender: TObject);
42    private
43      { Private declarations }
44  
45      //Procedure to display remaining Pebbles
46      procedure DispPebbles(const nPebs: integer);
47      //Procedure to reset Variables and Labels after a game is finished
48      procedure Finish;
49      //Procedure to enable or Disable some controls;
50      procedure ManageEnabling(lValue: Boolean);
51      //Procedure to display winning or loosing messages
52      procedure Messaging(const lost: Boolean; const resigning: Boolean);
53      //Real Procedure for playing the game
54      procedure LearnByMistakes;
55    public
56      { Public declarations }
57    end;
58  
59  var
60    Form1: TForm1;
61  
62  implementation
63  
64  {$R *.DFM}
65  
66  var
67    PlayedGames, ComputerWon: Integer;
68    totPebs, RemainPebs, nTurn: Integer;
69    OldPageNum, OldLineNum: Integer;
70  
71    //18 possible permutations of 3 digits: 1,2 and 3
72    //to determine the number of pebbles to take for the computer
73    aPermutations: array[1..6, 1..3] of integer;
74    // A book containing the instruction pages(4) and lines(3 in each page) to draw 
75  the pebbles for the computer
76    aPages: array[1..4, 1..3] of integer;
77  
78  procedure TForm1.FormCreate(Sender: TObject);
79  // Permutations of 1,2 and 3  in the group of 3s to fill the permutation Array
80  const
81    cPermutations = '123132213231312321';
82  var
83    x, y: Integer;
84  begin
85    //Filling  Permutation Array
86    for x := 1 to 6 do
87      for y := 1 to 3 do
88        aPermutations[x, y] := strtoInt(copy(cPermutations, (x - 1) * 3 + y, 1));
89    //Filling the lines of all pages of the instruction Book (array 
90  aPages[1..4,1..3]).
91    for x := 1 to 4 do
92      for y := 1 to 3 do
93        aPages[x, y] := y;
94    ComboBox1.ItemIndex := 1;
95    PlayedGames := 0;
96    ComputerWon := 0;
97    totPebs := 17; {Default to 17 pebbles}
98    RemainPebs := 17; {All are intact up to Now}
99  end;
100 
101 procedure TForm1.DispPebbles(const nPebs: integer);
102 begin
103   LabelPeb1.Caption := intToStr(nPebs);
104   LabelPeb2.Caption := LabelPeb1.Caption;
105 end;
106 
107 procedure TForm1.ManageEnabling(lValue: Boolean);
108 begin
109   BitBtn1.Enabled := lValue;
110   BitBtn2.Enabled := lValue;
111   BitBtn3.Enabled := lValue;
112   Labelbtn.Enabled := lValue;
113   BitBtnResign.Enabled := lValue;
114   GroupBox1.Enabled := lValue;
115   if (RemainPebs < 3) and (RemainPebs > 0) and lValue then
116   begin
117     if RemainPebs < 3 then
118       BitBtn3.Enabled := False;
119     if RemainPebs < 2 then
120       BitBtn2.Enabled := False;
121   end
122 
123 end;
124 
125 procedure TForm1.Finish;
126 begin
127   LabelTotPlayed.caption := intToStr(PlayedGames);
128   LabelIWon.caption := intToStr(ComputerWon);
129   LabelYouWon.caption := intToStr(PlayedGames - ComputerWon);
130   DispPebbles(strToInt(ComboBox1.Items[ComboBox1.ItemIndex]));
131   totPebs := strtoint(LabelPeb1.Caption);
132   RemainPebs := totPebs;
133   ManageEnabling(True);
134   ComboBox1.Enabled := True;
135   Labelbtn.Enabled := False;
136   BitBtnResign.Enabled := False;
137 end;
138 
139 procedure TForm1.ComboBox1Change(Sender: TObject);
140 begin
141   DispPebbles(strToInt(ComboBox1.Items[ComboBox1.ItemIndex]));
142   totPebs := strtoint(LabelPeb1.Caption);
143   RemainPebs := totPebs;
144 end;
145 
146 procedure TForm1.Messaging(const lost: Boolean; const resigning: Boolean);
147 begin
148   inc(PlayedGames);
149   MESSAGEBEEP(0);
150   if lost then
151   begin
152     if resigning then
153       showmessage('I Resign ! You won the game again !!')
154     else
155       showmessage('Congratulations ! You won !! I acknowledges defeat !');
156   end
157   else
158   begin
159     inc(ComputerWon);
160     showmessage('Hi !  You lost ! I WON THIS GAME !!');
161   end;
162   Finish;
163 end;
164 
165 procedure TForm1.LearnByMistakes;
166 var
167   x, PageNum, LineNum, nTemp, nTakes: integer;
168 begin
169   if RemainPebs <= 0 then
170   begin
171     //Openent drew the last pebble(s) ! Computer won !!
172     //DispPebbles(0);
173     Messaging(False, False);
174     exit;
175   end;
176   nTemp := random(6) + 1;
177   PageNum := RemainPebs mod 4;
178   if PageNum = 0 then
179     PageNum := 4;
180   for x := 1 to 3 do
181   begin
182     LineNum := aPermutations[nTemp, x];
183     if (aPages[PageNum, LineNum] > 0) then
184       break;
185   end;
186   if x > 3 then {No unmarked instructions remained ! All are redmarked !!}
187   begin
188     // The effect of this move was unknown previously. But it proved to be fatal 
189 this time !
190     // RedMark This oldLineNum of this oldPageNum !
191     aPages[OldPageNum, OldLineNum] := -99;
192     // Lost with Resigning Message !
193     ListBox1.Items.add(intTostr(nTurn) + '. I resigned ');
194     DispPebbles(RemainPebs);
195     Messaging(True, True);
196     exit;
197   end;
198   nTakes := aPages[PageNum, LineNum];
199   if nTakes >= RemainPebs then
200   begin
201     ListBox1.Items.add(intTostr(nTurn) + '. I (Computer): ' + intTostr(RemainPebs));
202     //I am the last drawer and I lost !!
203     // RedMark This LineNum of this PageNum !
204     aPages[PageNum, LineNum] := -99;
205     DispPebbles(0);
206     Messaging(True, False);
207     exit;
208   end;
209   ListBox1.Items.add(intTostr(nTurn) + '. I (Computer): ' + intTostr(nTakes));
210   showmessage('I (Computer) take: ' + IntTostr(nTakes) +
211     ' Pebble(s) for this turn !');
212   OldPageNum := PageNum;
213   oldLineNum := LineNum;
214   RemainPebs := RemainPebs - nTakes;
215   ManageEnabling(True);
216   DispPebbles(RemainPebs);
217 end;
218 
219 procedure TForm1.BitBtnResignClick(Sender: TObject);
220 begin
221   inc(PlayedGames);
222   inc(ComputerWon);
223   inc(nTurn);
224   ListBox1.Items.add(intTostr(nTurn) + '. You resigned');
225   Finish;
226 end;
227 
228 procedure TForm1.LabelbtnClick(Sender: TObject);
229 begin
230   BitBtnResign.SetFocus;
231   BitBtnResign.Click;
232 end;
233 
234 procedure TForm1.BitBtn1Click(Sender: TObject);
235 var
236   nTaken: Integer;
237 begin
238   with Sender as TBitBtn do
239     nTaken := Tag;
240   ManageEnabling(False);
241   if (RemainPebs = totPebs) then
242   begin
243     ComboBox1.Enabled := False;
244     ListBox1.Clear;
245     nTurn := 1;
246   end
247   else
248     inc(nTurn);
249   ListBox1.Items.add(intTostr(nTurn) + '. You: ' + intTostr(nTaken));
250   RemainPebs := RemainPebs - nTaken;
251   DispPebbles(RemainPebs);
252   LearnByMistakes;
253 end;
254 
255 end.


A zipped file game.zip contains all stuffs (forms, units and project file) of the 
running project of this game. This project was compiled and run with Delphi - 3. 

At last for the interested friends: 
I have programmed a game of strategic thinking using the principles of Artificial 
Intelligence.  The game is somehow like Chess. 
The game was programmed using PowerBasic and Assembly Language. This game has won 
the First Prize in The First All Nepal Software Competition, SOFTWARE MEET 2000. 
The game can be downloaded at: 
http//:www.viewnepal.com/gamezip.exehttp//:www.viewnepal.com/gamezip.exe (a self 
extracting pkzip file). I will be glad to provide a copy through e-mail if the 
download does not work. 

For users, who are interessted in Artificical intelligence: You can find a Delphi 
component for ArtificialNeuronalNetworks under 
http://www.logiware.de/annhttp://www.logiware.de/ann

Component Download: http://www.delphi3000.com/redirect.asp?Link=../article/2551/game.zip

			
Vote: How useful do you find this Article/Tip?
Bad Excellent
1 2 3 4 5 6 7 8 9 10

 

Advertisement
Share this page
Advertisement
Download from Google

Copyright © Mendozi Enterprises LLC