Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  

Poll

What programming topic would you want the next challenge to be about?  (It might be a good opportunity to focus on a subject you're not familiar with or to reinforce knowledge on one that you already know)

Control Flow
- 2 (2.2%)
Arrays, Strings, Pointers, and References
- 8 (9%)
Functions
- 4 (4.5%)
Basic object-oriented programming
- 30 (33.7%)
A bit more advanced OOP (Composition, Operator overloading, Inheritance, Virtual Functions)
- 18 (20.2%)
Templates
- 8 (9%)
Other (Explain)
- 4 (4.5%)
Working with files?  (Streams)
- 15 (16.9%)

Total Members Voted: 89


Pages: 1 ... 24 25 [26] 27 28 ... 78

Author Topic: Programming Challenges & Resources (#bay12prog) Initiative  (Read 95921 times)

lordnincompoop

  • Bay Watcher
  • Allusionist
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #375 on: September 15, 2010, 09:36:45 am »

I don't find them all too hard, but I'm having difficulty seeing a use for them.
Logged

alway

  • Bay Watcher
  • 🏳️‍⚧️
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #376 on: September 15, 2010, 10:21:26 am »

I don't find them all too hard, but I'm having difficulty seeing a use for them.
As I said before, its for readability. Definitely not useful in every case, or even most cases, but it will come in handy when you have a large number of integer used for identifying states or some other attribute. You could use #define's, but they have the disadvantage of being (to my knowledge) global, whereas enums can be defined in a class or function and have them act like any other local or class variables. And defines are also longer to write out.
Consider the following 3 codes which should do pretty much the same thing:

Enum code:
Code: [Select]
class myClass
{
public:
     function();
private:
     enum classStates {UnInitialized, Initializing, Initialized, Running, Ending, Done};
     int state;
};

void myClass::function()
{
    switch(state)
    {
     case UnInitialized: {
           DoInitializingStuff();
     } break;
     case Initializing: {
           WaitForCompletionOfInit();
     } break;
     case Initialized: {
           Run();
     } break;
...ect
}

#define code
Code: [Select]
#define UnInitialized = 0;
#define Initializing = 1;
#define Initialized = 2;
#define Running = 3;
#define Ending = 4;
#define Done = 5;
class myClass
{
public:
     function();
private:
     int state;
};

void myClass::function()
{
    switch(state)
    {
     case UnInitialized: {
           DoInitializingStuff();
     } break;
     case Initializing: {
           WaitForCompletionOfInit();
     } break;
     case Initialized: {
           Run();
     } break;
...ect
}

pure numbers code
Code: [Select]
class myClass
{
public:
     function();
private:
     int state;
};

void myClass::function()
{
    switch(state)
    {
     case 0: {
           DoInitializingStuff();
     } break;
     case 1: {
           WaitForCompletionOfInit();
     } break;
     case 2: {
           Run();
     } break;
...ect
}

In the enum code, the cases are clear as to when they occur, and the enum is local to the class so none of the enumerated state names will mess with code written in the future. The define code is clear, but includes a somewhat lengthy bunch of variables to be #define'd and the values will be global and thus have the ability to possibly mess with other code. The pure numbers approach doesn't have the scope issues of #define, but also makes it a real pain to figure out what each number means if you either didn't write the code or wrote it long ago enough to forget the minute details of what state corresponds to what int.
« Last Edit: September 15, 2010, 10:23:15 am by alway »
Logged

lordnincompoop

  • Bay Watcher
  • Allusionist
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #377 on: September 15, 2010, 10:27:55 am »

Thanks, but I meant bitshifts. :P
Logged

alway

  • Bay Watcher
  • 🏳️‍⚧️
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #378 on: September 15, 2010, 11:38:40 am »

Ah. Same as enums then, but in addition to the handy things plain old enums do, you can store multiple states in the same variable. So if you were making a class for something like a window, you might have states indicating the various non-mutually exclusive aspects of said window. For example, a window for which you want to use your pre-defined title bar, pre-defined status bar, and pre-defined x button in the corner you might have something like: windowState = TITLE | STATUS | EXITBUTTON; thus avoiding the need for half a dozen or more state variables corresponding to various pre-defined parts. Which is useful in various ways, not the least of which is allowing you to shorten function parameters. For example: "SetWindowState(states);" as opposed to "SetWindowState(state1, state2, state3, state4, state5, state6, state7);"

In my code, it seems to come up somewhat often when I'm using DirectX (setting device states) and Win32 (various window states).
« Last Edit: September 15, 2010, 11:42:17 am by alway »
Logged

qwertyuiopas

  • Bay Watcher
  • Photoshop is for elves who cannot use MSPaint.
    • View Profile
    • uristqwerty.ca, my current (barren) site.
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #379 on: September 15, 2010, 11:50:17 am »

Using bits has the added advantage that you can have unused bits, and define them at a later date without breaking any existing programs(when used as a library or API), and doesn't require any modification. It's a similar argument as  #define MAP_SIZE 5 so that you can change it with only a recompile.
Logged
Eh?
Eh!

eerr

  • Bay Watcher
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #380 on: September 17, 2010, 02:51:49 pm »

Eh I'm not really sure.
« Last Edit: September 17, 2010, 03:08:53 pm by eerr »
Logged

lordnincompoop

  • Bay Watcher
  • Allusionist
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #381 on: September 19, 2010, 04:10:52 am »

Okay, what am I doing wrong? The tutorial skips straight ahead to the welcome message without waiting for input.

Code: [Select]
int GameTutorial()
{
    using namespace std;
    cout << "Welcome to [REDACTED], a steadily growing text adventure game!" << endl;

    return 0;
}

int GameTutorialStart()
{
    using namespace std;
    cout << "Do you want to go through the tutorial? [y/n]" << endl;
    char x;
    cin >> x;
    if (x == 'y' || 'Y')
    {
        GameTutorial();
        return 1;
    }
    else
    {
        return 0;
    }
    return 0;
}

int main()
{
    //IntroScreen();
    std::cout << "Enter anything to begin." << std::endl;
    int anything;
    std::cin >> anything;
    GameTutorialStart();
    return 0;
}
Logged

Nintenlord

  • Bay Watcher
  • Adamantine fever
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #382 on: September 19, 2010, 06:56:03 am »

It works with Visual Studio 2010. My guess would be that your compiler does something odd with the if statement, since it always produces true as the result. x == 'y' || 'Y' is the same as (x == 'y') || 'Y', meaning you always get non-zero, which is the same as true. What you want is probably (x == 'y') || (x == 'Y') or something similar. Also, why is 'anything' an int?

Edit: anything, not everything.
« Last Edit: September 19, 2010, 07:14:11 am by Nintenlord »
Logged
The only dragon I've seen in game walked into my rats nest of a fortress and died in the flames of the conflagration he started in the dining hall.  Of course, nearly every dwarf was dead by then, but we consider it a tactical victory.

lordnincompoop

  • Bay Watcher
  • Allusionist
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #383 on: September 19, 2010, 09:17:24 am »

I'm using VS10. And thanks, I'll try that.
Logged

ed boy

  • Bay Watcher
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #384 on: September 20, 2010, 12:06:30 pm »

I'm having a bit of a problem with HTML/PHP, in a game I'm working on. What I currently have is this:
Spoiler: image (click to show/hide)
However, those gaps underneath the buttons should not be there. I know it comes from the presence of the buttons, as when I remove them the table cells return to their proper size. Several hours of googling have not helped, so can anybody here help?
Logged

qwertyuiopas

  • Bay Watcher
  • Photoshop is for elves who cannot use MSPaint.
    • View Profile
    • uristqwerty.ca, my current (barren) site.
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #385 on: September 20, 2010, 12:25:47 pm »

I don't know from just an image, but look for extra <br>s, check for accidental CSS affecting them. It's a table, so the problem could be with one button in each row, and it expands the entire row as a result.


Edit: < br > without the spaces or anything else is accepted as a line break by the forum.
Logged
Eh?
Eh!

ed boy

  • Bay Watcher
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #386 on: September 20, 2010, 12:39:48 pm »

There aren't any line breaks in there. Here is a copy of the code:

Code: (entire table) [Select]
<table style="border-collapse: collapse; background-color: #000000" border="2" bordercolor="#0000ff" cellpadding="0" cellspacing="0">
<tbody>
<tr height="150px" valign="top">
<td width="150px">
<form method="POST" action="<?php echo $_SERVER['PHP_SELF'?>">
  <input type="hidden" name="DESTINATION" value="map">
  <input type="hidden" name="DIRECTION" value="NW">
  <input type="hidden" name="Xpos" value="<?php echo $Xpos?>">
  <input type="hidden" name="Ypos" value="<?php echo $Ypos?>">
  <input type="hidden" name="Zpos" value="<?php echo $Zpos?>">
  <input type="submit" value="<?php
echo "Type: ";
echo  
$NWtype;
echo 
"&#38;#10;People: ";
echo 
mysql_num_rows(mysql_query("select * from characters where inround = true and Xpos = ".($Xpos-1)." and Ypos = ".($Ypos+1).";"));
  
?>
" name="Submit"  style="width:150;height:150;color: #0000FF;  background-color: #<?php echo backgroundcolour($NWtype); ?>">
  </form></td>
<td width="150px">
<form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>">
  <input type="hidden" name="DESTINATION" value="map">
  <input type="hidden" name="DIRECTION" value="N">
  <input type="hidden" name="Xpos" value="<?php echo $Xpos?>">
  <input type="hidden" name="Ypos" value="<?php echo $Ypos?>">
  <input type="hidden" name="Zpos" value="<?php echo $Zpos?>">
  <input type="submit" value="<?php
echo "Type: ";
echo  
$Ntype;
echo 
"&#38;#10;People: ";
echo 
mysql_num_rows(mysql_query("select * from characters where inround = true and Xpos = ".($Xpos)." and Ypos = ".($Ypos+1).";"));
  
?>
" name="Submit"  style="width:150;height:150;color: #0000FF;  background-color: #<?php echo backgroundcolour($Ntype); ?>">
  </form></td>
<td width="150px">
<form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>">
  <input type="hidden" name="DESTINATION" value="map">
  <input type="hidden" name="DIRECTION" value="NE">
  <input type="hidden" name="Xpos" value="<?php echo $Xpos?>">
  <input type="hidden" name="Ypos" value="<?php echo $Ypos?>">
  <input type="hidden" name="Zpos" value="<?php echo $Zpos?>">
  <input type="submit" value="<?php
echo "Type: ";
echo 
$NEtype;
echo 
"&#38;#10;People: ";
echo 
mysql_num_rows(mysql_query("select * from characters where inround = true and Xpos = ".($Xpos+1)." and Ypos = ".($Ypos+1).";"));
  
?>
" name="Submit" style="width:150;height:150;color: #0000FF;  background-color: #<?php echo backgroundcolour($NEtype); ?>">
  </form></td>
<td width="300">
<?php
echo "Name: ";
echo 
mysql_result(mysql_query("select charname from characters where ID = ".$_SESSION['CID'].";"),0);
echo 
"<br>Xpos: ";
echo 
$Xpos;
echo 
"<br>Ypos: ";
echo 
$Ypos;
echo 
"<br>Elevation: ";
echo 
$BBp+$Bbp+$Bap+$B0p+$BAp+$bBp+$bbp+$bap+$b0p+$bAp+$aBp+$abp+$aap+$a0p+$aAp+$_Bp+$_bp+$_ap+$_0p+$_Ap+$ABp+$Abp+$Aap+$A0p+$AAp;
echo 
"<br>Water level: ";
echo 
$BBw+$Bbw+$Baw+$B0w+$BAw+$bBw+$bbw+$baw+$b0w+$bAw+$aBw+$abw+$aaw+$a0w+$aAw+$_Bw+$_bw+$_aw+$_0w+$_Aw+$ABw+$Abw+$Aaw+$A0w+$AAw;
echo 
"<br>Drainage: ";
echo 
$BBd+$Bbd+$Bad+$B0d+$BAd+$bBd+$bbd+$bad+$b0d+$bAd+$aBd+$abd+$aad+$a0d+$aAd+$_Bd+$_bd+$_ad+$_0d+$_Ad+$ABd+$Abd+$Aad+$A0d+$AAd;
?>

</td>
<td rowspan="3" width="300">ITEMS</td></tr>
<tr height="150px" valign="top">
<td>
<form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>">
  <input type="hidden" name="DESTINATION" value="map">
  <input type="hidden" name="DIRECTION" value="W">
  <input type="hidden" name="Xpos" value="<?php echo $Xpos?>">
  <input type="hidden" name="Ypos" value="<?php echo $Ypos?>">
  <input type="hidden" name="Zpos" value="<?php echo $Zpos?>">
  <input type="submit" value="<?php
echo "Type: ";
echo  
$Wtype;
echo 
"&#38;#10;People: ";
echo 
mysql_num_rows(mysql_query("select * from characters where inround = true and Xpos = ".($Xpos-1)." and Ypos = ".($Ypos).";"));
  
?>
" name="Submit"  style="width:150;height:150;color: #0000FF;  background-color: #<?php echo backgroundcolour($Wtype); ?>">
  </form></td>
<td>
<form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>">
  <input type="hidden" name="DESTINATION" value="map">
  <input type="hidden" name="DIRECTION" value="CENTER">
  <input type="submit" value="<?php
echo "Type: ";
echo  
$Ctype;
echo 
"&#38;#10;People: ";
echo 
mysql_num_rows(mysql_query("select * from characters where inround = true and Xpos = ".($Xpos)." and Ypos = ".($Ypos)." and PID <> ".$_SESSION['ID'].";"));
  
?>
" name="Submit"  style="width:150;height:150;color: #0000FF;  background-color: #<?php echo backgroundcolour($Ctype); ?>">
  </form></td>
<td>
<form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>">
  <input type="hidden" name="DESTINATION" value="map">
  <input type="hidden" name="DIRECTION" value="E">
  <input type="hidden" name="Xpos" value="<?php echo $Xpos?>">
  <input type="hidden" name="Ypos" value="<?php echo $Ypos?>">
  <input type="hidden" name="Zpos" value="<?php echo $Zpos?>">
  <input type="submit" value="<?php
echo "Type: ";
echo  
$Etype;
echo 
"&#38;#10;People: ";
echo 
mysql_num_rows(mysql_query("select * from characters where inround = true and Xpos = ".($Xpos+1)." and Ypos = ".($Ypos).";"));
  
?>
" name="Submit"  style="width:150;height:150;color: #0000FF;  background-color: #<?php echo backgroundcolour($Etype); ?>">
  </form></td>
<td rowspan="2">
<?php $chars=mysql_query("select charname from characters where inround = true and Xpos = ".($Xpos)." and Ypos = ".($Ypos)." and PID <> ".$_SESSION['ID'].";");
$numchars=mysql_num_rows($chars);
if (
$numchars==0){echo "There are no other people here<br>";}
if (
$numchars==1){echo "There is only one person here: <br>"?>

<form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>" style="position: relative; float: left">
<p><input type="submit" value="<?php echo mysql_result($chars,0); ?>" name="BUTTON" style="color: #0000FF; border: 3px double #FFFFFF; background-color: #000000"></p>
<input type="hidden" name="DESTINATION" value="character">
<input type="hidden" name="CHARNAME" value="<?php echo mysql_result($chars,0); ?>">
</form><?php }
if (
$numchars>=2){echo "There are $numchars people here: <br>";
for(
$i=0$i<=($numchars-1); $i++){ ?>

<form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>" style="position: relative; float: left">
<p><input type="submit" value="<?php echo mysql_result($chars,$i); ?>" name="BUTTON" style="color: #0000FF; border: 3px double #FFFFFF; background-color: #000000"></p>
<input type="hidden" name="DESTINATION" value="character">
<input type="hidden" name="CHARNAME" value="<?php echo mysql_result($chars,0); ?>">
</form><?php
}} ?>

</td></tr>
<tr height="150px" valign="top">
<td>
<form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>">
  <input type="hidden" name="DESTINATION" value="map">
  <input type="hidden" name="DIRECTION" value="SW">
  <input type="hidden" name="Xpos" value="<?php echo $Xpos?>">
  <input type="hidden" name="Ypos" value="<?php echo $Ypos?>">
  <input type="hidden" name="Zpos" value="<?php echo $Zpos?>">
  <input type="submit" value="<?php
echo "Type: ";
echo  
$SWtype;
echo 
"&#38;#10;People: ";
echo 
mysql_num_rows(mysql_query("select * from characters where inround = true and Xpos = ".($Xpos-1)." and Ypos = ".($Ypos-1).";"));
  
?>
" name="Submit"  style="width:150;height:150;color: #0000FF;  background-color: #<?php echo backgroundcolour($SWtype); ?>">
   </form></td>
<td>
<form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>">
  <input type="hidden" name="DESTINATION" value="map">
  <input type="hidden" name="DIRECTION" value="S">
  <input type="hidden" name="Xpos" value="<?php echo $Xpos?>">
  <input type="hidden" name="Ypos" value="<?php echo $Ypos?>">
  <input type="hidden" name="Zpos" value="<?php echo $Zpos?>">
  <input type="submit" value="<?php
echo "Type: ";
echo  
$Stype;
echo 
"&#38;#10;People: ";
echo 
mysql_num_rows(mysql_query("select * from characters where inround = true and Xpos = ".($Xpos)." and Ypos = ".($Ypos-1).";"));
  
?>
" name="Submit"  style="width:150;height:150;color: #0000FF;  background-color: #<?php echo backgroundcolour($Stype); ?>">
  </form></td>
  <td>
<p>
<form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>">
  <input type="hidden" name="DESTINATION" value="map">
  <input type="hidden" name="DIRECTION" value="SE">
  <input type="hidden" name="Xpos" value="<?php echo $Xpos?>">
  <input type="hidden" name="Ypos" value="<?php echo $Ypos?>">
  <input type="hidden" name="Zpos" value="<?php echo $Zpos?>">
  <input type="submit" value="<?php
echo "Type: ";
echo  
$SEtype;
echo 
"&#38;#10;People: ";
echo 
mysql_num_rows(mysql_query("select * from characters where inround = true and Xpos = ".($Xpos+1)." and Ypos = ".($Ypos-1).";"));
  
?>
" name="Submit"  style="width:150;height:150;color: #0000FF;  background-color: #<?php echo backgroundcolour($Stype); ?>">
  </form></td></tr></tbody></table>

Code: (one cell) [Select]
  <td>
<p>
<form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>">
  <input type="hidden" name="DESTINATION" value="map">
  <input type="hidden" name="DIRECTION" value="SE">
  <input type="hidden" name="Xpos" value="<?php echo $Xpos?>">
  <input type="hidden" name="Ypos" value="<?php echo $Ypos?>">
  <input type="hidden" name="Zpos" value="<?php echo $Zpos?>">
  <input type="submit" value="<?php
echo "Type: ";
echo  
$SEtype;
echo 
"&#38;#10;People: ";
echo 
mysql_num_rows(mysql_query("select * from characters where inround = true and Xpos = ".($Xpos+1)." and Ypos = ".($Ypos-1).";"));
  
?>
" name="Submit"  style="width:150;height:150;color: #0000FF;  background-color: #<?php echo backgroundcolour($Stype); ?>">
  </form></td>
Logged

qwertyuiopas

  • Bay Watcher
  • Photoshop is for elves who cannot use MSPaint.
    • View Profile
    • uristqwerty.ca, my current (barren) site.
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #387 on: September 20, 2010, 01:08:43 pm »

A quick mockup shows that the problem is the form tags.

Code: [Select]
<table style="border-collapse: collapse; background-color: #000000" border="2" bordercolor="#0000ff" cellpadding="0" cellspacing="0">
 <tr height="150px" valign="top">
  <td width="150px">
   <!--<form>-->
    <input type="submit" value="test" style="width:150;height:150;color:#0000FF;background-color:#00FF00"/>
   <!--</form>-->
  </td>
  <td>
   <input type="submit" value="test" style="width:150;height:150;color:#0000FF;background-color:#00FF00"/>
  </td>
  <td>
   <input type="submit" value="test" style="width:150;height:150;color:#0000FF;background-color:#00FF00"/>
  </td>
 </tr>
 <tr>
  <td>
   <input type="submit" value="test" style="width:150;height:150;color:#0000FF;background-color:#00FF00"/>
  </td>
  <td>
   <input type="submit" value="test" style="width:150;height:150;color:#0000FF;background-color:#00FF00"/>
  </td>
  <td>
   <input type="submit" value="test" style="width:150;height:150;color:#0000FF;background-color:#00FF00"/>
  </td>
 </tr>


</table>

Try with and without them.

I'm going to see if it is a specific part of the default style that can be fixed.

Edit: It's not a style problem, as far as I can tell. I think that a form just forces a line break or two.

Edit again:
Google found http://www.velocityreviews.com/forums/t163792-prevent-line-break-after-form.html

Adding a style of "display: inline;" to the form fixes it.
« Last Edit: September 20, 2010, 01:18:26 pm by qwertyuiopas »
Logged
Eh?
Eh!

ed boy

  • Bay Watcher
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #388 on: September 20, 2010, 01:17:11 pm »

Many thanks. I had not a clue that the form tags were responsible. Once you found that, I quickly found the solution was to add:
Code: [Select]
style="margin-bottom:0; margin-top:0;"to the <form> tags.
Logged

alway

  • Bay Watcher
  • 🏳️‍⚧️
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #389 on: October 05, 2010, 08:20:34 pm »

How can I compare types in C++? Or to be more specific, how can I do the same thing the keyword 'is' does in C#?
Nvm, more or less figured it out.
« Last Edit: October 05, 2010, 10:24:51 pm by alway »
Logged
Pages: 1 ... 24 25 [26] 27 28 ... 78