Hi guys,
First of all I am new here and i'd like to say hello to all!
Now is the hard part for me...
I have a assignement for next week that i just can't really do it. I have to use minimax to make TTT game.Because i am not studying programming is a bit hard for me. I like programming but this seems to hard for me.I cannot understand how to write the algoritm in c++.So i have the mini max algoritm and TTT game.I have searched all over the Internet and i found some links to TTT games and source code but it does not help me.It pased a while since i had no programming course and know i must get it done. I know to program in c++ but only basics.If it was TTT for 2 players it was ok: no problem to get it done.But i have to play against computer and...big problem. So guyz can u help me understand the algorithm? As i said i can find it on the internet but it has no value for me...
How should i build a game tree?
What i return at a leaf?
How do i search entire tree?
On paper i can do this assignement and i know to play around with but when i should implement it ...
I am a bit dissapointed because i don't really get it ... So if you are willing to take some of your time to explain me in C++ it will be great.I am using VC++2008.
Thank you!
18 replies to this topic
#1
Posted 11 October 2011 - 09:56 AM
|
|
|
#2
Posted 11 October 2011 - 01:16 PM
Most TTT programs you will find do NOT implement mini-max, they will used static logic that corresponds to what the mini-max algorithm would do. What you have to do, each time it is the computer's turn, is consider each possible move, playing out all possible game results. When doing a virtual "self" move, it should always choose the move that gives it the best guaranteed result, and when doing a virtual "other" move, it should always choose the move that gives the worst guaranteed result. Usually, what you will do is use recursion with the recursive function receiving a game "state", proposed move, and player "turn", and returning a result that indicates "win", "lose", or "tie".
Without having some of your existing code (say, just to get it playable by two players), there's not a whole lot more to say without risk of giving you the solution.
Without having some of your existing code (say, just to get it playable by two players), there's not a whole lot more to say without risk of giving you the solution.
#3
Posted 12 October 2011 - 08:29 AM
huh is quite hard for me...
so the algorithm is like this:
int mm( int depth, int a, int b) // a, coresponding to min and max ?
{ if (depth==1)
return value of that leaf
else if node is a Max
return max value of childnodes
else if node is a Min
return mim value of childnodes
}
I get it on the paper but i don't get in C. So how do i climb up or down in a tree?
how could i build up that tree ?
WingedPanther how can i implement what you said ?
so the algorithm is like this:
int mm( int depth, int a, int b) // a, coresponding to min and max ?
{ if (depth==1)
return value of that leaf
else if node is a Max
return max value of childnodes
else if node is a Min
return mim value of childnodes
}
I get it on the paper but i don't get in C. So how do i climb up or down in a tree?
how could i build up that tree ?
WingedPanther how can i implement what you said ?
#4
Posted 12 October 2011 - 01:19 PM
Okay, your tree is a based on the following:
Given current state, child nodes represent possible "next" moves. So a blank board has 9 children, a board with an X has 8 children, a board with an X and an O has 7 children, etc.
The value of a leaf can be 0 = lose, 1 = tie, 2 = win.
I would use recursion to climb up and down a "virtual" tree so that only one path exists at any given time, along with the current "best" move found. Storing the entire tree in memory, while viable for TTT, is not a good idea in general.
As I said before, you really need to post your existing TTT code before I can offer much additional direction.
Given current state, child nodes represent possible "next" moves. So a blank board has 9 children, a board with an X has 8 children, a board with an X and an O has 7 children, etc.
The value of a leaf can be 0 = lose, 1 = tie, 2 = win.
I would use recursion to climb up and down a "virtual" tree so that only one path exists at any given time, along with the current "best" move found. Storing the entire tree in memory, while viable for TTT, is not a good idea in general.
As I said before, you really need to post your existing TTT code before I can offer much additional direction.
#5
Posted 14 October 2011 - 07:41 AM
WingedPanther said:
As I said before, you really need to post your existing TTT code before I can offer much additional direction.
should i post 2 player TTT ?
#6
Posted 14 October 2011 - 09:54 AM
If that's what you have.
#7
Posted 14 October 2011 - 12:15 PM
#include<iostream>
using namespace std;
char a[]={' ',' ',' ',' ',' ',' ',' ',' ',' '};
int size=9;
void matrice()
{cout<<"+-----+"<<endl;
cout<<"|"<<a[1]<<"|"<<a[2]<<"|"<<a[3]<<"|"<<endl;
cout<<"+-+-+-+"<<endl;
cout<<"|"<<a[4]<<"|"<<a[5]<<"|"<<a[6]<<"|"<<endl;
cout<<"+-+-+-+"<<endl;
cout<<"|"<<a[7]<<"|"<<a[8]<<"|"<<a[9]<<"|"<<endl;
cout<<"+-----+"<<endl;
}
int castiga()
{
if ((a[1]=='X' && a[2]=='X' && a[3]=='X') || (a[1]=='O' && a[2]=='O' && a[3]=='O'))
return 1;
if ((a[1]=='X' && a[4]=='X' && a[7]=='X') || (a[1]=='O' && a[4]=='O' && a[7]=='O'))
return 1;
if ((a[1]=='X' && a[5]=='X' && a[9]=='X') || (a[1]=='O' && a[5]=='O' && a[9]=='O'))
return 1;
if ((a[2]=='X' && a[5]=='X' && a[8]=='X') || (a[2]=='O' && a[5]=='O' && a[8]=='O'))
return 1;
if ((a[3]=='X' && a[5]=='X' && a[7]=='X') || (a[3]=='O' && a[5]=='O' && a[7]=='O'))
return 1;
if ((a[3]=='X' && a[6]=='X' && a[9]=='X') || (a[3]=='O' && a[6]=='O' && a[9]=='O'))
return 1;
if ((a[4]=='X' && a[5]=='X' && a[6]=='X') || (a[4]=='O' && a[5]=='O' && a[6]=='O'))
return 1;
return 0;
}
void player1()
{ int x,i;
reread:
cout<<"Enter pozition player1:";cin>>x;
if ((a[x]=='X') || (a[x]=='O'))
{cout<<"Used!";cout<<endl; goto reread;}
else {a[x]='X'; i=castiga();}
if (i==1)
{cout<<"Player 1 wins!"<<endl;size=0;}
}
void player2()
{int x,i;
reread:
cout<<"Enter pozition player2:";cin>>x;
if ((a[x]=='X') || (a[x]=='O'))
{cout<<"Used!";cout<<endl; goto reread;}
else {a[x]='O';i=castiga();}
if (i==1)
{cout<<"Player 2 wins!"<<endl;size=0;}
}
void main ()
{
cout<<" Tic Tac Toe"<<endl;
matrice();
while ( size>0)
{ player1();matrice();
size--;
player2();matrice();
size--;
}
system("pause");
}
This is the code for 2 player game.I've made it in 30 mins i think... I wasn't too hard. Now how do i implement AI player?
Edited by WingedPanther, 14 October 2011 - 04:05 PM.
add code tags (the # button)
#8
Posted 14 October 2011 - 04:07 PM
Start by changing player2() to make a call to a recursive function that uses your castiga() to check for a win.
#9
Posted 15 October 2011 - 02:05 AM
Something like this ?
else {a[x]='O';
if(castiga())
{cout<<"Player 2 wins!"<<endl;size=0;}
}
#10
Posted 15 October 2011 - 02:34 PM
Except you run through options in a loop, checking to see which are valid/optimal.
#11
Posted 15 October 2011 - 11:41 PM
i am thinking about this but i have no idea...
#12
Posted 16 October 2011 - 12:33 PM
Please, write clearly with proper structure. Double spacing makes the text feel un-jointed, Capitalizing Every Word Means People Stop Before Every Word Sub-Consciously Which Is A Pain In The Backside, and use code tags! (The right most styling box).
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account


Back to top









