Battleship project

  1. ‹ Older
  2. 2 weeks ago

    @LouisDesjardins
    Making progress on my own... be prepared because I may ask a lot of things. (Tell me if it's a problem)

    //draw a board
    dim i,j as byte
    for i=1 to 10
      for j=1 to 10
        g.ForeColor=&c0000ff
        g.penwidth=2
        g.fillrect(1*i,1*j,25,25)
        j=j+1
      next j
      i=i+1
    next i

    Could you tell me what's wrong with this code ?
    In my head, this should work as follow :

    1. Enter for i
    2. Enter for j
    3. Draw a 25x25 pixels rectangle at position 1,1 in the canvas
    4. add 1 to j and add one to i
    5. Do it again with new variables.

    But instead it just makes a square with a huge border.

  3. Dave S

    May 15 San Diego, California USA

    Louis, please remember a few things, (and I don't mean to be harsh)

    • its a homework assignment. so either your instructor has given you something beyond the current level of the class, or you haven't been studying as required, or (least likely) you have a terrible instructor
    • this is a community based volunteer forum, and we are here to help you, not to do it for you, so as the other Louis said

      @LouisDesjardins Once you made some progress on your own, don't hesitate to ask

    . But it is not our place (nor should it be) to replace your instructor, replace you own need to research. What we will do is guide you, one you show some due dilegence. "I have this code and I don't understand why it doesn't work" , but "Not design my software for me"

    Note : this project will be near impossible without knowing what/how a 2D array is... (see bullet #1 above)

  4. Markus W

    May 15 Pre-Release Testers, Xojo Pro #JeSuisHuman Germany, Heidelb...

    P.S. you should also step through your code, and at least twice through each loop, to make sure they do what you think they do. If you would do so you‘d have noticed immediately that your code is not doing what you thought it should.

  5. Markus W

    May 15 Pre-Release Testers, Xojo Pro #JeSuisHuman Germany, Heidelb...
    Edited 2 weeks ago

    Damn. Accidentally deleted my earlier post.

    You are drawing lots of overlapping rectangles, so of course you end up with one big rectangle.

    Walk through your code - and at least twice through each loop - and check how the values of your variables change. Doing so would have quickly revealed that the code is not doing what you think it is doing.

    The first thing you need to do - before writing any code - is to write down the step by step instructions on HOW to solve the problem. Those can be in plain English. Then each step just needs to be converted into code. Identical steps become methods.

    The way I start a new project is to write the step by step instructions as comments, adding methods (with their own comments) as required. That way you end up with a perfect blueprint for your code AND you have already well documented it.

  6. An 2 D(imensional) array has two directions :
    Direction X for horizontal
    Direction Y for vertical.

    An array is always the same type, string, integer, double but never can be mixed types. Also note that array indexes start from "0". Individual positions are called "elements".

    So if you have an array lets say "intBoard(15,15)" there are 16 elements horizontally (X) and 16 elements vertically (Y).

    As an excercise, please workout the following code without running it (cheating). Make sure you predict those values correctly by double checking your results. Here it is :

    // Dimensioning (creating) an 2D array
    // containing 16 elements horizontally (X) and
    // 16 elements vertically (Y)
    Dim intBoard(15,15) As Integer
    Dim intLoop_X_Direction As Integer
    Dim intLoop_Y_Direction As Integer
    Dim intValue As Integer
    
    intValue = 0
    For intLoop_Y_Direction = 0 To 15
      // Outer For loop moving vertically
      For intLoop_X_Direction = 0 To 15
        // Inner For loop moving horizontally
        intBoard(intLoop_X_Direction, intLoop_Y_Direction) = intValue
        intValue = intValue + 1
      Next intLoop_X_Direction
    Next intLoop_Y_Direction
    
    // Without running the code and see the results,
    // take a piece of paper and write down what the 
    // value of the following positions are :
    // intBoard(15,15) :
    // intBoard(15,14) :
    // intBoard(14,15) :
    
    // Hint :
    // When an array or any datastructure is not clear,
    // draw it out in a drawing program or on paper.
    // You will see it is easier to understand.
    
    // Now considering the next code :
    intValue = 0
    For intLoop_Y_Direction = 0 To 15
      // Outer For loop moving vertically
      intValue = intValue + 1
      For intLoop_X_Direction = 0 To 15
        // Inner For loop moving horizontally
        intBoard(intLoop_X_Direction, intLoop_Y_Direction) = intValue
      Next intLoop_X_Direction
    Next intLoop_Y_Direction
    
    // Now workout what happens with the positions :
    // intBoard(15,15) :
    // intBoard(15,14) :
    // intBoard(14,15) :
    
    // Don't test it but work it out without running the above code.
    // When you can predict what happens correctly, you do 
    // understand 2 dimensional arrays.

    However, for your board I would use a "Class array". This has the advantage that you can store more data for a particular field in one element of the class array. For example the horizontal, vertical board position, also what type of "ship" is occupying that element, amount of hits and so on.

    For applying Artificial Intelligence (AI), Scirra has some very good tutorials on this subject. As an excercise, look at those tutorials and apply that information with Xojo.

    This all can look very difficult and complex and sometimes it is. However when developing any kind of computer program, break things in smaller parts. As an example :

    1. Create the board
    2. Fill the board with default values
    3. Create the type of ships
    4. Put those ships on the board
    5. ...

    Now you have a general structure or idea what in the example above the beginning of the game has to do.

    Now you workout those individual points. This method can make a problem which you fail to overview, nicely visible. Don't just start coding as you go. Plan, structure and then code.

    You see, when you organize and structure your idea and creations, you will finish your battleship project nicely.

    Hope this helps.

    Chris

  7. Massimo V

    May 16 Pre-Release Testers, Xojo Pro Europe (Germany, Würzburg)
    Edited 2 weeks ago

    This is a good algorithm for (sort of) AI: http://datagenetics.com/blog/december32011/index.html

    With this I almost won the Battleship contest at XDC 2013 in Orlando. Defeated by Bob Delaney having an algorithm made to spot THIS one. ;)

  8. Markus W

    May 16 Pre-Release Testers, Xojo Pro #JeSuisHuman Germany, Heidelb...

    @Massimo V This is a good algorithm for (sort of) AI: http://datagenetics.com/blog/december32011/index.html

    Very nice indeed, and a perfect example on how to start programming …

  9. last week

    Thanks for your big help! Now I need to see tomorrow if it works or not... I'll work all weekend on it for sure.

  10. 3 days ago

    Ok, so I've made a bit of progress. I got my "boards" working, so now I'm working on positionning the boats on it by the players as the game starts. I was wondering how to create a rectangle object from the code without using classes (the same one that would be created from the drag and drop interface). I know there's a way but I've got no idea how to do it and I didn't see it in the xojo help. By the way big thanks to Chris Verberne for making me understand how to use 2D arrays properly.

  11. Dave S

    May 24 San Diego, California USA

    Rectangle
    Class (inherits from RectControl)
    Draws a rectangle.

  12. 2 days ago

    Jeff T

    May 24 Midlands of England, Europe

    Create a matching array of RECTs holding the corner positions of the rectangles

    When the screen paints, use g.drawrect x,y,width, height to draw them if required.?

  13. @Jeff T Create a matching array of RECTs holding the corner positions of the rectangles

    When the screen paints, use g.drawrect x,y,width, height to draw them if required.?

    How would I access those ? I'll be using those rectangles as the boats.

    I'll have to use the answer Dave game me and research on how to use classes.

  14. Massimo V

    May 24 Pre-Release Testers, Xojo Pro Europe (Germany, Würzburg)

    @LouisRobitaille I'll have to use the answer Dave game me and research on how to use classes.

    Being a homework (as it seems) this would be a very instructive research. ;)

  15. Dave S

    May 24 San Diego, California USA

    @Dave S its a homework assignment. so either your instructor has given you something beyond the current level of the class, or you haven't been studying as required, or (least likely) you have a terrible instructor

    So is the new approach to teaching programming the "sink or swim method"..... I would have thought the cirriculum would have talked about alll the "parts" (2D arrays, classes, graphics etc), and the "homework" would be to put together what had been learned into a cohesive project, but based on what I am seeing here, that isn't the case.

  16. @Dave S So is the new approach to teaching programming the "sink or swim method"..... I would have thought the cirriculum would have talked about alll the "parts" (2D arrays, classes, graphics etc), and the "homework" would be to put together what had been learned into a cohesive project, but based on what I am seeing here, that isn't the case.

    I asked my classmates and he actually didn't teach us everything I need for this project. He never mentionned anything about graphics nor the 2D arrays. Some other guys in my class even use folders... Which I got no idea how to use, and won't use anyway.

  17. Dave S

    May 24 San Diego, California USA

    @LouisRobitaille I asked my classmates and he actually didn't teach us everything

    So, you never even went to class.... "Folders" for this kind of project? Dude, if you PAID for this class, get your money back.

  18. I'm only 17 year old actually. I'm still in "must go to school" stage. Anyway I did go to every class but 2 because I had surgery.
    The only way I would use folders would be to save the shortest number of shots fired to win. Would make sense to use folders to store high scores between games in my book.

  19. 19 hours ago

    Jeff T

    19 hours ago Midlands of England, Europe

    I asked my classmates and he actually didn't teach us everything I need for this project. He never mentionned anything about graphics nor the 2D arrays.

    You know, one interpretation of
    'I have to make a battleship game'

    Could be 'write something that prints a grid on a piece of paper for use as a Battleship game using pencils and humans'
    Thats about your current coding level.

    Did he actually mention Xojo??
    Or are you supposed to be doing this with some game development tool for iPads that does hit detection and similar?

    'make a battleship game'' sounds a lot like this assignment, which is way beyond what you are ready for, unless there was some amazing tutoring going on during the 2 sessions you didnt attend.

    http://lsub.org/pae/homework/battleship.pdf

  20. 17 hours ago

    Markus W

    17 hours ago Pre-Release Testers, Xojo Pro #JeSuisHuman Germany, Heidelb...

    I think Jeff makes a very valid point. We are all working on assumptions, so you should post the actual assignment.

or Sign Up to reply!