Sign in to follow this  

First game - submitted for feedback

This topic is 4381 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I've self-taught C++ for about a month. I have just completed my first game, an implementation of "High Low." It's very simple I know, but I will appreciate any feedback. Particularly, I am looking for whether anything I've done here could be done better/easier/faster, and any code design tips. Thanks in advance! // start code #include<iostream> #include<cstdlib> #include<string> #include<ctime> using namespace std; int roll(int dice = 1, int sides = 6); int max(int a, int b); void round(int& a, int& b, int& aWins, int& bWins, int& rds); int main(){ string p1Name; int p1Roll, compRoll; int rounds = 0, p1Wins = 0, compWins = 0; char playAgain = ' '; cout << "Welcome to the game of High Low!\n\n"; cout << "What is your name? " << flush; cin >> p1Name; cout << "Hello, " << p1Name << "!\n\n"; while (playAgain != 'n' && playAgain != 'N'){ round(p1Roll, compRoll, p1Wins, compWins, rounds); cout << "\nYou have won " << p1Wins << " out of " << rounds << " rounds."; cout << "\n\nWould you like to play again? " << flush; cin >> playAgain; } return 0; } int roll(int dice, int sides){ int temp = 0; int result = 0; static srand((int)time(0)); for (int i = 0; i < 10; i ++) rand(); // randomize it a little better for (int i = 0; i < dice; i++) temp = rand() % sides + 1; result += temp; return result; } int max(int a, int b){ if (a > b) return a; else if (b > a) return b; else return 0; } void round(int& a, int& b, int& aWins, int& bWins, int& rds){ int winner; a = roll(); b = roll(); winner = max(a, b); cout << "You rolled " << a << endl; cout << "Computer rolled " << b << endl; if (a == winner){ cout << "\n\nYou win!"; aWins ++; } else if (b == winner){ cout << "\n\nComputer wins!"; bWins ++; } else if (winner == 0) cout << "\n\nIt's a tie!"; rds ++; } // end code

Share this post


Link to post
Share on other sites
Just at a quick glance, you don't need to seed rand() each time you roll, only once (before the while loop in main is where I would put it). Not necessarily a bug, but it is worth mentioning.

Share this post


Link to post
Share on other sites
Not bad for a first game, but here are some points you can work on:

- refresh the screen every time you enter 'y' to start a new round.
- Try to make some difficult levels where you can choose from, like 'easy', 'medium', 'hard'. And make sure that in 'easy'-mode you get more chance in winning then in 'medium' and so on.
- Make a file on your HD where you store the best 10 players in, so you can make a high-score
- ...

These are all small thing to add in a tekst based game, but it will give you a good excersie (or how do you spell it?) on what you should do when you make a graphical game.
Also as a next step, you could try to make this game graphical, with the help of a simple multimedia library, like SDL or so.

Share this post


Link to post
Share on other sites
Welcom to gamedev. Okay, I have tried it. Not bad at all for a first program. It seems you have taken care of most important things which cannot be said for many people posting their first program here. For one month C++ exprience I'd say this is nice.

The following comments are just suggestions though:
- you can post on gamedev by surrounding your code with [ code ] tags; this will make it a lot easier to read, provide syntax highlighting and preserve identation
- the program is the same at each runs (the same numbers come up). It seems your call to srand() fails. You might want to remove the word static away from it.
- some minor input check. If one enters a name that consists of multiple words (for example first name and surname), cin by default takes only the first word. That leaves the second word on the stream and this drives the game mad because for the firs x times you have already answered "yes I'd like to play again".
- some people consider it bad to use 'using namespace'; they use std::cout etc. instead.
- for later, it might be wise to start using header files and implementation files consistently. In this case, that would move the three function declarations (at the top) into the header file, and the main cpp file would include it's own header.

Just hints. Good luck,

Illco

@Spipo: it's spelled 'exercise'. Leuk trouwens, een 'tekst'-based game.

Share this post


Link to post
Share on other sites
And another remark:

I think you are pretty new to programming in C++ (sorry if i'm wrong), but also a good thing would be:

Create a class for players or so, with the name, score, value of the dice, ... in it. In the main, use that class to make a player and a computer or something like that.
Because working with classes is very handy in game development, so try to use them as much as posible.

Share this post


Link to post
Share on other sites
Thanks for the replies! I'm glad to have experienced feedback.

Nit - thanks for the tip on placing srand(). I should have seen that before, especially when every round was a tie since it was pulling the same number twice.

I am only about a third of the way through the book I am reading to learn C++ so many of the tips mentioned may become apparent to me later, but for now...

- How do I refresh the screen every time? This is something I would love to know for my next games, esp rogue-likes

- How do I use header files as mentioned by Illco? I would like to be able to use the roll() function I wrote here in other games, and I think this may be the answer.

Thanks again!

Share this post


Link to post
Share on other sites
There are a lot of ways to refresh the screen.
The one I use the most is

System("CLS");


To use header-files, you just need to write a .h-file, and add it in your project. Then in the .cpp file, at the top, you also need to include it with

#include "myheader.h" // Use the real header-file


This way you can write your roll() function in the header, and include this header file with other games and use it over and over.

Share this post


Link to post
Share on other sites
Glad you appreciate the comments. More specific on the header files: the usual way is to put function declarations in the header file and the implementation in a CPP file. What I think is the best organisation is to have them appear in pairs -- i.e. Roll.h and Roll.cpp; that way you know what to include where. This is especially useful when you move towards object-oriented programming, then each header file defines one class and the CPP file implements it.

It is often hard for beginners to get header files right and prevent all sorts of errors such as circular inclusions etc. The following way is useful and prevents most problems:

The header file:

// 1. Guard your header file against multiple inclusions
#ifndef _ROLL_H_
#define _ROLL_H_

// 2. Include function declarations
int roll(int dice = 1, int sides = 6);

// 3. End of the guard
#endif



The CPP file:

// 1. Include standard header files
#include <cstdlib>

// 2. Include your own dependencies; in this case none

// 3. Include the CPP-files matching header file
// This is not required but useful because
// - it automatically validates the definitions with their declarations
// - it allows functions in one CPP to use each other regardless of their order
#include "Roll.h"

// 4. Implement the functions
int roll(int dice, int sides){
int temp = 0;
int result = 0;
static srand((int)time(0));
for (int i = 0; i < 10; i ++)
rand(); // randomize it a little better
for (int i = 0; i < dice; i++)
temp = rand() % sides + 1;
result += temp;
return result;
}



Illco

Share this post


Link to post
Share on other sites

This topic is 4381 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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