Author Topic: A question about C++.  (Read 1184 times)

RussianVodka

  • Hydlaa Notable
  • *
  • Posts: 689
    • View Profile
A question about C++.
« on: August 04, 2005, 03:47:03 am »
So I\'m making a program that plays tic-tac-toe with you, and I\'m getting sorta annoyed with having the simple yet long functions cover up the screen.

So I\'m thinking of setting up my own header file for this \"project\", and I\'m wondering how it\'s done. Would I just create a normal c++ file except give it the extention \'.h\', and then just #include it and call the functions as if they were declared in the program itself.

Or is there another way I\'m suposed to do this?
« Last Edit: August 04, 2005, 03:47:46 am by RussianVodka »



Q: How many Planeshifters does it take to expalin a simple concept to a newb?
A: Six. Five to argue on who\'s explanation is right, and Moogie to lock the thread.

ramlambmoo

  • Hydlaa Notable
  • *
  • Posts: 567
    • View Profile
(No subject)
« Reply #1 on: August 04, 2005, 03:50:52 am »
Yeah, thats pretty much it, just put it in the same directory and at the top, with all your other includes, type
#include \"something.h\"
Im pretty sure it has to be in quotation marks, not < and >, becase #include would indicate a standard header file in the include folder of the compilers directory.

RussianVodka

  • Hydlaa Notable
  • *
  • Posts: 689
    • View Profile
(No subject)
« Reply #2 on: August 04, 2005, 04:23:02 am »
Also, the headers are placed in the same folder as the source file?



Q: How many Planeshifters does it take to expalin a simple concept to a newb?
A: Six. Five to argue on who\'s explanation is right, and Moogie to lock the thread.

ramlambmoo

  • Hydlaa Notable
  • *
  • Posts: 567
    • View Profile
(No subject)
« Reply #3 on: August 04, 2005, 04:32:57 am »
Yes, though you can place them in a folder in that directory and then refer to them in the directory:
E.G Make a folder in the same directory as the .cpp file called \"Headers\", and then
#include \"Headers/something.h\"
Though for your project, just placing them in the same folder as the .cpp will be fine.

Seytra

  • Forum Addict
  • *
  • Posts: 2052
  • No system can compensate lack of common sense.
    • View Profile
No!
« Reply #4 on: August 04, 2005, 04:35:10 am »
This is not how you do it!

Header files are not for code! They are for declarations only! They are used to declare prototypes, classes, macros, etc. ( i.e., \"nothing that generates code\" ). Everything else is to be done in source files!

Header files usually get their own folder. The project then looks like this:
Project main folder:
makefile
src
include

What you can do is to split up your project into multiple source files by purpose or per class, the way it usually is being done.
However, you must then compile the files to object files and then link them. An IDE will create makefiles for you to automate this process.

It is also possible to #include a source file, which would be the quick and dirty way. :)
« Last Edit: August 04, 2005, 04:38:09 am by Seytra »

aarobber

  • Traveller
  • *
  • Posts: 12
    • View Profile
(No subject)
« Reply #5 on: August 04, 2005, 05:28:06 am »
It is possible to place the implementation in the header file.  In fact, pretty much every coder does that for very simple functions.

The issue is that problems arise when placing implementation in a header file.  One big one you\'ll probably run into in this situation is that a lot of build utilities won\'t look for changes in a header file and won\'t recompile everything dependent on your latest implementation changes to the header file.  This will result in wierd errors that would be difficult for a newbie to pinpoint.

RussianVodka

  • Hydlaa Notable
  • *
  • Posts: 689
    • View Profile
(No subject)
« Reply #6 on: August 05, 2005, 07:13:20 pm »
Ok, so far I have three .cpp files:
Tic-Tac-Toe_Source (contains the main code)
Tic-Tac-Toe_Functions (contains most of the functions, by far the longest file)
Tic-tac-Toe_Turns_AI (contains the code for the turns, and also the computer AI)

Both \"Tic-Tac-Toe_Functions\" and \"Tic-tac-Toe_Turns_AI\" both plug into \"Tic-Tac-Toe_Source\".


Now the problem that I am encountering is that I will have to use some code from \"Tic-Tac-Toe_Functions\" in \"Tic-tac-Toe_Turns_AI\". So how will the compiler look apon that?

Also, will I have to #include in \"Tic-Tac-Toe_Functions\" and \"Tic-tac-Toe_Turns_AI\"?



Q: How many Planeshifters does it take to expalin a simple concept to a newb?
A: Six. Five to argue on who\'s explanation is right, and Moogie to lock the thread.

ramlambmoo

  • Hydlaa Notable
  • *
  • Posts: 567
    • View Profile
(No subject)
« Reply #7 on: August 08, 2005, 03:35:50 am »
Quote
Now the problem that I am encountering is that I will have to use some code from \"Tic-Tac-Toe_Functions\" in \"Tic-tac-Toe_Turns_AI\". So how will the compiler look apon that?  


#Include The Functions.cpp before you #include the AI.cpp, and the AI.cpp will be able to use the code from functions.  Or, you could create a header file with the Declarations of the functions in Functions.cpp, and include that at the top.

Quote
Also, will I have to #include in \"Tic-Tac-Toe_Functions\" and \"Tic-tac-Toe_Turns_AI\"?


Uh, I dont think so, if you #include before you #include \"AI.cpp\", in the main.cpp file.

Cha0s

  • Veteran
  • *
  • Posts: 1860
    • View Profile
(No subject)
« Reply #8 on: August 08, 2005, 04:27:11 am »
You can think of #include like a big giant copy/paste that the compiler does. That\'s not actually what it does, but when conceptualizing, it helps. Just imagine that whatever you included was copied, line-by-line into the other file. This means that you need to link all your includes together in a big chain.

P.S. In regard to where to put the code (header or cpp file), the common practice is to make a cpp file with all the actual code and header file with the function declarations and comments describing their purpose, etc. However, if you\'re using templates, this doesn\'t work. Everything must go either in the cpp file or the header file in this case. In addition, random problems (yes, I mean literally random problems, since the few times it\'s happened, I\'ve seen no pattern) can arise from having them separate. Basically, try to keep them separate if you can, but if you get a lot of compile errors, you may be forced to put them together.

EDIT: As a recap on what ramlambmoo said, have AI include Functions and then Source include AI.
« Last Edit: August 08, 2005, 04:29:51 am by Cha0s »
Cha0s
Mac OS X Forum Moderator
In-Game Roleplay Forum Moderator
Please search and skim existing threads before posting!

RussianVodka

  • Hydlaa Notable
  • *
  • Posts: 689
    • View Profile
(No subject)
« Reply #9 on: August 08, 2005, 05:40:09 pm »
Well, yesturday I fixed most of the program. I threw the AI code into the Funcions.cpp file, rewriten the code that checks for a win, edited most of the functions to have them run on pointers (I used to thing pointers were useless... I\'ve proven myself wrong), and made a slightly nicer user interface (still using windows command prompt though).

Now all I have left is write a better AI for the computer, right now all it does is pick a random square, and then repic if it has been taken.


Edit:

A sorta unrelated question... I\'m wondering what the computer will perform faster, my curent code that checks for a win, or my old one?


Curent:
Code: [Select]

int checkForWin(char squares[NUMBER]){

    int i, xCount, oCount;

    for(i=0; i<7; i+=3){       // Checks horizantal rows.
        xCount = oCount = 0;
        for(int x=i; x<(i+3); x++){
//cout << \"\\nHorizantal check: \" << x << \"  xCount = \" << xCount << \" oCount = \" << oCount;
            if(squares[x]==\'x\') xCount++;
            if(squares[x]==\'o\') oCount++;
        }
        if(xCount==3) return 1;
        if(oCount==3) return 2;
    }

    for(i=0; i<3; i++){         // Checks vertical rows.
        xCount = oCount = 0;
        for(int x=i; x<(i+7); x+=3){
//cout << \"\\nVertical check: \" << x << \"  xCount = \" << xCount << \" oCount = \" << oCount;
            if(squares[x]==\'x\') xCount++;
            if(squares[x]==\'o\') oCount++;
        }
        if(xCount==3) return 1;
        if(oCount==3) return 2;
    }

    xCount = oCount = 0;
    for(i=0; i<9; i++){         // Checks for draw.
        if(squares[i]==\'x\') xCount++;
        if(squares[i]==\'o\') oCount++;
        if(xCount+oCount==9) return 3;
    }

    return 4;
}


Old: (I deleted the code, but it was somethin like this)
Code: [Select]


int checkForWin(char squares[NUMBER]){

  //Check \'x\'  
    if(((squares[0]==\'x\')&&(squares[1]==\'x\')&&(squares[2]==\'x\'))||
    ((squares[3]==\'x\')&&(squares[4]==\'x\')&&(squares[5]==\'x\'))||
   ((squares[6]==\'x\')&&(squares[7]==\'x\')&&(squares[0]==\'x\'))||
   ((squares[0]==\'x\')&&(squares[3]==\'x\')&&(squares[6]==\'x\'))||
   ((squares[1]==\'x\')&&(squares[4]==\'x\')&&(squares[7]==\'x\'))||
   ((squares[2]==\'x\')&&(squares[5]==\'x\')&&(squares[8]==\'x\')))
    return 1;

   //Check \'o\'
   if(((squares[0]==\'o\')&&(squares[1]==\'o\')&&(squares[2]==\'o\'))||
    ((squares[3]==\'o\')&&(squares[4]==\'o\')&&(squares[5]==\'o\'))||
   ((squares[6]==\'o\')&&(squares[7]==\'o\')&&(squares[0]==\'o\'))||
   ((squares[0]==\'o\')&&(squares[3]==\'o\')&&(squares[6]==\'o\'))||
   ((squares[1]==\'o\')&&(squares[4]==\'o\')&&(squares[7]==\'o\'))||
   ((squares[2]==\'o\')&&(squares[5]==\'o\')&&(squares[8]==\'o\')))
    return 2;

   //Check for draw
    if(((squares[0]!=\'x\')&&(squares[1]!=\'x\')&&(squares[2]!=\'x\'))&&
    ((squares[3]!=\'x\')&&(squares[4]!=\'x\')&&(squares[5]!=\'x\'))&&
   ((squares[6]!=\'x\')&&(squares[7]!=\'x\')&&(squares[0]!=\'x\'))&&
   ((squares[0]!=\'x\')&&(squares[3]!=\'x\')&&(squares[6]!=\'x\'))&&
   ((squares[1]!=\'x\')&&(squares[4]!=\'x\')&&(squares[7]!=\'x\'))&&
   ((squares[2]!=\'x\')&&(squares[5]!=\'x\')&&(squares[8]!=\'x\'))&&
   ((squares[0]!=\'o\')&&(squares[1]!=\'o\')&&(squares[2]!=\'o\'))&&
   ((squares[3]!=\'o\')&&(squares[4]!=\'o\')&&(squares[5]!=\'o\'))&&
   ((squares[6]!=\'o\')&&(squares[7]!=\'o\')&&(squares[0]!=\'o\'))&&
   ((squares[0]!=\'o\')&&(squares[3]!=\'o\')&&(squares[6]!=\'o\'))&&
   ((squares[1]!=\'o\')&&(squares[4]!=\'o\')&&(squares[7]!=\'o\'))&&
   ((squares[2]!=\'o\')&&(squares[5]!=\'o\')&&(squares[8]!=\'o\'))){
   int i=0;
    while(i<9){
     if((square[i]==\'x\')||(square[i]==\'o\'));
     else return 3;
      i++;
    }
   return 4;
}


With both pieces of code NUMBER == 9, and the return velues are as such:

return 1 == X wins.
return 2 == o wins.
return 3 == draw.
return 4 == game still commences.


Anyways... my question is would the computer process faster the unreasonably long if statements with less variables, or the decent sized for loops with many variables?
« Last Edit: August 08, 2005, 06:06:06 pm by RussianVodka »



Q: How many Planeshifters does it take to expalin a simple concept to a newb?
A: Six. Five to argue on who\'s explanation is right, and Moogie to lock the thread.

ramlambmoo

  • Hydlaa Notable
  • *
  • Posts: 567
    • View Profile
(No subject)
« Reply #10 on: August 08, 2005, 05:50:06 pm »
Quote
Now all I have left is write a better AI for the computer, right now all it does is pick a random square, and then repic if it has been taken.


Lol, thats some good AI...