Jump to content
  • Advertisement
Sign in to follow this  

Code review for my first game ?

This topic is 2123 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

Hello , my name is kostas and i  am 18 years old student at ionian university( computer science ). My hopes are that someday i will become a good game / graphics programmer. After 7 hours of work i finished my first game tic tac toe ( 2 players no AI ) using c++(ide visual studio 2012 prof since its free for students ) and SDL . I will post my c++ fille and hopefully you could do a review of my code and suggest me what should i do better.... Your suggestions are necessary so i can become better! Thanks a lot for your time. link here : http://www.sendspace.com/file/6ktmss . For a reason i couldnt attach the cpp fille so i had to upload it ?!

Share this post

Link to post
Share on other sites

You have several typoes or spelling mistakes in code or comments, these are confusing or annoying to a maintainer but not really a problem (most notably SCREEN_WITDH)


There are a lot of global variables, some of which are not very descriptive or helpful, e.g. event / gameEvent. You might reduce the global variables by combining some of them into structs and/or putting them into arrays or some other container. It's also possible that some of them don't need to be global at all, and can be passed as parameters instead and/or held in a local somewhere.


Your variable and function names are not very descriptive and a maintenance programmer will need to refer to their declaration comments a lot. "state" might be better as "board_state", then we know what it's the state of. (conceptually, every variable is a state of something). Some function names don't start with a verb, for example "position" or "score".


"using namespace std" - I could possibly get into a Holy War over this, but I prefer not to use this nowadays.


Function position() ... this function needs the most work.

1. It is trying to do at least two things at once - determine the mouse click target and draw things on the screen. Logically, I'd try to separate these two operations

2. It does many rectangle-tests for the mouse-position, it looks like these could easily be refactored into an array or something else.

3. There is a great deal of repeated code


I suggest you might refactor position() into several stages, possibly different functions:

a. Determine whether the mouse-button has been clicked on a square, and which one

b. Check whether the square is a legal move (i.e. not already taken)

c. Draw the mark in the box


Obviously I can't see what's in your background image, but I suspect that the board has a regular grid, so you might be able to use a bit of arithmetic to work out the boxes' coordinates programmatically and not have to hard-code the locations of 9 squares.


However, if your tic-tac-toe grid is highly irregular, you could make an array of structs to store their coords thus:


struct myrect { int x1,y1,x2,y2; };

myrect rects[9] = {

  {30,25,210,165} , ...




Or whatever the correct syntax is!

Share this post

Link to post
Share on other sites

Thank you very much for your feedback and i am looking forward to correct all the things you said me. Also about the std i agree that is essential but since the program wasnt big enough i prefered not to use it. I will create my next game now , propably a blackjack and then a pong maybe ? Anyway when i finish it , i will post it here and hopefully i my code will be better eventually. Thank again and i appreciate your help!

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!