Skip to content

Commit

Permalink
made the d EVEN BETTER
Browse files Browse the repository at this point in the history
  • Loading branch information
101itsGabe committed Sep 26, 2023
1 parent 8ee8db7 commit 1296b60
Show file tree
Hide file tree
Showing 8 changed files with 179 additions and 83 deletions.
43 changes: 28 additions & 15 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,14 @@ void renderText(SDL_Renderer *renderer, Tile *tile, TTF_Font *font, int check)
int tileY = tile->getTileData().tileY;

string tileId;

if (check == 3)
tileId = "3P";
else if (check == 2)
tileId = "2P";
else if (check == 1)
tileId = "H";
else

tileId = to_string(tile->getTileId());
SDL_Color textColor = {85, 85, 85};
if (font != NULL)
Expand All @@ -93,10 +93,11 @@ void renderText(SDL_Renderer *renderer, Tile *tile, TTF_Font *font, int check)
}
}

Menu renderMenu(SDL_Renderer *renderer, TTF_Font *font, Tile *t, int windowWidth, int windowHeight)
Menu renderMenu(SDL_Renderer *renderer, TTF_Font *font, vector<Tile> *tList, int windowWidth, int windowHeight, Player *p)
{
SDL_Rect rect;
Menu curMenu(windowWidth, windowHeight);
curMenu.setData(p, tList, windowWidth);
rect.x = curMenu.getMenuData().menuX;
rect.y = curMenu.getMenuData().menuY;
rect.w = curMenu.getMenuData().menuW;
Expand Down Expand Up @@ -228,10 +229,6 @@ bool InitSDL()
printf("Failed to load image. Error: %s\n", IMG_GetError());
}

random_device rd;
mt19937 eng(rd());
int check = 0;

// Show the updated Frame
SDL_RenderPresent(renderer);

Expand All @@ -242,7 +239,7 @@ bool InitSDL()
bool clicked = false;
bool menu = false;
bool highlight = true;
bool players = true;
bool move = false;
Player *clickedPlayer = nullptr;
while (!quit)
{
Expand Down Expand Up @@ -274,7 +271,8 @@ bool InitSDL()
}
}
// cout << curTile->getTileId() << endl;

Player &fPlayer = (*PM.getPlayerList())[0];
GM.SetBallPlayer(fPlayer);
if (e.type == SDL_MOUSEBUTTONDOWN && e.button.button == SDL_BUTTON_LEFT)
{
if (curTile)
Expand All @@ -287,7 +285,6 @@ bool InitSDL()
{
clickedPlayer = &p;
clickedPlayer->setClicked(true);
GM.SetCurPlayer(p);
menu = true;
break;
}
Expand All @@ -296,11 +293,16 @@ bool InitSDL()
}
}

if (e.type == SDL_KEYDOWN && e.key.keysym.sym == SDLK_b)
{
move = true;
}

PM.RenderPlayers(renderer, tiles);
if (menu)
{
highlight = false;
Menu curMenu = renderMenu(renderer, font, curTile, windowWidth, windowHeight);
Menu curMenu = renderMenu(renderer, font, TM.getTileList(), windowWidth, windowHeight, clickedPlayer);
if (e.type == SDL_MOUSEBUTTONDOWN && e.button.button == SDL_BUTTON_LEFT)
{
if (curMenu.mouseCheck(mouseX, mouseY) == 1)
Expand All @@ -321,8 +323,6 @@ bool InitSDL()
menu = false;
highlight = true;
clickedPlayer->setClicked(false);
if (clickedPlayer != NULL)
GM.RenderShotPercent(renderer, font, windowWidth, windowHeight, clickedPlayer);
}

else if (curMenu.mouseCheck(mouseX, mouseY) == 2)
Expand All @@ -334,6 +334,16 @@ bool InitSDL()
clickedPlayer->setClicked(false);
}
}
if (clickedPlayer != NULL)
GM.RenderShotPercent(renderer, font, windowWidth, windowHeight, clickedPlayer, PM.getPlayerList());

if (e.type == SDL_KEYDOWN && e.key.keysym.sym == SDLK_ESCAPE)
{
clicked = false;
menu = false;
highlight = true;
clickedPlayer->setClicked(false);
}
}

else if (clicked == true)
Expand All @@ -344,11 +354,14 @@ bool InitSDL()
clickedPlayer->SetTile(curTile->getTileId());
clickedPlayer->setClicked(false);
clicked = false;
players = false;
}
}

GM.RenderShotPercent(renderer, font, windowWidth, windowHeight, clickedPlayer);
else if (move)
{
GM.MoveAI(PM.getPlayerList());
PM.RenderPlayers(renderer, TM.getTileList());
move = false;
}

SDL_RenderPresent(renderer);
}
Expand Down
Binary file modified main.exe
Binary file not shown.
103 changes: 82 additions & 21 deletions src/gameManager.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include "gameManager.h"
#include <iostream>
#include <vector>
#include <climits>
#include <chrono>
#include <algorithm>
#include <SDL.h>
#include <SDL_ttf.h>
Expand Down Expand Up @@ -165,21 +167,15 @@ int GameManager::madeShot(Tile *tile, Player *player, vector<Player> pList)
}
}

Player *dPlayer = ifDefense(*player, pList);
int defense = 0;
if (dPlayer != NULL)
{
defense = dPlayer->getStats().Defense;
}
std::mt19937 gen(std::time(0)); // Initialize with current time as seed
std::uniform_int_distribution<int> dist(1, 100); // Define a distribution
int defense = ifDefense(*player, pList);
std::random_device rd; // Will be used to obtain a seed for the random number engine
std::mt19937 gen(std::chrono::high_resolution_clock::now().time_since_epoch().count() + rd()); // Combine current time with random_device
std::uniform_int_distribution<int> dist(1, 100); // Define a distribution

int r = dist(gen); // Generate a random number
cout << r << endl;
cout << "Random Num: " << r << endl;
if (or32)
{
cout << "Player 3: " << player->getStats().ThreePointer << endl;
cout << "Defense: " << defense << endl;
if (r < (player->getStats().ThreePointer - defense))
{
cout << "GIMMI THREEEEEEE" << endl;
Expand All @@ -189,7 +185,6 @@ int GameManager::madeShot(Tile *tile, Player *player, vector<Player> pList)
}
else if (!or32)
{
cout << "hello?" << endl;
if (r < (player->getStats().TwoPointer - defense))
{
cout << "AND MPS GOOD for another 2" << endl;
Expand All @@ -201,12 +196,19 @@ int GameManager::madeShot(Tile *tile, Player *player, vector<Player> pList)
return 0;
}

void GameManager::RenderShotPercent(SDL_Renderer *renderer, TTF_Font *font, int windowWidth, int windowHeight, Player *p)
void GameManager::RenderShotPercent(SDL_Renderer *renderer, TTF_Font *font, int windowWidth, int windowHeight, Player *p, vector<Player> *pList)
{
SDL_Color color = {0, 150, 30};
if (p != NULL)
{
string chanceText = "Shot %:" + to_string(p->getStats().ThreePointer);

int defense = ifDefense(*p, *pList);
auto it = find(TwoPointerSpots.begin(), TwoPointerSpots.end(), p->GetTile());
string chanceText = "Shot: ";
if (it != TwoPointerSpots.end())
chanceText += to_string(p->getStats().TwoPointer - defense) + "%";
else
chanceText += to_string(p->getStats().ThreePointer - defense) + "%";
SDL_Surface *surface = TTF_RenderText_Solid(font, chanceText.c_str(), color);
if (surface != NULL)
{
Expand All @@ -222,22 +224,81 @@ void GameManager::RenderShotPercent(SDL_Renderer *renderer, TTF_Font *font, int
}
}

void GameManager::SetCurPlayer(Player &p)
void GameManager::SetBallPlayer(Player &p)
{
curPlayer = &p;
ballPlayer = &p;
}

Player *GameManager::ifDefense(Player &player, vector<Player> pList)
int GameManager::ifDefense(Player &player, vector<Player> pList)
{
// Need to implement hoop logic for which team is on which side
int pTile = player.GetTile();
int closestDefenderDistance = INT_MAX; // Initialize with a large value
int closestDefenderDefense = 0;
for (Player &p : pList)
{
if (p.GetTile() + 1 == pTile || p.GetTile() + 15 == pTile || p.GetTile() - 1 == pTile || p.GetTile() - 15 == pTile)
int curTile = p.GetTile();
if (p.getTeam() == "Team2")
{
cout << "DEFENSE" << endl;
return &p;
int temp = p.getStats().Defense;
const int numCols = 15;
const int numRows = 9;

int playerRow = pTile / numCols;
int playerCol = pTile % numCols;
int defenderRow = curTile / numCols;
int defenderCol = curTile % numCols;

int rowDiff = abs(playerRow - defenderRow);
int colDiff = abs(playerCol - defenderCol);

int maxDistance = (numCols - 1) / 2 + (numRows - 1) / 2; // Maximum distance in this grid
int defense = maxDistance - (rowDiff + colDiff) + temp;
if (rowDiff + colDiff < closestDefenderDistance)
{
closestDefenderDistance = rowDiff + colDiff;
if (closestDefenderDistance == 2)
{
cout << "Yeah m8" << endl;
closestDefenderDefense = defense - 5;
}
else
closestDefenderDefense = defense;
}
}
}
return NULL;
if (closestDefenderDistance > 2)
return 0;
if (closestDefenderDefense <= INT_MAX)
return closestDefenderDefense;
else
return 0;
}

void GameManager::MoveAI(vector<Player> *pList)
{
Uint32 startTime = SDL_GetTicks();
Uint32 lastTime = startTime;
const Uint32 interval = 500; // 3 seconds in milliseconds
// Check if it's time to call the function
while (true)
{
Uint32 currentTime = SDL_GetTicks();

// Check if it's time to call the function
if (currentTime - lastTime >= interval)
{
for (Player &p : *pList)
{
if (p.getTeam() == "Team2")
{
cout << p.GetTile() << endl;
p.SetTile(p.GetTile() - 1);
}
}
break;

lastTime = currentTime; // Update lastTime for the next interval
}
}
}
9 changes: 5 additions & 4 deletions src/gameManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ class GameManager
vector<int> TwoPointerSpots;
vector<int> T1StartingPoints;
vector<int> T2StartingPoints;
Player *curPlayer;
Player *ifDefense(Player &, vector<Player>);
Player *ballPlayer;
int ifDefense(Player &, vector<Player>);

public:
GameManager();
Expand All @@ -33,8 +33,9 @@ class GameManager
void AddTeamScore2(int);
void SetUpPointVecs();
void RenderScore(SDL_Renderer *, TTF_Font *, int, int);
void RenderShotPercent(SDL_Renderer *, TTF_Font *, int, int, Player *);
void SetCurPlayer(Player &);
void RenderShotPercent(SDL_Renderer *, TTF_Font *, int, int, Player *, vector<Player> *);
void SetBallPlayer(Player &);
void MoveAI(vector<Player> *);

int madeShot(Tile *, Player *, vector<Player>);
int check3(int);
Expand Down
19 changes: 19 additions & 0 deletions src/menu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,23 @@ void Menu::RenderText(SDL_Renderer *renderer, TTF_Font *font)
vector<TextData> &Menu::getTextData()
{
return textData;
}

void Menu::setData(Player *p, vector<Tile> *tList, int windowWidth)
{
int pTile = p->GetTile();
Tile curTile;
for (Tile &t : *tList)
{
if (t.getTileId() == pTile)
{
curTile = t;
break;
}
}

if (curTile.getTileData().tileX > windowWidth / 2)
menuData.menuX = windowWidth / 4;
else
menuData.menuX = windowWidth / 1.5;
}
40 changes: 21 additions & 19 deletions src/menu.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,43 +5,45 @@
#include <string>
#include "SDL_ttf.h"
#include "SDL.h"
#include <Player.h>
#include <tile.h>

using namespace std;

struct MenuData{
struct MenuData
{
int menuX;
int menuY;
int menuW;
int menuH;
};

struct TextData{
struct TextData
{
string name;
int menuX;
int menuY;
int menuW;
int menuH;

};

class Menu
{
private:
bool isMouse;
MenuData menuData;
vector<TextData> textData;

public:
Menu();
Menu(int, int);

class Menu{
private:
bool isMouse;
MenuData menuData;
vector<TextData> textData;
public:
Menu();
Menu(int,int);

int mouseCheck(int, int);
MenuData getMenuData();
vector<TextData>& getTextData();
int mouseCheck(int, int);
MenuData getMenuData();
vector<TextData> &getTextData();

void RenderText(SDL_Renderer*, TTF_Font*);
void RenderMenu(SDL_Renderer*, TTF_Font*);
void RenderText(SDL_Renderer *, TTF_Font *);
void RenderMenu(SDL_Renderer *, TTF_Font *);
void setData(Player *, vector<Tile> *, int);
};
#endif


Loading

0 comments on commit 1296b60

Please sign in to comment.