### Connect 4 Program with AI :)

Hey this is connect 4 program with Minimax algorithm using NegaMax algorithm and I don't know actually if I implemented it correctly ... The problem is I don't know how to evaluate neutral moves please check it out and tell me how to implement it better
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252`` ``````#include #include #include #include using namespace std; bool provocation = false; // used to display a provocative screen char input[43]; // There are 42 places to play in the board .. this array represent them void Board(); int PlayOut = 0; int EVA = 0; // EVA and PlayOut are working while NegaMax Function is working .. in NegaMax's way it will definitely make any winning move // as it goes deep so when this happens PlayOut increases by 1 and EVA increases by 1 if its a winnning move or decreases by 1 if it the opposite // so we can evaluate neutral moves for MiniMax by dividing EVA / PlayOut * 100 so we get a percentage int winning(); int GetValue(int t); int AIManager(); int NegaMax(int Depth); void clean() { provocation = false; for(int i = 0 ; i<= 80 ; i++) input[i]=' '; } int GetValue(int column) // pass this function a column that you want to play in and it will return its value in input array .. { if(column > 7) return 0; int n; for(int i = 0 ; i<= 6 ; i++) { if( input[column+7*i] == ' ' ) { n = column+7*i; break; } } if ( n > 42 ) return 0; return n; } int winning() // Winning algorithm { int temp=0; for(int i = 1 ; i<= 42 ; i++) { if(input[i] != ' ') { temp++; if( i - int((i-1)/7) * 7 <= 4 ) if( input[i] == input [i+1] && input[i] == input[i+2] && input[i] == input[i+3] ) if(input[i] == 'X' ) return 1 ; else return 2; if( i <= 21 ) if ( input[i] == input[i+7] && input[i] == input[i+14] && input[i] == input[i+21] ) if(input[i] == 'X' ) return 1 ; else return 2; if( i - int((i-1)/7) * 7 <= 4 && i<=18 ) if(input[i] == input[i+8] && input[i] == input[i+16] && input[i]==input[i+24]) if(input[i] == 'X' ) return 1 ; else return 2; if( i - int((i-1)/7) * 7 >= 4 && i <= 21 ) if(input[i] == input[i+6] && input[i] == input[i+12] && input[i]==input[i+18]) if(input[i] == 'X' ) return 1 ; else return 2; } } if (temp == 42) return 3; return 0; } void Board() // Draw board { cout<>sth; sth=GetValue(sth); if( sth != 0 ) { input[sth] = XO; return ; } else cout<<"ERROR"<= 100) provocation = true; if( chance[0] > temp ) { chance[0] = temp ; chance[1] = PlayNumber; } // cout<
Last edited on
first of all, good job!.

I made it run and it beat me. I never claimed that I was good at connect 4.
But you have successfully created a program that is smarter then me :)

I did have a little problem running it though. INFINITE wasnt defined and you didnt include <string>, and in function getValue() n wasn't initialized which resulted in a run time error. did you run it on Linux? or maybe an IDE that includes those automatically for you, and auto initializes variables?

this line is very dangerous`float temp = -(100*NegaMax(1)+( (100*EVA)/PlayOut));`

you see when this line of code is called PlayOut is 0; which means that you devide by 0. That would be the case but NegaMax() function is evaluated first and changes PlayOut to not be zero. However how can you be so sure that NegaMax() will be called before the devision? It might just be that a different compiler or a new version of the same, or given some optimization flags, the devision will be evaluated before the NegaMax() function. Basically this should be split into 2 lines of code, with perhaps checking for devision by 0

Anyways, an improvement could be a more file oriented design, using object oriented programming. If you would like I am going to be working on a tutorial for a chess program sometime in the future. It will be posted on my site www.proswaggrammer.com its under construction.

If you want to continue to develop AI algorithms. Then you should pick up a book for AI programming/data mining/statistics. Its a very hot and promising programming field, used heavily in advertising campaigns, and statistical predictions, and image processing.

If you want to continue to develop more complex video games on C++, then you should start learning new programming technologies such as SDL/SFML/OpenGL look for tutorials online, there are a lot of very complicated algorithms, (MinMax, shortest path), that video games use. And lots of design patterns that create a good game environment. A simple game can easily exceed 10,000 lines of code. And as you now know, it will be impossible to create without a good design of code. However this field is the MOST difficult path to succeed in, you will become a very good programmer though.

If you want to however just have some fun on modern devices(IPhones,Galaxies). Just try some Java or C# for those stuff. I personally am not attracted to that way.

good luck and keep at it!
Hi :)
 I did have a little problem running it though. INFINITE wasnt defined and you didnt include , and in function getValue() n wasn't initialized which resulted in a run time error. did you run it on Linux? or maybe an IDE that includes those automatically for you, and auto initializes variables?

INFINITE is defined in math.h library but any ways you're right :D I'll define it or I will just put a huge number ..
and about GetValue function its already initialized or I maybe didn't get what you mean :D sry still beginner
 ``1234`` ``````int winning(); int GetValue(int t); int AIManager(); int NegaMax(int Depth);``````

 this line is very dangerous `float temp = -(100*NegaMax(1)+( (100*EVA)/PlayOut)); ` you see when this line of code is called PlayOut is 0; which means that you devide by 0

you are absolutely right :D I even had some bugs I never knew the cause .... NegaMax is evaluated first cuz temp = NegaMax().... so its evaluated first and NegaMax changes the Values of PlayOut and EVA but in some cases the value of PlayOut become 0 at the end of the board ... I should check first whether PlayOut is zero or not

unfortunately I know nothing about object oriented programming I'm still beginner .. but I will learn :D

 If you want to continue to develop more complex video games on C++

yeah I want to .. I will learn first more about C++ cuz I'm not fully acquainted with it then I'll start with OpenGl
Last edited on
C++ is a great language, and object oriented programming is very important. However for beginners it might be very intimidating learning object oriented programming rules. Truth is that you only really need a small portion of these rules :). If you understand how to make classes/constructors/destructors/inheriting/and polymorphism. Then you will be able to make most designs out there.

however only knowing classes and their constructors will be plenty sufficient for programming games.

OpenGL is a VERY advanced library, famously known for being very hard to begin in, you need many math skills. good luck with that :)
Thanks ^_^
Topic archived. No new replies allowed.