master
IoTcat 5 years ago
parent c49de4a9ea
commit a30058e832
  1. 7
      lib/ovo.cpp
  2. 113
      src/controller.cpp
  3. 4
      src/controller.h
  4. 19
      src/map.cpp
  5. 66
      src/map.h
  6. 2
      src/player.cpp
  7. 16
      src/player.h
  8. 23
      src/square.cpp
  9. 41
      src/square.h

@ -2002,7 +2002,10 @@ ovo::data ovo::db::getData(const string& key){
string t_second;
ifstream ins(fName.c_str());
if(!ins) return data;
if(!ins){
data.insert("_isExist", "NO");
return data;
}
while(!ins.eof()){
ins >> t_first >> t_second;
if(_AES){
@ -2014,6 +2017,8 @@ ovo::data ovo::db::getData(const string& key){
return data;
}
/**
* delete data from database
*

@ -180,9 +180,9 @@ void Controller::DrawGame()//绘制游戏界面
/*绘制地图*/
SetColor(3);
map = new Map();
map->PrintInitmap();
this->sb1->print(*p1);
this->sb2->print(*p2);
@ -216,25 +216,25 @@ int Controller::PlayGame()//游戏二级循环
//this->hintBox.print(to_string(this->map->_map[this->map->_playerPos].getPrice()));
//GO
if(this->map->_map[map->_playerPos].type == GO){
if(this->map->_map[map->_playerPos].type == "GO"){
this->p1->gain(200);
this->msgBox.print("Hi Player~", "You are into the GO zone..", "Gain $ 200 !!");
this->UpdateScore();
Sleep(1500);
}else if(this->map->_map[map->_playerPos].type == JAIL){ //JAIL
}else if(this->map->_map[map->_playerPos].type == "JAIL"){ //JAIL
ice_p1 = 1;
this->msgBox.print("Hi Player~", "You are into the JAIL zone..", "You need to wait for one round !!");
Sleep(1500);
}else if(this->map->_map[map->_playerPos].ownerType == ""){ //无人领地
}else if(this->map->_map[map->_playerPos].ownerType == " "){ //无人领地
tmp = this->msgBox.print("Hi Player~", "This square is unoccupied.", "Do you want to BUY it?", "Yes", "No");
if(tmp == 1){
this->map->_map[map->_playerPos].owner = p1->getName();
this->map->_map[map->_playerPos].ownerType = "PLAYER";
p1->cost(this->map->_map[this->map->_playerPos].getPrice());
this->map->_map[map->_playerPos].buy(*p1);
this->map->_map[map->_playerPos].print();
this->UpdateScore();
}
@ -284,26 +284,26 @@ int Controller::PlayGame()//游戏二级循环
Sleep(300);
//GO
if(this->map->_map[map->_aiPos].type == GO){
if(this->map->_map[map->_aiPos].type == "GO"){
this->p2->gain(200);
this->msgBox.print("WOW ~", "Computer into the GO zone..", "Gain $ 200 !!");
this->UpdateScore();
Sleep(1000);
}else if(this->map->_map[map->_aiPos].type == JAIL){ //JAIL
}else if(this->map->_map[map->_aiPos].type == "JAIL"){ //JAIL
ice_p2 = 1;
this->msgBox.print("AHA ...", "Computer are into the JAIL zone..", "He need to wait for one round !!");
Sleep(1000);
}else if(this->map->_map[map->_aiPos].ownerType == ""){ //无人领地
}else if(this->map->_map[map->_aiPos].ownerType == " "){ //无人领地
tmp = this->msgBox.print("Lala..", "Computer Find a unoccupied space.", "What will he do?");
Sleep(1500);
tmp = rand() % 2;
if(tmp == 1){
tmp = rand() % 6;
if(1){
this->map->_map[map->_aiPos].owner = p2->getName();
this->map->_map[map->_aiPos].ownerType = "AI";
p2->cost(this->map->_map[this->map->_aiPos].getPrice());
this->map->_map[map->_aiPos].buy(*p2);
this->map->_map[map->_aiPos].print();
this->UpdateScore();
tmp = this->msgBox.print("Lala..", "Computer Find a unoccupied space.", "The Computer BOUGHT it!!");
@ -316,8 +316,8 @@ int Controller::PlayGame()//游戏二级循环
tmp = this->msgBox.print("Ahm..", "Computer meet his own square.", "What will he do?");
Sleep(1500);
tmp = rand()%2;
if(tmp == 1){
tmp = rand()%6;
if(1){
this->map->_map[map->_aiPos].levelup();
this->map->_map[map->_aiPos].print();
this->p2->cost(this->map->_map[map->_aiPos]._price * .5);
@ -348,6 +348,7 @@ int Controller::PlayGame()//游戏二级循环
ice_p2 = 0;
}
/*调出选择菜单*/
/* if (0) //按Esc键时
{
@ -400,21 +401,39 @@ int Controller::PlayGame()//游戏二级循环
*/
}
/*蛇死亡*/
std::string t_score = "Your Score: ";
t_score += this->score;
tmp = this->msgBox.print("Game Over !!!", "You Lost!", "Play Again?", "OK", "Quit");//绘制游戏结束界面,并返回所选项
hintBox.print("Press esc to quit");
switch (tmp)
{
case 1:
return 1;//重新开始
case 2:
return 2;//退出游戏
default:
return 2;
if(p1->getBalance() > 0){ //玩家赢
hintBox.color(10);
hintBox.print("You WIN!!!");
msgBox.title("Game Over");
tmp = this->msgBox.print("You Win!!!", "Good Job!", "Play Again?", "OK", "Quit");//绘制游戏结束界面,并返回所选项
switch (tmp){
case 1:
return 1;//重新开始
case 2:
return 2;//退出游戏
default:
return 2;
}
}else{
hintBox.color(12);
hintBox.print("You LOST!!!");
msgBox.title("Game Over");
tmp = this->msgBox.print("You LOST!!!", ">_<!", "Play Again?", "OK", "Quit");//绘制游戏结束界面,并返回所选项
p1->reset();
p2->reset();
switch (tmp){
case 1:
return 1;//重新开始
case 2:
return 2;//退出游戏
default:
return 2;
}
}
}
void Controller::UpdateScore()//更新分数
@ -560,6 +579,7 @@ void Controller::Game()//游戏一级循环
{
Start();//开始界面
login();
srand(time(NULL));
while (true)//游戏可视为一个死循环,直到退出游戏时循环结束
{
Select();//选择界面
@ -568,6 +588,7 @@ void Controller::Game()//游戏一级循环
if (tmp == 1) //返回值为1时重新开始游戏
{
system("cls");
delete map;
continue;
}
else if (tmp == 2) //返回值为2时退出游戏
@ -584,9 +605,45 @@ void Controller::Game()//游戏一级循环
void Controller::login(){
system("cls");
std::string uName;
std::cout << "Your Name = ";
std::cin >> uName;
/* 创建两个玩家 */
this->p1 = new Player("yimian", "hhh");
this->p2 = new Player("ai");
this->d_player = db.getData(uName);
srand((unsigned)time(NULL)+55);//设置随机数种子,如果没有 食物的出现位置将会固定
std::string s = "AI";
if(this->d_player["_isExist"] == "NO"){
this->p1 = new Player(uName, uName);
this->p2 = new Player(s.append(m.md5(to_string(rand()))).substr(0, 7));
map = new Map(*p1, *p2);
p1->setMap(map->getId());
p2->setMap(map->getId());
}else{
this->p1 = new Player(this->d_player);
map = new Map(p1->getMap());
this->d_player.clear();
this->d_player = db.getData(map->_p2);
if(this->d_player["_isExist"] == "NO"){
this->p2 = new Player(s.append(m.md5(to_string(rand()))).substr(0, 7));
}else{
this->p2 = new Player(d_player);
}
}
this->sb1 = new Scoreboard(32, 20, 14);
this->sb2 = new Scoreboard(42, 20, 13);

@ -7,6 +7,7 @@
#include "player.h"
#include "map.h"
#include "scoreboard.h"
#include "../lib/ovo.h"
class Controller
{
@ -37,5 +38,8 @@ private:
Player *p1, *p2;
Map *map;
Scoreboard *sb1, *sb2;
ovo::db db;
ovo::math m;
ovo::data d_player;
};
#endif // CONTROLLER_H

@ -10,6 +10,9 @@ void Map::PrintInitmap()
square.print();
Sleep(10);
}
this->_map[this->_playerPos].draw_player();
this->_map[this->_aiPos].draw_ai();
}
@ -33,6 +36,8 @@ void Map::player_move(int i){
this->_playerPos += i;
if(this->_playerPos >= 40) this->_playerPos -= 40;
this->save();
}
@ -54,5 +59,19 @@ void Map::ai_move(int i){
this->_aiPos += i;
if(this->_aiPos >= 40) this->_aiPos -= 40;
this->save();
}
void Map::save(){
this->_d.insert("_map_playerPos", this->_playerPos);
this->_d.insert("_map_aiPos", this->_aiPos);
this->_d.insert("_map_p1", this->_p1);
this->_d.insert("_map_p2", this->_p2);
this->_d.insert("_map_id", this->_id);
db.pushData(this->_d, this->_id);
}

@ -4,6 +4,11 @@
#include <vector>
#include "point.h"
#include "square.h"
#include "../lib/ovo.h"
#include "player.h"
#include <ctime>
#include <cstdlib>
const unsigned short g_mapLength = 13;
const unsigned short g_mapWidth = 7;
@ -12,8 +17,14 @@ const unsigned short g_mapWidth = 7;
class Map
{
public:
Map()//默认构造函数,将正方形各点压入initmap
Map(Player& p1, Player& p2)//默认构造函数,将正方形各点压入initmap
{
this->_p1 = p1.getName();
this->_p2 = p2.getName();
srand(time(NULL) + p1.getName().length() * 1 + p2.getName().length() * 10);
this->_id = m.md5(to_string(rand()).append(p1.getName()).append(p2.getName()));
for(int i = 0; i < g_mapLength; i ++){
this->_map.emplace_back(Square(1 + g_squareLength * i, 1));
@ -34,31 +45,62 @@ public:
this->_map.emplace_back(Square(1, 1 + i * g_squareWidth));
}
this->_map[0].type = GO;
this->_map[20].type = JAIL;
for(int i = 0; i < 40; i ++){
this->_d.insert(i, this->_map[i].getId());
}
this->_map[0].type = "GO";
this->_map[20].type = "JAIL";
this->_playerPos = 0;
this->_aiPos = 0;
player_move(0);
ai_move(0);
//this->_map[15].player_buy();
//this->_map[35].ai_buy();
this->save();
}
Map(std::string id){
this->_d = db.getData(id);
this->_playerPos = atoi(this->_d["_map_playerPos"].c_str());
this->_aiPos = atoi(this->_d["_map_aiPos"].c_str());
this->_id = this->_d["_map_id"];
this->_p1 = this->_d["_map_p1"];
this->_p2 = this->_d["_map_p2"];
for(int i = 0; i < 40; i ++){
this->_map.emplace_back(Square(this->_d[i]));
}
this->_d.clear();
for(int i = 0; i < 40; i ++){
this->_d.insert(i, this->_map[i].getId());
}
};
void PrintInitmap();//绘制初始地图
void player_move(int i);
void ai_move(int i);
void save();
inline std::string getId(){
return this->_id;
}
friend class Controller;
private:
std::vector<Point> initmap;//保存初始地图
std::vector<Square> _map;
std::string _id;
unsigned int _playerPos;
unsigned int _aiPos;
void _draw_player(int pos);
void _draw_ai(int pos);
void _remove_player(int pos);
void _remove_ai(int pos);
std::string _p1, _p2;
ovo::db db;
ovo::data _d;
ovo::math m;
};
#endif // MAP_H

@ -9,7 +9,7 @@ void Player::save(){
this->_d.insert("_player_map", this->_map);
db.pushData(this->_d, this->_uName);
db.pushData(this->_d, this->getName());
}
double Player::cost(double fine){

@ -17,6 +17,7 @@ public:
this->_passwd = m.sha256(passwd);
this->_balance = balance;
this->type = "PLAYER";
this->_map = " ";
};
Player(std::string uName, std::string passwd){
@ -24,6 +25,7 @@ public:
this->_passwd = m.sha256(passwd);
this->_balance = 5000;
this->type = "PLAYER";
this->_map = " ";
};
Player(ovo::data d){
@ -39,12 +41,19 @@ public:
this->_passwd = m.sha256(ai_id);
this->_balance = 5000;
this->type = "AI";
this->_map = " ";
}
void save();
double cost(double fine);
double gain(double money);
void setMap(std::string t_map){
this->_map = t_map;
};
std::string getMap(){
return this->_map;
};
inline std::string getName(){
return m.base64_decode(this->_uName);
}
@ -53,6 +62,12 @@ public:
return this->_balance;
};
inline void reset(){
this->_balance = 5000;
this->_map = "";
this->save();
}
//void AI();
std::string type;
@ -65,6 +80,7 @@ private:
ovo::data _d;
ovo::math m;
ovo::db db;
};

@ -8,7 +8,8 @@ void Square::print()//绘制单元格
{
if(this->ownerType == "PLAYER") SetColor(14);
if(this->ownerType == "AI") SetColor(13);
else if(this->ownerType == "AI") SetColor(13);
else SetColor(3);
for (auto& point : _rowLines) //水平制表
{
@ -22,10 +23,10 @@ void Square::print()//绘制单元格
Sleep(4);
}
if(this->type == GO){
if(this->type == "GO"){
SetCursorPosition(this->_X + 1, this->_Y + 2);
std::cout << " GO " ;
}else if(this->type == JAIL){
}else if(this->type == "JAIL"){
SetCursorPosition(this->_X + 1, this->_Y + 2);
std::cout << "JAIL" ;
}else{
@ -37,7 +38,7 @@ void Square::print()//绘制单元格
std::cout << "$ " << setprecision(4) << this->getPrice();
}
this->save();
}
@ -101,3 +102,17 @@ void Square::buy(Player& p){
else SetColor(13);
this->print();
}
void Square::save(){
this->_d.insert("_square_X", this->_X);
this->_d.insert("_square_Y", this->_Y);
this->_d.insert("_square_price", this->_price);
this->_d.insert("_square_level", this->_level);
this->_d.insert("_square_type", this->type);
this->_d.insert("_square_owner", this->owner);
this->_d.insert("_square_ownerType", this->ownerType);
db.pushData(this->_d, this->_id);
}

@ -6,19 +6,19 @@
#include <vector>
#include "point.h"
#include "player.h"
#include "../lib/ovo.h"
const unsigned short g_squareLength = 4;
const unsigned short g_squareWidth = 4;
enum squareType{NORMAL, GO, JAIL};
class Square
{
public:
Square(int x, int y)//默认构造函数,绘制单元格外形
{
srand(time(NULL) + x * 10 + y *1000);
this->_id = m.md5(to_string(rand() * x * y));
this->_X = x;
this->_Y = y;
this->_drawLines(x, y);
@ -27,12 +27,31 @@ public:
this->_price = (rand() % (290 + 1)) + 10;
this->type = NORMAL;
this->owner = "";
this->ownerType = "";
this->type = "NORMAL";
this->owner = " ";
this->ownerType = " ";
this->save();
}
Square(std::string id){
this->_d = db.getData(id);
this->_id = id;
this->_X = atoi(this->_d["_square_X"].c_str());
this->_Y = atoi(this->_d["_square_Y"].c_str());
this->_level = atoi(this->_d["_square_level"].c_str());
this->_price = atoi(this->_d["_square_price"].c_str());
this->_drawLines(this->_X, this->_Y);
this->type = this->_d["_square_type"];
this->owner = this->_d["_square_owner"];
this->ownerType = this->_d["_square_ownerType"];
}
squareType type;
std::string type;
std::string owner;
std::string ownerType;
void draw_player();
@ -41,6 +60,10 @@ public:
void clear_ai();
void print();//绘制初始地图
void buy(Player& p);
void save();
inline std::string getId(){
return this->_id;
}
inline double getPrice(){
return (double)this->_price * (1 + this->_level * .05);
@ -65,6 +88,10 @@ private:
unsigned int _price;
unsigned int _X;
unsigned int _Y;
std::string _id;
ovo::data _d;
ovo::db db;
ovo::math m;
void _drawLines(int x, int y);

Loading…
Cancel
Save