Author Topic: Can anybody explain this TIC TAC TOE code ?  (Read 4816 times)

0 Members and 1 Guest are viewing this topic.

pllaybuoy

  • Guest
Can anybody explain this TIC TAC TOE code ?
« on: October 30, 2012, 10:09:00 am »
I was good with tits but then there came somebody with TICS and TACS
so well
The terrible creature has added none comments in his code and he says I should read and rewrite this myself with a few more features for practicing OOP
Can anybody explain this code ?
I understand the GAMEBOARD class , but I dont get the part from class game and don'tget the main either :/
I would be really thankful , There are no comments in the code thats why I am bugging you guys , help please  :-\
edit : p.s try compiling it wont compile either ,few errors but i can't debug it since I dont understand it in the first place
Code: [Select]
#include <iostream>
#include <string>
using namespace std;

enum squarestate{blank=' ',X='X',O='0'};
class GAMEBOARD{
private :
int HEIGHT;
int WIDTH;
int *gameboard;

public:
//Constructor
GAMEBOARD():WIDTH(3),HEIGHT(3){ //height and width in terms ofblocks
gameboard=new int[9]; //points to first elments of dynamic array
for(int i=0;i<9;i++){
*(gameboard+1)=blank;  //set all the elements to blank of gameboard
}
}
//destructor
~GAMEBOARD(){delete [] gameboard;}

void setX(int h,int w);
void set0(int h,int w);
bool istaken(int h, int w);
squarestate isline();
void draw();
};

void GAMEBOARD::setX(int h,int w){
*(gameboard+h*HEIGHT+w)=X;
}

void GAMEBOARD::set0(int h,int w){
*(gameboard+h*HEIGHT+w)=O;}

bool GAMEBOARD::istaken(int h , int w){

return *(gameboard+h*HEIGHT+w)!=' ';
}


squarestate GAMEBOARD::isline(){
//check for horizontal combinations
if(*gameboard==X&&*(gameboard+1)==X&&*(gameboard+2)==X)
return X;
if(*gameboard==O&&*(gameboard+1)==O&&*(gameboard+2)==O)
return O;
if(*(gameboard+3)==X&&*(gameboard+4)==X&&*(gameboard+5)==X)
return X;
if(*(gameboard+3)==O&&*(gameboard+4)==O&&*(gameboard+5)==O)
return O;
if(*(gameboard+6)==X&&*(gameboard+7)==X&&*(gameboard+8)==X)
return X;
if(*(gameboard+6)==O&&*(gameboard+7)==O&&*(gameboard+8)==O)
return O;
//check for vertical combinations
if(*gameboard==X&&*(gameboard+3)==X&&*(gameboard+6)==X)
return X;
if(*gameboard==O&&*(gameboard+3)==O&&*(gameboard+6)==O)
return O;
if(*(gameboard+1)==X&&*(gameboard+4)==X&&*(gameboard+7)==X)
return X;
if(*(gameboard+1)==O&&*(gameboard+4)==O&&*(gameboard+7)==O)
return O;
if(*(gameboard+2)==X&&*(gameboard+5)==X&&*(gameboard+8)==X)
return X;
if(*(gameboard+2)==O&&*(gameboard+5)==O&&*(gameboard+8)==O)
return O;
//check for diagonals
if(*(gameboard)==X&&*(gameboard+4)==X&&*(gameboard+8)==X)
return X;
if(*(gameboard)==O&&*(gameboard+4)==O&&*(gameboard+8)==O)
return O;
if(*(gameboard+2)==X&&*(gameboard+4)==X&&*(gameboard+6)==X)
return X;
if(*(gameboard+2)==O&&*(gameboard+4)==O&&*(gameboard+6)==O)
return O;
return blank;
}
//done


void GAMEBOARD::draw(){

cout<<"\n";
for(int i=0;i<HEIGHT;i++){
cout<<(char)*(gameboard+i*HEIGHT);
for(int c=1;c<3;c++)cout<<"|"<<(char)*(gameboard+i*WIDTH+c);
cout<<"\n"<<"--------"<<"\n";
}
}


class GAME{
public:
GAMEBOARD* doinput(string player,GAMEBOARD * gb);
bool inrange(int test);

}


GAMEBOARD* GAME::doinput(string player,GAMEBOARD * gb){
gb->draw();
string letter;
if(player.compare("one")==O)letter="X";
else if(player.compare("two")==O)letter="0";
else return gb;

int input1,input 2;
do{

do{
cout<<"\nplayer "<<player.c_str()<<", please enter a row to put an "<<letter.c_srt()<<": ";
cin>>input1;
}while(!inrange(input1));
do{
cout<<"\n Please enter a column number to put an "<<letter.c_srt<<": ";
cin>>input2;
}while(!inrange(input2));

}
while(gb->istaken(input1,input2));

if(player.compare("one")==O)
gb->setX(input1,input2);
else gb->set0(input1,input2);
return gb;}

bool GAME::inrange(int test){
return test>-1&&test<3;}




int main( void )
{
      using std::cout;
      using std::cin;

      gameboard* gb = new GAMEBOARD;
      GAME g;
      string player1, player2;
      cout << "Welcome to Tic Tac Toe!"
            << "\nPlayer one, please enter your name: ";
      cin >> player1;
      cout << "\nPlayer two, please enter your name: ";
      cin >> player2;

      while (gb->isline() == ' ')
      {
            gb = g.doinput("one",gb);
            gb = g.doinput("two",gb);
        }
      gb->draw();
      if(gb->is;ine() == X)
            cout << "\nPlayer one, you win!\nGame Over.";
      else
            cout << "\nPlayer two, you win!\nGame Over.";
      return 0;
}




}

« Last Edit: October 30, 2012, 10:10:20 am by pllaybuoy »

Offline Dark Nebulae

  • Peasant
  • *
  • Posts: 117
  • Cookies: -79
  • Unleash the Hacker within you
    • View Profile
Re: Can anybody explain this TIC TAC TOE code ?
« Reply #1 on: October 30, 2012, 10:34:17 am »
I compiled it in my compiler and it said that there were two errors.
ERROR NO. 1 ==> There is no need of the last brace( } ) in the program.
ERROR NO. 2==> There is a declaration syntax error at (103,11)--> [GAMEBOARD* GAME::doinput(string player,GAMEBOARD * gb){ ] at the 11th position.
Hope it will help.........
Trust is like a piece of paper.Once it is crumbled,it can never be perfect.

pllaybuoy

  • Guest
Re: Can anybody explain this TIC TAC TOE code ?
« Reply #2 on: October 30, 2012, 11:23:21 am »
As mentioned there are a few errors , I dont  get it how to fix the ERROR number 2 you mentioned
and hey can anybody explain what I asked for ? the stuff after the GAMEBOARD CLASS ?

Offline Daemon

  • VIP
  • Baron
  • *
  • Posts: 845
  • Cookies: 153
  • A wise man fears a gentle mans anger
    • View Profile
Re: Can anybody explain this TIC TAC TOE code ?
« Reply #3 on: October 30, 2012, 06:05:30 pm »
Playybuoy...you call yourself a C++ programmer yet your constantly asking us to do the work for you. I don't get it tbh. Try taking the time to sit down with a pen and a piece of paper and I'm sure you can figure out what's going on. I spent 1 semester in a c++ class and I can figure it out, so I'm sure you can too. Just gotta put in a little time and effort to do so. Good luck man
This lifestyle is strictly DIY or GTFO - lucid

Because sexploits are for h0edays - noncetonic


Xires burns the souls of HF skids as a power supply

pllaybuoy

  • Guest
Re: Can anybody explain this TIC TAC TOE code ?
« Reply #4 on: October 30, 2012, 08:20:15 pm »
Playybuoy...you call yourself a C++ programmer yet your constantly asking us to do the work for you. I don't get it tbh. Try taking the time to sit down with a pen and a piece of paper and I'm sure you can figure out what's going on. I spent 1 semester in a c++ class and I can figure it out, so I'm sure you can too. Just gotta put in a little time and effort to do so. Good luck man
For real man ? Can't you guys just leave a single chance of mocking up anybody ? I don't hold any degree or have attended any IT class ever ,I have learned all by myself and still striving for it and yes I do call myself a programmer , thank you so much for the helpful reply . I appreciate that .

p.s: I've NEVER asked any of you to do something for me,I never made a post like  "Write a program that does this or that " ,however I do post when I get confused but before posting here I try my level best to understand it by my own , anyways sorry if that bugs you :) cheers
« Last Edit: October 30, 2012, 08:26:58 pm by pllaybuoy »

Offline p_2001

  • Royal Highness
  • ****
  • Posts: 684
  • Cookies: -64
    • View Profile
Re: Can anybody explain this TIC TAC TOE code ?
« Reply #5 on: October 30, 2012, 08:24:55 pm »
How about a little tip that i use...
Do this... Understand the logic of the program..
Run it on paper.. You might find the error..
If not, just write your own code.. This way you learn...
"Always have a plan"

pllaybuoy

  • Guest
Re: Can anybody explain this TIC TAC TOE code ?
« Reply #6 on: October 30, 2012, 08:27:35 pm »
How about a little tip that i use...
Do this... Understand the logic of the program..
Run it on paper.. You might find the error..
If not, just write your own code.. This way you learn...
Roger that , Gonna write the whole code again and see if I get it to work, thanks:)

Offline Daemon

  • VIP
  • Baron
  • *
  • Posts: 845
  • Cookies: 153
  • A wise man fears a gentle mans anger
    • View Profile
Re: Can anybody explain this TIC TAC TOE code ?
« Reply #7 on: October 30, 2012, 08:37:11 pm »
For real man ? Can't you guys just leave a single chance of mocking up anybody ? I don't hold any degree or have attended any IT class ever ,I have learned all by myself and still striving for it and yes I do call myself a programmer , thank you so much for the helpful reply . I appreciate that .

p.s: I've NEVER asked any of you to do something for me,I never made a post like  "Write a program that does this or that " ,however I do post when I get confused but before posting here I try my level best to understand it by my own , anyways sorry if that bugs you :) cheers

don't get so defensive, I'm not insulting or belittling or mocking you. I'm pointing out that a programmer has to be willing to put in the time and effort. Especially if your still learning.
How about a little tip that i use...
Do this... Understand the logic of the program..
Run it on paper.. You might find the error..
If not, just write your own code.. This way you learn...

Which is exactly what I said to do but I get yelled at and you get a thanks...
* Daemon shrugs
Whatcha gonna do right?
This lifestyle is strictly DIY or GTFO - lucid

Because sexploits are for h0edays - noncetonic


Xires burns the souls of HF skids as a power supply

Offline p_2001

  • Royal Highness
  • ****
  • Posts: 684
  • Cookies: -64
    • View Profile
Re: Can anybody explain this TIC TAC TOE code ?
« Reply #8 on: October 30, 2012, 08:50:09 pm »
Playybuoy...you call yourself a C++ programmer yet your constantly asking us to do the work for you. I don't get it tbh. Try taking the time to sit down with a pen and a piece of paper and I'm sure you can figure out what's going on. I spent 1 semester in a c++ class and I can figure it out, so I'm sure you can too. Just gotta put in a little time and effort to do so. Good luck man

What you did, is.. Well.. Look.. Don't take offense.. But your negotiation skills must suck in real life...
I mean no offense..
See.. You started your post by directly insulting him.
Every human has an ego.. If you begin by insulting it, no one is going to read the post and consider it.. They will read it but won't think on it.

#2) no offense to playboy, but if he didn't get the code, it's likely that he is either not bright or simply not aged enough or, has difficulty understanding the language and hence can't understand the subtle hint  or a combination of all.
Rather than giving a vague hint of pen and paper, be precise and tell him exactly what to do.

Now perhaps you understand..
I wrote the post in an almost exact manner in which you wrote your first postt .... If you're not offended and understood what i said, I would say most likelly did it in your second reading. Because i just called you lacking in some regards and most likely you didn't appreciate it.

i am prepared for a -1 and a caustic reply.
"Always have a plan"

pllaybuoy

  • Guest
Re: Can anybody explain this TIC TAC TOE code ?
« Reply #9 on: October 30, 2012, 08:57:01 pm »
What you did, is.. Well.. Look.. Don't take offense.. But your negotiation skills must suck in real life...
I mean no offense..
See.. You started your post by directly insulting him.
Every human has an ego.. If you begin by insulting it, no one is going to read the post and consider it.. They will read it but won't think on it.

#2) no offense to playboy, but if he didn't get the code, it's likely that he is either not bright or simply not aged enough or, has difficulty understanding the language and hence can't understand the subtle hint  or a combination of all.
Rather than giving a vague hint of pen and paper, be precise and tell him exactly what to do.

Now perhaps you understand..
I wrote the post in an almost exact manner in which you wrote your first postt .... If you're not offended and understood what i said, I would say most likelly did it in your second reading. Because i just called you lacking in some regards and most likely you didn't appreciate it.

i am prepared for a -1 and a caustic reply.

Right , I am not good at it , just a determined learner and I really got confused with the code , had no choice but to ask for help , I will however try my best by coding this again from the scratch , thanks :)

Offline p_2001

  • Royal Highness
  • ****
  • Posts: 684
  • Cookies: -64
    • View Profile
Re: Can anybody explain this TIC TAC TOE code ?
« Reply #10 on: October 30, 2012, 09:01:31 pm »
Right , I am not good at it , just a determined learner and I really got confused with the code , had no choice but to ask for help , I will however try my best by coding this again from the scratch , thanks :)

Just stay put at it... Hard work trumps natural genius every fucking time.. It's just a matter of time...
I know it from experience... Sadly I'm at the losing end every time...
"Always have a plan"

Offline Chronic x

  • Peasant
  • *
  • Posts: 91
  • Cookies: 24
  • Former GMOD.
    • View Profile
Re: Can anybody explain this TIC TAC TOE code ?
« Reply #11 on: October 31, 2012, 12:32:12 am »
At the end of the day this site is for discussion learning and HELP, so if someone gets it why not give the guy a hand, Daemon your basically saying if a cop ask's you "What did he look like" Your gonna say "Well your a cop you went to school for it, shouldn't you know?" that's not what it's all about this is a community, you should PRIDE your self on helping someone else not throw them a pen and paper, my 2 cents, disregard it if you want.
Can't Stop The Crooks

Offline Daemon

  • VIP
  • Baron
  • *
  • Posts: 845
  • Cookies: 153
  • A wise man fears a gentle mans anger
    • View Profile
Re: Can anybody explain this TIC TAC TOE code ?
« Reply #12 on: October 31, 2012, 01:00:30 am »
At the end of the day this site is for discussion learning and HELP, so if someone gets it why not give the guy a hand, Daemon your basically saying if a cop ask's you "What did he look like" Your gonna say "Well your a cop you went to school for it, shouldn't you know?" that's not what it's all about this is a community, you should PRIDE your self on helping someone else not throw them a pen and paper, my 2 cents, disregard it if you want.

Learning isn't the problem, I do enjoy helping people. What I don't enjoy is when people don't take the time to try and help themselves first. First off, whats not to understand? It's a class titled game and main. Do you understand what main is? Do you understand what a class is? If your a C++ programmer, then yes you do. If not, then don't call yourself one. OP, i know you know what a class is from your other posts. So what's not to get about it? To me it seems like you went through and put some comments by all the stuff that took 2 seconds to understand and then anything that took more than a minute you said F it I'll ask EZ. It bothers me because a programmer needs to be able to do more critical thinking than that, a programmer needs to be able to know the how to problem solve and not ask for help every time something doesn't come to them right away. I'm not trying to be rude or belittle the OP, it's just a pet peeve of mine that so many people know "text book learning" but don't know how to problem solve. What I would like to see is some more problem solving/critical thinking from our OP here, because I feel like I havent seen much of it from ANY of his posts in this board. I guess the reason I seem so harsh and uncaring here is because it's been building up and spilling over into this one post, and my apologies for that OP.

To clarify; I have no problem helping someone learn. But I really do wish our OP would do some more helping himself first, and when you do need our help ask more specific questions instead of what does this class do? If you know C++ you should have a general idea of what the class does even if one specific aspect of it throws you off.

Just my 0.02 take it as you will.


@p_2001
No caustic remarks or -1 from me, your absolutely right in that I did come off rude. I seriously wasn't trying to insult OP in my first post, and you hit the hammer on the nail as far how stuff is presented matters almost as much if not more so than what has been presented. As for insulting my ego, you can't. Not to be rude, but it's just not possible. I am very aware of my strengths and weaknesses, and I scorn people who delude themselves about their respective traits. To me that delusion of self is one of the leading causes of what's wrong with the world, in fact if you would take a moment and think back to most of the fights you have ever had (verbal or physical) or seen others have, or even world problems...odds are you'll see someone somewhere who is deluded about themself and so gets defensive which escalates the confrontation to idiotic proportions.
On the few occasions multiple people point out a weakness of mine that I didn't think was a weakness, I usually consult one or more people I trust for an honest opinion then take some time to reflect on what they say. I've got more to say on self delusion, but this is a C++ board. I'm finished here.

OP, good luck
This lifestyle is strictly DIY or GTFO - lucid

Because sexploits are for h0edays - noncetonic


Xires burns the souls of HF skids as a power supply

pllaybuoy

  • Guest
Re: Can anybody explain this TIC TAC TOE code ?
« Reply #13 on: October 31, 2012, 06:28:21 am »
Learning isn't the problem, I do enjoy helping people. What I don't enjoy is when people don't take the time to try and help themselves first. First off, whats not to understand? It's a class titled game and main. Do you understand what main is? Do you understand what a class is? If your a C++ programmer, then yes you do. If not, then don't call yourself one. OP, i know you know what a class is from your other posts. So what's not to get about it? To me it seems like you went through and put some comments by all the stuff that took 2 seconds to understand and then anything that took more than a minute you said F it I'll ask EZ. It bothers me because a programmer needs to be able to do more critical thinking than that, a programmer needs to be able to know the how to problem solve and not ask for help every time something doesn't come to them right away. I'm not trying to be rude or belittle the OP, it's just a pet peeve of mine that so many people know "text book learning" but don't know how to problem solve. What I would like to see is some more problem solving/critical thinking from our OP here, because I feel like I havent seen much of it from ANY of his posts in this board. I guess the reason I seem so harsh and uncaring here is because it's been building up and spilling over into this one post, and my apologies for that OP.

To clarify; I have no problem helping someone learn. But I really do wish our OP would do some more helping himself first, and when you do need our help ask more specific questions instead of what does this class do? If you know C++ you should have a general idea of what the class does even if one specific aspect of it throws you off.

Just my 0.02 take it as you will.


@p_2001
No caustic remarks or -1 from me, your absolutely right in that I did come off rude. I seriously wasn't trying to insult OP in my first post, and you hit the hammer on the nail as far how stuff is presented matters almost as much if not more so than what has been presented. As for insulting my ego, you can't. Not to be rude, but it's just not possible. I am very aware of my strengths and weaknesses, and I scorn people who delude themselves about their respective traits. To me that delusion of self is one of the leading causes of what's wrong with the world, in fact if you would take a moment and think back to most of the fights you have ever had (verbal or physical) or seen others have, or even world problems...odds are you'll see someone somewhere who is deluded about themself and so gets defensive which escalates the confrontation to idiotic proportions.
On the few occasions multiple people point out a weakness of mine that I didn't think was a weakness, I usually consult one or more people I trust for an honest opinion then take some time to reflect on what they say. I've got more to say on self delusion, but this is a C++ board. I'm finished here.

OP, good luck
Removed "c++ coder" from my profile info , guess its all good now , peace .

Offline relax

  • Sir
  • ***
  • Posts: 562
  • Cookies: 114
  • The one and only
    • View Profile
Re: Can anybody explain this TIC TAC TOE code ?
« Reply #14 on: October 31, 2012, 03:28:02 pm »
to me its the same witch ever language you try to learn.
you need to find your way to code not the way other people code just so you understand what your doing and feel comfortable with it. eventually you will notice that you coding looks more and more like "pro" coding.

When i learned java witch was my first "real" programming language i had to do a tick tack toe
and i was in spain on vecation without internet, it took more time then needed but it felt good when it was done without any help. when i got back from spain i looked up the answer code and realized that my program was better then the one the book suggested.

what iam trying to say is dont give up do it you way