Thread: Need some help with a basic tic tac toe game

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

    Need some help with a basic tic tac toe game

    Out of boredom one day I decided to see how much programming I forgot since I finished high school (it was alot)

    I plugged this out without help but since I don't want to leave it as 1's and 2's I am trying to convert to x, o's but I can't remember anything except int's

    My main problem is int's and char's interacting

    The following code works fine in Linux but is based on 1's and 2's

    // Program: Tic Tac Toe
    // Created by: Christopher Hodgins
    // E-Mail: [email protected]
    // License: GPL
    // Version: 0.2

    int ch, pl, tn, wn,
    aa, ab, ac,
    ba, bb, bc,
    ca, cb, cc;

    main()
    {
    start:;

    system("clear");
    printf("\n %d | %d | %d",aa,ab,ac);
    printf("\n-----------");
    printf("\n %d | %d | %d",ba,bb,bc);
    printf("\n-----------");
    printf("\n %d | %d | %d",ca,cb,cc);

    if (aa==pl && ab==pl && ac==pl) { wn=pl; }
    if (ba==pl && bb==pl && bc==pl) { wn=pl; }
    if (ca==pl && cb==pl && cc==pl) { wn=pl; }
    if (aa==pl && ba==pl && ca==pl) { wn=pl; }
    if (ab==pl && bb==pl && cb==pl) { wn=pl; }
    if (ac==pl && bc==pl && cc==pl) { wn=pl; }
    if (aa==pl && bb==pl && cc==pl) { wn=pl; }
    if (ac==pl && bb==pl && ca==pl) { wn=pl; }
    if (wn >= 1) { printf("\n\nPlayer %d wins.\n\n",pl); goto end; }

    if (pl == 2) { pl=0; }
    pl=pl+1;
    tn=tn+1;

    if (tn == 10) { printf("\n\nCat's Game\n\n"); goto end; }

    printf("\n\nPlayer %d what is your move? ",pl);
    scanf("%d",&ch);
    switch (ch)
    {
    case 1: { aa=pl; goto start; }
    case 2: { ab=pl; goto start; }
    case 3: { ac=pl; goto start; }
    case 4: { ba=pl; goto start; }
    case 5: { bb=pl; goto start; }
    case 6: { bc=pl; goto start; }
    case 7: { ca=pl; goto start; }
    case 8: { cb=pl; goto start; }
    case 9: { cc=pl; goto start; }
    case 0: { goto end; }
    default: { goto start; }
    }
    end:;
    }

    ---------------------------------------------------

    Here is my current try at converting it what ever is commented is not converted yet

    ---------------------------------------------------

    // Program: Tic Tac Toe
    // Created by: Christopher Hodgins
    // E-Mail: [email protected]
    // License: GPL
    // Version: 0.2.5

    int ch, pl, tn, wn;
    char aa[1], ab[1], ac[1],
    ba[1], bb[1], bc[1],
    ca[1], cb[1], cc[1];

    main()
    {
    start:;

    system("clear");
    printf("\n %s | %s | %s",aa,ab,ac);
    printf("\n-----------");
    printf("\n %s | %s | %s",ba,bb,bc);
    printf("\n-----------");
    printf("\n %s | %s | %s",ca,cb,cc);

    // if (aa==pl && ab==pl && ac==pl) { wn=pl; }
    // if (ba==pl && bb==pl && bc==pl) { wn=pl; }
    // if (ca==pl && cb==pl && cc==pl) { wn=pl; }
    // if (aa==pl && ba==pl && ca==pl) { wn=pl; }
    // if (ab==pl && bb==pl && cb==pl) { wn=pl; }
    // if (ac==pl && bc==pl && cc==pl) { wn=pl; }
    // if (aa==pl && bb==pl && cc==pl) { wn=pl; }
    // if (ac==pl && bb==pl && ca==pl) { wn=pl; }
    // if (wn >= 1) { printf("\n\nPlayer %d wins.\n\n",pl); goto end; }

    if (pl == 2) { pl=0; }
    pl=pl+1;

    tn=tn+1;
    if (tn == 10) { printf("\n\nCat's Game\n\n"); goto end; }

    printf("\n\nPlayer %d what is your move? ",pl);
    scanf("%d",&ch);

    switch (ch)
    {
    // case 1: { if (pl=1) { aa=x }; goto start; }
    // case 2: { ab=pl; goto start; }
    // case 3: { ac=pl; goto start; }
    // case 4: { ba=pl; goto start; }
    // case 5: { bb=pl; goto start; }
    // case 6: { bc=pl; goto start; }
    // case 7: { ca=pl; goto start; }
    // case 8: { cb=pl; goto start; }
    // case 9: { cc=pl; goto start; }
    // case 0: { goto end; }
    // default: { goto start; }
    }
    end:;
    }

  2. #2
    Registered User red_baron's Avatar
    Join Date
    May 2002
    Posts
    274
    this should be in the games board!
    its talked there everyday (if u can believe it),
    i come on like once a week and there is always a person who made their "own" version of tic tac toe.

    anyways %c is characters
    declare em as
    Code:
    char ch, pl, tn, wn, 
    aa, ab, ac, 
    ba, bb, bc, 
    ca, cb, cc;
    to print em use:
    Code:
    printf("%c", variable);
    you want characters not strings btw.
    thats basically it (dont forget to check the games board)
    ¿Red Baron?

    "Imagination is more important than knowledge"
    -Albert Einstein (1879-1955)

    Check out my games!

    [code] /* dont forget code tags! */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. tic tac toe troubles...
    By mkylman in forum Game Programming
    Replies: 8
    Last Post: 09-16-2006, 06:44 PM
  2. A Tic Tac Toe game
    By Rusty_chainsaw in forum C++ Programming
    Replies: 1
    Last Post: 04-12-2006, 06:16 AM
  3. help in tic tac toe with looping only
    By amreena in forum C++ Programming
    Replies: 6
    Last Post: 11-17-2003, 07:42 AM
  4. Tic - Tac - Toe...
    By TheUnknowingOne in forum Game Programming
    Replies: 1
    Last Post: 09-10-2002, 06:01 AM
  5. Replies: 22
    Last Post: 11-08-2001, 11:01 PM