Thread: Need help with Tic Tac Toe

  1. #1
    Registered User
    Join Date
    Jul 2002
    Posts
    1

    Need help with Tic Tac Toe

    I'm taking an introductory C++ class. I'm having trouble with a current assignment. I have to write a simple tic tac toe program. It doesn't have an AI. It just goes back and forth between two users. The program has to use Arrays, and Functions. We haven't gotten to fancy displays or anything. So the whole program in displayed in text. I can't figure out an efficient structure for my program. If anyone could help it would be much appreciated. Thank you.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > I can't figure out an efficient structure for my program
    What do you mean by this?

    int board[3][3];

    Seems to be a start

  3. #3
    Unregistered
    Guest

    but

    how does the actual algorithm work ?

  4. #4
    Registered User
    Join Date
    May 2002
    Posts
    317
    Think of how the game is played. First you have your 3x3 board, that would be the int board[3][3]. Second you allow the player to select an empty location, so you would obtain the requested spot and check if that was free. If it isn't request another choice; if it is check to see if the player won. If he did, print Player 1(2) wins, if he didn't get input from the other player.

    Big picture, visualize how you play TicTacToe and then code that.

  5. #5
    Green Member Cshot's Avatar
    Join Date
    Jun 2002
    Posts
    892
    There would also be 3 possible values for each of the spaces. An 'X', 'O', or empty space. You would store them into the arrays as each player inputs his move.

  6. #6
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Also you may want to think about enumerating some types to help you with your 2D array. For example if you had 0 as free, 1 as X and 2 as O it would not be that intuitive to someone else what those values meant. Instead you could do something like

    enum STATES { PLAYER_O, PLAYER_X, FREE };

    Then you could check those symbolic constants instead of remembering the actual values. Just an idea though.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  7. #7
    Akilla
    Guest

    hmmm

    I don't know about enums yet..

    but.. I was thinking.... how do we tell the
    computer to select the place to put an 'X' or
    an 'O' in order to win the game ???

    i was thinking..

    let's say...

    1) 0 ... for empty space
    2) 1 ... for 'O'
    3) 2 ... for 'X'

    let's say the computer is playing 'X's for example..

    so it will first check horizontally vertically and diagonally
    to see if there are any empty spaces (or zeros)

    and then what ?

    How do you write the strategy ?
    is it as simple as summing up the arrays (horizontally, vertically or diagonally) whereever the empty spaces or 'X's are and checking to see which sum brings the highest number.... and telling it to place the X in that location ?????


    COOL PROGRAMS @ www.akilla.tk

  8. #8
    Green Member Cshot's Avatar
    Join Date
    Jun 2002
    Posts
    892
    That would require AI. I believe the original post only wanted a 2-player game. I suggest you search for tic-tac-toe in the Game Programming forum. There's bound to be a topic on AI.

  9. #9
    Akilla
    Guest

    Unhappy ifs

    I see that people are using 100s of if conditions and calling it AI

    Is that ok ?

    I wan't to do it in a simple and effective way. Like- relating it to mathematics. Is there a way to somehow "add up" the values and search for the best position to place the 'X' or the 'O' ?

    Any advise is appreciated.

    CHECK OUT - MATH BRAIN @ www.akilla.tk

  10. #10
    Green Member Cshot's Avatar
    Join Date
    Jun 2002
    Posts
    892
    You can use search trees to search for possible combinations and return the best position. Depth first search and breadth first search are popular in AI. Since there's not too many possibilities in TTT, a ton of if conditions will suffice. However with more complicated games, it would take forever to search all possibilities so cutting the search to a certain number of levels will do.

    For instance, you would analyze the positions in TTT and rank these situations and assign them points:

    1) 3 in a row win for you
    2) blocking an opponent's 2 in a row
    3) creating simultaneous 2 in a row situations for certain win
    4) preventing opponent from any chance of winning
    etc...

    For opening moves you can do something like adding a certain number of points for moves that give you the most possible combinations for a victory. Then you can subtract it with the opponent's strenghth in position which would give you the total score to choose your move.

    Games such as chess commonly use an opening book for preset moves. This way the AI won't search through unnecessary paths which are inferior.

  11. #11
    Akilla
    Guest

    Question codes

    are there any sample code that I can refer to that use this tree search ?

  12. #12
    Registered User
    Join Date
    Jun 2002
    Posts
    79
    Try this. Do not worry about the warnings, most of them can be removed by using type casting.
    Compiler:Borland C++ 5.0

  13. #13
    Seeking motivation... endo's Avatar
    Join Date
    May 2002
    Posts
    537
    Y'know its just my opinion, but if your taking an introductory C++ class I wouldn't get too involved with complicated data structures and algorithms just yet. When I did a 2 player tictactoe game the checkForWin() function just contained a few if statements with all the winning possibilities covered. Thats plenty if your not too experienced with C++.

  14. #14
    Akilla
    Guest

    hmm

    >Try this. Do not worry about the warnings, most of them can be >removed by using type casting.
    >Compiler:Borland C++ 5.0

    I've already seen that file.. there are too many if conditions in it. Maybe all the possibilities of that game.
    I don't want to think about all the possibilities. I am trying to find a relation mathematically (if it exists) that will teach the computer a strategy to win the game.

    >Y'know its just my opinion, but if your taking an introductory >C++ class I wouldn't get too involved with complicated data >structures and algorithms just yet. When I did a 2 player >tictactoe game the checkForWin() function just contained a few >if statements with all the winning possibilities covered. Thats >plenty if your not too experienced with C++.

    I am not taking an intro class. Programming is my hobby,
    although I didn't write very complicated programs yet. Just
    learning and I am new to C/C++
    I don't mind working hard on writing a Tic Tac Toe program.
    However, I don't want to use too many if conditions
    (if that's possible.)

    Thanks for the replies.

    MY HOMEPAGE @ www.akilla.tk

  15. #15
    Seeking motivation... endo's Avatar
    Join Date
    May 2002
    Posts
    537
    Whoever started the thread mentioned an introductory class thats why I said it. Anyway, I think 2 player TTT is simple enough not to require complex AI. Here is the function I was talking about, in case it helps at all:

    Code:
    int CLineWindow :: checkForWin( )
    {
    	if ( (m_nGridState [ 0 ] == 1 && m_nGridState [ 1 ] == 1 && m_nGridState [ 2 ] == 1) ||
    		(m_nGridState [ 3 ] == 1 && m_nGridState [ 4 ] == 1 && m_nGridState [ 5 ] == 1) ||
    		(m_nGridState [ 6 ] == 1 && m_nGridState [ 7 ] == 1 && m_nGridState [ 8 ] == 1) ||
    		(m_nGridState [ 0 ] == 1 && m_nGridState [ 3 ] == 1 && m_nGridState [ 6 ] == 1) ||
    		(m_nGridState [ 1 ] == 1 && m_nGridState [ 4 ] == 1 && m_nGridState [ 7 ] == 1) ||
    		(m_nGridState [ 2 ] == 1 && m_nGridState [ 5 ] == 1 && m_nGridState [ 8 ] == 1) ||
    		(m_nGridState [ 0 ] == 1 && m_nGridState [ 4 ] == 1 && m_nGridState [ 8 ] == 1) ||
    		(m_nGridState [ 2 ] == 1 && m_nGridState [ 4 ] == 1 && m_nGridState [ 6 ] == 1) )
    	{
    		return 1;		// X wins!
    	}
    
    	if ( (m_nGridState [ 0 ] == 2 && m_nGridState [ 1 ] == 2 && m_nGridState [ 2 ] == 2) ||
    		(m_nGridState [ 3 ] == 2 && m_nGridState [ 4 ] == 2 && m_nGridState [ 5 ] == 2) ||
    		(m_nGridState [ 6 ] == 2 && m_nGridState [ 7 ] == 2 && m_nGridState [ 8 ] == 2) ||
    		(m_nGridState [ 0 ] == 2 && m_nGridState [ 3 ] == 2 && m_nGridState [ 6 ] == 2) ||
    		(m_nGridState [ 1 ] == 2 && m_nGridState [ 4 ] == 2 && m_nGridState [ 7 ] == 2) ||
    		(m_nGridState [ 2 ] == 2 && m_nGridState [ 5 ] == 2 && m_nGridState [ 8 ] == 2) ||
    		(m_nGridState [ 0 ] == 2 && m_nGridState [ 4 ] == 2 && m_nGridState [ 8 ] == 2) ||
    		(m_nGridState [ 2 ] == 2 && m_nGridState [ 4 ] == 2 && m_nGridState [ 6 ] == 2) )
    	{
    		return 2;		// O wins!
    	}
    
    	for ( int i = 0; i < 9; i++ )
    	{
    		if ( m_nGridState [ i ] == 0 )
    		{
    			return 0;	// game not finished
    		}
    	}
    	return -1;			// game drawn
    }
    I'm sure there are more concise ways of accomplishing the same thing, cos thats some damn ugly code!!
    Last edited by endo; 07-19-2002 at 08:32 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help me with my simple Tic tac toe prog
    By maybnxtseasn in forum C Programming
    Replies: 2
    Last Post: 04-04-2009, 06:25 PM
  2. tic tac toe
    By holden in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 05-09-2004, 09:59 AM
  3. Help with Tic Tac Toe game
    By snef73 in forum C++ Programming
    Replies: 1
    Last Post: 04-25-2003, 08:33 AM
  4. tic tac toe game
    By Leeman_s in forum Game Programming
    Replies: 9
    Last Post: 04-24-2002, 03:24 AM
  5. my tic tac toe game, please try it
    By Leeman_s in forum C++ Programming
    Replies: 2
    Last Post: 04-14-2002, 05:16 PM