Mirage Source
http://web.miragesource.net/forums/

Dice Game
http://web.miragesource.net/forums/viewtopic.php?f=142&t=5484
Page 1 of 1

Author:  Tony [ Mon Apr 13, 2009 10:08 am ]
Post subject:  Dice Game

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 383 times

Author:  Pbcrazy [ Mon Apr 13, 2009 12:04 pm ]
Post subject:  Re: Dice Game

may i ask where you got your 17 online lessons?

Author:  Tony [ Mon Apr 13, 2009 12:50 pm ]
Post subject:  Re: Dice Game

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.

Author:  Pbcrazy [ Mon Apr 13, 2009 8:52 pm ]
Post subject:  Re: Dice Game

sweet thanks, i'll be sure to watch them.

Author:  Dragoons Master [ Tue Apr 14, 2009 1:35 am ]
Post subject:  Re: Dice Game

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!

Author:  Tony [ Tue Apr 14, 2009 2:28 pm ]
Post subject:  Re: Dice Game

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.

Author:  katcode [ Thu Apr 16, 2009 7:29 am ]
Post subject:  Re: Dice Game

Wow XoaX's Tutorials are Really good.
Thanks for sharing them :D

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/