Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++: Based on these coding guidelines, can someone check/add if necessary to my

ID: 3762692 • Letter: C

Question

C++: Based on these coding guidelines, can someone check/add if necessary to my code, thanks.

This is the code here, at the bottom are the guidelines.

Nim.h

#include <iostream>
#include <fstream>
#include <string>
#include <time.h>

using namespace std;

class Nim
{
public:

   static const int KSIZE = 8;
   int m_rows[KSIZE];
   int m_row_cnt;
   int userPins;
   int brainPins;
   //public: // for rel
   void setup();
   void hello();
   void listen();
   void conversation();
   void show_game_state();
   void show_move(int rme, int rrow, int rpins);
   bool is_win();
};

Nim.cpp

#include <iostream>
#include <fstream>
#include "Nim.h"

void Nim::show_game_state()
{
   for (int ix = 0; ix < m_row_cnt; ++ix)
   {
       cout << ix << " : ";
       for (int iy = 0; iy < m_rows[ix]; ++iy)
       {
           cout << "|";
       }
       cout << endl;
   }
}
// the user picks what row(heap) and how many objects to remove.
void Nim::show_move(int rme, int rrow, int rpins)
{
   cout << (rme ? "I " : "You") << " Take " << rpins << " from row " << rrow << endl;
}
//Setup the game
void Nim::setup()
{
   userPins = 0;
   brainPins = 0;
   srand (time(NULL));
   m_row_cnt = rand() % 3 + 3;
   for(int i =0 ; i < m_row_cnt ; i++)
   {
       m_rows[i] = rand() % 6 + 3;
   }
}

void Nim::conversation()
{
   int rme = 0;
   int row, pins;
   setup();
   //Show initital Game state
   cout<<"Current Game State : "<<endl;
   show_game_state();
   while(true)
   {

       if(rme == 0){
           cout<<"Enter the row number and number of pins to remove: ";
           cin>>row>>pins;
           if(m_row_cnt-1 < row)
           {
               cout<<"Please enter valid row number";
              
           }
           if(m_rows[row] <= 0)
           {
               cout<<"At least one pin must be taken";
              
           }

           if(m_rows[row] < pins)
               pins = m_rows[row];

           show_move(rme,row,pins);
           userPins ++;
       }
       else{
           row = rand() % m_row_cnt;
           if(m_rows[row] == 0)
              
           pins = rand() % m_rows[row] + 1;
           show_move(rme,row,pins);
           brainPins++;
       }
       m_rows[row] = m_rows[row]-pins;
       rme = 1-rme;
      
       if(is_win())
           break;
       cout<<"Current Game State : "<<endl;
       show_game_state();
   }
   if(userPins > brainPins)
       cout<<"You Won !";
   else if(userPins < brainPins)
       cout<<"I Won !";
   else
       cout<<"Match Tie";
}

bool Nim::is_win()
{
   int cnt = 0;
   for (int ix = 0; ix < m_row_cnt; ++ix)
   {   
       cnt += m_rows[ix];
   }
   if(cnt == 0)
       return true;
   else
       return false;
}

Nim2.cpp

#include "Nim.h"

void main()
{
   Nim obj;
   obj.conversation();

}

Pick Short Meaningful Names for Functions They should look mostly like verb_object( ):

1. Describe what the function does in one sentence, without the word "and". This makes it a simple sentence. Use Imperative sentence style. Imperative means command. Don't use the verb words "be", "is" or "are", unless the function is a test for true or false. Example: "Tell the user about his/her progress toward guessing the secret."

2. Put that sentence as the first comment before the function stub definition. Example: // Tell the user about his/her progress toward guessing the secret. void <name> ( ) { } 3. Find the important word(s) in the direct object phrase of the sentence. Example: "progress"

4. Combine the sentence's verb and important word(s) as the function name. Example: tell_progress

5. Create a stub definition for the function under it's sentence comment. Example: // Tell the user about his/her progress toward guessing the secret. void tell_progress( ) { DOUT << " tell_progress." ENDLDOUT; } Function Name Practice // check for an empty board void check_board_empty( )... // Display row ix void display_row( int rix )... // Get a random # of pins int get_rand_pins( )... // Like rand( ). // Get a random non-empty row int get_rand_good_row( )...

// Is the row empty? int is_row_empty( int rrow )... Note: A function that tests whether something is true or false should start with "is_".

Variable Names

1. Every variable name is 2 or more characters long, and start with a lower-case letter.

2. Every local variable used across more than 4 lines gets a nice meaningful name. For less than that, you can use names like "ax". An exception for a loop variable

3. Promote a local variable to be global if it is used in more than one function. A global variable name must begin with "g_". If a global is only used in one function, demote it to be a local.

4. If a global (or several globals) is used by three or more functions, strongly consider making those functions the methods of a class and those globals as variables/slots in that class. A class's variable/slot name must begin with "m_". Note, the point of a class is to encapsulate the "globals" used by a group of functions. This encapsulation is at the heart of Object-Oriented Programming (OOP).

5. An array variable must begin with "a".

Constant Names

1. A constant name must begin with "K", and be in all capital letters.

Class Names

1. A class name must begin with a capital letter, and must not be in all capital letters.

Conditional Tests 1. Always place a literal constant on the left-hand side of a relational operator (==, !=, <, <=, >, >=). Example: if (0 == foo) Example: while (0 < foo) Never: if (foo == 0) // Causes too much time debugging.

Loops

1. Always use a FOR loop when the end is known (as in "walking" an array).

2. Don't play with your loop index in more than one place. It can cause skipped items, doublecounting, and infinite loops.

Switches

1. Always use a switch when checking for several int or char values.

2. Always Break your cases. You may use an exception if several values share the exact same body of code (e.g., checking a for char in both lower and upper case ).

Fatal Operations

1. Always DOUT a divisor and a modulus. Dividing by zero crashes.

2. Always initialize your sum (accumulator) just outside your loop, not remotely.

3. Always do complicated expressions one step at a time into "intermediate" use-once variables, until you thoroughly understand the steps. And DOUT each step's variable value.

Comments Recall every object method is a function.

1. Every active line, except a cout line, should have a comment telling what is going on in simple language. Either put the comment at the end of the active line if there is enough room, or insert it above the active line.

Examples   // Convert the User's suit+rank guess to a card num.   g_guess_card = cvt_card( g_user_suit, g_user_rank );   tell_progress( delta, g_prev_delta ); // Tell user warmer/colder/win.   respond( ); // Respond to user's input.   g_win = 1; // Flag win for endchk.   srand( (unsigned int)(time( 0 ))); // Get new set of random numbers.   g_prev_delta = delta; // Update previous delta before next guess.   int retval = rrank - 1; // Cvt rank: 2..14 --> 1..13.

2. Every global variable, and also every class slot, should have a comment describing its purpose. Note that this should be mildly obvious from the variable name, but using a comment phrase helps elaborate the meaning.

Examples int g_give_help = 1; // Whether to give user help for input. int g_guess_card; // User's card num. int g_guess_cnt = 0; // How many guesses has the user taken? int g_prev_delta = 1000; // Delta from user's prev guess. Default, big. int g_quit = 0; // Whether the user wants to quit early. int g_secret_card; // Secret card num. int g_user_rank; // User's card rank guess.

3. Every function should have it's one-line sentence just about the function header, to introduce the function. Example // Check that the card suit and rank are legal. //... void Card_guess::check_suit_rank( char rsuit, int rrank )

4. Every function should have a line(s) indicating which global variables it accesses, labeled "Globals:" or "Touches:". Example // Touches: g_quit, g_win

5. Every method should have a line(s) indicating which object variables it accesses, labeled "Touches:". Example // Touches: m_user_suit, m_user_rank, m_guess_cnt 6. Every function should indicate its active line count. Example // Line count = 3

Explanation / Answer

Everything looks good except few things.Also i can see that you have declared few methods in Nim.h but have'nt used them. If they are not required then you can remove from the class

Below is the updated program

Nim.cpp

#include <iostream>
#include <fstream>
#include "Nim.h"


void Nim::show_game_state()
{
for (int ix = 0; ix < m_row_cnt; ++ix)
{
cout << ix << " : ";
for (int iy = 0; iy < m_rows[ix]; ++iy)
{
cout << "|";
}
cout << endl;
}
}


//User gives input to remove n objects from x row(heap)
void Nim::show_move(int rme, int rrow, int rpins)
{
cout << (rme ? "I " : "You") << " Take " << rpins << " from row " << rrow << endl;
}

//Setup the game
//Touches :m_row_cnt,userPins,brainPins
void Nim::setup()
{
userPins = 0;
brainPins = 0;
srand (time(NULL));
m_row_cnt = rand() % 3 + 3;
for(int i =0 ; i < m_row_cnt ; i++)
{
m_rows[i] = rand() % 6 + 3;
}
}

//Print Steps taken in the Game
//Touches :m_row_cnt,userPins,brainPins
void Nim::conversation()
{
int rme = 0;
int row, pins;
setup();
//Show initital Game state
cout<<"Current Game State : "<<endl;
show_game_state();
while(true)
{
if(rme == 0){
cout<<"Enter the row number and number of pins to remove: ";
cin>>row>>pins;
if(m_row_cnt-1 < row)
{
cout<<"Please enter valid row number";
  
}
if(m_rows[row] <= 0)
{
cout<<"At least one pin must be taken";
  
}
if(m_rows[row] < pins)
pins = m_rows[row];
show_move(rme,row,pins);
userPins ++;
}
else{
row = rand() % m_row_cnt;
if(m_rows[row] == 0)
  
pins = rand() % m_rows[row] + 1;
show_move(rme,row,pins);
brainPins++;
}
m_rows[row] = m_rows[row]-pins;
rme = 1-rme;
  
if(is_win())
break;
cout<<"Current Game State : "<<endl;
show_game_state();
}
if(userPins > brainPins)
cout<<"You Won !";
else if(userPins < brainPins)
cout<<"I Won !";
else
cout<<"Match Tie";
}

//Return Game result
bool Nim::is_win()
{
int cnt = 0;
for (int ix = 0; ix < m_row_cnt; ++ix)
{   
cnt += m_rows[ix];
}
if(cnt == 0)
return true;
else
return false;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote