Tic Tac Toe problem

This is a discussion on Tic Tac Toe problem within the C++ Programming forums, part of the General Programming Boards category; I need to write a program that tells the state of a Tic-Tac-Toe game, based off a board input by ...

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    3

    Tic Tac Toe problem

    I need to write a program that tells the state of a Tic-Tac-Toe game, based off a board input by the user. The program needs to return a single character: X if x wins, O if o wins, or D if it is a draw, and * if none of those conditions exist. I'm supposed to write a function to do this, and call it in the main. I'm a bit stuck on how to go about doing this. So far all I have is a main function that prints the board after inputted by the user. Suggestions? Here's my code:

    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
        char board[3][3];
        int i=0, j=0;
        cout << "Enter the board" << endl;
        for(i=0; i<3; i++)
        {
          for(j=0; j<3; j++)
          {
          cin >> board[i][j];
          }
        }
        for(i=0; i<3; i++)
        {
        for(j=0; j<3; j++)
        {
        cout << board[i][j];
        }
        cout << endl;
        }
        
        return 0;
    }

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,555
    Well perhaps something like
    Code:
    char checkBoard ( char board[3][3] ) {
      char winChar = '*';
      // your code to calculate 'X' 'O' or 'D'
      return winChar;
    }
    Your main then contains
    cout << checkBoard(board) << endl;
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Feb 2012
    Posts
    3
    Thank you. In addition though, I'm given a hint to use a for loop that compares each element of the diagonal with the elements of it's intersecting row and column, and then check diagonals. Unfortunately, the book I'm using doesn't really give much explanation, nor examples of how to actually do this. How does one exactly make that for loop?

  4. #4
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,425
    Indent your statements by one level within curly braces.
    IMHO The thing is with stuff like this is there is a certain amount of 'extrapolation' expected..methinks. I.E. if you understand that [3][3] represents a 3 by 3 square board, then it follows that [0][0] is top left corner [1][1] is centre etc, this then lets you manipulate the variables you use in the index to check for diagonal matches or whatever.

    Your board setup uses a nested for loop, you could use this idea to achieve your goal perhaps.
    Salem has shown a char checkBoard(..) function, you could take this idea and add an additional parameter, to check status of player X then O and allow your return to also be shown as D if that is found to be the outcome, the additional parameter is not really neccesary because logically you can gather the result in one call, but maybe play with it.

    And in a given board postion there is the always the possibility that play can continue, no win, no draw, so that must be accomodated too. Unless you only have one known outcome to test as part of your excercise of course, why work harder than neccesary ;->
    Last edited by rogster001; 02-24-2012 at 03:46 PM.
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  5. #5
    Registered User
    Join Date
    Feb 2012
    Posts
    3
    Thank you for the thoughts! But one last question, since my book hardly goes into any detail about 2 dimensional Arrays, I don't really know how to start the code to compare, say, the top left corner of the board to, say, the square to the right of it, or the square in the middle, etc. I know that writing [0][0] would be the top left corner, [0][1] would be one to the right, but how do I incorporate that into code? Lets just say I call my Array board[i][j], how would I start off with the top left corner, and compare it to the square next to it? and the one below it? Once I figure out how to do that, I think I have a good idea how to write the function.

  6. #6
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,425
    you have already used this:
    Code:
     for(i=0; i<3; i++)
        {
          for(j=0; j<3; j++)
          {
          cin >> board[i][j];
          }
        }
    [i] stays at 0 until [j] exits loop, i goes to 1 until j exits loop..etc.
    each time between the changes for [i] ; j is going 0 , 1 , 2 <3=break>

    so that is a way to step through all the board positions.

    so you might want to treat each [i][j] as the 'current' postion, and then for example test the top left position relative to the current i,j : ask: ¿what is contained in [i-1][j-1] compared to what is in [i][j] ? But you need to rememeber you cant try and look beyond bounds eg [-1][0] or [1][4] so your code has to protect against that
    Last edited by rogster001; 02-24-2012 at 05:49 PM.
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sleep() function problem or logic problem?
    By FernandoBasso in forum C Programming
    Replies: 7
    Last Post: 11-16-2011, 04:50 PM
  2. Replies: 4
    Last Post: 10-16-2008, 07:30 PM
  3. Replies: 27
    Last Post: 10-11-2006, 04:27 AM
  4. Visual Studio Linker problem or my problem?
    By OOPboredom in forum C Programming
    Replies: 2
    Last Post: 04-13-2004, 12:32 AM
  5. syntax linked list problem & struct problem
    By beely in forum C Programming
    Replies: 5
    Last Post: 11-11-2002, 08:14 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21