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

Basic console video game

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

Problem

This is my code for a very basic video game I created today. I did not create this to be a fun game to play. I created this to test my skills on conditional coding, etc. Please critique this.

// Created by Andrew Tew
// My Test Studio

#include 
#include 
using namespace std;

int main ()
{
string yay;
string actyay = "yes";
string nactyay = "no";
string EastEgg = "maybe";
string replay = "replay";
bool Chest_Unlocked;
bool Have_Knife;
bool Dead = !true;
Start:
system ("cls");
cout > yay;
if (yay == actyay){
    Have_Knife = true;
}if (yay == nactyay){
    Have_Knife = false;
}if (yay == EastEgg){
    goto EasterEgg;
}

system ("cls");

cout > yay;
if (yay == actyay){
    Chest_Unlocked = true;
}if (yay == nactyay){
    Chest_Unlocked = false;
}
if (Chest_Unlocked == true){
    system ("cls");
    cout > yay;
    if (yay == replay){
        goto Start;
}else {
    goto EasterEgg;
    }}

EasterEgg:
system ("cls");
cout << "You Win!";

system ("pause");
return 0;
}

Solution


  • Firstly you need to look at the way your code is formatted, you


should increase the indent after every opening brace.

  • You have a number variables that are declared and not initialised.



  • System ("pause") could be replaced with printf ("Press enter to continue\n"); fgetc(stdin);



  • GOTO there is almost never an excuse for using GOTO statements.



  • Try using functions to breakdown your code and eliminate repeated code, functions should be up to 20 lines long ideally, may be a hundred lines in exception circumstances.



  • Personally I don't like you Java style brace use. IMO each brace should be on a separate line.



  • Namespaces are there to provide a way of resolving ambiguity, the using namespace std; line takes this function away. Personally I think that line should be removed and you should always prefix the calls with the namespace. In this case it will make no difference, but as you progress you might find your self calling a function in the wrong namespace because of this shortcut. Again it's just my opinion and I know people will disagree.



Try recoding it and post it again and see what I can find wrong with it next time :)

Context

StackExchange Code Review Q#106692, answer score: 6

Revisions (0)

No revisions yet.