d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Craps Anyone?
Add Reply New Topic New Poll
Member
Posts: 4,332
Joined: Mar 31 2011
Gold: 71.00
May 23 2012 12:35pm
Code
#include <iostream>
#include <ctime>
using namespace std;

void DisplayDieOne();
void DisplayDieTwo();
void DisplayDieThree();
void DisplayDieFour();
void DisplayDieFive();
void DisplayDieSix();

void main()
{
char playAgain;
int die;
int i;
int roll = 0;
int point;
int totalDice;
srand(time(0));
do
{
 roll = 0;
 while(true)
 {
  totalDice = 0;
  roll ++;
  for (i = 1; i <= 2; i++)
  {
   die = rand() % 6 + 1;
   totalDice += die;
   switch (die)
   {
   case 1:
    DisplayDieOne();
    break;
   case 2:
    DisplayDieTwo();
    break;
   case 3:
    DisplayDieThree();
    break;
   case 4:
    DisplayDieFour();
    break;
   case 5:
    DisplayDieFive();
    break;
   case 6:
    DisplayDieSix();
    break;
   }
  }
  if (roll == 1)
  {
   if (totalDice == 7 || totalDice == 1)
   {
    cout << "You win!\n";
    break;
   }
   if (totalDice == 2 || totalDice == 3 || totalDice == 12)
   {
    cout << "You lose.\n";
    break;
   }
   else
   {
    point = totalDice;
    cout << "Roll again. Must roll " << point << " to win.\n";
   }

  }
  else
  {
   if (totalDice == 7)
   {
    cout << "You lost in " << roll << " rolls.\n";
    break;
   }
   if (totalDice == point)
   {
    cout << "You won in " << roll << " rolls!\n";
    break;
   }
   else
    cout << "Roll again. Must roll " << point << " to win.\n";

  }
 }
 cout << "Do you want to play again (y/n)? ";
 cin >> playAgain;
} while(toupper(playAgain) == 'Y');
cin.get();
}
void DisplayDieOne()
{
cout << " ----- \n";
cout << "|     |\n";
cout << "|  O  |\n";
cout << "|     |\n";
cout << " ----- \n";
}

void DisplayDieTwo()
{
cout << " ----- \n";
cout << "|    O|\n";
cout << "|     |\n";
cout << "|O    |\n";
cout << " ----- \n";
}
void DisplayDieThree()
{
cout << " ----- \n";
cout << "|    O|\n";
cout << "|  O  |\n";
cout << "|O    |\n";
cout << " ----- \n";
}
void DisplayDieFour()
{
cout << " ----- \n";
cout << "|O   O|\n";
cout << "|     |\n";
cout << "|O   O|\n";
cout << " ----- \n";
}
void DisplayDieFive()
{
cout << " ----- \n";
cout << "|O   O|\n";
cout << "|  O  |\n";
cout << "|O   O|\n";
cout << " ----- \n";
}
void DisplayDieSix()
{
cout << " ----- \n";
cout << "|O   O|\n";
cout << "|O   O|\n";
cout << "|O   O|\n";
cout << " ----- \n";
}



Thoughts?
Member
Posts: 4,605
Joined: Sep 15 2011
Gold: 9,464.00
May 23 2012 01:06pm
What do you want people to say or do? Good job? Pat on the back?

As a beginner's coding exercise it's perhaps useful. As anything else, it's completely uninteresting.
Member
Posts: 4,332
Joined: Mar 31 2011
Gold: 71.00
May 23 2012 08:48pm
Quote (irimi @ May 23 2012 02:06pm)
What do you want people to say or do?  Good job?  Pat on the back?

As a beginner's coding exercise it's perhaps useful.  As anything else, it's completely uninteresting.


No need to be an ass... I was asking if anyone had some input, maybe something to better the program, clean up my code, anything like that. Jesus, is this really what the jsp community has turned to?
Member
Posts: 30,717
Joined: Sep 19 2007
Gold: 254.00
May 23 2012 09:07pm
break this out into it's own method

Code
  switch (die)
  {
  case 1:
   DisplayDieOne();
   break;
  case 2:
   DisplayDieTwo();
   break;
  case 3:
   DisplayDieThree();
   break;
  case 4:
   DisplayDieFour();
   break;
  case 5:
   DisplayDieFive();
   break;
  case 6:
   DisplayDieSix();
   break;
  }



then think about extracting state to a struct to encapsulate the game state a bit better, then if you expand on the game and have more methods you pass around a state object instead of a bunch of variables
Member
Posts: 4,554
Joined: Dec 1 2008
Gold: 0.50
May 24 2012 08:09am
Quote (BouAnCafe @ May 23 2012 06:48pm)
No need to be an ass... I was asking if anyone had some input, maybe something to better the program, clean up my code, anything like that. Jesus, is this really what the jsp community has turned to?



No... He's just an ass.
Member
Posts: 4,605
Joined: Sep 15 2011
Gold: 9,464.00
May 24 2012 10:19am
Quote (Furio @ May 24 2012 07:09am)
No... He's just an ass.


This. But you should be asking better questions. "Thoughts?" is absolutely worthless as a question. If you asked for feedback on the code, you would have gotten that. Instead, you asked for my thoughts and got exactly that.

This post was edited by irimi on May 24 2012 10:20am
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
May 24 2012 10:56am
Quote (irimi @ May 24 2012 12:19pm)
This.  But you should be asking better questions.  "Thoughts?" is absolutely worthless as a question.  If you asked for feedback on the code, you would have gotten that.  Instead, you asked for my thoughts and got exactly that.


I assumed the silly orange text was part of his signature. :/
Member
Posts: 2,429
Joined: Jul 22 2010
Gold: 104.60
May 24 2012 03:08pm
Quote (carteblanche @ May 24 2012 06:56pm)
I assumed the silly orange text was part of his signature. :/


OP :thumbsup:
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
May 25 2012 12:49am
Code
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

unsigned long hex2int(char *a, unsigned int len);
void alphanum2upper(char *str);

int main()
{
   char *input;
   input = malloc(sizeof(char) * 8);
   unsigned long output;


   printf("Welcome to Abduction PWI Color Changer v1.1\n");

   while(1)
   {
       printf("Please enter the Hexadecimal color code to be converted or \"exit\": ");

       fgets(input, 7, stdin);

       if(*input == "exit"){ break; }

       alphanum2upper(input);

       output = hex2int(input, 6);
       printf("\n\nPWI Color Code: %ld", output - 16777216);

       getchar();
       getchar();
   }

   free(input);
   return 0;
}

unsigned long hex2int(char *a, unsigned int len)
{
   int i;
   unsigned long val = 0;

   for(i=0;i<len;i++)
      if(a[i] <= 57)
    val += (a[i]-48)*(1<<(4*(len-1-i)));
      else
    val += (a[i]-55)*(1<<(4*(len-1-i)));
   return val;
}

void alphanum2upper(char *str)
{
   int i = strlen(str);
   while (i-->0)
   {
       if (*str >= (int)'a' && *str <= (int)'z')
       {
           *str-=32;
       }
   str++;
   }
}


Thoughts?
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll