Thread: Helpmeplz

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    73

    Helpmeplz

    Hey guys there is some code that i need to finish my program, well at least the easy way, but I dont understand it. Im writting a tic tac toe program where the computer must never be beaten. This code is supposed to find how many x's there are in a row. But I dont know how to impliment it. Can any one help. Here it is.

    Code:
    row(i) = (i-1)/3+1

  2. #2
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    I'd have to see a little more code. For example, you could be using bits to hold the information or you could be using an array (or a 2d array). Perfect AI on a game like tic-tac-toe is impossible.

  3. #3
    Registered User
    Join Date
    Oct 2002
    Posts
    73
    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    bool endgame=false;
    
    int rungame();
    char board[9]={0,1,2,3,4,5,6,7,8};
    
    void printboard();
    
    	int main()
    	{
    		char x;
    
    		while (!endgame)
    		{
    
    			cout << "Welcome. Would you like to play a game? " << endl;
    			cout << "Press Y for yes and N for no. Followed by enter" << endl;
    			cin >> x;
    
    			if (x=='y')
    				rungame();
    			else
    			if (x=='n')
    				endgame=true;
    		}
    
    		return 0;
    	}
    
    	int rungame()
    	{
    		int move, x;
    
    		board[0]=' ';
    		board[1]=' ';
    		board[2]=' ';
    		board[3]=' ';
    		board[4]=' ';
    		board[5]=' ';
    		board[6]=' ';
    		board[7]=' ';
    		board[8]=' ';
    
    		for(x=1; x<=9; x++)
    		{
    			printboard();
    
    			if ((x==1)||(x==3)||(x==5)||(x==7) || (x==9))
    			{
    				cout << "Please make a move between 1 and 9. Followed by enter." << endl;
    
    				int move;
    				cin >> move;
    
    				if ((move==1)&&(board[0]!='x')&&(board[0]!='o'))
    				{
    					board[0]='x';
    					cout << board[0] << " takes square 1" << endl;
    				
    				}else
    				if ((move==2)&&(board[1]!='x')&&(board[1]!='o'))
    				{
    					board[1]='x';
    					cout << board[1] << " takes square 2" << endl;
    				
    				}else
    				if ((move==3)&&(board[2]!='x')&&(board[2]!='o'))
    				{
    					board[2]='x';
    					cout << board[2] << " takes square 3" << endl;
    			
    				}else
    				if ((move==4)&&(board[3]!='x')&&(board[3]!='o'))
    				{
    					board[3]='x';
    					cout << board[3] << " takes square 4" << endl;
    	
    				}else
    				if ((move==5)&&(board[4]!='x')&&(board[4]!='o'))
    				{
    					board[4]='x';
    					cout << board[4] << " takes square 5" << endl;
    
    				}else
    				if ((move==6)&&(board[5]!='x')&&(board[5]!='o'))
    				{
    					board[5]='x';
    					cout << board[5] << " takes square 6" << endl;
    				
    				}else
    				if ((move==7)&&(board[6]!='x')&&(board[6]!='o'))
    				{
    					board[6]='x';
    					cout << board[6] << " takes square 7" << endl;
    		
    				}else
    				if ((move==8)&&(board[7]!='x')&&(board[7]!='o'))
    				{
    					board[7]='x';
    					cout << board[7] << " takes square 8" << endl;
    
    				}else
    				if ((move==9)&&(board[8]!='x')&&(board[8]!='o'))
    				{
    					board[8]='x';
    					cout << board[8] << " takes square 9" << endl;
    			
    				}
    
    				if (move>10)
    					cout << "Illegal move!" << endl;
    				}else
    					if ((x==2) || (x==4) || (x==6) || (x==8))
    					{
    						//Computers first move
    						if (x==2)
    						{
    							if ((board[4]!='x')&&(board[4]!='o'))
    							{
    								board[4]='o';
    							}else
    								if ((board[0]!='x')&&(board[0]!='o'))
    								{
    									board[0]='o';
    								}
    						}
    
    						//Block x
    					}
    			}
    			cout << "CAT" << endl;
    			return 0;
    	}
    
    	void printboard()
    	{
    
    		cout << board[0] << "|" << board[1] << "|" << board[2] << endl;
    		cout << board[3] << "|" << board[4] << "|" << board[5] << endl;
    		cout << board[6] << "|" << board[7] << "|" << board[8] << endl;
    
    	}
    There is my source. Im not trying to make a perfect tic tac toe AI, it juz can't loose.

  4. #4
    Registered User
    Join Date
    Oct 2002
    Posts
    73
    Basically im trying to write a algorithm that will check all the differant squares for an 'x'. If there are 2 x's in a row, colum, or dialgnal then move to the square to block it. Thats all i need. After that I know how to program the win. Any ideas?

  5. #5
    Just a Member ammar's Avatar
    Join Date
    Jun 2002
    Posts
    953
    You can store the moves in a double subscripted array and check for certain patterns, and that's it, the only you have to do is to decide what patterns to look for.

  6. #6
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    Originally posted by master5001
    <snip>Perfect AI on a game like tic-tac-toe is impossible.
    Wow...that's saying quite a bit. I guess it'd depend on what you call 'perfect AI', but I'd sure hesitate a lot before I said it was impossible.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  7. #7
    Registered User JasonLikesJava's Avatar
    Join Date
    Mar 2002
    Posts
    175
    I'm curious too as to what you mean by "Perfect AI"....
    OS: Linux Mandrake 9.0
    Compiler: gcc-3.2
    Languages: C, C++, Java

    If you go flying back through time and you see somebody else flying forward into the future, it's probably best to avoid eye contact.

  8. #8
    Registered User
    Join Date
    Jan 2002
    Posts
    559
    The only way I know to do it is to just check each possible combination. There are eight ways to win. I suppose you could make an algorithm by checking each perimeter square and checking it's two immediate neighbors, one horizontal, one vertical, and only check the third if a match is made in the first check, and add a similar diagonal check at the corner, but that would be more work. Might be useful on a larger grid board, 10 x 10 or so.
    Truth is a malleable commodity - Dick Cheney

  9. #9
    Registered User
    Join Date
    Aug 2001
    Posts
    244
    I can see it being impossible to create an AI that always WINS - but I think it would be entirely possible to create one that always ties with the human player.

    Tic-tac-toe is the sort of game where if both player pays attention and isn't a moron, the game will ALWAYS end up a tie.

    Thats why its sort of a dumb game to play once you're older than 10... but if you're bored, hey who cares, right?

  10. #10
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    I agree...this was an assignment I had for one of my classes. Programming it so that the computer never loses isn't too hard; I doubt you could program it so that the computer always wins. Maybe that's the "perfect AI"?
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  11. #11
    Registered User Azuth's Avatar
    Join Date
    Feb 2002
    Posts
    236
    That would have made an interesting end to 'War Games'.
    "Sir, the computer just launched a full first strike package at the Russians!"
    "My god, but why?"
    "Well, the kid was playing Tic-Tac-Toe, trying to prove that with two smart opponents, in some games, there can be no winnerr; the computer decided in order for there to be a winner, one player has to not finish."
    Demonographic rhinology is not the only possible outcome, but why take the chance

  12. #12
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    Originally posted by master5001
    Perfect AI on a game like tic-tac-toe is impossible.
    hahaha.

    try working it out a little: its not to difficult to create a tic tac toe AI that will never lose.
    hello, internet!

Popular pages Recent additions subscribe to a feed