Merge pull request #792 from Vaxick/main

Chief, Cube and Dice in C
This commit is contained in:
Jeff Atwood
2022-09-03 10:30:04 -07:00
committed by GitHub
3 changed files with 260 additions and 0 deletions

View File

@@ -0,0 +1,92 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
//check if windows or linux for the clear screen
#ifdef _WIN32
#define CLEAR "cls"
#else
#define CLEAR "clear"
#endif
void show_solution(float guess);
float guess_number(float number);
void game();
float guess_number(float number){
float guess;
guess = ((((number - 4) * 5) / 8) * 5 - 3);
return guess;
}
void game(){
float number,guess;
char answer[4];
printf("Think a number\n");
printf("Then add to it 3 and divide it by 5\n");
printf("Now multiply by 8, divide by 5 and then add 5\n");
printf("Finally substract 1\n");
printf("What is the number you got?(if you got decimals put them ex: 23.6): ");
scanf("%f",&number);
guess = guess_number(number);
printf("The number you thought was %f am I right(Yes or No)?\n",guess);
scanf("%s",answer);
for(int i = 0; i < strlen(answer); i++){
answer[i] = tolower(answer[i]);
}
if(strcmp(answer,"yes") == 0){
printf("\nHuh, I Knew I was unbeatable");
printf("And here is how i did it:\n");
show_solution(guess);
}
else if (strcmp(answer,"no") == 0){
printf("HUH!! what was you original number?: ");
scanf("%f",&number);
if(number == guess){
printf("Huh, I Knew I was unbeatable");
printf("And here is how i did it:\n");
show_solution(guess);
}
else{
printf("If I got it wrong I guess you are smarter than me");
}
}
else{
system(CLEAR);
printf("I don't understand what you said\n");
printf("Please answer with Yes or No\n");
game();
}
}
void show_solution(float guess){
printf("%f plus 3 is %f\n",guess,guess + 3);
printf("%f divided by 5 is %f\n",guess + 3,(guess + 3) / 5);
printf("%f multiplied by 8 is %f\n",(guess + 3) / 5,(guess + 3) / 5 * 8);
printf("%f divided by 5 is %f\n",(guess + 3) / 5 * 8,(guess + 3) / 5 * 8 / 5);
printf("%f plus 5 is %f\n",(guess + 3) / 5 * 8 / 5,(guess + 3) / 5 * 8 / 5 + 5);
printf("%f minus 1 is %f\n",(guess + 3) / 5 * 8 / 5 + 5,(guess + 3) / 5 * 8 / 5 + 5 - 1);
}
void main(){
char answer[4];
printf("I am CHIEF NUMBERS FREEK, The GREAT INDIAN MATH GOD.\n");
printf("Are you ready to take the test you called me out for(Yes or No)? ");
scanf("%s",answer);
for(int i = 0; i < strlen(answer); i++){
answer[i] = tolower(answer[i]);
}
if(strcmp(answer,"yes") == 0){
game();
}else if (strcmp(answer,"no") == 0){
printf("You are a coward, I will not play with you.%d %s\n",strcmp(answer,"yes"),answer);
}
else{
system(CLEAR);
printf("I don't understand what you said\n");
printf("Please answer with Yes or No\n");
main();
}
}

View File

@@ -0,0 +1,142 @@
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
//check if windows or linux for the clear screen
#ifdef _WIN32
#define CLEAR "cls"
#else
#define CLEAR "clear"
#endif
typedef struct{
int x;
int y;
int z;
}coords;
void instuctions(){
printf("\nThis is a game in which you will be playing against the\n");
printf("random decisions of the computer. The field of play is a\n");
printf("cube of side 3. Any of the 27 locations can be designated\n");
printf("by inputing three numbers such as 2,3,1. At the start,\n");
printf("you are automatically at location 1,1,1. The object of\n");
printf("the game is to get to location 3,3,3. One minor detail:\n");
printf("the computer will pick, at random, 5 locations at which\n");
printf("it will plant land mines. If you hit one of these locations\n");
printf("you lose. One other detail: You may move only one space\n");
printf("in one direction each move. For example: From 1,1,2 you\n");
printf("may move to 2,1,2 or 1,1,3. You may not change\n");
printf("two of the numbers on the same move. If you make an illegal\n");
printf("move, you lose and the computer takes the money you may\n");
printf("have bet on that round.\n\n");
printf("When stating the amount of a wager, printf only the number\n");
printf("of dollars (example: 250) you are automatically started with\n");
printf("500 dollars in your account.\n\n");
printf("Good luck!\n");
}
void game(int money){
coords player,playerold,mines[5];
int wager,account = money;
char choice;
if(money == 0){
printf("You have no money left. See ya next time.\n");
exit(0);
}
player.x = 1;
player.y = 1;
player.z = 1;
printf("You have $%d in your account.\n",account);
printf("How much do you want to wager? ");
scanf("%d",&wager);
while(wager > account){
system(CLEAR);
printf("You do not have that much money in your account.\n");
printf("How much do you want to wager? ");
scanf("%d",&wager);
}
srand(time(NULL));
for(int i=0;i<5;i++){
mines[i].x = rand()%3+1;
mines[i].y = rand()%3+1;
mines[i].z = rand()%3+1;
if(mines[i].x == 3 && mines[i].y == 3 && mines[i].z == 3){
i--;
}
}
while(player.x != 3 || player.y != 3 || player.z != 3){
printf("You are at location %d.%d.%d\n",player.x,player.y,player.z);
if(player.x == 1 && player.y == 1 && player.z == 1)
printf("Enter new location(use commas like 1,1,2 or else the program will break...): ");
else printf("Enter new location: ");
playerold.x = player.x;
playerold.y = player.y;
playerold.z = player.z;
scanf("%d,%d,%d",&player.x,&player.y,&player.z);
if(((player.x + player.y + player.z) > (playerold.x + playerold.y + playerold.z + 1)) || ((player.x + player.y + player.z) < (playerold.x + playerold.y + playerold.z -1))){
system(CLEAR);
printf("Illegal move!\n");
printf("You lose $%d.\n",wager);
game(account -= wager);
break;
}
if(player.x < 1 || player.x > 3 || player.y < 1 || player.y > 3 || player.z < 1 || player.z > 3){
system(CLEAR);
printf("Illegal move. You lose!\n");
game(account -= wager);
break;
}
for(int i=0;i<5;i++){
if(player.x == mines[i].x && player.y == mines[i].y && player.z == mines[i].z){
system(CLEAR);
printf("You hit a mine!\n");
printf("You lose $%d.\n",wager);
game(account -= wager);
exit(0);
}
}
if(account == 0){
system(CLEAR);
printf("You have no money left!\n");
printf("Game over!\n");
exit(0);
}
}
if(player.x == 3 && player.y == 3 && player.z == 3){
system(CLEAR);
printf("You made it to the end. You win!\n");
game(account += wager);
exit(0);
}
}
void init(){
int account = 500;
char choice;
printf("Welcome to the game of Cube!\n");
printf("wanna see the instructions? (y/n): ");
scanf("%c",&choice);
if(choice == 'y'){
system(CLEAR);
instuctions();
}
else if (choice == 'n'){
system(CLEAR);
printf("Ok, let's play!\n");
}
else{
system(CLEAR);
printf("Invalid choice. Try again...\n");
init();
exit(0);
}
game(account);
exit(0);
}
void main(){
init();
}

View File

@@ -0,0 +1,26 @@
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
float percent(int number, int total){
float percent;
percent = (float)number / (float)total * 100;
return percent;
}
int main(){
int dice1,dice2,times,rolls[13] = {0};
srand(time(NULL));
printf("This program simulates the rolling of a pair of dice\n");
printf("How many times do you want to roll the dice?(Higher the number longer the waiting time): ");
scanf("%d",&times);
for(int i = 0; i < times; i++){
dice1 = rand() % 6 + 1;
dice2 = rand() % 6 + 1;
rolls[dice1 + dice2]+=1;
}
printf("The number of times each sum was rolled is:\n");
for(int i = 2; i <= 12; i++){
printf("%d: rolled %d times, or %f%c of the times\n",i,rolls[i],percent(rolls[i],times),(char)37);
}
}