Jump to content

  • Log In with Google      Sign In   
  • Create Account


Member Since 16 Mar 2005
Offline Last Active Today, 02:33 PM

#5174941 Using old array or stl

Posted by rip-off on Today, 01:38 AM

You might see a perf difference for std::vector over a C array because vector throws exceptions on out-of-bounds access, meaning it has to check every access. I don't recall if std::array throws on out of bounds access, but I'd imagine it does.

std::vector::at throws an exception, but std::vector::operator[] does not. Some light Googling indicates that std::array is similar.


Most implementations of the C++SL will do range checking, e.g. as an assertion, in debug builds, I believe at least some versions of VC++ went for range checking in all builds, but with a separate flag to disable them.

#5174559 Help me, i can't understand well :)

Posted by rip-off on 18 August 2014 - 03:58 PM

Your question is still very vague. Do you understand what seek is supposed to do? Do you understand the purpose of rewind, and why it is implemented as it was?

#5174379 Bashing my face against the ArcSynthesis.org brick wall.

Posted by rip-off on 18 August 2014 - 01:28 AM

Thanks for all that, if that was intended as somehow to answer and solve why these examples don't work, well then i give up on c++ because its just too complicated to learn. Not the code, but how to even get a simple example to run. I mean no disrespect at all, but I don't understand a single thing in your post. 

Unfortunately, this is one of the most archaic things about C++. C++ inherited its toolchain model from C, which was designed in the early 70s under the constraints of the computers at the time - memory probably in the low kilobytes and perhaps not more than a few megabytes of disk storage.


While the language has evolved over time, the tools to build and link function conceptually almost the same as they did back then.


This is a common stumbling block, but you can make your way through it if you persevere.

#5174134 Dendy games copyright

Posted by rip-off on 16 August 2014 - 01:48 PM

I'd recommend against cloning a game like that. There is a grey area around any intellectual property as to whether it is so similar to be considered a derived work. You would be at risk of being sued, and whether you're actually infringing or not it can be prohibitively expensive to defend yourself against a large company.


Far better to write your own, unique game that you can be certain doesn't infringe, no?

#5174110 C++ Runtime Error With Odd Workaround

Posted by rip-off on 16 August 2014 - 11:28 AM

I'd recommend in future making it very clear if your example program actually demonstrates the behaviour or if it is just intended to be a simplification. I'd also recommend actually trying to make your example program exhibit the behaviour: the process of reducing the problem to the minimum conditions that reproduce it can be a very effective way of solving the problem.


I sort of blame VC++ for reporting the wrong line # at which the access error occurs

This is surprising. It is unclear to me how experienced you are, but regardless, are you sure you weren't a simple mistake like trying to debug a release build? I've heard of the debug database getting corrupt or out of date in older versions, maybe a clean build would have helped.


It might be worthwhile re-introducing such a bug and trying to get VC++ to give you the correct information, as you'll almost certainly need to investigate similar issues in future.

#5174107 Trademark question, worried

Posted by rip-off on 16 August 2014 - 11:20 AM

Disclaimer: I'm not a lawyer, and in any case laws vary from place to place.


As far as I understand, there are two types of trade marks, registered and unregistered. I believe you can more or less start using Any TermTM, with the addition those magical little floating letters you are now claiming an unregistered trade mark - provided you're careful not to step on an existing one!


I believe there are certain exemptions for trade marks in the case of something provably established prior to the trade mark.


The bad news is that legal disputes are expensive, and unless you're willing to do all the legal legwork yourself or find someone who will do them for free, you may have extreme difficulty paying for a defence against a big company that insists they own a certain term.


If you're not already familiar with them, I'd recommend researching recent cases where King were attempting to assert their Saga trade mark, and also their attempt to trade mark the term Candy.

#5174094 Looking for 'Visual' HTML/PHP editor.

Posted by rip-off on 16 August 2014 - 09:27 AM

I've no experience with using such tools, but one option to quickly make a website look better is to include something like bootstrap - though you'll probably still want to customise it eventually.

#5174086 Help with ArrayList Java

Posted by rip-off on 16 August 2014 - 08:03 AM

It sounds like you want to take the class name from a file and instantiate objects of that class type. You can do the following:

Class<?> type = Class.forName("net.gamedev.SomeMoveClass");
Move move = (Move)type.newInstance();

If you need to pass arguments, you can use the getConstructor() method and attempt to call newInstance(/* ... */) on that.


You may want to do some sanity checking that the class does implement or extend the Move interface / superclass.


That said, this smells like possibly a more complex solution than you actually need. I'd have to understand the surrounding concepts a bit more to say for sure, but I'd guess there may be other, easier ways to do this.

#5173856 Reading Files

Posted by rip-off on 15 August 2014 - 04:59 AM

In future, please don't post screenshots of code and terminal output, use copy / paste and the forum editing tools instead. As text, we can easily copy your code or output into our own system and play with it. As an image, we would have to re-type your code, which is tedious and error prone - so we probably won't. While some problems can be solved just by looking at the code and output, not all can.


If you find the forum editing tools a little fiddly, you can disable them and instead manually type [code][/code] around your code.


It will appear like so:

# Your code here!

#5173729 Best way to traverse a 2d array to dertermine the winner in Tic-Tac-Toe

Posted by rip-off on 14 August 2014 - 04:40 PM

Here is a version which, while still containing a few "magic" numbers, I think they can be more readily be deciphered than Álvaro's linked version due to the names involved. I also think the logic of the algorithm is more obvious than Álvaro's neat little trick:

enum Piece {
typedef Piece Board[3][3];
struct VictoryCondition {
    int startIndex;
    int stride;
    int index(int i) const {
        return startIndex + (i * stride);
const VictoryCondition possibleVictories[] = {
    {0, 1}, {3, 1}, {6, 1},
    {0, 3}, {1, 3}, {2, 3},
    {0, 4}, {2, 2}
Piece winner(Board board) {
    Piece *pointer = &board[0][0];
    for(VictoryCondition v : possibleVictories) {
        Piece possibleVictor = pointer[v.index(0)];
        if(possibleVictor != None && possibleVictor == pointer[v.index(1)] && possibleVictor == pointer[v.index(2)]) {
            return possibleVictor;
    return None;

I wouldn't say this is the "best", personally I think the tedious but obvious set of loops is probably the way to go without a compelling reason otherwise.

#5172496 Battleship in java

Posted by rip-off on 09 August 2014 - 01:37 PM

Having studied your code a bit more, there are a few more points I would make:


The method names like arrayAssignShip and arrayAssignInput are very mechanical, they describe what they do rather than the intent behind them. The first might be called "placeShip", the other is a little more tricky (I'll talk about this later), but in it's current state perhaps something like "mapInputToBoard". Now, play is a far better name, but it is still a bit vague. A better name might be something like "takeTurn" or "makeMove".


And while you've improved some of the names, the vast majority of them are unchanged. Names like the following are still very poor:

for(Ship e: playerOneShips)
    arrayAssignShip(e, playerOneBoard);
// ...
int i = 0;
int j = 0;
int k = 0;
int l = 0;
int m = 0;
int b = 0;
char c;
int inrow;
// ...

for(Ship x : playerShips)
    // ...


The methods you split up are still quite large. This means that they are still difficult to understand. The shortest one, arrayAssignInput, is about as long as I ideally let a method get. This way, you can see all of a method without scrolling. Shorter methods again tend to be more "obvious", and are easier to give good names as they are clearly focussed on one task.


You should try to separate display from your internal data structures. For example, all your arrays have 11 elements, but you're using elements 1 through 10. This is convenient as the indices you are using and the inputs you accept from the user. However, this is highly unconventional. While it isn't particularly problematic for the numeric input, it starts getting very inconvenient indeed for the character input. Instead, focus on quickly validating the user input and converting it to an internal format. In this case, the number the user inputs should be validated as between 1 and 10, and then you subtract one and use as your row index. Likewise, your character input should be validated for valid range and converted to an index, as LennyLen indicated how to do.


This is most obvious in the difficulty you appear to have handling the columns of the board state and the ship - by handling this as character based indexes internally, it certainly complicates a lot of things, even though it would be technically possible to achieve.


By elevating the concept of a "point", "location" or "co-ordinate" to something that can be described in the vocabulary of your program, 


Related to how you represent indices is how you represent ships and guesses. This is structuring your data in a way that is convenient for the program. There are often multiple ways to do so.


For instance, instead of an array of 1s and 0s (which probably should have been booleans), you can represent the board as a grid of Ship references. So if you have a patrol boat at the top left horizontally, those two occupied grid entries could point back to this Ship. This allows you to quickly get from a grid location to the Ship that cross it, so you can call methods to mark the ship as being hit.


In another representation, you could potentially dispense with the Ship objects themselves - all you need is something like a ShipType enumeration, with an entry for each Ship type, and an be used to get the display name and length of the ship. Your grid could consist of ShipType values of the appropriate length. Once you know a ship has been hit, you can attempt to access the adjacent horizontal or vertical entries in the grid to determine if it has been sunk.


Yet another way is to dispense with the grid of ship locations. Instead, your Ship class can each contain an array of booleans the length of that ship, each element of which represents whether that location has been hit. So to resolve if a hit has occurred, just iterate through the Ships, determine if the grid location lies between the locations spanned by the ship, and if so calculate the index into that array and mark it as hit.


Currently, you're storing redundant information - you store what you call a "count", which appears to be the number of hits that player has scored. You can also calculate this on demand by iterating though the Ships and summing the "hits taken" of each one. This isn't always a bad idea, in fact sometimes it can make sense to do so - when it might take a long time to iterate through all the elements in question. Here, it isn't going to take any time at all. However, it can cause difficulty as the values can get out of sync, though this isn't likely in your example, despite the fact that you've actually made this more complex still as you have the count arrays, and also more count variables.


I mentioned about your guess representation and algorithm. It is very complex. Part of the problem is that you're mapping grid co-ordinates to a separate grid, and then looping simultaneously between this array and your other arrays. Instead, try to structure the algorithm to avoid the temporary array and instead access the array of interest directly.


Some more general notes that are more purely stylistic:


I'd recommend scoping your variables much more tightly. For example, loop counters are best declared inside the loop statement, like so:

for(int i = 1; i <= 10; i++)
    for(int j = 1; j <= 10; j++)
        if(playerGuess[i][j] == 3)
            System.out.print("* ");
        if(playerGuess[i][j] == 1)
            System.out.print("X ");
        if(playerGuess[i][j] == 0)
            System.out.print(playerGuess[i][j] + " "); 


Another example is here:

System.out.print("\nEnter a charactor from A - k for column and and number from 1 - 10 \nrespectively for the quardents of your shot: ");
c = in.next().charAt(0);
inrow  = in.nextInt();

These variables should be declared at this point, like so:

System.out.print("\nEnter a charactor from A - k for column and and number from 1 - 10 \nrespectively for the quardents of your shot: ");
char c = in.next().charAt(0);
int inrow  = in.nextInt();


Encapsulation is capturing logic inside a class and not having it leak outside. Ideally, all variables in all classes will be "private". Not only that, but the details of this state, even if private, should not be exposed lightly. Here is an example:

// ...
if(ship.getHit() == ship.getSize())
    System.out.print("you sunk my : " + ship.getName() + "\n");
    System.out.print("\n\nPlayer hit a ship\n");

Here, we can hit the "hit" value by having more meaningful method names that are not necessarily related to the variables that are used by them:

// ...
    System.out.print("you sunk my : " + ship.getName() + "\n");
    System.out.print("\n\nPlayer hit a ship\n");
// ...
class Ship {
     private final int length;
     private int hitsTaken;
     // ...
     public void markDamaged() {
         assert hitsTaken < length;
         hitsTaken += 1;
     public boolean isSunk() {
         return hitsTaken >= length;


I've already mentioned about the magic numbers, but it bears repeating. It is very difficult to understand code where you see strange constants like 3 or 5 that have no meaning in an of themselves. You have to reason about the consequences before you can discover the meaning, and it can be easy to make an incorrect guess if you make a mistake or are impatient. Far better to name constants like 17 as "HitsRequiredToWin".


If you're only concerned about 0 or 1, using a boolean is clearer. If you're not dealing with numeric data, then consider using an enumeration.


I'd recommend moving your Ship class, and any other classes you make, to a separate file.

#5172147 Battleship in java

Posted by rip-off on 07 August 2014 - 04:21 PM

There are numerous issues with your current program that make it very difficult to read. The first is the variable names. Short names like "s1", "e" and "b1" are devoid of meaning. While you might know what they mean now, we don't and in a few weeks or months you won't remember either.


Instead, use descriptive names like playerOneFleet. You'll almost never regret using a longer word - remember a good IDE can offer you auto-completion so you won't even need to type it out fully!


The next issue is that your program contains lots of magic numbers. The integers in the "g" array seem to correspond with various states, but these are all implicit. Consider using named constants:

final int UNKNOWN = 0;
final int MISS = 1
final int HIT = 2;

// Later ...

if (boardPosition[y][x] == UNKNOWN) {
    if (containsShip(y, x)) {
        damageShip(y, x);
        boardPosition[y][x] = HIT;
    } else {
        boardPosition[y][x] = MISS;
} else {
    System.out.println("You've already made that move!");

Better still would be to use an enum to represent this, but I'm not sure if you're familiar with them yet.


Another thing is modelling. You've modelled the Ship explicitly, but everything else is in a bunch of primitive arrays. Consider adding a class that represents the playing board itself, or perhaps two classes, one for the opponents "view" and another for the "true state" board. Try to move much of the logic into these classes.


Then, you should be able to cut the main() method's size in half by saying that a game consists of two player's boards - an array. You have an integer index for the active player. Each turn, you switch the active player between 0 and 1, and delegate to the appropriate object, maybe something like this:

public static void main(String args[]) {
    Board[] boards = { new Board(), new Board() };
    int activePlayer = 0;
    while (!isGameOver(boards)) {
        Board activeBoard = boards[activePlayer];
        Position position = new Position();
        while (!position.isValid()) {
            position = activeBoard.getMove();
        if (activePlayer == 0) {
            activePlayer = 1;
        } else {
            activePlayer = 0;

#5172143 Battleship in java

Posted by rip-off on 07 August 2014 - 03:53 PM

I believe this post should be in the coding horror forum...

Oh the horror of such a long method.


Your code is horribly hard to read, maybe split it up into methods to make things easier for us? wacko.png

Well, this is For Beginners, so if you're not being constructive then please don't respond at all. Presumably, the OP wouldn't be asking the question if they found this easy to do.

#5171614 Cohesion and coupling

Posted by rip-off on 05 August 2014 - 07:10 AM

Can navigation mesh building / updating be de-coupled from object creation?

#5171599 How to use mkdir in java without knowing full path

Posted by rip-off on 05 August 2014 - 04:51 AM

Searching for Java user files directory, the third result I got looks especially promising.