Hi!
First: sorri for my english
I'm new to SFML library (and generally to game development):
i've just developed "tic tac toe" but i think that i have made it in a wrong way
this is the source code:
#include <SFML/Graphics.hpp>
#include <iostream>
const int windowWidth{ 600 }, windowHeight{ 600 };
sf::RenderWindow window(sf::VideoMode(windowWidth, windowHeight), "Tic Tac Toe");
int mouseX() { return sf::Mouse::getPosition(window).x; };
int mouseY() { return sf::Mouse::getPosition(window).y; };
int main()
{
sf::Texture grid, textureX, textureO;
if (!grid.loadFromFile("C:/Users/Admin/Desktop/Tic Tac Toe/graphics/Grid.bmp") ||
!textureX.loadFromFile("C:/Users/Admin/Desktop/Tic Tac Toe/graphics/X.png") ||
!textureO.loadFromFile("C:/Users/Admin/Desktop/Tic Tac Toe/graphics/O.png"))
std::cout << "Immagine non trovata";
sf::Sprite background(grid);
sf::Sprite spriteX(textureX);
sf::Sprite spriteO(textureO);
int round = 0;
bool player{ false };
int mark[9] = { 0,0,0,0,0,0,0,0,0};
bool marked[9] = { false, false, false, false, false, false, false, false, false };
int markCoordX[9] = {0, 200, 400, 0, 200, 400, 0, 200, 400};
int markCoordY[9] = {0, 0, 0, 200, 200, 200, 400, 400, 400};
while (window.isOpen())
{
sf::Event event;
window.draw(background);
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed || sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Escape))
window.close();
std::cout << "Mouse Coordinates: (" << mouseX() << ";" << mouseY() << std::endl;
if (mouseX() > 0 && mouseY() > 0 && mouseX() < 200 && mouseY() < 200)
{
if (sf::Mouse::isButtonPressed(sf::Mouse::Button::Left) && marked[0] == false)
{
if (round % 2 == 0)
{
mark[0] = 1;
std::cout << "Turno "<<round<<" del player X" << std::endl;
player = false;
marked[0] = true;
round++;
}
else
{
mark[0] = 2;
std::cout << "Turno "<<round<<" del player O" << std::endl;
player = true;
marked[0] = true;
round++;
}
}
}
if (mouseX() > 200 && mouseY() > 0 && mouseX() < 400 && mouseY() < 200)
{
if (sf::Mouse::isButtonPressed(sf::Mouse::Button::Left) && marked[1] == false)
{
if (round % 2 == 0)
{
mark[1] = 1;
std::cout << "Turno "<<round<<" del player X" << std::endl;
player = false;
marked[1] = true;
round++;
}
else
{
mark[1] = 2;
std::cout << "Turno "<<round<<" del player O" << std::endl;
player = true;
marked[1] = true;
round++;
}
}
}
if (mouseX() > 400 && mouseY() > 0 && mouseX() < 600 && mouseY() < 200)
{
if (sf::Mouse::isButtonPressed(sf::Mouse::Button::Left) && marked[2] == false)
{
if (round % 2 == 0)
{
mark[2] = 1;
std::cout << "Turno "<<round<<" del player X" << std::endl;
player = false;
marked[2] = true;
round++;
}
else
{
mark[2] = 2;
std::cout << "Turno "<<round<<" del player O" << std::endl;
player = true;
marked[2] = true;
round++;
}
}
}
if (mouseX() > 0 && mouseY() > 200 && mouseX() < 200 && mouseY() < 400)
{
if (sf::Mouse::isButtonPressed(sf::Mouse::Button::Left) && marked[3] == false)
{
if (round % 2 == 0)
{
mark[3] = 1;
std::cout << "Turno " << round << " del player X" << std::endl;
player = false;
marked[3] = true;
round++;
}
else
{
mark[3] = 2;
std::cout << "Turno " << round << " del player O" << std::endl;
player = true;
marked[3] = true;
round++;
}
}
}
if (mouseX() > 200 && mouseY() > 200 && mouseX() < 400 && mouseY() < 400)
{
if (sf::Mouse::isButtonPressed(sf::Mouse::Button::Left) && marked[4] == false)
{
if (round % 2 == 0)
{
mark[4] = 1;
std::cout << "Turno " << round << " del player X" << std::endl;
player = false;
marked[4] = true;
round++;
}
else
{
mark[4] = 2;
std::cout << "Turno " << round << " del player O" << std::endl;
player = true;
marked[4] = true;
round++;
}
}
}
}
if (mouseX() > 400 && mouseY() > 200 && mouseX() < 600 && mouseY() < 400)
{
if (sf::Mouse::isButtonPressed(sf::Mouse::Button::Left) && marked[5] == false)
{
if (round % 2 == 0)
{
mark[5] = 1;
std::cout << "Turno " << round << " del player X" << std::endl;
player = false;
marked[5] = true;
round++;
}
else
{
mark[5] = 2;
std::cout << "Turno " << round << " del player O" << std::endl;
player = true;
marked[5] = true;
round++;
}
}
}
if (mouseX() > 0 && mouseY() > 400 && mouseX() < 200 && mouseY() < 600)
{
if (sf::Mouse::isButtonPressed(sf::Mouse::Button::Left) && marked[6] == false)
{
if (round % 2 == 0)
{
mark[6] = 1;
std::cout << "Turno " << round << " del player X" << std::endl;
player = false;
marked[6] = true;
round++;
}
else
{
mark[6] = 2;
std::cout << "Turno " << round << " del player O" << std::endl;
player = true;
marked[6] = true;
round++;
}
}
}
if (mouseX() > 200 && mouseY() > 400 && mouseX() < 400 && mouseY() < 600)
{
if (sf::Mouse::isButtonPressed(sf::Mouse::Button::Left) && marked[7] == false)
{
if (round % 2 == 0)
{
mark[7] = 1;
std::cout << "Turno " << round << " del player X" << std::endl;
player = false;
marked[7] = true;
round++;
}
else
{
mark[7] = 2;
std::cout << "Turno " << round << " del player O" << std::endl;
player = true;
marked[7] = true;
round++;
}
}
}
if (mouseX() > 400 && mouseY() > 400 && mouseX() < 600 && mouseY() < 600)
{
if (sf::Mouse::isButtonPressed(sf::Mouse::Button::Left) && marked[8] == false)
{
if (round % 2 == 0)
{
mark[8] = 1;
std::cout << "Turno " << round << " del player X" << std::endl;
player = false;
marked[8] = true;
round++;
}
else
{
mark[8] = 2;
std::cout << "Turno " << round << " del player O" << std::endl;
player = true;
marked[8] = true;
round++;
}
}
}
//Controllo caselle da checkare
for (int i = 0; i < 9; i++)
{
if (marked[i] == true)
{
if (mark[i] == 1)
{
spriteX.setPosition(markCoordX[i], markCoordY[i]);
window.draw(spriteX);
}
else if (mark[i] == 2)
{
spriteO.setPosition(markCoordX[i], markCoordY[i]);
window.draw(spriteO);
}
}
}
window.display();
//controllo vittoria
if ((mark[0] == 1 && mark[1] == 1 && mark[2] == 1) ||
(mark[3] == 1 && mark[4] == 1 && mark[5] == 1) ||
(mark[6] == 1 && mark[7] == 1 && mark[8] == 1) ||
(mark[0] == 1 && mark[4] == 1 && mark[8] == 1) ||
(mark[2] == 1 && mark[4] == 1 && mark[6] == 1) ||
(mark[0] == 1 && mark[3] == 1 && mark[6] == 1) ||
(mark[1] == 1 && mark[4] == 1 && mark[7] == 1) ||
(mark[2] == 1 && mark[5] == 1 && mark[8] == 1))
std::cout << "Player X ha vinto!" << std::endl;
else
if ((mark[0] == 2 && mark[1] == 2 && mark[2] == 2) ||
(mark[3] == 2 && mark[4] == 2 && mark[5] == 2) ||
(mark[6] == 2 && mark[7] == 2 && mark[8] == 2) ||
(mark[0] == 2 && mark[4] == 2 && mark[8] == 2) ||
(mark[2] == 2 && mark[4] == 2 && mark[6] == 2) ||
(mark[0] == 2 && mark[3] == 2 && mark[6] == 2) ||
(mark[1] == 2 && mark[4] == 2 && mark[7] == 2) ||
(mark[2] == 2 && mark[5] == 2 && mark[8] == 2))
std::cout << "Player O ha vinto!" << std::endl;
}
return 0;
}
is along 310 lines of code ... it is not too long?
have you advice for me?
thanks a lot!