• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Christopher James Jepson

Looking for ways to improve code...

8 posts in this topic

//*******************************************************************
// File:   CIT245_CJepson_Assignment_2a.cpp
// Author: cjepson
// Created on February 7, 2013, 6:49 PM
// Language: C++
// IDE: Netbeans IDE 7.2.1
// Compiler: /usr/include/c++/4.7/x86_64-linux-gnu
// Class: CIT245-BIN1 Data Structures and Programming C++
// Assignment: Assignment_2a
//*******************************************************************

#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <cmath>

using namespace std;

// Used for system() function, to allow easy and bug free translation between OS.
#define WINDOWS 0
#define LINUX 1
#define SYSTEMOUTPUT WINDOWS //WINDOWS = cmd: pause / LINUX = cmd: sleep 1

int main() {
    // Console commands between Linux/Windows differ, pause will not work on Linux
    // This code identifies which OS is current in use, executes appropriate console command.
#if SYSTEMOUTPUT == WINDOWS
    int sysOut = 0;
#define CHOSEN WINDOWS
#else
    int sysOut = 1;
#define CHOSEN LINUX
#endif
    // *** End of C++ Preprocessor Directives ***  

    int y = 0;
    bool x = false;
    //*** DECLARE VARIABLES HERE ***
    int userInput;
    int randResults;
    int sum[12] = {0, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
    int sumRolled[12] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ,0};
    int odds[12] = {10, 1, 2, 3, 4, 5, 6, 5, 4, 3, 2 ,1};
    int digits = 0;
    double errors[12] = {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0};
    srand(12);
    //*** END OF VARIABLE DECLARATION ***
    
    do {
        //*** ENTER CODE HERE ***

        // Recieve user input
        cout << "Enter an integer value: ";
        cin >> userInput;
        cout << endl;        
        
        // Generate numbers user input number of times
        for(int count = 0, cnt = 0; count <= userInput; count++, cnt++){
            randResults = (rand( ) % 6 ) + (rand( ) % 6) + 1;
            sumRolled[randResults]++;
          }
        
        // Acquire number of integers user inputed (e.g., 248 = 3).
        sum[0] = userInput;
        while (sum[0] != 0) { 
            sum[0] /= 10; 
            digits++; 
        }

        // Recreate number based on integers keyed in.
        // *** Critical for calculating the odds length.
        sum[0] = 
        (digits == 1 ? 1 :
        (digits == 2 ? 1 :
        (digits == 3 ? 10 :
        (digits == 4 ? 100 :
        (digits == 5 ? 1000 :
        (digits == 6 ? 10000 :
        100000))))));

        // Text output and formatting   
        cout << "Sum\t  #Rolled\tOdds\t%Error" << endl;
        cout.precision(4); // Floating point precision
        cout.setf(ios::fixed,ios::floatfield); // Aligns decimal point
        
        for(int count = 1; count < 12; count++){
            cout << setw(2) << right << sum[count] << ":\t";
            cout << setw(9) << right << sumRolled[count] << setw(11) << right 
                    << odds[count] * sum[0] << setw(11) << right 
                    << abs(((float)sumRolled[count] / (float)(odds[count] * sum[0]) * 100) - 100)
                    << endl;
        }

        // Resetting variable values for loop
        userInput = 0;
        randResults = 0;
        digits = 0;
        sum[0] = 0;
        
        for(int count = 0; count < 12; count++){
            sumRolled[count] = 0;
            errors[count] = 0.0;
        }

        
        //*** END CODE HERE ***
        do {
            cout << "Do you wish to continue? (1 = yes / 0 = no): ";
            cin >> y;
            if (y == 0) {
                x = true;
            } else if (y > 1) {
                cout << "Please try again!\n";
            }
        } while (y > 1);
    } while (x == false);

    // Requires the C++ Preprocessor Directives
    if (sysOut == 0) {
        system("pause");
    } else if (sysOut == 1) {
        system("sleep 1");
    }
    return 0;
} // End of main()


Since I haven't been accepted to any hobbiest projects yet, I figured I'll learn something new here while I wait for someone to accept me.  Anyone want to hit this code for ways of improving performance (w/o elliminating features & functionality)?  The code is in C++.

0

Share this post


Link to post
Share on other sites

The application is IO bound; it spends most of it's time printing. So you can't really improve performance in a meaningful way.

1

Share this post


Link to post
Share on other sites

Just for the sake of being nit picky I would use precompiler directives to separate the wait so that your not wasting a comparison tick every time you want to wait.

 

#ifdef WIN32

    system("pause");

#else

    system("sleep 1");

#endif

 

Using this method the compiler will only include the correct call for the correct system in which it is compiled on.  In real world applications Windows programs are packaged as compiled executable's 99% of the time and Linux is always in source form.  Making this calculation once at compile time vs once per execution (or more if you where to use this again somewhere) doesn't really save much of anything it just gets you in the habit of what I consider a better practice.

 

::Edit::

   I used #ifdef WIN32 because Microsoft's Visual Studio normally defines this for you by default when you have the sub system set to windows.  It could be defined as something different or you can use your own #define WINMODE or whatever you wish, just be sure that the compiler or the source have the correct platform defined for the build you are running.

Edited by Dan Mayor
2

Share this post


Link to post
Share on other sites

Just for the sake of being nit picky I would use precompiler directives to separate the wait so that your not wasting a comparison tick every time you want to wait.

 

#ifdef WIN32

    system("pause");

#else

    system("sleep 1");

#endif

 

Using this method the compiler will only include the correct call for the correct system in which it is compiled on.  In real world applications Windows programs are packaged as compiled executable's 99% of the time and Linux is always in source form.  Making this calculation once at compile time vs once per execution (or more if you where to use this again somewhere) doesn't really save much of anything it just gets you in the habit of what I consider a better practice.

 

::Edit::

   I used #ifdef WIN32 because Microsoft's Visual Studio normally defines this for you by default when you have the sub system set to windows.  It could be defined as something different or you can use your own #define WINMODE or whatever you wish, just be sure that the compiler or the source have the correct platform defined for the build you are running.

This is awesome!  I've been looking for a way to automate this.  After Googling, I did see some stuff on it but couldn't get it to work.  Thanks a bunch! laugh.png

0

Share this post


Link to post
Share on other sites

One idea is to not fork a new process just to wait for input from a user. Simply using std::cin.get() should suffice:

 

 
void wait() {
    cout << "Press return key to continue...";
    cin.get();
}

Bonus: this code is completely cross platform, and actually does the same thing on all platforms (in contrast, pause and sleep 1 do not do the same thing).

 

There are lots of non-performance related improvements. Some of your variable names are very poor, "x" and "y" in particular. Your use of the first element of the "sum" array as two unrelated temporary values is very confusing.

 

Overall, the intent behind the program is not really shining through the code. It is difficult to assess it's correctness without knowing what is supposed to be happening.

Never thought about that!  Thanks a bunch!  You do have some valid points and I can definately concor with your concerns. Two things learned from such a simple code post.  This is bloodly awesome.  biggrin.png   I'm doing this more often. hahaha!

Edited by Subtle_Wonders
1

Share this post


Link to post
Share on other sites

On VS you can put breakpoint on last return:

return 0;

 

instead

system("pause");

or

cout << "Press return key to continue...";
    cin.get();
0

Share this post


Link to post
Share on other sites
I see two bugs:

1. Lines 57 and 58 do not agree.
Either the comment is wrong, or the for-loop is wrong. If I enter 2, for example, the loop runs with values of 0, then 1, then 2, for 'count'. That's three iterations. A typical correct loop uses less-than within the comparison.

2. Lines 65 to 68 are incorrect. The value "0" is one digit!
In the past I've found that the best way to count digits, is to use a do..while loop instead. Edited by iMalc
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0