User:Simfish

From Wikipedia, the free encyclopedia

This is an old revision of this page, as edited by 152.157.79.253 (talk) at 21:07, 20 January 2005. The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

Jump to navigation Jump to search

//Anton Grobman //This program simulates the game of roulette with diffrent rules

  1. include <stdio.h>
  2. include <stdlib.h>
  3. include <time.h>

float total (float,float); //fuction prototype float check (float); //fuction prototype float average (float,float); //fuction prototype void highscore (); //fuction prototype struct strbets{ //structure of the bets int place; //placement of bet float money; //money placed on bet }; struct strroll{ //structure of the ball roll int number; //number ball landed on int color; //color ball landed on }; struct score{ //structure of player profile char name[3]; //name of player float worth; //net worth of player }; struct multiply{ //structure that controls the number that multiplies the bet if you win float number; //what multiplies the place of bet float color; //what multiplies the color of bet }; struct strbets bets[31]; //array that holds 31 bets struct strroll roll; //ball roll struct score profile; //profile of player struct multiply earn; //multiple of the winnings char sm; //charecture that makes a smiley face int main() { int gloop; //integer that controls whether the game loop again int bloop; //integer that controls whether you make another bet int bcount; //count that countrols the number of bets placed int temp; //temparary float wins; //money won float loses; //money lost float profit; //total money won minus money lost

sm=1; //makes it so char sm always makes a smiley face printf("\a\n\n\n\n\n\n\n\n\t\t\t%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c\n\t\t\t%c\t\t\t %c\n\t\t\t%c Welcome to Roulette 1.6 %c\n\t\t\t%c made by Anton Grobman %c\n\t\t\t%c Good luck and have fun! %c\n\t\t\t%c\t\t\t %c\n\t\t\t%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c\n\n\n\n\n\n\n\n",sm,sm,sm,sm,sm,sm,sm,sm,sm,sm,sm,sm,sm,sm,sm,sm,sm,sm,sm,sm,sm,sm,sm,sm,sm,sm,sm,sm,sm,sm,sm,sm,sm,sm,sm,sm,sm,sm,sm,sm,sm,sm,sm,sm,sm,sm,sm,sm,sm,sm,sm,sm,sm,sm,sm,sm,sm,sm,sm,sm,sm,sm,sm,sm); printf("Hello my name is Roulette 1.6, but you can call me BOB for short\n"); printf("Enter your initials (3 letters max)\n"); scanf("%s",profile.name); //stores your name so that it can be placed as the high score profile.worth=1000; //gives you $1000 dollors to start with earn.number=(float) 26.9; //makes the multiple of a place win 26.9 earn.color=(float) 1.9; //makes the multiple of a color win 1.9

if (profile.name[0]==98 && profile.name[1]==79 && profile.name[2]==98){ //cheat that gives you $10000 instead of $1000 printf("WRONG NAME!!!\nENTER NEW NAME.\n"); scanf("%s",profile.name); profile.worth=10000; } if (profile.name[0]==71 && profile.name[1]==109 && profile.name[2]==77){ //cheat that makes the multiple of a place and color win more in your favor printf("WRONG NAME!!!\nENTER NEW NAME.\n"); scanf("%s",profile.name); earn.number=40; earn.color=3; } printf("\nThank You, here is $1000\n\n"); srand( (unsigned) time( NULL )); //sets seed using the time gloop=1;

while (gloop==1){ //controls whether game loops or not for(temp=0;temp<=29;temp++){ //makes the whole array of bets 0 bets[temp].place='0'; bets[temp].money='0.0'; } bloop=1; bcount=0; loses=0; wins=0;

while (bcount<=29){ //controls whether you can make another bet so that it you don't make too many

if (bloop==1){ //controls whether you want to make another bet printf("Please enter what you'd like to bet on, followed by how much you'd like to bet. You can only make 30 bets.(1-27 number bets, 28 for red, 29 for black)\n\n"); scanf("%d",&bets[bcount].place); //stores the place you bet on

while (bets[bcount].place>29 || bets[bcount].place<0){ //makes sure the place you bet on is a valid place printf("Invalid place. Please enter in a valid place\n"); scanf("%d",&bets[bcount].place); //re-stores your place if it was invalid } printf("$"); scanf("%f",&bets[bcount].money); //stores money placed on bet

while (loses+bets[bcount].money>profile.worth){ //makes sure you have enough money to make that bet printf("Not enough money. Please place a smaller bet\n$"); scanf("%f",&bets[bcount].money); //re-stores money placed on bet if it was invalid } loses+=bets[bcount].money; //adds the money that you placed on the bet to a running total of how much money you have spent so far printf("\nWould you like to place another bet?(1 for yes, 2 for no)\n\n"); scanf("%d",&bloop); // stores whether you would like to place another bet or not

while (bloop!=1 && bloop!=2){ //makes sure the number you entered above is valid printf("Entry not valid. Please input again.\n"); scanf("%d",&bloop); //re-stores bloop if it was invalid }

if (bloop==2) //if you wish to not bet again then it stores bcount as 30 to stop the loop bcount=30; bcount++; //adds one to bcount to keep track of how many bets you placed } } roll.number=1+rand()%27; //random ball roll for number roll.color=28+rand()%2; //random ball roll for color wins=check(wins); //calls check function

if (roll.color==28) //displays the outcome of the roll printf("Roll Outcome:\t%d Red\n\n",roll.number); if (roll.color==29) printf("Roll Outcome:\t%d Black\n\n",roll.number);

profit=average(wins,loses); //calls average function profile.worth=total(profit,profile.worth); //calls total function printf("What would you like to do next?\n1)Play again\n2)Leave the table\n\n"); scanf("%d",&gloop); //stores whether you would like to play again or not

while (gloop!=1 && gloop!=2){ //makes sure you entered a valid number printf("Invalid entry please try again.\n"); scanf("%d",&gloop); //re-stores gloop if it was invalid } } highscore(); //calls high score function printf("Goodbye %c\n",sm); getchar(); return 0; }

float check (float winnings) //this function checks if your bet was a winner, and displays what you bet on { int i; //index printf("What you bet on:\tHow much you bet:\t\n\n");

for (i=0;bets[i].place!=0;i++){ //runs tell the array is out of vailid bets that you placed if (roll.number==bets[i].place) //checks if the places you bet on, won or lost winnings+=(bets[i].money*earn.number); //if you won then your bet is multiplied ny a certain number and then added to your winnings if (roll.color==bets[i].place) //checks if color you bet on, won or lost winnings+=(bets[i].money*earn.color); //if you won then your bet is multiplied ny a certain number and then added to your winnings //diplays the bets that you placed if (bets[i].place==28) printf("Red\t\t\t$%.2f\n",bets[i].money); if (bets[i].place==29) printf("Black\t\t\t$%.2f\n",bets[i].money); if (bets[i].place>=1 && bets[i].place<=27) printf("%d\t\t\t$%.2f\n",bets[i].place,bets[i].money); } printf("\n"); return winnings; //returns your total winnings }

float total (float pro, float net) //calculates your net worth and prints it out { net+=pro; //calulates net worth printf("Your net worth:\t $%.2f\n\n",net); return net; //returns new net worth }

float average (float income,float expenses) //claculates your winnings and prints it out { float pro; //profit pro=income-expenses; //calculates the total profit printf("Winnings are: \t$%.2f\n\n",pro); return pro; //returns profit }

void highscore() //checks if you have the new high score and then displays the high score { int count; //count struct score top; //high score FILE *cfPtr; if((cfPtr = fopen("credit.dat","r")) == NULL) printf("File could not be opened.\n"); //opens file to get the old high score else{ fscanf(cfPtr,"%s%f",top.name,&top.worth); fclose(cfPtr); }

if (profile.worth>top.worth){ //checks if you have the new high score printf("\nYou have the new high score!!!\n\n"); top.worth=profile.worth; //replaces old high score with your's for(count=0;count<4;count++) top.name[count]=profile.name[count]; } printf("\nThe high score is:\t"); //displays high score for(count=0;count<3;count++) printf("%c",top.name[count]); printf("  %.2f\n",top.worth); if((cfPtr = fopen("credit.dat","w")) == NULL) printf("File could not be opened\n"); //saves and closes the high score else{ fprintf(cfPtr,"%s %f",top.name,top.worth); fclose(cfPtr); } }

/*Tweaking: -Switched bcount loop and bloop loop placement -At end of bloop loop I added if statement to change bcount to 30 -Took random number generater out of function and placed into main -Reset wins to 0 in bcount -Added cheats -Reset numbers in bets structure to 0 */

  1. include <stdio.h>
  2. include <string.h>

//This program allows the user to type in a message and then encodes it into Caesar code. //(Caesar code is an encoding where each letter of a message is shifted a certain number of places //to the right or left in the alphabet.

int main (){ char message[80]; int num, i;

printf ("Enter a message \n"); gets ((char*)&message); fflush (stdin);

printf ("\nNumber of letters to shift: \n"); scanf ("%d", &num);

for (i = 0; i < strlen (message); i++){ if ((message[i] >= 65 && message[i] <= 90) || //For uppercase letters. (message[i] >= 97 && message[i] <= 122)) //For lowercase letters. message[i] += num; }

printf ("\n\nThe encoded message is: %s\n\n", message);

return 0; }

  1. include <stdio.h>

//This program takes an inputted text and calculates the number of words in that text.

int main (){ char text[80]; int numofwords = 0 /*Automatically counts first word [see note]*/, i = 0; bool in_word = false;

printf ("Enter text: \n"); gets ((char*)&text); //Recieves inputted text.

while (text[i] != '\0'){ //Finds number of words based on spaces (' ' = 32)[ASCII]) if (text[i] != 32 && in_word == false) { in_word = true; numofwords++; }else{ if (in_word == true && text[i] == 32) in_word = false; } i++; }

printf ("\nNumber of words = %d\n\n", numofwords);

return 0; }

  1. include <stdio.h>
  2. include <string.h>

//This program decodes a Caesar code into a message.

int main (){ char message[80]; int num, i;

printf ("Enter an encoded message \n"); gets ((char*)&message); fflush (stdin);

printf ("\nNumber of letters shifted: \n"); scanf ("%d", &num);

for (i = 0; i < strlen (message); i++){ if ((message[i] >= 65 && message[i] <= 90) || //For uppercase letters. (message[i] >= 97 && message[i] <= 122)) //For lowercase letters. message[i] -= num; }

printf ("\n\nThe decoded message is: %s\n\n", message);

return 0; }