ok, so i'm just about done with my simple crappy tick tack toe game, however i'm running into a problem that i just cant seem to fix when the game reaches the last turn (note: as of right now the game doesn't check for victory at all, the player chooses and then the computer chooses) for some reason on turn 9 when the player is trying to make his final move the game just kinda gets stuck near the bottom of main, when the event for mouse being clicked is polled, the function call for the button class, well, it just just sits there. i'm not really sure why, and the debugging menu is just confusing me more right now. i know my problem is probably extremely stupid, but i'm still a noob so i hope you can fogive me, any advice on my predicament would be appreciated.
//global var for counting when a turn for the computer or player has passed
int globalturns = 0;
//mutex makes locked data only able to be accessed by one thread
// sf::Mutex mutex;
// class that determines if a sprite is drawn or not, and is used to draw the right sprites
class Drawn
{
private:
int state = 0;
sf::Sprite buttonpressed, button, compo;
// sf::Sprite hovored use for later
float buttonh, buttonw;
sf::Vector2f location1;
public:
int getState()
{
return state;
}
void setState(int sstate)
{
state = sstate;
}
//button function, must edit
bool V2i_inRange(sf::Vector2i a)
{
sf::Vector2i mousePos=a;
if (location1.x + buttonw >= mousePos.x && location1.y + buttonh >= mousePos.y && state == 0)
if (location1.x <= mousePos.x && location1.y <= mousePos.y)
{
state = 3;
globalturns++;
return true;
}
else
{
return false;
}
else
{
return false;
}
}
void setbutton(sf::Sprite pressed, sf::Sprite butto, sf::Sprite comp, sf::Vector2f location)
{
location1 = location;
buttonpressed = pressed;
button = butto;
compo = comp;
buttonh = butto.getGlobalBounds().height;
buttonw = butto.getGlobalBounds().width;
button.setPosition(location);
buttonpressed.setPosition(location);
compo.setPosition(location);
}
sf::Sprite draw()
{
if (state==3)
{
return buttonpressed;
}
else if (state==4)
{
return compo;
}
else
{
return button;
}
}
};
//function that generates turn for computer
int randomChoice()
{
unsigned int choice=0;
choice = rand() % 9;
return choice;
};
void computerChoice(Drawn arr[])
{
if (globalturns < 9)
{
bool found = false;
unsigned int choice = 0;
choice = randomChoice();
while (found == false)
{
if (arr[choice].getState() == 0)
{
found = true;
arr[choice].setState(4);
globalturns++;
return;
}
else
{
choice = randomChoice();
}
}
}
return;
}
int main()
{
srand(time(NULL)); //rand numb
Drawn butt[10]; //arraw of buttons
sf::RenderWindow window;
sf::Clock clock;
sf::Time t1;
float secon=0;
window.create(sf::VideoMode(800, 600, 32), "Loading...", sf::Style::Default);
while (secon<1)
{
t1 = clock.getElapsedTime();
secon = t1.asSeconds();
}
sf::Font font;
if (!font.loadFromFile("Medici Text.ttf"))
{
cout << "Error loading file :(\n";
}
window.setVerticalSyncEnabled(true);
//Position var of mouse
sf::Vector2i mousePosition;
//Graphic add
sf::Texture ttt;
sf::Sprite gameb, x1,o1;
if (!ttt.loadFromFile("tttsprite.png"))
{
std::cout << "Sorry, bro your sprites didnt load/n";
}
sf::Texture button;
sf::Sprite button1;
if (!button.loadFromFile("norm.png"))
{
std::cout << "you be buttonless foo!";
}
gameb.setTexture(ttt);
gameb.setPosition(150, 100);
//sprites for X's and O's
x1.setTexture(ttt);
x1.setScale(0.75f, 0.75f);
x1.setTextureRect(sf::IntRect(35, 88, 54, 54));
x1.setColor(sf::Color(255, 255, 255, 128));
o1.setTexture(ttt);
o1.setScale(0.75f, 0.75f);
o1.setTextureRect(sf::IntRect(35, 192, 54, 56));
o1.setColor(sf::Color(255, 255, 255, 128));
//make buttons for where player will click to add x
button1.setTexture(button);
button1.setScale(0.5f, 0.5f);
button1.setPosition(310, 180);
//array of button locations
sf::Vector2f arrayl[9];
arrayl[0] = { 310, 180 };
arrayl[1] = { 370, 180 };
arrayl[2] = { 430, 180 };
arrayl[3] = { 310, 240 };
arrayl[4] = { 370, 240 };
arrayl[5] = { 430, 240 };
arrayl[6] = { 310, 300 };
arrayl[7] = { 370, 300 };
arrayl[8] = { 430, 300 };
//assigning values to the array of Drawn buttons
for (int n = 0; n < 9; n++)
{
butt[n].setbutton(x1, button1, o1, arrayl[n]);
}
sf::Text text;
text.setFont(font);
text.setString("Press any key to continue");
text.setCharacterSize(32);
text.setColor(sf::Color::Red);
//text.setStyle(sf::Text::Bold);
text.setString("Welcome to Basic Tick Tack Toe 2.0!");
window.setTitle("Tick Tack Toe 2.0");
text.setCharacterSize(32);
sf::Music theme;
theme.setVolume(50);
theme.setLoop(true);
if (!theme.openFromFile("theme.ogg"))
{
std::cout << "Poor you, your theme won't play :(/n";
}
theme.play();
//Main Program ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
while (window.isOpen())
{
sf::Event event;
while (globalturns < 9)
{
while (window.pollEvent(event))
{
switch (event.type)
{
case sf::Event::Closed:
window.close();
break;
case sf::Event::KeyPressed:
break;
case sf::Event::MouseButtonPressed:
if (sf::Mouse::isButtonPressed(sf::Mouse::Left))
{
// left mouse button is pressed: is it in range of button
// get position of mouse, pass to button class for each button
mousePosition = sf::Mouse::getPosition(window);
bool pressed = false;
for (int n = 0; n < 9; n++)
{
cout << n << endl;
if (globalturns < 9)
{
//problem occurs over here----
pressed = butt[n].V2i_inRange(mousePosition);
}
if (pressed)
{
n = 9;
}
}
if (globalturns<9)
{
if (pressed)
{
computerChoice(butt);
pressed = false;
}
}
else
{
break;
}
}
break;
case sf::Event::MouseMoved:
break;
default:
break;
}
}
window.clear(sf::Color::Red);
window.draw(text);
window.draw(gameb);
for (int n = 0; n < 9; n++)
{
window.draw(butt[n].draw());
}
window.display();
}
}
cout << "The game is over!";
return 0;
}