HiveBrain v1.2.0
Get Started
← Back to all entries
patterncppModerate

Random number game

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
randomgamenumber

Problem

I have created a small 'game' which basically asks the user for the row number and column number and then picks a random number and if that number is above 11, out of 15, the user wins. When the user wins that position in the array is replaced with a X or O to indicate a win or loss.

The program works fine. As it is my first real program, I know that there is room for improvement.

I'm looking for constructive feedback on program efficiency, possible performance improvements and just better ways to code a similar program (at a basic level).

#include 
#include 
#include 
#include 
#include 

using namespace std;

void fill(char Array[10][10]);

    int xRan;
    int choicei = 12;
    int choicej = 12;

int main()
{
    char Array[10][10];

    srand(time(0));

    xRan = rand() % 15 + 1;

    while (choicei > 10 || choicej > 10)
    {
        cout > choicei;
        cout > choicej;
        cout  11)
    {
        cout  11)
    {
        for (int i = 0; i < 1; i++)
        {
            for (int j = 0; j < 10; j++)
            {
                Array[choicei - 1][choicej - 1] = 'X';
            }
        }
    }
    else
    {
        for (int i = 0; i < 1; i++)
        {
            for (int j = 0; j < 10; j++)
            {
                Array[choicei - 1][choicej - 1] = 'O';
            }   
        }
    }

}

Solution

-
Try not to use using namespace std.

-
` is a C library. Use for C++.

-
These shouldn't be global:

int xRan;
int choicei = 12;
int choicej = 12;


As you're using
xRan in another function, initialize it in main() only:

int xRan = rand() % 15 + 1;


and pass it to that function as an argument.

I also don't see why
choicei and choicej are initialized to 12. If there's a reason for that, there should be a comment specifying that. Since it looks needless, just declare them in main() closely in scope with the input:

cout > choicei;


cout > choicej;


On another note,
choicei and choicej are not good names. What are i and j? One would assume they have to do with simple loop counters, but this doesn't seem to be the case here. Consider something like rowNumberChoice and colNumberChoice.

-
I don't know how high your compiler warnings are turned up, but your
std::srand() may produce warnings corresponding to "possible loss of data." This is usually remedied by casting the std::time() return to an unsigned int.

If these warnings become present, call
std::srand() like this:

// if you're using C++11, use nullptr instead of NULL
std::srand(static_cast(std::time(NULL));


-
In C++, prefer to use standard containers over C-style arrays. You especially shouldn't pass one to a function as it would decay into a pointer. It's best to avoid this in C++.

I recommend using
std::array`, if you're using C++11. As 2D, it would be initialized like this:

std::array, 10>, 10> gameBoard;


As @nvuono mentioned, you could have row and column constants so that minimal updating is needed. You could then have this:

const unsigned int rows = 10;
const unsigned int cols = 12;

std::array, rows>, cols> gameBoard;


To pass this 2D array to functions:

// you may use a typedef to "rename" this type for less typing
// name should be capitalized as it is a type
typedef std::array, rows>, cols> Board;

// pass as const-ref as the board shouldn't be modified
void displayBoard(Board const& gameBoard) { }

Code Snippets

int xRan;
int choicei = 12;
int choicej = 12;
int xRan = rand() % 15 + 1;
cout << "Enter The Row Number Less Than 10!" << endl;
int choicei;    // declare here
cin >> choicei;
cout << "Enter The Column Number Less Than 10!" << endl;
int choicej;    // declare here
cin >> choicej;
// if you're using C++11, use nullptr instead of NULL
std::srand(static_cast<unsigned int>(std::time(NULL));

Context

StackExchange Code Review Q#38752, answer score: 10

Revisions (0)

No revisions yet.