In: Computer Science
Project [2]: Expressions and Operators
Project Goals
The goal of this project is to:
1. Get students familiar with expressions and operators.
Important Notes:
1. Formatting: Make sure that you follow the precise
recommendations for the output content and formatting. For example,
do not change the text from
“You’re walking through the woods and come upon a chest.
Do you open it? 1 – yes 2 - no” to
“Do you open the chest? ”.
Your assignment will be auto-graded and any changes in formatting
will result in a loss in the grade.
2. Comments: Header comments are required on all files and
recommended for the rest of the program. Points will be deducted if
no header comments are included.
Program
Save your program as adventure.c
Write a program that will allow the user to play a simple Choose
Your Own Adventure game.
The program should behave as follows:
The user starts out with 10 Health Points and 0.0 Wealth Points.
The user encounters a series of choices which affect their Health
and Wealth Point totals. In the end, if their Health Points are
over 10 and their Wealth Points are over 4.25, they win! Otherwise
they lose.
The first choice comes when the user encounters a chest. If they
open the chest, they gain 3.75 Wealth Points and a sword. If they
don’t, they stub their toe and lose a Health Point.
The second choice comes when the user encounters a bag. If they
open the bag, they gain 5.15 Wealth Points and a shield. If they
don’t, they get a blister and lose a Health Point.
The final choice is whether to engage in a battle with an ogre. If
they choose to fight and they don’t have a sword or shield, they
lose all their Health Points. If they choose to fight and they have
only the sword, they lose all their Health Points. If they choose
to fight and they have only the shield, they don’t lose or gain any
Health Points. If they choose to fight and have both their sword
and shield, they gain 12 Health Points. If they choose to flee,
they trip and fall, get stomped on by the ogre and lose all their
Health Points.
/******************************************************************************
Online C Compiler.
Code, Compile, Run and Debug C program online.
Write your code in this editor and press "Run" button to compile
and execute it.
*******************************************************************************/
#include <stdio.h>
int main()
{ //store points in varible
int HealthPoint=10;
float WealthPoint=0.00;
//take accept input
int input;
//boolean for checking conditions
int have_sword=0;
int have_shield=0;
printf("You’re walking through the woods and come upon a chest. Do
you open it? 1 – yes 2 - no");
scanf("%d",&input);
if(input ==1) // chooses to open
{
WealthPoint+=3.75;
have_sword=1;
printf("You have got 3.75 Wealth Point and sword\n");
}else if(input ==2){ // refuses to open
printf("You have lost Health Point\n");
HealthPoint--;
}
printf("You’re walking through the woods and come upon a bag. Do
you open it? 1 – yes 2 - no");
scanf("%d",&input);
if(input ==1) // chooses to open
{
WealthPoint+=5.15;
have_shield=1;
printf("You have got 5.15 Wealth Point and shield\n");
}else if(input ==2){ // refuses to open
printf("You have lost Health Point\n");
HealthPoint--;
}
printf("You’re walking through the woods and Do you want to engage
in a battle with an ogre? 1 – yes 2 - no");
scanf("%d",&input);
if(input ==1) // chooses to fight
{
if(have_sword==0||have_shield==0){
HealthPoint=0;
printf("You have lost all Health Point\n");
}
if(have_sword==1&&have_shield==0){
HealthPoint=0;
printf("You have lost all Health Point\n");
}
if(have_sword==0&&have_shield==1)
{ printf("You have not gain Health Point or Wealth Point\n");
}
if(have_sword==1&&have_shield==1){
HealthPoint+=12;
printf("You have got 12 Health Point\n");
}
}else if(input ==2){ // refuses to fight
HealthPoint=0;
printf("You have lost all Health Point\n");
}
// check whether wins or losses game
if (HealthPoint >10 && WealthPoint >4.25){
printf("You have won the game and ");
printf("You have %d HealthPoint and %f WealthPoint\n",
HealthPoint,WealthPoint);
}
else{
printf("You have loss the game");
}
return 0;
}
==================================================================
Output:
akshay@akshay-Inspiron-3537:~$ gcc game.c
akshay@akshay-Inspiron-3537:~$ ./a.out
You’re walking through the woods and come upon a chest. Do you open
it? 1 – yes 2 - no1
You have got 3.75 Wealth Point and sword
You’re walking through the woods and come upon a bag. Do you open
it? 1 – yes 2 - no1
You have got 5.15 Wealth Point and shield
You’re walking through the woods and Do you want to engage in a
battle with an ogre? 1 – yes 2 - no1
You have got 12 Health Point
You have won the game and You have 22 HealthPoint and 8.900000
WealthPoint