Mirage Source

Free ORPG making software.
It is currently Tue Apr 23, 2024 12:44 pm

All times are UTC




Post new topic Reply to topic  [ 7 posts ] 
Author Message
 Post subject: Dice Game
PostPosted: Mon Apr 13, 2009 10:08 am 
Offline
Persistant Poster
User avatar

Joined: Wed Nov 29, 2006 11:25 pm
Posts: 860
Location: Ayer
My first game programmed with C++
No tutorial was followed, just all my knowledge from 17 online lessons :D

People that do know C++, feedback on the source and what I can do better to improve
my programming skills would be appreciated.

source code:
SPOILER: (click to show)
Code:
#include <iostream>
#include <ctime>


void RollDice(int iPlayerGuess);
void GiveGold(int iAmount);
void TakeGold(int iAmount);
void PlayerWin(int iDie1, int iDie2);
int iPlayerGold;

void main() {
   int iPlayerGuess;
   std::cout << "\tPick a number between 1 - 12" << std::endl;
   std::cin >> iPlayerGuess;
   RollDice(iPlayerGuess);
}

void RollDice(int iPlayerGuess) {
   time_t t;
   time(&t);
   srand(t);
   int iDie1;
   int iDie2;

   if (iPlayerGuess < 0 || iPlayerGuess > 12){
      main();
   }

   std::cout << "\n\tRolling dice.." << std::endl;

   iDie1 = (rand() % 7);
   iDie2 = (rand() % 7);
   std::cout << "\t\t[ " << iDie1 << " ]   [ " << iDie2 << " ]" << std::endl;
   if (iPlayerGuess == (iDie1 + iDie2)) {
      PlayerWin(iDie1, iDie2);
   } else {
      TakeGold(200);
      main();
   }
}


void PlayerWin(int iDie1, int iDie2) {
   int iAmountOfGold;
   iAmountOfGold = 20 * (iDie1 * iDie2);
   GiveGold(iAmountOfGold);
   std::cout << "+" << iAmountOfGold << "G!\n" << std::endl;
   main();
}

void GiveGold(int iAmount) {
   iPlayerGold = iPlayerGold + iAmount;
   std::cout << "You now have " << iPlayerGold << " gold." << std::endl;
}

void TakeGold(int iAmount) {
   iPlayerGold = iPlayerGold - iAmount;
   std::cout << "-" << iAmount << " gold" << std::endl;
   std::cout << "You now have " << iPlayerGold << " gold.\n" << std::endl;
}


Attachments:
Dice Game.rar [4.64 KiB]
Downloaded 367 times

_________________
Image
Top
 Profile  
 
 Post subject: Re: Dice Game
PostPosted: Mon Apr 13, 2009 12:04 pm 
Offline
Pro
User avatar

Joined: Tue Apr 22, 2008 2:15 am
Posts: 597
may i ask where you got your 17 online lessons?


Top
 Profile  
 
 Post subject: Re: Dice Game
PostPosted: Mon Apr 13, 2009 12:50 pm 
Offline
Persistant Poster
User avatar

Joined: Wed Nov 29, 2006 11:25 pm
Posts: 860
Location: Ayer
http://xoax.net/comp/cpp/index.php

I've reached lesson 17 in a day. He explains so much in so little time which amazes me.

_________________
Image


Top
 Profile  
 
 Post subject: Re: Dice Game
PostPosted: Mon Apr 13, 2009 8:52 pm 
Offline
Pro
User avatar

Joined: Tue Apr 22, 2008 2:15 am
Posts: 597
sweet thanks, i'll be sure to watch them.


Top
 Profile  
 
 Post subject: Re: Dice Game
PostPosted: Tue Apr 14, 2009 1:35 am 
Offline
Pro
User avatar

Joined: Mon May 29, 2006 3:26 pm
Posts: 493
Location: São Paulo, Brasil
Google Talk: blackagesbr@gmail.com
WTF???
Code:
   if (iPlayerGuess < 0 || iPlayerGuess > 12){
      main();
   }

That's just not a good programming practice. Use a while, try to avoid recursions at most!

_________________
http://www.blackages.com.br
Image
Dave wrote:
GameBoy wrote:
www.FreeMoney.com
I admit I clicked. I immediately closed upon realizing there was, in fact, no free money.
Robin wrote:
I love you and your computer.Marry me.


Top
 Profile  
 
 Post subject: Re: Dice Game
PostPosted: Tue Apr 14, 2009 2:28 pm 
Offline
Persistant Poster
User avatar

Joined: Wed Nov 29, 2006 11:25 pm
Posts: 860
Location: Ayer
Dragoons Master wrote:
WTF???
Code:
   if (iPlayerGuess < 0 || iPlayerGuess > 12){
      main();
   }

That's just not a good programming practice. Use a while, try to avoid recursions at most!


Oh! Alright :)

Anyways, I've added you on MSN.

_________________
Image


Top
 Profile  
 
 Post subject: Re: Dice Game
PostPosted: Thu Apr 16, 2009 7:29 am 
Offline
Regular
User avatar

Joined: Thu Mar 06, 2008 1:48 pm
Posts: 54
Wow XoaX's Tutorials are Really good.
Thanks for sharing them :D

_________________
The Apocalypse Is coming!
Code:
[b]The [i][color=#FF0000]Apocalypse[/color][/i] Is coming![/b]

Image


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 7 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 6 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB® Forum Software © phpBB Group