UML Diagram for single class program

This is a discussion on UML Diagram for single class program within the C++ Programming forums, part of the General Programming Boards category; My professor wants us to write a UML diagram for our programs, however my previous professor never taught this to ...

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    117

    UML Diagram for single class program

    My professor wants us to write a UML diagram for our programs, however my previous professor never taught this to us.

    I looked online and all of the examples have multiple classes in them.

    Just wanted a little help on how setting this code up bc the way I read it, the UML would only have a single box to it. (and that is how my professor wants it).

    Thanks!

    main.cpp
    Code:
    #include <iostream>
    #include "TicTacToe.h"
    using namespace std;
    
    int main()
    {
    	TicTacToe game;
    	int count;
    
        do
        {
            game.Initialize();
    
            for(count = 0; count < 9; count++)
            {
                game.setTile(count);
    
                if(game.getWin() == true)
                    break;
            }
    
            if(count == 9)
                cout << endl << "Out of Spaces. It's a tie!" << endl;
    
            else
                cout << endl << "Player " << game.getPlayer() << " wins!" << endl;
    
        }while(game.isQuit() == false);
    
    	return 0;
    }
    TicTacToe.cpp
    Code:
    #include <iostream>
    #include "TicTacToe.h"
    using namespace std;
    
    TicTacToe::TicTacToe()
    {
        Initialize();
    }
    
    void TicTacToe::Initialize()
    {
        for(int a = 0; a<3; a++)
        for(int b = 0; b<3; b++)
            Grid[a][b] = ' '; // space = empty
    
        win = false;
    }
    
    void TicTacToe::outputGrid()
    {
    	cout << (char)201 << (char)205 << (char)205 << (char)205 << (char)205 << (char)209 << (char)205 << (char)205 << (char)205 << (char)205 << (char)209 << (char)205 << (char)205 << (char)205 << (char)205 << (char)187 << endl;
        cout << (char)186 << "1   " << (char)179 << "2   " << (char)179 << "3   " <<  (char)186 << endl;
        cout << (char)186 << "  " << Grid[0][0] << " " << (char)179 << "  " << Grid[0][1] << " " << (char)179 << "  " << Grid[0][2] << " " <<  (char)186 << endl;
        cout << (char)186 << "    " << (char)179 << "    " << (char)179 << "    " <<  (char)186 << endl;
        cout << (char)199 << (char)196  << (char)196 << (char)196 << (char)196 << (char)197 << (char)196  << (char)196 << (char)196 << (char)196 << (char)197 << (char)196  << (char)196 << (char)196  << (char)196  << (char)182 << endl;
        cout << (char)186 << "4   " << (char)179 << "5   " << (char)179 << "6   " <<  (char)186 << endl;
        cout << (char)186 << "  " << Grid[1][0] << " " << (char)179 << "  " << Grid[1][1] << " " << (char)179 << "  " << Grid[1][2] << " " <<  (char)186 << endl;
        cout << (char)186 << "    " << (char)179 << "    " << (char)179 << "    " <<  (char)186 << endl;
        cout << (char)199 << (char)196  << (char)196 << (char)196 << (char)196 << (char)197 << (char)196  << (char)196 << (char)196 << (char)196 << (char)197 << (char)196  << (char)196 << (char)196  << (char)196  << (char)182 << endl;
        cout << (char)186 << "7   " << (char)179 << "8   " << (char)179 << "9   " <<  (char)186 << endl;
        cout << (char)186 << "  " << Grid[2][0] << " " << (char)179 << "  " << Grid[2][1] << " " << (char)179 << "  " << Grid[2][2] << " " <<  (char)186 << endl;
        cout << (char)186 << "    " << (char)179 << "    " << (char)179 << "    " <<  (char)186 << endl;
        cout << (char)200 << (char)205 << (char)205 << (char)205 << (char)205 << (char)207 << (char)205 << (char)205 << (char)205 << (char)205 << (char)207 << (char)205 << (char)205 << (char)205 << (char)205 << (char)188 <<"\n\n";
    }
    
    void TicTacToe::setTile(int count)
    {
        setPlayer(count);
    
        do
        {
            outputGrid();
            cout << "Where do you want to place your mark player " << player << "? ";
            cin >> tile;
        }   while(checkTileRange() == false || isFilled() == true);
    
        setGrid();
        checkWin();
    }
    
    bool TicTacToe::checkTileRange()
    {
    	if (tile < 1 || tile > 9)
    	{
    		cout << endl << "Error. Please enter a number between 1 and 9." << endl;
    		return false;
    	}
    
    	else
    		return true;
    }
    
    bool TicTacToe::isFilled()
    {
        if(Grid[(tile-1)/3][(tile-1)%3]  == ' ')
            return false;
    
        else
        {
            cout << endl << "Error. Tile is already filled." << endl;
            return true;
        }
    
    }
    
    void TicTacToe::setGrid()
    {
        Grid[(tile-1)/3][(tile-1)%3] = player;
    }
    
    void TicTacToe::checkWin()
    {
        for (int a = 0; a < 3; a++)
        {
            if(Grid[a][0] != ' ' && Grid[a][0] == Grid[a][1] && Grid[a][0] == Grid[a][2])
            {
                win = true;
                outputGrid();
            }
    
            if(Grid[0][a] != ' ' && Grid[0][a] == Grid[1][a] && Grid[0][a] == Grid[2][a])
            {
                win = true;
                outputGrid();
            }
        }
    
        if(Grid[0][0] != ' ' && Grid[0][0] == Grid[1][1] && Grid[0][0] == Grid[2][2])
        {
            win = true;
            outputGrid();
    
        }
    
        if(Grid[0][2] != ' ' && Grid[0][2] == Grid[1][1] && Grid[0][2] == Grid[2][0])
        {
            win = true;
            outputGrid();
        }
    }
    
    bool TicTacToe::getWin()
    {
        return win;
    }
    
    void TicTacToe::setPlayer(int count)
    {
        if (count%2 == 0)
        player = 'X';
    
        else
            player = 'O';
    
    }
    
    char TicTacToe::getPlayer()
    {
        return player;
    }
    
    bool TicTacToe::isQuit()
    {
        do
        {
            cout << "Continue? (Y = Yes, N = No) ";
            cin >> quit;
        } while(toupper(quit) != 'Y' && toupper(quit) != 'N' );
    
    
        if(toupper(quit) == 'Y')
            return false;
    
        else
            return true;
    
    }
    TicTacToe.h
    Code:
    class TicTacToe
    {
    	private:
    		char Grid[3][3];
    		char player;
    		int tile; // tile player ants to go
    		bool emptyTile,  win;
    		char quit;
    
    	public:
    		TicTacToe();
    		void Initialize();
    
    		void outputGrid();
    
    		void setTile(int);
    
            bool checkTileRange(); //returns true if space isn't taken
            bool isFilled();
    		void setGrid();
    		void checkWin();
    		bool getWin();
    
    		void setPlayer(int);
    		char getPlayer();
    
    		bool isQuit();
    };
    My Ctrl+S addiction gets in the way when using Code Blocks...

  2. #2
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,305
    It depends on what kind of UML diagram you make.
    A "sequence diagram" for example would have a bit more to show.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  3. #3
    Registered User
    Join Date
    Sep 2011
    Posts
    117
    Is this even close? I didn't know what to put on top of each long box, and dint' know how to put a loop

    http://i47.tinypic.com/34dln5i.jpg
    My Ctrl+S addiction gets in the way when using Code Blocks...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Building a class in a single file
    By Amoxaphobic in forum C++ Programming
    Replies: 5
    Last Post: 11-10-2011, 01:34 PM
  2. box-and-circle diagram
    By kezkez in forum C Programming
    Replies: 0
    Last Post: 02-12-2010, 09:34 PM
  3. UML: Context diagram
    By society in forum Tech Board
    Replies: 3
    Last Post: 01-05-2006, 11:54 AM
  4. drawing flow-charts for every single program??
    By LogicError in forum A Brief History of Cprogramming.com
    Replies: 19
    Last Post: 05-30-2005, 10:22 AM

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