• Post Reply Bookmark Topic Watch Topic
  • New Topic

BattleShip AI with Reactive Logic  RSS feed

 
Greenhorn
Posts: 20
C++ Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, I am trying to create an AI that will strategically place shots on a 10x10 game board that is laid out like this:

Wow, that turned out to be a larger image than I intended
I took the liberty of indicating where my ships would be, to helper further down the road.

Anyways, both axes start at 0 because they are both represented by a two-dimensional String array called currentBoard. When I said strategically earlier, I mean that it should focus its shots before returning to a predetermined pattern in which to place its shots.

So, currently, I have the AI set to follow this logic when taking shots:

Now I have 2 other methods that I use:



Here is the shipLayout class for those who would like to see it:

And the Constants class:


If you guys want, I can also provide a picture demonstration of what I want my AI to do once it receives feedback that its previous shot was a hit. However, I don't know where I might I'll come back to this tomorrow ASAP (probably morning) (Saturday, 11/4/17).
 
Marshal
Posts: 56610
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please explain the logic behind that code in plain English. We need to know what you are thinking if you want us to interpret the code.
Who told you there is even such a thing as a 2D array? Java® doesn't support 2D arrays; it supports arrays of arrays which are better.
Why are you using Strings when an enumerated type might be a lot better? Since enum constants are full‑blown objects, you can have something like CRUISER('c' 4) which denotes that a cruiser has four 'c's.
Never write == true or == false, which are both poor style and error‑prone.
If you are going to play the game against an opponent, you are going to have to work out placement
 
Sheriff
Posts: 11495
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Many of the code comments you have written do not match the logic they seem to be associated with. For example,

Here, "increment loopCounter" is part of the comment for the assignment statement of line 29, which is part of the if-statement on line 26. The placement of the comment implies that you're thinking that loopCounter will be incremented if the value of y goes outside the grid bounds. That is not what the code, as written, does though: loopCounter is incremented regardless of the value of y.

This contradiction creates ambiguity for the reader. Is your true intent expressed in the comment or in the code?

Other comments that contradict what the code is actually doing are:

Line 7: comments starts with "if the AI...", whereas the code is a while-loop
Line 10: comment says "create a loop counter"; the variable isn't "created" there, that happens on line 5
Line 10: "...increments itself" yet the statement on line 14 resets the value of loopCounter to 0
Line 12: "...it will decrement the value that x increases by" - that is not what x -= 1 does: it simply decreases the value of x by 1. Line 17 actually increases the value of x by 4 each time through the loop.
Line 21: "...add y to the value x value..." - the code actually adds [b]1[/tt] to the value of y and subtracts 10 from the value of x
 
Sheriff
Posts: 4293
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Minor point: I wouldn't say you are writing an AI.  Artificial Intelligence is a very advanced technology.  You are writing code that follows an algorithm (set of instructions).
 
Junilu Lacar
Sheriff
Posts: 11495
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The dissonance between your comments and the actual code logic clearly illustrates how comments are very unreliable. Most experienced developers have a tendency to distrust comments and often ignore them altogether in favor of what the code actually says.

I have found that writing code that is more expressive often eliminates the need to write comments. Most of the comments you wrote are redundant anyway.  Take this comment for example:

That's like saying "I'm putting my left foot forward" to someone who is watching you as you put your left foot forward. That only makes sense if you're teaching someone how to "Do the Hokey Pokey." It's completely meaningless and without value in a computer program.

Also, consider these:

The third alternative makes a comment unnecessary because the code clearly tells you its intent.
 
Junilu Lacar
Sheriff
Posts: 11495
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You shouldn't write a class like Constants. I don't know if you came up with this yourself or if this is something you were taught to do. Regardless, what you did there is very bad practice when programming to Java 5 or greater (I can't think of why you'd want to use any version less than JDK 1.5 to learn to program in Java)

Use enum types instead. Java enum types are type-safe and they eliminate the need to roll your own methods to convert strings to enum values and vice versa.
 
Zachary House
Greenhorn
Posts: 20
C++ Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have found a solution after
  • Literally, and I do mean do literally, 30+ hours of trial and error
  • consulting my java professor (who I learned has a PhD in Computer Science)
    and even
  • Posting a terribly formed question here on a whim because I've received immensely helpful suggestions here before


  • I do apologize for the last one, as it has only been met with disapproval, as it should be. On the plus side, I will definitely use the advice in the comments in future projects. However, as of now, I do not wish to rename the classes and have to rework and refactor an entire multiple-class project.
    Should you guys wish to see the code, I can provide it. I'll do my best to user better, clearer comments where they are needed.
    However, as far as the class names go, I cannot change those as it would require quite a bit of reworking, and that doesn't just include the project code.
     
    Junilu Lacar
    Sheriff
    Posts: 11495
    180
    Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Modern IDEs like Eclipse, NetBeans, and IntelliJ IDEA make refactoring to rename a class relatively trivial and painless, even for the largest projects. I doubt your code will present any problems for them to rename a class even when there are thousands of existing uses of the name scattered all over the place. It's your call though; I'm just saying that there's really no valid excuse to not do the right thing when it comes to renaming things so your code is more readable, especially with the kind of things our modern development tools can do for you.
     
    Junilu Lacar
    Sheriff
    Posts: 11495
    180
    Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Zachary House wrote:I do apologize for the last one, as it has only been met with disapproval, as it should be.

    "Disapproval" has a more negative connotation to it. While I suppose a hint of disapproval is unavoidable, we like to think of the comments we offer here more as constructive criticism.
     
    It is sorta covered in the JavaRanch Style Guide.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!