Thread: Printing out all possible tic tac toe board combinations to a text file

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    42

    Printing out all possible tic tac toe board combinations to a text file

    It's for an AI experiment, I decided that since tic tac toe is essentially solved, I can just store all possible boards inside of the program. However, it would be easiest to store all board combinations inside of a text file, so would the best way to do it would be to create a C script to create the text file with all possible board combinations?

    The format would look something like this (blank board):
    "board000000: | | \n--------\n | | \n--------\n | | "

    The first board would probably look like this:
    "board000001: X| | \n--------\n | | \n--------\n | | "

    And so on.

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    I guess you could write a C program to do it but by the time you actually think of all the tick tack toe boards, you could have written it down in a text editor.

    [edit]
    I'm actually thinking of won positions. I thought it was a bit crazy what I was suggesting: crack research proves that inkling correct. There are 255,168 possible positions excluding the symmetrical ones. A file is definitely not the most efficient way to store this information, if you are hell bent on doing so.
    [/edit]
    Last edited by whiteflags; 03-04-2014 at 12:37 PM.

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Yes, you could write a C program to generate all possible moves and write them to a text file.

    The question you have to ask yourself, though ... is it worth it?

    For instance, how would you go about determining which arrangement should follow the current one? If you implement this with AI, then you're doing the AI anyway, so you might as well go for the more traditional approach of just working with the board as is.

    Or you could make a list ("if player moves and the board looks like this, then pull up that move from the list"). But it seems an awfully large undertaking, and certainly a lot more work than the traditional "analyze current positions and determine move based on a list of rules" approach.

    Also note, both for the text file and the actual TTT program, the internal representation of the board does not have to match the visual representation.

    For instance, you don't need this in your code:

    Code:
    char gameboard[5][5] = {
        "X| | ",
        "-+-+-",
        "X|O|X",
        "-+-+-",
        " |O| "
    };
    Instead, you could have a simpler representation of the positions in your code (and text file); e.g.

    Code:
    char gameboard[9] = {'X',' ',' ','X','O','X',' ','O',' '};
    And have a function just for printing that will print the game board to the screen in its visually recognizable form.

  4. #4
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    Using symmetry you could use a single unsigned char to represent the positions. The extra bits could be used to store metadata (e.g. board rotation)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Printing all combinations of a string loop problem
    By amadeus111 in forum C Programming
    Replies: 4
    Last Post: 01-28-2013, 04:14 PM
  2. Reading a text file and printing it out
    By rolsis in forum C Programming
    Replies: 4
    Last Post: 02-13-2011, 07:54 AM
  3. Reading and Printing a text file
    By Mikahcho in forum C Programming
    Replies: 20
    Last Post: 12-06-2010, 03:46 PM
  4. printing a text file
    By j_spinto in forum C Programming
    Replies: 13
    Last Post: 06-25-2005, 05:33 PM
  5. help printing text file
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 05-19-2002, 10:33 PM