Skip to content
Permalink
Browse files
  • Loading branch information
carreira committed Mar 27, 2019
2 parents 57f6f34 + 39870ac commit 637fdd23b0f0e6b3754d3275085a5ad91bebe912
Show file tree
Hide file tree
Showing 4 changed files with 213 additions and 115 deletions.
@@ -1,37 +1,33 @@
#pragma once
#include "stdafx.h" // Visual studio always need this header
#include "pch.h"
#include "stdafx.h" // Necessary for Visual studio
#include "pch.h" // turning off precompiled headers
#include <iostream>
#include <cstdlib>
#include "libsqlite.hpp" // The sqlite connection class
#include "libsqlite.hpp"
#include "Stats.h"// The sqlite connection class
#include <time.h> // taking value from the cpu (for random)
#include <stdio.h> //to make random ...
#include "Save.h"
#include <cstdlib> // Required for random function/numbers
#include "libsqlite.hpp" // Connection class for sqlite
#include "Stats.h"// Taking data/statistics for "Player" and "Monster" from database
#include <time.h> // taking value from the cpu (for random numbers)
#include <stdio.h>
#include "Save.h" // is calling finction, which is making changes in database for "Player"

using namespace std;

int cnt;


int cnt; // counter: to limited "heavy attack" mode


//Generating "fight" between player and "monster" by using variables taken from database. Return modified data and save it in database.
void playerAttack()
{

playerStats();
//monsterStats(int lvl);
//Player and "monster" variables (live and damage)
int M_HP;
int P_HP;
M_HP = Monster_Health;
P_HP = Player_Health;
int damage;
int damageM;
cnt = 20;
cnt = 20;


while (M_HP> 0 && P_HP >0)
while (M_HP> 0 && P_HP >0) // Finction will be executed only if player's and monster's live/HP is above 0.
{
int i;

@@ -41,7 +37,7 @@ void playerAttack()
system("cls");
switch(i)
{
case 1:
case 1: //"Normal attack" - taking monster's and player's data from SQL and modified it by random function.
srand(time(NULL));
damage = ((rand() % ((Player_Max_attack + 1) - Player_Min_attack)) + Player_Min_attack) - Monster_Defence;
if (damage < 0)
@@ -50,7 +46,7 @@ void playerAttack()
}

break;
case 2:
case 2: //"Heavy attack" - stronger than "normal attack" and limited by counter (cnt)
while (cnt != 0)
{
srand(time(NULL));
@@ -66,7 +62,7 @@ void playerAttack()
}
cout << "You attacked with " << damage << " damage" << endl;
M_HP = M_HP - damage;
if (M_HP < 0)
if (M_HP < 0) // Checking if monster HP after attack is not showing negative value, otherwise changing it into 0.
{
M_HP = 0;
}
@@ -77,33 +73,24 @@ void playerAttack()
damageM = 0;
}




//damageM = Monster_attack - Player_Defence;
cout << "Monster takes " << damageM << " HP" << endl;
P_HP = P_HP - damageM;
if (P_HP < 1)
if (P_HP < 0) // Checking if player HP after attack is not showing negative value, otherwise changing it into 0.
{
P_HP = 0;
cout << "Game Over" << endl;
cout << "No more life " << endl;
}
cout << "Your HP: " << P_HP << endl;






if (cnt == 0 && i == 2)
if (cnt == 0 && i == 2) // User is not allowed to use a heavy attack option (buttom 2) while counter is 0.
{
cout << "Sorry, no more heavy attack option in this round! " << endl;
}

//F_SAVE()

}
if (M_HP < 1 && P_HP>0)
if (M_HP < 1 && P_HP>0) /* While player won the game, printing stats, taking monster "gold" and "experience", and adding it into
player database. */
{
cout << "You win! " << endl;
cout << "You get: " << Monster_Gold << " gold, and " << Monster_Exp << " experience points " << endl;
@@ -115,22 +102,8 @@ void playerAttack()
}

Player_Health = P_HP;
//int i;
F_SAVE();
cout << "Press 0 to go to main" << endl;

}


//cin >> i;

/*switch (i)
{
case 0:
pAttack = false;
break;
}*/


save(); // Saving all changes in database.
cout << "Press 0 to go to main" << endl;

//void monsterAttack()
}
@@ -9,6 +9,10 @@
using namespace std;


//Bartek
void save(); //Saves any changes to the database


void save() //Saves every changes in the database
{
try
@@ -1,17 +1,16 @@
#pragma once
// testpaddy.cpp : This file contains the 'main' function. Program execution begins and ends there.
//
#include "stdafx.h" // Visual studio always need this header
#include "pch.h"
#include "stdafx.h" // Necessary for Visual studio
#include "pch.h" // turning off precompiled headers
#include <iostream>
#include <cstdlib>
#include "libsqlite.hpp" // The sqlite connection class
#include <stdlib.h>
#include "libsqlite.hpp" // The sqlite connection class
#include <cstdlib> // general utilities library
#include "libsqlite.hpp" // Connection class for sqlite
#include <stdlib.h> // general utilities library
using namespace std;



// Player variables which represent data from SQLite database
string Player_Name;
int Player_id;
int Player_Lvl;
@@ -21,7 +20,7 @@ int Player_Min_attack;
int Player_Max_attack;
int Player_Defence;
int Player_Gold;
//monster
// Monster variables which represent data from SQLite database
string Monster_Name;
int Monster_id;
int Monster_Lvl;
@@ -32,24 +31,20 @@ int Monster_Defence;
int Monster_Gold;



//Displaying player statistics when function is call.
void playerStats()
{
string sqliteFile = "GameProject.db"; //set database name
string sqliteFile = "GameProject.db"; //Define database file name
try
{
sqlite::sqlite db(sqliteFile);
//string id;
auto cur = db.get_statement();
//cout << "Enter the id ";
//cin >> id;
//cur->set_sql("select * from Player WHERE Player_id = ?;");
cur->set_sql("select * from Player ");
//cur->bind(1, id);
cur->prepare();
sqlite::sqlite db(sqliteFile); //Opening database file
auto cur = db.get_statement(); //Statement which allows taking data from SQLite database file
cur->set_sql("select * from Player "); // SQL command to get data from Player database table
cur->prepare(); //Preparing for execution

while (cur->step())
while (cur->step()) // Going through the database values
{
// Assigning database values to variables
Player_id = cur->get_int(0);
Player_Name = cur->get_text(1);
Player_Lvl = cur->get_int(2);
@@ -61,20 +56,16 @@ void playerStats()
Player_Gold = cur->get_int(8);
}



/*cout << "Player name: " << Player_Name << endl;
cout << "Current level: " << Player_Lvl << endl;
cout << "Experience: " << Player_Exp << endl;
cout << "Health: " << Player_Health << " " << Player_Min_attack << " " << Player_Max_attack << " " << Player_Defence << " " << Player_Gold << " " << endl;
*/
}
catch (sqlite::exception e) // catch all sql issues
catch (sqlite::exception e) // Catching all sql issues
{
std::cerr << e.what() << std::endl; //Display errors
std::cerr << e.what() << std::endl; //Displaying errors (if some occur)
}
}



//Bartek
void monsterStats(int lvl)
{
string sqliteFile = "GameProject.db";
@@ -111,53 +102,31 @@ void monsterStats(int lvl)

}


/*This function is called from "playerAttack" function. While "monster" is beaten it is taking its id and storing in table "Monsters_Beaten"
to do not fight with the same opponent second time */
void monstersBeaten()
{
string sqliteFile = "GameProject.db"; //set database name
string sqliteFile = "GameProject.db"; //Define database file name
try
{
sqlite::sqlite db(sqliteFile);

auto cur = db.get_statement();
cur->set_sql("INSERT INTO Monsters_Beaten (Monster_id, Player_id) values('"+to_string(Monster_id)+"','"+to_string(Player_id)+"')");
cur->prepare();
sqlite::sqlite db(sqliteFile); //Opening database file
auto cur = db.get_statement(); //Statement which allows taking data from SQLite database file
cur->set_sql("INSERT INTO Monsters_Beaten (Monster_id, Player_id) values('"+to_string(Monster_id)+"','"
+to_string(Player_id)+"')"); // SQL command to save "Monster_id" and "Player_id" in new table "Monster Beaten"
cur->prepare(); //Preparing for execution

while (cur->step())
while (cur->step()) // Going through the database values
{
Monster_id = cur->get_int(0);
Player_id = cur->get_int(1);

}



/*cout << "Player name: " << Player_Name << endl;
cout << "Current level: " << Player_Lvl << endl;
cout << "Experience: " << Player_Exp << endl;
cout << "Health: " << Player_Health << " " << Player_Min_attack << " " << Player_Max_attack << " " << Player_Defence << " " << Player_Gold << " " << endl;
*/
}
catch (sqlite::exception e) // catch all sql issues
catch (sqlite::exception e) // Catching all sql issues
{
std::cerr << e.what() << std::endl; //Display errors
std::cerr << e.what() << std::endl; //Displaying errors (if some occur)
}
}



/*Player_Name = cur->get_text(1);
Player_Lvl = cur->get_int(2);
Player_Exp = cur->get_int(3);
Player_Health = cur->get_int(4);
Player_Min_attack = cur->get_int(5);
Player_Max_attack = cur->get_int(6);
Player_Defence = cur->get_int(7);
Player_Gold = cur->get_int(8);
Monster_Name = cur->get_text(1);
Monster_Lvl = cur->get_int(7);
Monster_Exp = cur->get_int(6);
Monster_Health = cur->get_int(2);
Monster_attack = cur->get_int(4);
Monster_Defence = cur->get_int(3);
Monster_Gold = cur->get_int(5);*/
}

0 comments on commit 637fdd2

Please sign in to comment.