Error with bullets spawn in SFML - c++

I have a little problem with spawning bullets in my SFML game.
There's code:
#include <SFML/Graphics.hpp>
#include <iostream>
#include <cmath>
int pad_id;
int pociski = 3;
int d_pad_x, d_pad_y;
int klatki;
bool p_1;
bool p_2;
bool p_3;
sf::Texture tekstura, pocisk;
sf::Sprite kulka, pocisk_1, pocisk_2, pocisk_3;
sf::Clock _clock;
float accumulator = 0;
float TIME_STEP = 0.03f;
sf::ContextSettings settings;
sf::RenderWindow window(sf::VideoMode(1920, 1080), "Wrecking balls", sf::Style::Fullscreen, settings);
class bullet
{
public:
sf::Texture bullet_texture;
sf::Sprite _sprite;
void spawn_new_bullet(sf::Vector2f from, sf::Vector2f to);
};
void bullet::spawn_new_bullet(sf::Vector2f from, sf::Vector2f to)
{
bullet_texture.loadFromFile("pocisk.png");
_sprite.setTexture(bullet_texture);
_sprite.setPosition(from);
_sprite.setRotation(atan2(to.y - from.y, to.x - from.x));
window.draw(_sprite);
}
int main()
{
settings.antialiasingLevel = 2;
window.setFramerateLimit(60);
window.setVerticalSyncEnabled(true);
if(!tekstura.loadFromFile("ball.png"))
{
std::cout << "error" << std::endl;
}
if (!pocisk.loadFromFile("pocisk.png"))
{
std::cout << "error" << std::endl;
}
kulka.setTexture(tekstura);
pocisk_1.setTexture(pocisk);
pocisk_2.setTexture(pocisk);
pocisk_3.setTexture(pocisk);
for (int i=0; i<8; i++)
{
if (sf::Joystick::isConnected(i))
{
std::cout << "Pad " << i << " is connected." << std::endl << "Number of buttons: " << sf::Joystick::getButtonCount(i) << std::endl;
pad_id = i;
}
}
kulka.setScale(0.050, 0.050);
pocisk_1.setScale(0.050, 0.050);
pocisk_2.setScale(0.050, 0.050);
pocisk_3.setScale(0.050, 0.050);
while (window.isOpen())
{
accumulator += _clock.restart().asSeconds();
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
{
window.close();
}
if (event.type == sf::Event::JoystickButtonPressed)
{
if (sf::Joystick::isButtonPressed(0, 0))
{
std::cout << "0" << std::endl;
}
if (sf::Joystick::isButtonPressed(0, 1))
{
std::cout << "1" << std::endl;
}
if (sf::Joystick::isButtonPressed(0, 2))
{
std::cout << "2" << std::endl;
}
if (sf::Joystick::isButtonPressed(0, 3))
{
std::cout << "3" << std::endl;
}
if (sf::Joystick::isButtonPressed(0, 4))
{
std::cout << "4" << std::endl;
}
if (sf::Joystick::isButtonPressed(0, 5))
{
std::cout << "5" << std::endl;
if (pociski > 0)
{
if (pociski == 3)
{
p_3 = true;
}
if (pociski == 2)
{
p_2 = true;
}
if (pociski == 1)
{
p_1 = true;
}
pociski -= 1;
}
}
if (sf::Joystick::isButtonPressed(0, 6))
{
std::cout << "6" << std::endl;
}
if (sf::Joystick::isButtonPressed(0, 7))
{
std::cout << "7" << std::endl;
}
if (sf::Joystick::isButtonPressed(0, 8))
{
std::cout << "8" << std::endl;
}
if (sf::Joystick::isButtonPressed(0, 9))
{
std::cout << "9" << std::endl;
}
}
}
if(accumulator > TIME_STEP)
{
window.clear();
if (pociski < 3)
{
klatki += 1;
}
d_pad_x = sf::Joystick::getAxisPosition(pad_id, sf::Joystick::X);
d_pad_y = sf::Joystick::getAxisPosition(pad_id, sf::Joystick::Y);
kulka.move(d_pad_x / 5, d_pad_y / 5);
pocisk_1.setPosition(kulka.getPosition().x + 10, kulka.getPosition().y + 13.75);
pocisk_2.setPosition(kulka.getPosition().x + 20, kulka.getPosition().y + 13.75);
pocisk_3.setPosition(kulka.getPosition().x + 30, kulka.getPosition().y + 13.75);
//std::cout << d_pad_x << d_pad_y << std::endl;
window.draw(kulka);
if (pociski == 1)
{
window.draw(pocisk_1);
}
else if (pociski == 2)
{
window.draw(pocisk_1);
window.draw(pocisk_2);
}
else if (pociski == 3)
{
window.draw(pocisk_1);
window.draw(pocisk_2);
window.draw(pocisk_3);
}
if (p_3 == true)
{
bullet::spawn_new_bullet(kulka.getPosition(), sf::Vector2(100, 100));
p_3 = false;
}
if (p_2 == true)
{
bullet::spawn_new_bullet(kulka.getPosition(), sf::Vector2(100, 100));
p_2 = false;
}
if (p_1 == true)
{
bullet::spawn_new_bullet(kulka.getPosition(), sf::Vector2(100, 100));
p_1 = false;
}
accumulator -= TIME_STEP;
window.display();
}
if (klatki >= 166)
{
if (pociski < 3)
{
pociski += 1;
klatki = 0;
}
}
}
return 0;
}
The error I got in line 162 is: "missing template arguments before '(' token". Can anyone tell me what I should do? And why this method has to be used with a template?

Vector2 is a template type. Either provide the template parameters, or use one of the provided typedefs such as sf::Vector2i – Neil Kirkckquote
Give correct second argument to your function spawn_new_bullet, for example Vector2f().
Cheer!

Related

Problem with array of class objects leads to crash

So I am making a textbased RPG and I wanted to have multiple enemy encounter at once. So I modified my function that determines whether an object of the class Monster, to fill in the Monster(s) into an array of the class monster and set the objects bool to true, as you can see here:
Monster * Map::checkRandomEncounter()
{
Monster* monster = new Monster[3];
for (int i = 0; i < 3; i++)
{
int roll = Random(0, 20);
if (roll <= 5)
{
//No encounter
return 0;
}
else if (roll > 6 && roll < 10)
{
monster[i] = Monster();
monster[i].giveID("Orc", 10, 8, 200, 100, 1, "Short Sword", 2, 7);
monster[i].isFilled();
std::cout << "You encounter an Orc!" << std::endl;
std::cout << "Prepare for battle!" << std::endl;
std::cout << std::endl;
}
else if (roll >= 11 && roll <= 15)
{
monster[i] = Monster();
monster[i].giveID("Goblin", 6, 6, 100, 75, 0, "Dagger", 1, 5);
monster[i].isFilled();
std::cout << "You encounter a Goblin!" << std::endl;
std::cout << "Prepare for battle!" << std::endl;
std::cout << std::endl;
}
else if (roll >= 16 && roll <= 19)
{
monster[i] = Monster();
monster[i].giveID("Ogre", 20, 12, 500, 200, 2, "Club", 3, 8);
monster[i].isFilled();
std::cout << "You encounter an Ogre!" << std::endl;
std::cout << "Prepare for battle!" << std::endl;
std::cout << std::endl;
}
else if (roll == 20)
{
monster[i] = Monster();
monster[i].giveID("Orc Lord",
25,
15,
2000,
1000,
5,
"Two Handed Sword",
5,
20);
monster[i].isFilled();
std::cout << "You encounter an Orc Lord!" << std::endl;
std::cout << "Prepare for battle!" << std::endl;
std::cout << std::endl;
}
}
return monster;
}
The function above will be called in my main function, which looks like this:
int main()
{
srand(time(0));
Map gameMap;
Player mainPlayer;
mainPlayer.createClass();
//Beginn adventure
bool done = false;
while (done == false)
{
// Each Loop Cycle outputs player pos and selection menu
gameMap.printPlayerPos();
int selection = 1;
std::cout << "1) Move 2) Rest 3) View Stats 4) Quit: ";
std::cin >> selection;
Monster* monster = 0;
switch (selection)
{
case 1: // Move the player
gameMap.movePlayer();
if (gameMap.getPlayerXPos() == 2
&& gameMap.getPlayerYPos() == 3)
{
std::cout << "You see a store nearby !" << std::endl;
}
if (gameMap.getPlayerXPos() == 2
&& gameMap.getPlayerYPos() == 4)
{
Store store;
store.enter();
store.showInventory(mainPlayer);
}
//Check for a random encounter
//returns a null pointer if no encounter happened
monster = gameMap.checkRandomEncounter();
//'monster' not null, start battle script
if (monster != 0)
{
//Loop until a break statement
for (int i = 0; i < 3; i++)
{
//Display Hitpoints
mainPlayer.displayHitPoints();
monster[i].displayHitPoints();
std::cout << std::endl;
//Players turn to attack first
**bool runAway = mainPlayer.attack(monster, mainPlayer);** // Crash happening in this area
if (runAway) // Player flees
{
break;
}
if (monster[i].isDead())
{
mainPlayer.victory(monster->getXPReward(),
monster->getGoldReward());
mainPlayer.levelUp(mainPlayer);
}
break;
//Monster attacks
monster[i].attack(mainPlayer);
if (mainPlayer.isDead())
{
mainPlayer.gameOver();
done = true;
break;
}
}
//Pointer to Monster must destroy created instance of Monster
//to make sure that there is no Memory leak
}
delete monster;
monster = 0;
break;
case 2: // resting
mainPlayer.rest();
monster = gameMap.checkRandomEncounter();
//'monster' not null, start battle script
monster = gameMap.checkRandomEncounter();
//'monster' not null, start battle script
if (monster != 0)
{
//Loop until a break statement
for (int i = 0; i < 3; i++)
{
//Display Hitpoints
mainPlayer.displayHitPoints();
monster[i].displayHitPoints();
std::cout << std::endl;
//Players turn to attack first
bool runAway = mainPlayer.attack(monster, mainPlayer);
if (runAway) // Player flees
{
break;
}
if (monster[i].isDead())
{
mainPlayer.victory(monster->getXPReward(),
monster->getGoldReward());
mainPlayer.levelUp(mainPlayer);
}
break;
//Monster attacks
monster[i].attack(mainPlayer);
if (mainPlayer.isDead())
{
mainPlayer.gameOver();
done = true;
break;
}
}
//Pointer to Monster must destroy created instance of Monster
//to make sure that there is no Memory leak
}
delete monster;
monster = 0;
break;
case 3: // viewing stats
mainPlayer.viewStats();
break;
case 4: // quitting
done = true;
break;
}
}
return 0;
}
and finally the last puzzle piece, the function where the player attacks the Monster(s):
bool Player::attack(Monster Monster[], Player& Player)
{
int ArmorBefore = 0;
int Roll = 0;
int selection = 1;
int i;
if (Monster[0].isFilled() == true)
{
i = 0;
}
else if (Monster[1].isFilled() == true)
{
i = 1;
}
else if (Monster[2].isFilled() == true)
{
i = 2;
}
if (Monster[i].isFilled() == true)
{
std::cout << "1) Attack 2) Run 3) Cast Spell 4) Use Item: ";
std::cin >> selection;
std::cout << std::endl;
switch (selection)
{
case 1: // Player fights
std::cout << " You attack an " << Monster[i].getName()
<< " with a " << mWeapon.mName << std::endl;
if (Random(0, 20) < mAccuracy) // Player hits Monster
{
int damage = Random(mWeapon.mDamageRange);
int totalDamage = damage - Monster[i].getArmor();
if (totalDamage <= 0) // Armor is equal or higher than player atk
{
std::cout << "Your attack failed to penetrate "
<< Monster[i].getName() << "'s armor !"
<< std::endl;
return false;
}
else // Attack is higher than Monsters armor
{
std::cout << "You hit " << Monster[i].getName()
<< " for " << totalDamage << " damage !"
<< std::endl;
// Subtract dmg from Monsters hp
Monster[i].takeDamage(totalDamage);
return false;
}
}
else // Player Misses
{
std::cout << "You miss !" << std::endl;
}
std::cout << std::endl;
return false;
break;
case 2: // Player runs with a 25% chance
Roll = Random(1, 4);
if (Roll == 1) // Success
{
std::cout << "You run away !" << std::endl;
return true; // <- Return out of the function
}
else
{
std::cout << "You failed to escape !" << std::endl;
return false;
}
case 3: // Casting Spell
{
int SpellSelect;
// Spells for the Fighter
if (Player.mClassName == "Fighter")
{
std::cout << std::endl;
std::cout << "1) Shield 2) Mighty Blow: ";
std::cin >> SpellSelect;
if (SpellSelect == 1)
{
if (Player.mMagicPoints >= 10) // checks for player mana
{
std::cout << "You cast a mighty shield!"
<< std::endl;
ArmorBefore = Player.mArmor;
Player.shield(Player);
Player.mMagicPoints -= 10;
}
else
{
std::cout << "Not enough Mana" << std::endl;
break;
}
}
else
{
if (Player.mMagicPoints >= 5) // casting Mighty Blow
{
int damage = Random(mMightyBlow.mDamageRange);
std::cout
<< "You strike with all your might ! and Deal "
<< damage << " damage !" << std::endl;
Monster[i].takeDamage(damage);
Player.mMagicPoints -= 5;
return false;
}
else
{
std::cout << "Not enough Mana" << std::endl;
return false;
}
}
}
//Spells for the Wizard
else if (Player.mClassName == "Wizard")
{
std::cout << "1) Fireball";
std::cin >> SpellSelect;
if (Player.mMagicPoints >= 45)
{
int damage = Random(mFireball.mDamageRange);
std::cout << "You cast a Fireball and deal " << damage
<< " damage !" << std::endl;
Monster[i].takeDamage(damage);
Player.mMagicPoints -= 45;
return false;
}
else
{
std::cout << "Not enough Mana" << std::endl;
return false;
}
}
// Spells for the Cleric
else if (Player.mClassName == "Cleric")
{
std::cout << "1) Magic Missile";
std::cin >> SpellSelect;
if (Player.mMagicPoints >= 35)
{
int damage = Random(mMagicMissile.mDamageRange);
std::cout << "You cast a Magic Missile and deal "
<< damage << " damage !" << std::endl;
Monster[i].takeDamage(damage);
Player.mMagicPoints -= 35;
}
else
{
std::cout << "Not enough Mana" << std::endl;
return false;
}
}
}
case 4: // using Item
int invSlot;
std::cout << "1) HP Potion: ";
std::cin >> invSlot;
if (invSlot == 1) // Potion slot
{
if (mHPot.mAmount.size() > 0)
{
std::cout << "You used a Potion and healed for 5 HP !"
<< std::endl;
int currentSize = mHPot.mAmount.size();
mHPot.mAmount.resize(currentSize - 1);
Player.mHitPoints += 5;
if (Player.mHitPoints > Player.mMaxHitPoints)
{
Player.mHitPoints = Player.mMaxHitPoints;
}
return false;
}
else
{
std::cout << "You have no Potions!" << std::endl;
return false;
}
}
else // wrong slot
{
std::cout << "No Item found!" << std::endl;
return false;
}
}
// Clearing stat boosts
if (Player.shield(Player) == true)
{
Player.mArmor = ArmorBefore;
}
}
return false;
}
When I run the game, I sometimes have the problem, that after filling in a Monster in a slot of the array, no battle will be triggered. And if a battle will be triggered, I get a crash with an error report every time, which says:
_CrtlValidHeadPointer(block)
I guess that something with my pointer is not functioning well.... but since I am a beginner I am pretty much stuck. I would be very grateful for some enlightenment :)
This place can potentially call undefined behavior and crash:
int i;
if (Monster[0].isFilled() == true)
{
i = 0;
}
else if (Monster[1].isFilled() == true)
{
i = 1;
}
else if (Monster[2].isFilled() == true)
{
i = 2;
}
/*else // one of solutions
break;*/
//"i" can be unset! and can have any value from INT_MIN to INT_MAX!
if (Monster[i].isFilled() == true) //potentially index over array
{
Also there are memory leaks and undefined behavior with memory management:
Monster* monster = new Monster[3];
...
delete monster
must be delete [] monster
but it is recommended to use smart pointers, vector, array, etc, for memory management

SFML Bullet not spawning

So I am using SFML and c++ to create a simple space game. For the life of can't get a bullet to spawn. This is my source... I am just trying to learn how to go about spawning new sprites into a game.
```
#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
#include <iostream>
#include <string>
int main()
{
// Create the main window
sf::RenderWindow app(sf::VideoMode(800, 600), "SFML window");
// Load a sprite to display
sf::Texture texture_sprite;
if (!texture_sprite.loadFromFile("cb.bmp"))
return EXIT_FAILURE;
sf::Sprite sprite(texture_sprite);
sf::Texture texture_background;
if (!texture_background.loadFromFile("space.png"))
return EXIT_FAILURE;
sf::Sprite background(texture_background);
sf::Texture texture_fire;
if (!texture_background.loadFromFile("fire_1.png"))
return EXIT_FAILURE;
sf::Sprite fire_sprite(texture_fire);
fire_sprite.setScale(4.0f, 1.6f);
std::string direction = "";
bool fire = false;
// Start the game loop
while (app.isOpen())
{
// Process events
sf::Event event;
while (app.pollEvent(event))
{
if (event.type == sf::Event::Closed)
{
app.close();
}
else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left) && sprite.getPosition().x > -20)
{
sprite.move(-10,0);
std::cout << "X = " << sprite.getPosition().x << std::endl;
std::cout << "Y = " << sprite.getPosition().y << std::endl;
direction = "Left";
}
else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right) && sprite.getPosition().x < 670 )
{
sprite.move(10,0);
std::cout << "X = " << sprite.getPosition().x << std::endl;
std::cout << "Y = " << sprite.getPosition().y << std::endl;
direction = "Right";
}
else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up) && sprite.getPosition().y > 0)
{
sprite.move(0,-10);
std::cout << "X = " << sprite.getPosition().x << std::endl;
std::cout << "Y = " << sprite.getPosition().y << std::endl;
direction = "Up";
}
else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down) && sprite.getPosition().y < 480 )
{
sprite.move(0,10);
std::cout << "X = " << sprite.getPosition().x << std::endl;
std::cout << "Y = " << sprite.getPosition().y << std::endl;
direction = "Down";
}
else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Space))
{
fire = true;
}
else if (event.type == sf::Event::KeyReleased && event.key.code == sf::Keyboard::Space)
{
fire = false;
}
if(fire == true)
{
std::cout << "FIRE!!!" << std::endl;
fire_sprite.setPosition((sprite.getPosition()));
std::cout << "Fire positions is " <<fire_sprite.getPosition().x << " " << fire_sprite.getPosition().y << "The direction is " << direction <<std::endl;
}
}
app.clear();
// Draw the sprite
app.draw(background);
app.draw(sprite);
app.draw(fire_sprite);
// Update the window
app.display();
}
return EXIT_SUCCESS;
}
```
sf::Texture texture_fire;
if (!texture_background.loadFromFile("fire_1.png"))
Instead of loading texture to texture_fire you load again to texture_background. It should be:
if (!texture_fire.loadFromFile("fire_1.png"))

Reading from vectors C++

What is wrong with this code? It detects whether I've reached the coordinates of a monster but only one of them, or the closest one at least. If I travel to other coordinates it doesn't tell me the monster has appeared. Don't know why because I am looping through the monster vector every time I type 'north'.
Here is the code.
Monster Class:
class Monster
{
public:
std::vector<std::string> names;
std::vector<double> posx; // North - South
std::vector<double> posy; // East - West
bool compareCoords(double monsterPosX, double monsterPosY);
void cMonster(std::string monsterName, double monsterPosX, double monsterPosY);
void randomSpawn();
protected:
private:
};
compareCoords to detect whether a monster already exists at those coordinates and the cMonster (create monster) functions:
void Monster::cMonster(std::string monsterName, double monsterPosX, double monsterPosY)
{
if (compareCoords(monsterPosX, monsterPosY) == false)
{
names.push_back(monsterName);
posx.push_back(monsterPosX);
posy.push_back(monsterPosY);
std::cout << "Monster " << monsterName << " has been created at X: " << monsterPosX << " Y: " << monsterPosY << std::endl;
}
}
bool Monster::compareCoords(double monsterPosX, double monsterPosY)
{
for (unsigned int i = 0; i < posx.size(); i++)
{
if (monsterPosX == posx[i] && monsterPosY == posy[i])
{
return true;
}
}
return false;
}
Main:
int main()
{
srand(time(0));
Monster newenemy;
Character newplayer;
newenemy.cMonster("Weezo", 1, 0);
newenemy.cMonster("Weezo", 2, 0);
newplayer.posx.push_back(0);
newplayer.posy.push_back(0);
home:
std::cout << "-->> ";
std::string userInput;
std::cin.clear();
getline(std::cin, userInput);
if (!userInput.compare("north"))
{
newplayer.headNorth();
for (unsigned int i = 0; i < newenemy.names.size(); i++)
{
if (newplayer.posx[i] == newenemy.posx[i] && newplayer.posy[i] == newenemy.posy[i])
{
std::cout << "A " << newenemy.names[i] << " has appeared." << std::endl;
}
}
}
else if (!userInput.compare("south"))
{
newplayer.headSouth();
for (unsigned int i = 0; i < newenemy.posx.size(); i++)
{
if (newplayer.posx[i] == newenemy.posx[i] && newplayer.posy[i] == newenemy.posy[i])
{
std::cout << "A " << newenemy.names[i] << " has appeared." << std::endl;
}
}
}
else
{
std::cout << "You have entered an invalid command." << std::endl;
}
}
^ As you can see here, it shows when I'm at the coordinates of Gorilla but not the second monster, Donald Trump. It just ignores the second one.
I've been stuck here for hours, I don't understand what could be wrong. Thank you!

While loop is going one too far

So the problem I'm having is this: When a win condition is met the program should stop the while loop and just display the win/lose/draw message, but instead it allows the X and O of the game to take one more turn and I'm rather at a loss as to why. Any help would be greatly appreciated.
#include <iostream>
#include <cmath>
#include <string>
#include <cstdlib>
#include <ctime>
const int yCoordMax = 6;
const int xCoordMax = 2;
int xCoord;
int yCoord;
int square = 0;
const char PLAYER1 = 'X';
const char COMPUTER = 'O';
const int MAXTURN = 9;
char playerChar ; //the current turn's player's symbol
const std::string WIN = "You won! How nice.\n";
const std::string LOSE = "You lost.\n";
const std::string DRAW = "It's a draw.\n";
const std::string PLAY = "You will be the X's against the computer O's\n\n";
const std::string INSTRUCTIONS = "Enter the number of the square you wish to mark\nwith 1 being top left and 9 being bottom right.\n\n";
const std::string INVALIDSQUARE = "Please enter a correct square number between 1 and 9.\n";
const std::string SQUAREISFULL = "That square is already marked. Choose another.\n";
bool gameOver = false;
bool isGameDraw = false;
char boardChoices[3][3] = {{'1', '2', '3'},{'4', '5', '6'},{'7', '8', '9'}};
void drawBoard(void);
void playGame(void);
bool checkForWinner(void);
void isMoveValid(void);
int main()
{
std::srand(time(0)); //sets the seed for computer only once
std::cout << PLAY;
std::cout << INSTRUCTIONS;
playerChar = PLAYER1;
while(!gameOver)
{
drawBoard();
playGame();
isMoveValid();
gameOver = checkForWinner();
}
if (playerChar == 'O' && !isGameDraw)
{
drawBoard();
std::cout << std::endl << std::endl << "Player 1 [X] Wins! Game Over!\n";
}
else if (playerChar == 'X' && !isGameDraw)
{
drawBoard();
std::cout << std::endl << std::endl << "Player 2 [O] Wins! Game Over!\n";
}
else
{
drawBoard();
std::cout << std::endl << std::endl << "It's a draw! Game Over!\n";
}
return 0;
}
void drawBoard()
{
std::cout << std::endl << std::endl << "gameover says " << gameOver << std::endl;
std::cout << "+----" << "+----+" << "----+" << std::endl; // 0
std::cout << "| " << boardChoices[0][0] << " " << "| " << boardChoices[0][1] << " |" << " " << boardChoices[0][2] << " |" << std::endl; // 1 input here only [1][0], [1][1], [1][2]
std::cout << "+----" << "+----+" << "----+" << std::endl; // 2
std::cout << "| " << boardChoices[1][0] << " " << "| " << boardChoices[1][1] << " |" << " " << boardChoices[1][2] << " |" << std::endl; // 3 input here only [3][0], [3][1], [3][2]
std::cout << "+----" << "+----+" << "----+" << std::endl; // 4
std::cout << "| " << boardChoices[2][0] << " " << "| " << boardChoices[2][1] << " |" << " " << boardChoices[2][2] << " |" << std::endl; // 5 input here only [5][0], [5][1], [5][2]
std::cout << "+----" << "+----+" << "----+" << std::endl;
}
void playGame()
{
std::cout << std::endl;
if(playerChar == PLAYER1)
{
std::cout << "X's turn :: ";
std::cin >> square;
}
else if(playerChar == COMPUTER)
{
square = rand() % 9 + 1;
std::cout << "O's turn:: " << square << std::endl << std::endl;
}
if (square == 1)
{yCoord = 0;
xCoord = 0;}
if (square == 2)
{yCoord = 0;
xCoord = 1;}
if (square == 3)
{yCoord = 0;
xCoord = 2;}
if (square == 4)
{yCoord = 1;
xCoord = 0;}
if (square == 5)
{yCoord = 1;
xCoord = 1;}
if (square == 6)
{yCoord = 1;
xCoord = 2;}
if (square == 7)
{yCoord = 2;
xCoord = 0;}
if (square == 8)
{yCoord = 2;
xCoord = 1;}
if (square == 9)
{yCoord = 2;
xCoord = 2;}
}
void isMoveValid()
{
if(playerChar == PLAYER1 && boardChoices[yCoord][xCoord] != PLAYER1 && boardChoices[yCoord][xCoord] != COMPUTER)
{
boardChoices[yCoord][xCoord] = playerChar;
playerChar = COMPUTER;
}
else if(playerChar == COMPUTER && boardChoices[yCoord][xCoord] != PLAYER1 && boardChoices[yCoord][xCoord] != COMPUTER)
{
boardChoices[yCoord][xCoord] = COMPUTER;
playerChar = PLAYER1;
}
else
{
if(playerChar == PLAYER1)
std::cout << SQUAREISFULL;
}
}
bool checkForWinner()
{
std::string victoryOrDefeat;
if(playerChar == COMPUTER)
{
victoryOrDefeat = LOSE;
}
else if(playerChar == PLAYER1)
{
victoryOrDefeat = WIN;
}
if(boardChoices[0][0] == playerChar && boardChoices[0][1] == playerChar && boardChoices[0][2] == playerChar) // Horizontal
{std::cout << victoryOrDefeat;
return true;}
if(boardChoices[1][0] == playerChar && boardChoices[1][1] == playerChar && boardChoices[1][2] == playerChar) // Horizontal
{std::cout << victoryOrDefeat;
return true;}
if(boardChoices[2][0] == playerChar && boardChoices[2][1] == playerChar && boardChoices[2][2] == playerChar) // Horizontal
{std::cout << victoryOrDefeat;
return true;}
if(boardChoices[0][0] == playerChar && boardChoices[1][0] == playerChar && boardChoices[2][0] == playerChar) // Vertical
{std::cout << victoryOrDefeat;
return true;}
if(boardChoices[0][1] == playerChar && boardChoices[1][1] == playerChar && boardChoices[2][1] == playerChar) // Vertical
{std::cout << victoryOrDefeat;
return true;}
if(boardChoices[0][2] == playerChar && boardChoices[1][2] == playerChar && boardChoices[2][2] == playerChar) // Vertical
{std::cout << victoryOrDefeat;
return true;}
if(boardChoices[0][0] == playerChar && boardChoices[1][1] == playerChar && boardChoices[2][2] == playerChar) // Diagonal
{std::cout << victoryOrDefeat;
return true;}
if(boardChoices[0][2] == playerChar && boardChoices[1][1] == playerChar && boardChoices[2][0] == playerChar) // Diagonal
{std::cout << victoryOrDefeat;
return true;}
for (int i = 0; i < 3; i++)//Check for draw
{
for (int j = 0; j < 3; j++)
{
if (boardChoices[i][j] != 'X' && boardChoices[i][j] != 'O')
{
return false;
}
}
}
isGameDraw = true;
return true;
}
I have got it..You are changing playerChar after each isMoveValid() check as a result when you should be checking with X you are checking with O..simple do this change after checking the winner..that will give correct result.
Initially when player1 gives X position you are checking the win move for giving O and in later cases too this happens.. That's why it's getting the error.

Code::Blocks executable file doesn't have textures

I've made a basic program using the SFML library in Code::Blocks, and now all I want to do is send it to a friend. I've built my program in both debug and release mode, yet whenever I run the .exe file from my bin, it simply will not load the textures. Of course when I run it within the Code::Blocks client, it works perfectly. I'm not sure if I need to package my work or send the files separately or what, but any help would be much appreciated
Here's the code in case it's helpful somehow (I know it's not pretty):
#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
#include <string>
#include <iostream>
#include <Windows.h>
int main(){
sf::RenderWindow Window;
Window.create(sf::VideoMode(800, 600), "Enjoy :)");
sf::Clock clock;
int pixelFindX = 0, pixelFindY = 0;
int oldLoc = 0, newLoc = 0;
int changeVal = 1;
int tripleCheck = 0;
int xLoc = 0, yLoc = 100;
double timeChecker = 0;
bool execute = false, secondExecute = false;
bool oldSide = false, newSide = false, changer = true;
bool drawLeft = false, drawRight = false, drawCenter = false;
sf::Texture pTexture;
sf::Sprite playerImage;
if(!pTexture.loadFromFile("Pixel Dude.png")){
std::cout << "Could not load pTexture file" << std::endl;
}
sf::Texture bTexture;
sf:: Sprite backgroundImage;
if(!bTexture.loadFromFile("molester moon background IS DONE.jpg")){
std::cout << "Could not load bTexture file" << std::endl;
}
sf::Texture bridgeTexture;
sf::Sprite bridgeImageL;
sf::Sprite bridgeImageR;
if(!bridgeTexture.loadFromFile("pixel bridge.jpg")){
std::cout << "Could not load bridgeTexture file" << std::endl;
}
sf::Texture trophyTexture;
sf::Sprite trophyImage;
if(trophyTexture.loadFromFile("trophy pixeled.png")){
std::cout << "Could not load trophyTexture file" << std::endl;
}
sf::String stringL = "Move to the Left!", stringR = "Move to the Right!", stringC = "You are currently balanced.";
sf::String stringGo = stringL;
sf::Font font;
if(!font.loadFromFile("arial.ttf")){
std::cout << "Could not load font file" << std::endl;
}
sf::Text text(stringGo, font, 50);
text.setPosition(xLoc, yLoc);
text.setStyle(sf::Text::Bold);
playerImage.setTexture(pTexture);
playerImage.setPosition(336, 355);
playerImage.setScale(4,4);
playerImage.setTextureRect(sf::IntRect(pixelFindX*32, pixelFindY*32, 32, 32));
backgroundImage.setTexture(bTexture);
backgroundImage.setPosition(0,0);
bridgeImageL.setTexture(bridgeTexture);
bridgeImageL.setPosition(0, 450);
bridgeImageL.setScale(4.17,4.68);
bridgeImageR.setTexture(bridgeTexture);
bridgeImageR.setPosition(400,450);
bridgeImageR.setScale(4.17, 4.68);
trophyImage.setTexture(trophyTexture);
trophyImage.setPosition(600, 305);
trophyImage.setScale(2,2);
sf::View view;
sf::View viewReg;
view.reset(sf::FloatRect(0, 0, 800, 600));
view.setViewport(sf::FloatRect(0, 0, 1, 1));
viewReg.reset(sf::FloatRect(0, 0, 800, 600));
viewReg.setViewport(sf::FloatRect(0, 0, 1, 1));
std::cout << "Player Dimensioms: Width = " << playerImage.getGlobalBounds().width << ", Height = " << playerImage.getGlobalBounds().height << std::endl;
while(Window.isOpen()){
clock.restart();
sf::Event Event;
while(Window.pollEvent(Event)){
switch(Event.type){
case sf::Event::Closed:
Window.close();
break;
case sf::Event::KeyPressed:
if(Event.key.code == sf::Keyboard::Escape){
Window.close();
}
break;
}
}
//std::cout << timeChecker << std::endl;
timeChecker += clock.getElapsedTime().asSeconds();
if(timeChecker >= .00025){
execute = true;
timeChecker = 0;
//std::cout << "execute = true" << std::endl;
}
oldLoc = playerImage.getPosition().x;
oldSide = changer;
if(sf::Keyboard::isKeyPressed(sf::Keyboard::D) && execute){
playerImage.move(1, 0);
pixelFindY = 2;
tripleCheck++;
changer = true;
//std::cout << "d called" << std::endl;
} else if(sf::Keyboard::isKeyPressed(sf::Keyboard::A) && execute){
playerImage.move(-1, 0);
pixelFindY = 1;
tripleCheck++;
changer = false;
//std::cout << "a called" << std::endl;
}
if(playerImage.getPosition().x < -96){
playerImage.setPosition(800, playerImage.getPosition().y);
} else if(playerImage.getPosition().x > 800){
playerImage.setPosition(-96, playerImage.getPosition().y);
}
newLoc = playerImage.getPosition().x;
newSide = changer;
pixelFindX += changeVal;
if(pixelFindX > 2){
pixelFindX -= 2;
changeVal = -1;
} else if(pixelFindX < 0){
pixelFindX += 2;
changeVal = 1;
}
if(oldLoc != newLoc && tripleCheck > 40 || oldSide != newSide){
playerImage.setTextureRect(sf::IntRect(pixelFindX*32, pixelFindY*32, 32, 32));
tripleCheck = 0;
//std::cout << "Yes called, pixelFindX = " << pixelFindX << ", tripleCheck = " << tripleCheck << std::endl;
} else {
//std::cout << "-----Not called, pixelFindX = " << pixelFindX << ", tripleCheck = " << tripleCheck << std::endl;
}
execute = false;
/* if(playerImage.getPosition().x + 128 > trophyImage.getPosition().x && playerImage.getPosition().x < trophyImage.getPosition().x + 192){
view.rotate(.0001);
}
*/
if(playerImage.getPosition().x > 500 - 128){
view.rotate(-.01);
drawRight = true;
} else if (playerImage.getPosition().x < 300 - 128){
view.rotate(.01);
drawLeft = true;
} else{
drawCenter = true;
}
//std::cout << "Player X: " << playerImage.getPosition().x + 128 << ", Trophy X: " << trophyImage.getPosition().x << std::endl;
if(drawLeft){
stringGo = stringR;
xLoc = 0;
} else if (drawRight){
stringGo = stringL;
xLoc = 800 - text.getGlobalBounds().width - 10;
} else if(drawCenter){
stringGo = stringC;
xLoc = 400 - text.getGlobalBounds().width / 2;
}
text.setPosition(xLoc, yLoc);
text.setString(stringGo);
Window.setView(viewReg);
Window.draw(backgroundImage);
Window.draw(text);
Window.setView(view);
Window.draw(bridgeImageL);
Window.draw(bridgeImageR);
// Window.draw(trophyImage);
Window.draw(playerImage);
Window.display();
Window.clear();
drawLeft = false;
drawRight = false;
drawCenter = false;
//Sleep(50);
}
}
Copy the compiled executable into a new folder and paste there the textures too. This folder you can send your friend (e.g. in a rar-archive).The textures won't load because probably the textures weren't in the bin/debug or bin/release folder. There are in another place wich is in the debugging mode is set as the working directory.If you want to change that (what I would not recommend) you can go to Project -> Properties -> Build targets -> [name of target] -> Execution working dir and change it to the directory where the compiled executables went.