Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Oct 12th, 2005, 9:58 PM   #1
Jessehk
The Oblivious One
 
Jessehk's Avatar
 
Join Date: May 2005
Location: Ontario, Canada
Posts: 648
Rep Power: 4 Jessehk is on a distinguished road
is making an AI always this repetetive?

I always hate posting what looks like repetetive code, becuase I am worried I will get flamed...

Anyway...

In regard's to DaWei's programming "contest" where we are to make an AI for a tic-tac-toe game, I am finding the coding to be very frusterating and repetetive, example:

--first look for offensive moves,
--if there are 2 of "you" on any row, column, or diagonal line, then add yourself to the whitespot
--make sure you are not already on that spot
--then look if there are any of "the other player" on either a row, column, or diagonal.
--if there are 2, then go in the empty space, if you are not already there
--if there are neither an offensive or defencive moves, make a a randome move
--place "you" on the random spot, if it is free. Otherwise, get another spot


This pseudo-code has resulted in about 200 lines of repetetive for loops and function calls as well as if statements.

I would post it, but it is un-commented
Anyway, the point of the thread:

Is this all normal? :p
__________________
Dr. Zoidberg: [ecstatic] I'm going to a movie... with FRIENDS!
Jessehk is offline   Reply With Quote
Old Oct 13th, 2005, 4:28 AM   #2
iignotus
Professional Programmer
 
iignotus's Avatar
 
Join Date: Apr 2005
Location: Nowhere Special
Posts: 466
Rep Power: 4 iignotus is on a distinguished road
Send a message via AIM to iignotus
It sounds like you can do better.... 200 is quite a large number.
__________________
% rc4 hexkey < input > output
#define S ,t=s[i],s[i]=s[j],s[j]=t /* rc4 hexkey <file */
unsigned char k[256],s[256],i,j,t;main(c,v,e)char**v;{++v;while(++i)s[ 
i]=i;for(c=0;*(*v)++;k[c++]=e)sscanf((*v)++-1,"%2x",&e);while(j+=s[i]
+k[i%c]S,++i);for(j=0;c=~getchar();putchar(~c^s[t+=s[i]]))j+=s[++i]S;}
iignotus is offline   Reply With Quote
Old Oct 13th, 2005, 5:49 AM   #3
ivan
Professional Programmer
 
ivan's Avatar
 
Join Date: Sep 2005
Location: serbia & montenegro
Posts: 484
Rep Power: 4 ivan is on a distinguished road
Can you provide a link to your source somewhere in the net?
ivan is offline   Reply With Quote
Old Oct 13th, 2005, 8:12 AM   #4
ZenMasterJG
Hobbyist Programmer
 
ZenMasterJG's Avatar
 
Join Date: Nov 2004
Location: Boston, MA
Posts: 148
Rep Power: 5 ZenMasterJG is on a distinguished road
Send a message via AIM to ZenMasterJG
What language are you writing it in? One of the main reasons i prefer Python over Java now... Java's just to freakin' verbose.

You probably are repeating things you don't need to though. I'd use a minimax method for tic-tac-toe.
http://ai-depot.com/LogicGames/MiniMax.html
http://www.fierz.ch/strategy1.htm
(both look decent, didnt take the time to read 'em too carefully. google turns up tons of stuff.)
With tic-tac-toe i doubt you'd need to do any of that Alpha Beta tree pruning, although it might speed things up.

Seeing your code would be nifty, too.
Good luck!
ZenMasterJG is offline   Reply With Quote
Old Oct 13th, 2005, 8:30 AM   #5
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Obviously, it's by nature a repetetive task. The human brain has an innate pattern-recognition and evaluation ability the the microprocessor lacks entirely. This is what makes it a challenge to take something in the problem space of the human mind and work with it in the solution space of the microprocessor. Either you get down on the microprocessor's level, or you invest a lot in a layers of abstraction of various utility.

At the processor's level, the task will be highly repetetive. You must analyze the potential value of each square in terms of the conditions of all the other squares. This is where you attempt modularity of one degree or another, or just bite the bullet and crank it through. In a commercial environment, real-life constraints would affect the choices made.

If your code is too messy to show, then shame on you. You lack the discipline necessary to be a successful professional programmer. A huge number of programs pass into the hands of others for correction, validation, and maintenance. If, on the other hand, you just feel your program is too "amateurish", you shouldn't let that worry you if your goal is to learn from the members of the forum. They can't read your mind very well, they need to see the code. Since it's a contest, however, I'd defer submitting the code for review until you are prepared to put a "DONE" label on it. Not that it will be DONE in the final sense, but it should be DONE in the contest sense, unless you wish to flag it a team entry.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Oct 13th, 2005, 10:55 AM   #6
Jessehk
The Oblivious One
 
Jessehk's Avatar
 
Join Date: May 2005
Location: Ontario, Canada
Posts: 648
Rep Power: 4 Jessehk is on a distinguished road
I will fix the last bug I am encountering, comment the whole thing tediously, and the post it ( or atleast the header file, where all the work is done ).

For me, it is a learning expirience, not a "who is the best" type thing.
I don't care if people see my effort (or my progress )
__________________
Dr. Zoidberg: [ecstatic] I'm going to a movie... with FRIENDS!
Jessehk is offline   Reply With Quote
Old Oct 13th, 2005, 11:16 AM   #7
coldDeath
Expert Programmer
 
coldDeath's Avatar
 
Join Date: Aug 2005
Location: UK
Posts: 862
Rep Power: 4 coldDeath is on a distinguished road
Send a message via AIM to coldDeath Send a message via Yahoo to coldDeath
http://edais.mvps.org/Tutorials/TTTAI/

That link shows a good way of doing your Tic-Tac-Toe AI.
__________________
Join us at #programmingforums @ irc.freenode.net!

My software never has bugs. It just develops random features.
coldDeath is offline   Reply With Quote
Old Oct 13th, 2005, 11:18 AM   #8
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
I hope you aren't using C/C++. If you are, and the work is "done" in the header file, I would suspect you are misusing the header-file concept. That's not a certainty, but it's a high probability. On the other hand, "include" files in scripting languages like PHP are often code, but not really "headers."
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Oct 13th, 2005, 11:32 AM   #9
Jessehk
The Oblivious One
 
Jessehk's Avatar
 
Join Date: May 2005
Location: Ontario, Canada
Posts: 648
Rep Power: 4 Jessehk is on a distinguished road
Quote:
Originally Posted by DaWei
I hope you aren't using C/C++. If you are, and the work is "done" in the header file, I would suspect you are misusing the header-file concept. That's not a certainty, but it's a high probability. On the other hand, "include" files in scripting languages like PHP are often code, but not really "headers."
I was not aware of that, and I will move all the method definitions to a seperate file
__________________
Dr. Zoidberg: [ecstatic] I'm going to a movie... with FRIENDS!
Jessehk is offline   Reply With Quote
Old Oct 13th, 2005, 12:16 PM   #10
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5 Arevos is on a distinguished road
My AI logic looks something like:
def ai(self):
   for index in board.free_spaces:
      b = board.copy()
      b[index] = self.opponent.symbol
      if b.is_won:
         board[index] = self.symbol
         return
   random.choice(list(board.free_spaces))
Ain't Python concise?
Arevos is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 10:44 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC