Skip to content
Permalink
master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <sqlite3.h>
#include <ncurses.h>
#include <vector>
#include <random>
#include <ctime>
#include <math.h>
#include "sql.h"
std::vector<std::string> new_item(int seed, std::vector<std::string> vec)
{
srand(time(0) + seed);
int name = (rand()%3) + 1;
if (name == 1)
{
vec.push_back("Sword");
}
else if (name == 2)
{
vec.push_back("Bow");
}
else
{
vec.push_back("Staff");
}
srand(time(0) + seed);
int strength = (rand() % 20) + 1;
vec.push_back("Strength increase = " + std::to_string(strength));
int hp = (rand() % 20) + 1;
vec.push_back("HP Increase = " + std::to_string(hp));
int gold = (pow((double)strength, 2)) + hp;
vec.push_back("Gold cost = " + std::to_string(gold));
return vec;
}
int limit()
{
std::vector<std::string> ids;
std::string query = "SELECT ID FROM Items";
ids = sql_call(query);
if (ids.size() > 3)
{
return 1;
}
else
{
return 0;
}
}
int shop(int gold)
{
initscr();
noecho();
std::vector<std::string> item1;
item1 = new_item(1, item1);
std::vector<std::string> item2;
item2 = new_item(2, item2);
std::vector<std::string> item3;
item3 = new_item(3, item3);
int y = 10;
std::string s = "You have " + std::to_string(gold) + " gold.";
printw(s.c_str());
move(y, 30);
for(std::string x : item1)
{
printw(x.c_str());
y = y + 1;
move(y, 30);
}
y = y + 2;
move(y, 30);
for(std::string x : item2)
{
printw(x.c_str());
y = y + 1;
move(y, 30);
}
y = y + 2;
move(y, 30);
for(std::string x : item3)
{
printw(x.c_str());
y = y + 1;
move(y, 30);
}
move((y + 10), 0);
printw("Press 1, 2 or 3 to buy an item or press ESC to exit the shop.");
refresh();
int inp;
do
{
inp = getch();
if (inp == 49)
{
std::string check = item1[0];
if (check == "sold")
{}
else
{
int total;
std::string cost = item1[3];
cost = cost.substr(11, cost.find(" "));
total = gold - (std::stoi(cost));
if (total >= 0)
{
std::string itemName = item1[0];
move((y + 12), 0);
std::string message = "Are you sure you want to buy this " + itemName + "? You will be left with " + std::to_string(total) + " gold. Y or N.";
printw(message.c_str());
int confirm = 0;
do
{
inp = getch();
if (inp == 110)
{
move((y + 12), 0);
clrtoeol();
confirm = 1;
}
else if (inp == 121)
{
int lim = limit();
if (lim == 1)
{
move((y + 12), 0);
clrtoeol();
printw("You can't carry more items.");
confirm = 1;
}
else
{
move((y + 12), 0);
clrtoeol();
message = "You have bought " + itemName;
mvprintw((y + 12), 0,message.c_str());
gold = total;
s = "You have " + std::to_string(gold) + " gold.";
move(0, 0);
clrtoeol();
printw(s.c_str());
for (int i = 10; i < 14; i++)
{
move(i, 0);
clrtoeol();
}
item1[0] = "sold";
std::string hp = item1[2];
hp = hp.substr(14, hp.find(" "));
std::string str = item1[1];
str = str.substr(20, str.find(" "));
std::string query = "INSERT INTO Items(Name, Description, HP_Buff, Strength_Buff, Equipped, Price) VALUES('" + itemName + "', '', " + hp + ", " + str + ", 0, " + cost + ")";
sql_call(query);
confirm = 1;
}
}
else
{}
} while(confirm != 1);
}
else
{
move((y + 12), 0);
clrtoeol();
printw("Not enough gold.");
}
}
}
else if (inp == 50)
{
std::string check = item2[0];
if (check == "sold")
{}
else
{
int total;
std::string cost = item2[3];
cost = cost.substr(11, cost.find(" "));
total = gold - (std::stoi(cost));
if (total >= 0)
{
std::string itemName = item2[0];
move((y + 12), 0);
std::string message = "Are you sure you want to buy this " + itemName + "? You will be left with " + std::to_string(total) + " gold. Y or N.";
printw(message.c_str());
int confirm = 0;
do
{
inp = getch();
if (inp == 110)
{
move((y + 12), 0);
clrtoeol();
confirm = 1;
}
else if (inp == 121)
{
int lim = limit();
if (lim == 1)
{
move((y + 12), 0);
clrtoeol();
printw("You can't carry more items.");
confirm = 1;
}
else
{
move((y + 12), 0);
clrtoeol();
message = "You have bought " + itemName;
mvprintw((y + 12), 0,message.c_str());
gold = total;
s = "You have " + std::to_string(gold) + " gold.";
move(0, 0);
clrtoeol();
printw(s.c_str());
for (int i = 16; i < 20; i++)
{
move(i, 0);
clrtoeol();
}
item2[0] = "sold";
std::string hp = item2[2];
hp = hp.substr(14, hp.find(" "));
std::string str = item2[1];
str = str.substr(20, str.find(" "));
std::string query = "INSERT INTO Items(Name, Description, HP_Buff, Strength_Buff, Equipped, Price) VALUES('" + itemName + "', '', " + hp + ", " + str + ", 0, " + cost + ")";
sql_call(query);
confirm = 1;
}
}
else
{}
} while(confirm != 1);
}
else
{
move((y + 12), 0);
clrtoeol();
printw("Not enough gold.");
}
}
}
else if (inp == 51)
{
std::string check = item3[0];
if (check == "sold")
{}
else
{
int total;
std::string cost = item3[3];
cost = cost.substr(11, cost.find(" "));
total = gold - (std::stoi(cost));
if (total >= 0)
{
std::string itemName = item3[0];
move((y + 12), 0);
std::string message = "Are you sure you want to buy this " + itemName + "? You will be left with " + std::to_string(total) + " gold. Y or N.";
printw(message.c_str());
int confirm = 0;
do
{
inp = getch();
if (inp == 110)
{
move((y + 12), 0);
clrtoeol();
confirm = 1;
}
else if (inp == 121)
{
int lim = limit();
if (lim == 1)
{
move((y + 12), 0);
clrtoeol();
printw("You can't carry more items.");
confirm = 1;
}
else
{
move((y + 12), 0);
clrtoeol();
message = "You have bought " + itemName;
mvprintw((y + 12), 0,message.c_str());
gold = total;
s = "You have " + std::to_string(gold) + " gold.";
move(0, 0);
clrtoeol();
printw(s.c_str());
for (int i = 22; i < 26; i++)
{
move(i, 0);
clrtoeol();
}
item3[0] = "sold";
std::string hp = item3[2];
hp = hp.substr(14, hp.find(" "));
std::string str = item3[1];
str = str.substr(20, str.find(" "));
std::string query = "INSERT INTO Items(Name, Description, HP_Buff, Strength_Buff, Equipped, Price) VALUES('" + itemName + "', '', " + hp + ", " + str + ", 0, " + cost + ")";
sql_call(query);
confirm = 1;
}
}
else
{}
} while(confirm != 1);
}
else
{
move((y + 12), 0);
clrtoeol();
printw("Not enough gold.");
}
}
}
} while(inp != 27);
endwin();
return 0;
}