[RESOLVED] Tic-Tac-Toe Game - Applying OOP and Arrays
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: [RESOLVED] Tic-Tac-Toe Game - Applying OOP and Arrays

  1. #1
    Join Date
    Sep 2012
    Posts
    10

    [RESOLVED] Tic-Tac-Toe Game - Applying OOP and Arrays

    Objective
    Code a game allowing two human players to play tictactoe.

    Create 2 classes:
    -Create a 3 x 3, 2-D array board class to play the game.
    -Player; has a private string name data member and a method that reads the players’ row and column selections from the keyboard.

    Create 2 player objects from this class. Name the players Orestes and Xerxes.

    Think carefully about board and player classes responsibilities and how they interaction with one another. The players do not collaborate with one another but they collaborate with the board.

    My Problem
    The program compiles with the header file, but it the displaying is wrong as you will see when you enter your row and column.
    Please look into this problem and thank you for your time. I know the project isn't completed because this problem is stalling me.

    Sources
    Header file:
    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    
    class Board
    {
        public:
        void display(char Z[][3], int row, int col);
    };
    
    class Player1
    {
        public:
        Player1()
        {
            name = "Orestes";
        };
        string name;
    };
    
    class Player2
    {
        public:
        Player2()
        {
            name = "Xerxes";
        };
        string name;
    };
    
    void Board::display(char Z[][3], int row, int col)
    {
            cout << " _________________\n";
            cout << "|     |     |     | \n";
            cout << "|  " << Z[0][0] << "  |  " << Z[0][1] << "  |  " << Z[0][2] <<
    "  |\n";
            cout << "|_____|_____|_____|\n";
            cout << "|     |     |     |\n";
    
            cout << "|  " << Z[1][0] << "  |  " << Z[1][1] << "  |  " << Z[1][2] <<
    "  |\n";
            cout << "|_____|_____|_____|\n";
            cout << "|     |     |     |\n";
            cout << "|  " << Z[2][0] << "  |  " << Z[2][1] << "  |  " << Z[2][2] <<
    "  |\n";
            cout << "|_____|_____|_____|\n";
    
    }
    Main file:
    Code:
    #include "TTT.h"
    #include <iostream>
    #include <string>
    using namespace std;
    
    int main()
    {
        char X[][3] = {{'X','X','X'},{'X','X','X'},{'X','X','X'}};
        char O[][3] = {{'O','O','O'},{'O','O','O'},{'O','O','O'}};
        int row, col;
    
        Board board;
        Player1 player1;
        //board.display(NULL, NULL, NULL);
    
        cout << endl << player1.name;
        cout << "Enter row: ";
        cin >> row;
        cout << "Enter column: ";
        cin >> col;
    
        board.display(O, row, col);
    }
    Last edited by Lethargic; September 25th, 2012 at 09:29 PM.

  2. #2
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    1,999

    Re: [RESOLVED] Tic-Tac-Toe Game - Applying OOP and Arrays

    Quote Originally Posted by Lethargic View Post
    Think carefully about board and player classes responsibilities and how they interaction with one another. The players do not collaborate with one another but they collaborate with the board.
    Seems like you missed this step, which is really the most important one in the assignment. Using classes in your program is only useful if they encapsulate their inner workings. E.g. your Board class does not actually hold any information about the board. It's fine to represent the board as a 3x3 char array internally, but not every 3x3 char array is a valid board. Your class should make sure that the board is always in a valid state. So ask yourself the question how you can do that.
    Quote Originally Posted by Lethargic View Post
    My Problem
    The program compiles with the header file, but it the displaying is wrong as you will see when you enter your row and column.
    Please look into this problem and thank you for your time. I know the project isn't completed because this problem is stalling me.
    Did you debug your program? You don't do anything with the user input. You did not explain what this function is supposed to do. First think about what your program should do and write this out on paper before you start coding.
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center