Important alert: (current site time 5/25/2013 7:19:08 AM EDT)
 

VB icon

Mage Magic

Email
Submitted on: 2/21/2012 12:00:54 PM
By: Sam N  
Level: Intermediate
User Rating: Unrated
Compatibility: C++ (general)
Views: 1179
 
     It's a fun game for everyone!
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
//**************************************
// Name: Mage Magic
// Description:It's a fun game for everyone!
// By: Sam N 
//
//This code is copyrighted and has// limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=13755&lngWId=3//for details.//**************************************

#include <cstdlib>
#include <iostream>
#include <fstream>
#include <time.h>
#include <algorithm> 
#include <string>
#include <windows.h>
#include <winsock.h> 
using namespace std;
//struct decs
struct fence{
bool locked; 
void init(){
locked = 0; 
}
};
struct player {
int position; 
string inventory[10]; 
int health; 
string name; 
//FUNCTION THAT INITIALIZES THE PLAYER
void init(){
for(int i = 0; i < 9 ; i++){
inventory[i] = "null";
}
this->health = 100; 
this->position = 0; 
}
//FUNCTION WHICH SAVES THE PLAYERS ATTRIBUTES TO COMPUTER
void save_game(){
ofstream save_file("save.txt"); 
for(int b = 0 ; b < 9 ; b++){
save_file << inventory[b] << endl; 
}
save_file << this->position; 
}
//METHOD WHICH LOADS THE PLAYER's CORE INFO
void load_game(){
ifstream load_file("save.txt"); 
char buffer[80]; 
for(int f = 0 ; f < 9 ; f++){
//for some reason this doesn't work inventory[f] = load_file.getline(buffer , 10); 
}
}
//ALGORITHM WHICH ADDS THE OBJECT THAT NEEDS TO GET PICKED UP TO INVENTORY OF PLAYER
void pick_up(string object){
bool current = 0; 
int counter = 0; 
while(current == 0){
if(this->inventory[counter] != "null"){
counter++; 
}
else{
inventory[counter] = object; 
current = 1; 
}
}
}
void print_inventory(){
system("cls"); 
cout << "-=-=-=-=INVENTORY=-=-=-=-\n\n\n\n"; 
if(inventory[0] == "null"){
cout << "It would seem as though you don't have any items in your inventory.\n\n\n"; 
return; 
}
for(int d = 0 ; d < 9 ; d++){
if(inventory[d] != "null"){
cout << d+1 << ") "<< inventory[d] << "\n\n"; 
}
}
cout << endl; 
return; 
}
};
struct scene {
int tag; 
//image that will manifest the screen
string image; 
//message that goes along with the image
string scene_message; 
//objects contained at this place 
string objects[2]; 
//possible non-generic commands that user can give
string commands[10]; 
//initializes the structure
void init(){
for(int i = 0; i < 3 ; i++){
this->objects[i] = "null"; 
}
for(int i = 0; i < 3 ; i++){
this->commands[i] = "null"; 
}
 
}
//set the image of the scene
void setting_image(string *image){
this->image = *image; 
}
//DRAWS THE SCENE TO THE SCREEN 
void draw_scene(){
system("cls"); 
cout << this->image << "\n\n" << this->scene_message; 
}
//SETTER FUNCTIONS
void setting_objects(string obj_one , string obj_two , string obj_three){
this->objects[0] = obj_one; 
this->objects[1] = obj_two; 
this->objects[2] = obj_three; 
}
void setting_commands(string com_one , string com_two , string com_three){
this->commands[0] = com_one; 
this->commands[1] = com_two; 
this->commands[2] = com_three; 
}
};
struct response_container{
scene response_container[10]; 
};
struct game_struct{
fence cFence; 
//these vars store where the users have been
int has_been_axe; 
int has_been_forest; 
scene home;
scene gate; 
scene south_fork; 
scene woods; 
scene west_fork; 
scene south_monster; 
scene dead_end; 
scene well_with_rope; 
scene well_without_rope; 
scene axe_view; 
scene without_axe_view; 
scene shovel_and_tree_stump; 
scene well_sword; 
scene x_marks_spot; 
scene key_view; 
scene unlocked_fence; 
scene reaped_forest; 
scene scene_container[20]; 
response_container corresponding_responses[15]; 
player cPlayer; 
void init(){
cFence.init(); 
this->has_been_axe = 0; 
this->has_been_forest = 0; 
//DECLARE SOME OF THE POSITIONS
 
this->scene_container[0] = home; 
this->scene_container[0].tag = 0; 
this->scene_container[0].image = "==== \n!!!! \n ==========================\n%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \n || _____ _____|| \n || | | | | | ||| \n || |-|-| |-|-||| \n || ##### #####|| \n || || \n || ______________|| \n || | | |@@@@| | ||| \n || |-|-|@@@@|-|-||| \n || #####@@*@#####|| \n || @@@@|| \n******************____**************** \n **************************************"; 
this->scene_container[0].scene_message = "\nWelcome to the home screen of the game. This is the starting point of the game.\n\nIf you need the instructions, type 'help' and press enter. \n\n\nYou can go North , South , East , or West. \n\n"; 
this->scene_container[0].commands[0] = "west";
this->corresponding_responses[0].response_container[0] = west_fork; 
this->scene_container[0].commands[1] = "north";
this->corresponding_responses[0].response_container[1] = gate; 
this->scene_container[0].commands[2] = "south";
this->corresponding_responses[0].response_container[2] = south_fork; 
this->scene_container[0].commands[3] = "east"; 
this->corresponding_responses[0].response_container[3] = woods; 
this->scene_container[0].commands[4] = "NORTH";
this->corresponding_responses[0].response_container[4] = gate; 
this->scene_container[0].commands[5] = "SOUTH"; 
this->corresponding_responses[0].response_container[5] = south_fork; 
this->scene_container[0].commands[6] = "EAST"; 
this->corresponding_responses[0].response_container[6] = woods;
this->scene_container[0].commands[7] = "WEST"; 
this->corresponding_responses[0].response_container[7] = west_fork; 
this->scene_container[1] = woods; 
this->scene_container[1].tag = 1; 
this->scene_container[1].image = "(_/_)\n(__<_{}\n(_/_)\n |/ |\n/| /|\n /|\n |/\n ,.,.,|.,.,.\n ^`^`^`^`^`^ \n(_/_)\n(__<_{}\n(_/_)\n |/ |\n/| /|\n /|\n |/\n ,.,.,|.,.,.\n^`^`^`^`^`^ \n"; 
this->scene_container[1].scene_message = "Hm. These woods seem to be too thick to navigate through. \nCan you use something from your inventory?\n\n"; 
this->scene_container[2] = south_fork; 
this->scene_container[2].tag = 2; 
this->scene_container[2].image = "________ .====\n [________>< :===\n '====\n";
this->scene_container[2].scene_message = "You've hit the Southern road's fork. Would you like the southwest or southeast?\n\n"; 
this->scene_container[3] = west_fork; 
this->scene_container[3].tag = 3; 
this->scene_container[3].image = "________ .====\n [________>< :===\n '====\n";
this->scene_container[3].scene_message = "You've hit the Western road's fork. Would you like the northwest or southwest?\n\n"; 
this->scene_container[4] = gate; 
this->scene_container[4].tag = 4; 
this->scene_container[4].image = "\n|===|===|===|===|===|===|\n|===|===|===|===|===|===|\n|===|===|===|===|===|===|\n|===|===|===|===|===|===|\n\n.-''-.\n/ .--. N\n / /N N\n | || |\n | |.-''-.|\n /N/`.::::.`N\n||| ::/ N/:: ;\n||; ::N__/:: ;\n V/ '::::' /\n `=':-..-'`\n"; 
this->scene_container[4].scene_message = "\n\nThere's a gate, but it seems to be locked...\n\nDo you have anything that you can unlock it with?\n\n"; 
this->scene_container[5] = south_monster; 
this->scene_container[5].tag = 5; 
this->scene_container[5].image = " /()` \n V V___/ | \n /- _ `-/' \n (/V N /^ \n / /| `I\n O O) /| \n `-^--'`< ' \n(_.) _ )/ \n `.___N`/ \n`-----' / \n <----. __ / __N \n <----|====O)))==) )) /====\n<----'`--' `.__,' V \n|| \nN/ \n______( (_^______ \n ,' ,-----'|/ \n `--{__________)V ";
this->scene_container[5].scene_message = "\nThere is a fire demon, but he is too powerful. \nYou cannot defeat him without a weapon.\n\nChoose an item from your inventory to fight him.\n\n"; 
this->scene_container[6] = dead_end;
this->scene_container[6].tag = 6;
this->scene_container[6].image = "_____ _____ _____ _____ _____ _____ ___\n|_____|_____|_____|_____|_____|_____|___\n___|_____|_____|_____|_____|_____|_____|\n|_____|_____|_____|_____|_____|_____|___\n___|_____|_____|_____|_____|_____|_____|\n|_____|_____|_____|_____|_____|_____|___\n";
this->scene_container[6].scene_message = "\n\nYou seem to have hit a dead end...\n\n";
 
this->scene_container[7] = well_with_rope; 
this->scene_container[7].tag = 7;
this->scene_container[7].image = " _\nV V\nV '.V\n V'.V\n V '.V \n~|~~~~~|~\n +|-===-|;-.\n | | `-\n _|-----|_Artist : Lester - AMC\n|--.....--|\n|--.....--|\n|--.....--| .''.\n|--.....--| |~~|\n ~~-----~~~~"; 
this->scene_container[7].scene_message = "\n\nThere is a well, but there doesn't seem to be any rope...\n\nIs there an object you can use from your inventory?\n\n\n"; 
this->scene_container[8] = well_without_rope; 
this->scene_container[8].tag = 8; 
this->scene_container[8].image = " _\nV V\nV '.V\n V'.V\n V '.V \n~|~~~~~|~\n +|-===-|;-.\n | | | `-\n _|--|--|_Artist : Lester - AMC\n|--.....--|\n|--.....--|\n|--.....--| .''.\n|--.....--| |~~|\n ~~-----~~~~";
this->scene_container[8].scene_message = "\n\nI see you can use your noggin! You attach the rope to the well and pull the bucket up. \nInside the bucket, you find a sword. The sword goes into your inventory.\n\n\n"; 
this->scene_container[9] = axe_view; 
this->scene_container[9].tag = 9; 
this->scene_container[9].image = " _________________.---.______\n (_(______________(_o o_(____()\n .___.'. .'.___.\n V oYo V\nV V____V V\n'.__'-'__.'\n'''\nartist : mrf"; 
this->scene_container[9].scene_message = "\n\nHere is an axe. Would you like to put it into your inventory?\n\n"; 
this->scene_container[10] = without_axe_view;
this->scene_container[10].tag = 10; 
this->scene_container[10].image = "(THERE IS NOTHING HERE)"; 
this->scene_container[10].scene_message = "You have taken the axe. There is nothing left here.\n\n"; 
this->scene_container[11] = shovel_and_tree_stump;
this->scene_container[11].tag = 11; 
this->scene_container[11].image = "( . )\n)( )\n . '.' . ' . \n .' ) ( . ), ( , )( .\n ). , ( .( ) ( , ') .' ( ,)\n (_,) . ), ) _) _,') (, ) '. ) ,. (' )\n^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n\nauthor:jgs";
this->scene_container[11].scene_message = "\n\nYou have cut down the forest. Among the trees you found a rope. Later, you find a shovel.\n\nThey have both been added to your inventory.\n\n\n"; 
this->scene_container[12] = well_sword; 
this->scene_container[12].tag = 12; 
this->scene_container[12].image = " />_________________________________\n[########[]_________________________________>\n >\n Artist : Krog"; 
this->scene_container[12].scene_message = "\n\nWell, now look what the cat dragged in! You use the rope to complete the well and found a sword. \n\nThe sword has been added to your inventory.\n\n\n"; 
this->scene_container[13] = x_marks_spot; 
this->scene_container[13].tag = 13; 
this->scene_container[13].image = "# # \n ## \n # # \n#\n # # \n ## \n# #\n"; 
this->scene_container[13].scene_message = "\n\nCongragulations! You have defeated the monster. You find a red x on the ground.\n\nChoose the object that will help.\n\n\n"; 
this->scene_container[14] = key_view; 
this->scene_container[14].tag = 14; 
this->scene_container[14].image = ".--.\n /.-. '----------.\n \'-' .--'--''-'-'\njgs '--'";
this->scene_container[14].scene_message = "\n\nWell look at that, you dug up a key! It has been added to your inventory.\n\n"; 
this->scene_container[15] = unlocked_fence; 
this->scene_container[15].tag = 15; 
this->scene_container[15].image = "\n|===|===|===|===|===|===|\n|===|===|===|===|===|===|\n|===|===|===|===|===|===|\n|===|===|===|===|===|===|";
this->scene_container[15].scene_message = "\n\nYou have unlocked the fence... type 'mage' for the super surprise ending...\n\n\n"; 
this->scene_container[16] = reaped_forest; 
this->scene_container[16].tag = 16; 
this->scene_container[16].image = "( . )\n)( )\n . '.' . ' . \n .' ) ( . ), ( , )( .\n ). , ( .( ) ( , ') .' ( ,)\n (_,) . ), ) _) _,') (, ) '. ) ,. (' )\n^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n\nauthor:jgs"; 
this->scene_container[16].scene_message = "\n\n(THERE IS NOTHING LEFT HERE, YOU ALREADY TOOK SHOVEL AND ROPE)\n\n\n"; 
}
string choose_inv_number(int scenery){
this->scene_container[scenery].draw_scene(); 
if(this->cPlayer.inventory[0] == "null"){
system("PAUSE"); 
return "empty"; 
}
int num_choice; 
cout << "\n\n"; 
system("PAUSE"); 
this->cPlayer.print_inventory(); 
cout << "Be sure to type the NUMERIC value of the object, NOT the actual name for it.\n\n\n"; 
string inv_choice; 
cin >> num_choice; 
inv_choice = this->cPlayer.inventory[num_choice-1]; 
return inv_choice; 
}
char axe_decision(){
if(this->has_been_axe == 0){
this->scene_container[9].draw_scene(); 
string axe_choice; 
cin >> axe_choice;
if(axe_choice == "yes" || axe_choice == "YES"){
has_been_axe = 1; 
this->cPlayer.pick_up("axe");
return 'y'; 
}
else{
return 'n'; 
}
}
if(this->has_been_axe == 1){
return 'z'; 
}
}
};
struct MessageDecipherMainGame{
int current_scene; 
bool game_won; 
game_struct cGame; 
void init(){
game_won = 0; 
this->current_scene = 0; 
this->cGame.init(); 
this->cGame.cPlayer.init(); 
}
void render_current_scene(){
cGame.scene_container[this->current_scene].draw_scene(); 
cGame.cPlayer.position = this->current_scene; 
}
void begin_cipher(string input){
if(input == "back" || input == "BACK" || input == "home" || input == "HOME"){
this->current_scene = 0; 
}
if(input == "inventory" || input == "INVENTORY"){
this->cGame.cPlayer.print_inventory(); 
system("PAUSE"); 
}
if(input == "mage" || input == "MAGE" && this->current_scene == 15){
this->game_won = 1; 
}
if(input == "help" || input == "HELP"){
system("cls"); 
cout << "-=-=-=-INSTRUCTIONS-=-=-=-=-\n\n\n\nThe name of this game is Mage Magic.\n\n\n\nThe object of the game is to pick things up and navigate until you solve the \nmystery of Wizard Island.\n\n\n\nYou must have fun. \n\n\n\nType 'back' or 'home' at any point to return home; the starting point.\n\n\n\nTo see your inventory at any point, type 'inventory'.\n\n\n"; 
system("PAUSE"); 
}
if(input == "west" && this->current_scene == 0){
this->current_scene = this->cGame.scene_container[3].tag; 
}
if(input == "dig" || input == "dig up x" && this->current_scene == 13){
this->current_scene = this->cGame.scene_container[14].tag; 
this->cGame.cPlayer.pick_up("key");
}
if(input == "east" && this->current_scene == 0){
string decision;
decision = this->cGame.choose_inv_number(1); 
//put loop here
if(decision == "axe"){
this->current_scene = this->cGame.scene_container[16].tag; 
if(this->cGame.has_been_forest == 0){
this->current_scene = this->cGame.scene_container[11].tag; 
this->cGame.cPlayer.pick_up("shovel");
this->cGame.cPlayer.pick_up("rope"); 
this->cGame.has_been_forest = 1; 
}
}
if(decision == "empty"){
system("cls");
cout << "Whoops! It seems as though you have no items in your inventory.\n\n\nBringing you back home...\n\n\n"; 
system("PAUSE"); 
this->current_scene = this->cGame.scene_container[0].tag; 
}
if(decision != "empty" && decision != "axe"){
system("cls"); 
cout << "Whoops! It seems as though you have no items in your inventory.\n\n\nBringing you back home...\n\n\n"; 
system("PAUSE"); 
this->current_scene = this->cGame.scene_container[1].tag; 
}
}
if(input == "south" && this->current_scene == 0){ 
this->current_scene = this->cGame.scene_container[2].tag; 
}
if(input == "north" && this->current_scene == 0){
string fence_decision; 
fence_decision = this->cGame.choose_inv_number(4);
if(fence_decision == "key"){
this->current_scene = this->cGame.scene_container[15].tag; 
}
if(fence_decision == "empty"){
system("cls");
cout << "Whoops! It seems as though you have no items in your inventory.\n\n\nBringing you back home...\n\n\n"; 
system("PAUSE"); 
}
if(fence_decision != "empty" && fence_decision != "key"){
system("cls"); 
cout << "I'm sorry. You cannot use this to open the gate.\n\n";
this->current_scene = this->cGame.scene_container[4].tag; 
system("PAUSE"); 
}
}
if(input == "southwest" && this->current_scene == 2){
string weapon_choice;
weapon_choice = this->cGame.choose_inv_number(5); 
if(weapon_choice == "sword"){
string final_obj_choice; 
final_obj_choice = this->cGame.choose_inv_number(13);
if(final_obj_choice == "shovel"){
this->cGame.cPlayer.pick_up("key"); 
this->current_scene = this->cGame.scene_container[14].tag;
}
if(final_obj_choice != "shovel"){
this->current_scene = this->cGame.scene_container[13].tag; 
}
}
if(weapon_choice == "empty"){
system("cls");
cout << "Whoops! It seems as though you have no items in your inventory.\n\n\nBringing you back home...\n\n\n"; 
system("PAUSE"); 
}
if(weapon_choice != "empty" && weapon_choice != "sword"){
system("cls"); 
cout << "You cannot use a " << weapon_choice << " to defeat a fire demon.\n\n\n"; 
system("PAUSE"); 
}
}
if(input == "southeast" && this->current_scene == 2){
this->current_scene = this->cGame.scene_container[6].tag; 
}
if(input == "southwest" && this->current_scene == 3){
string well_choice; 
well_choice = this->cGame.choose_inv_number(7); 
if(well_choice == "rope"){
this->cGame.cPlayer.pick_up("sword"); 
this->current_scene = this->cGame.scene_container[12].tag; 
}
if(well_choice == "empty"){
system("cls");
cout << "Whoops! It seems as though you have no items in your inventory.\n\n\nBringing you back home...\n\n\n"; 
system("PAUSE"); 
this->current_scene = this->cGame.scene_container[0].tag;
}
if(well_choice != "emtpy" && well_choice != "rope"){
system("cls"); 
cout << "I'm sorry, but this can't be used for a well.\n\n\n"; 
system("PAUSE"); 
this->current_scene = this->cGame.scene_container[7].tag; 
}
}
if(input == "northwest" && this->current_scene == 3){
char axe_choice; 
axe_choice = this->cGame.axe_decision(); 
if(axe_choice == 'y'){
//make sure case is right
this->current_scene = this->cGame.scene_container[10].tag; 
}
if(axe_choice == 'n'){
this->current_scene = this->cGame.scene_container[9].tag; 
}
if(axe_choice == 'z'){
this->current_scene = this->cGame.scene_container[10].tag; 
}
}
}
};
int main(int argc, char *argv[])
{ 
system("TITLE Mage Magic"); 
 
MessageDecipherMainGame cMain; 
cMain.init(); 
cMain.cGame.cPlayer.init();
string current_choice; 
bool game_won = 0;
//implement a for loop to cycle through the beginning message
cout << "\n\n\n# # # #\n########## ################ # #### \n# # # # # # ## # # # # # # # ## # ## \n# # # ## # ##### # # # ## # # # \n# # ###### # ### # # # ###### # ### # # \n# # ## ## # # # ## ## # ## \n# # ## #### ####### # ## #### # ####\n\n\n\n\n\n CODED BY THE MZK PROGRAMMING CLUB\n\n\n\n\n\n "; 
system("PAUSE");
while(cMain.game_won == 0){
//main loop of game
cMain.cGame.cPlayer.position = cMain.current_scene;
cMain.render_current_scene(); 
cin >> current_choice; 
cMain.begin_cipher(current_choice); 
cMain.cGame.cPlayer.save_game(); 
} 
system("cls"); 
cout << "\n\n\n\n YYYY OOOOO UUUU WW WW OOOOO NNNN !!! \n YYYY OOOO UUUU WW WW OOOO NNN NN !!! \nYYYYY OOOO UUUU WWW WW OOOO NN N NN !!! \nYYYOOOO UUUUWW WWW WW OOOO NN NNN \nYYYOOOO0UUUUU WWWWOOOO0 NNNN !!!\n\n\n\n\n\n\n ";
system("PAUSE"); 
return EXIT_SUCCESS;
}


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Intermediate category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments

6/10/2012 1:41:08 AMGame

Game code
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.