Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Tic Tac Toe is too long?  (Read 1689 times)

0 Members and 1 Guest are viewing this topic.

Serus

  • Newbie
  • *
  • Posts: 2
    • View Profile
Tic Tac Toe is too long?
« on: July 06, 2014, 02:57:57 pm »
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! ;)

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: Tic Tac Toe is too long?
« Reply #1 on: July 06, 2014, 03:03:09 pm »
Seriously??? You are asking if 310 lines of code is too much?  :o You shouldn't be grading your code on how many LOC you have written (I will be honest here: 310 lines of code is nothing). Instead focus on your style and code readability. Oh, and maybe ask a more specific question instead of just coming here "here's my code, is it too long?".
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: Tic Tac Toe is too long?
« Reply #2 on: July 06, 2014, 03:08:12 pm »
Nope, 300 lines of code is nothing.  My gut feeling is that anything under a thousand lines could be considered a "toy" program.

However you have a LOT of duplicate code, which you should reduce, not for length reasons, but to increase flexibility and readability, and decrease the potential for bugs.  Specifically:
- You should not need to hardcode the positions/sizes of your tic-tac-toe squares in that many places.  A better design might be to have a vector of sf::Rects, and call contains() on every rect so you know which space was clicked on.  That way the exact pixel sizes of the spaces only need to appear once.
- Almost all of the code you execute for a click is identical, regardless of which space was clicked.  In the actual click handler you should only set an int or two that mean "this space was clicked on", and then only after all the event handling is over do you have a block of code that actually processes that click.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Tic Tac Toe is too long?
« Reply #3 on: July 06, 2014, 03:08:16 pm »
is along 310 lines of code ... it is not too long?
Yes, it's much too long. If you have a look at the code, you'll see that many parts of it have exactly the same structure. This code duplication can be avoided if you outsource the corresponding code to separate functions.

And instead of so many if-else cascades, you could use loops.

Seriously??? You are asking if 310 lines of code is too much?  :o You shouldn't be grading your code on how many LOC you have written (I will be honest here: 310 lines of code is nothing).
LOC on its own may not be an expressive measure, but with additional information, especially about what the code functionally does, it becomes more meaningful. In other words, I think compactness is also one aspect of code quality... but it should of course not come at the cost of other aspects such as correctness or readability.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Serus

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: Tic Tac Toe is too long?
« Reply #4 on: July 06, 2014, 03:14:23 pm »
Seriously??? You are asking if 310 lines of code is too much?  :o You shouldn't be grading your code on how many LOC you have written (I will be honest here: 310 lines of code is nothing). Instead focus on your style and code readability. Oh, and maybe ask a more specific question instead of just coming here "here's my code, is it too long?".
yes, you have reason...
when i said "310 lines of code ... it is not too long?" i meant "my code is well written?"...
my abilities in english are very horrible and i can't express myself well

thanks nexus and ixrec, i meant just what you said!
I will improve the code with yours advices ;)

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: Tic Tac Toe is too long?
« Reply #5 on: July 06, 2014, 03:16:26 pm »
Quote
In other words, I think compactness is also one aspect of code quality... but it should of course not come at the cost of other aspects such as correctness or readability.

That is exactly why I said 'grading', not measuring. And ended with the following statement.

Quote
ask a more specific question
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor