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

Author Topic: Battleship (Need help and idea with some codes)  (Read 5866 times)

0 Members and 1 Guest are viewing this topic.

piji

  • Newbie
  • *
  • Posts: 5
    • View Profile
Battleship (Need help and idea with some codes)
« on: September 04, 2013, 08:30:30 pm »
Hi, I just started using SFML a week ago and there is this assignment I need to complete which involve writing a Battleship game using SFML library. So far, I was able to complete the graphics and just done dealing with drag and drop all the ships. These are what I've wrote since 3 days ago:

http://codepad.org/W7cG5vsx

^Posted the code there cause it's more than 300 lines

These are what I've achieved (still to far away from completing the assignment  :-\ ) :
-Draw grids and ships
-Figured how to drag and drop the ships

This is the current problem which I focus on and don't know how to solve:
-Drop the ships onto the grids and assign their properties to the grids (This is for attacking and getting attacked)

If you don't get the idea, I'm bad in explaining things  :(
If you do get the idea, please help me out if you have any solutions or ideas  :D

Attached file is the screenshot of the program (I've moved the ships onto the grids)

Thank you

blojayble

  • Newbie
  • *
  • Posts: 19
  • whoa
    • View Profile
Re: Battleship (Need help and idea with some codes)
« Reply #1 on: September 04, 2013, 08:59:18 pm »
First of all, I'd suggest you trying to make this game in OOP(object oriented programming), then the code will be a whole lot easier to modify, arrange and understand to other people. Also, then you could make classes, such as ship, or tiles of your map. With this logic it will be easier to make this game.
« Last Edit: September 04, 2013, 09:01:17 pm by blojayble »
0 bottles of beer on the wall,
0 bottles of beer!
Take one down, pass it around,
4,294,967,295 bottles of beer on the wall!

piji

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: Battleship (Need help and idea with some codes)
« Reply #2 on: September 04, 2013, 09:10:03 pm »
First of all, I'd suggest you trying to make this game in OOP(object oriented programming), then the code will be a whole lot easier to modify, arrange and understand to other people. Also, then you could make classes, such as ship, or tiles of your map. With this logic it will be easier to make this game.

Actually, I'm taking the OOP subject next semester. But I'll take a look on it. Thank you  :D

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10829
    • View Profile
    • development blog
    • Email
AW: Battleship (Need help and idea with some codes)
« Reply #3 on: September 04, 2013, 09:48:33 pm »
Since you know the position of the ship you could take an array or better a vector and calculate the correct indices based on the position.
With these indices you can set the bool variable on the vector to true or maybe use an integer (or char) to indicate states such as (ship, hit, water, etc).
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

piji

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: AW: Battleship (Need help and idea with some codes)
« Reply #4 on: September 05, 2013, 05:47:38 am »
Since you know the position of the ship you could take an array or better a vector and calculate the correct indices based on the position.
With these indices you can set the bool variable on the vector to true or maybe use an integer (or char) to indicate states such as (ship, hit, water, etc).

Thank you for the idea. Now I see where I'm heading to.

In a meanwhile, I try to change the orientation of the ship from horizontal to vertical using RMB while LMB is pressed. It went good, but I've encountered some problems:
1. When changing the orientation from horizontal to vertical, the ship went far off the mouse pointer (Attached pic #1)
2. When I try to drag the ship after changing the ship's orientation, the ship went a slight off the mouse pointer, but still dragable (Attached pic #2)

these are what I've wrote
if (event.type == sf::Event::MouseButtonPressed && event.mouseButton.button == sf::Mouse::Right)
            {
                mouseClickedRight = true;
                if (draggingShip1 == true) //when LMB is pressed at the same time
                    ship1.setRotation(90);
                    ship1.setPosition(sf::Vector2f(mouseX,mouseY));

            }
 

How do I get the ship exactly under the mouse pointer even after I've changed its orientation?

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: Battleship (Need help and idea with some codes)
« Reply #5 on: September 05, 2013, 07:10:48 am »
There's no function in SFML or C++ to magically do this for you. You're going to have to compute an offset based on the size and orientation of the ship and adjust the position accordingly. Getting it right will probably take some experimentation, but it won't be too complicated if all you want is vertical and horizontal battleship pieces.

piji

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: Battleship (Need help and idea with some codes)
« Reply #6 on: September 05, 2013, 11:08:41 am »
Thanks, so back to my current problem again.

How to make the ships to associate with the grids? Please give some examples if possible.

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: Battleship (Need help and idea with some codes)
« Reply #7 on: September 05, 2013, 11:35:08 am »
Okay, this may sound a bit mean, but...

This is the kind of problem that's simple enough, yet also so specific to your program, that we really shouldn't write the code for you.  That's partly because only you know exactly what you want it to do (and if you can write down exactly what you want here in English, you might as well do it in C++ instead), but also because you need to learn to solve the simple things yourself if you're ever going to be able to finish a program.

Now, if you can describe a precise algorithm (say, in pseudocode) that solves your problem, and you just don't know how to implement a specific piece of it using SFML, then try searching the tutorials and documentation first, and if you can't find it, THEN you can ask us what to do, and we can probably help.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10829
    • View Profile
    • development blog
    • Email
AW: Battleship (Need help and idea with some codes)
« Reply #8 on: September 05, 2013, 11:22:12 pm »
The sprite transformation (rotation, translation, scaling) happen around the sprite origin. You could essentially change the origin to the point where the mouse is.

Snap-to-grid can be easily made by adjusting the sprite position to the next grid position. Correct math/logic is up to.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

piji

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: Battleship (Need help and idea with some codes)
« Reply #9 on: September 06, 2013, 09:20:32 am »
This in another problem that I just encountered, variable changing value in event space key pressed wont execute and I dont know why.

//VARIABLES
bool ship1Position;
bool ship2Position;
bool dragShip1 = false;
bool dragShip2 = false;
 

                case sf::Event::KeyPressed:
                {
                    switch(event.key.code)
                    {
                        case sf::Keyboard::Space:
                            if(dragShip1)
                                ship2Position = true;
                                dragShip1 = false;
                            break;
                    }
                    break;
                }

                case sf::Event::KeyReleased:
                {
                    switch(event.key.code)
                    {
                        case sf::Keyboard::Space:
                            break;
                    }
                }
            }
       }  

        window.clear();
        ship1Position = true;

        while(ship1Position)
        {
            window.draw(ship1);
            dragShip1 = true;
            ship1Position = false;

        }

        while(ship2Position)
        {
            window.draw(ship2);
            dragShip2 = true;
            ship2Position = false;
        }

 

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: Battleship (Need help and idea with some codes)
« Reply #10 on: September 06, 2013, 09:53:53 am »
Learn to use a debugger.  That's exactly the kind of question a debugger can help you answer far better than we can.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10829
    • View Profile
    • development blog
    • Email
AW: Battleship (Need help and idea with some codes)
« Reply #11 on: September 06, 2013, 11:08:46 am »
And/Or use some simple std::cout to check variable states.
Btw. regardless of your indentations, the if in the case statement only includes the first variable setting the second line will always get executed.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/