Jump to content

Hello Everyone,

 

I have a C program here trying to run throw a multidimensional array procedurally "shooting" at each place and keeping track of hits until it knows it has sunk every ship. For some reason, once it shoots the last part of the last ship, it excepts. I have absolutely no idea why this is. It does not print its success message, it just detects that it has won, and then commits suicide.

 

ANY RELEVANT INPUT/GUESSES ARE WELCOME

 

I have commented the code thoroughly, so I hope you won't be confused about anything regarding how it all works.

 

Code:

Spoiler

// Includes
#include <stdlib.h>
#include <stdio.h>
#include <string.h>


// My game board, 1 = ship, 0 = water
int hori[10][10] = {
    {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
    {0, 0, 0, 1, 1, 1, 0, 0, 0, 0},
    {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
    {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
    {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
    {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
    {0, 0, 1, 1, 1, 0, 0, 0, 0, 0},
    {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
    {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
    {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
};

/* Firing function - takes row, column, and the board, then checks
 * if that value is ship or not and returns accordingly (also prints
 * location fired at)
 */
int fire(int row, int col, int board[10][10]){
    printf("Shooting at %d %d\n", row, col);
    if(board[row][col] == 1){
        return 1;
    }
    return 0;
}

/* Baseline firing strategy by which I will measure all other
 * future strategies - takes only a multidemensional array
 * as the game board to iterate through
 */
int strat_method(int board[10][10]){
    // initialize hit counter
    int hits = 0;
    /* enter fancy loop that substitutes instead of nested loops like below
     * for(i = 0; i < 10; i++){
     *      for(j = 0; j < 10; j++){
     *          do stuff
     *      }
     * }
     */
    for(int i = 0; i < 100; i++){
        // store hit/miss result
        int hit_miss = fire((i-(i%10))/10, i%10, board);
        // if it's a hit, increments hits and print "hit"
        if(hit_miss == 1){
            hits++;
            printf("Hit %d\n", hits);
            /* if we get all six hits we print how many shots it took
             * and then exit with 0
             */
            if(hits > 5){
                printf("Shots fired: %s", i);
                return 0;
            }
        }
    }
    /* because we are going to be shooting literally every place on the board,
     * there is no reason we should not hit every ship in that above loop,
     * therefore throw something different if we don't sink every ship,
     * regardless of the fact that main does not check this value
     * (will fix in the future)
     */
    return 1;
}

// Main just calls our strategy on the board and then returns
int main(){
    strat_method(hori);
    return 0;
}

 

 

Thanks in advance

Join the Appleitionist cause! See spoiler below for answers to common questions that shouldn't be common!

Spoiler

Q: Do I have a virus?!
A: If you didn't click a sketchy email, haven't left your computer physically open to attack, haven't downloaded anything sketchy/free, know that your software hasn't been exploited in a new hack, then the answer is: probably not.

 

Q: What email/VPN should I use?
A: Proton mail and VPN are the best for email and VPNs respectively. (They're free in a good way)

 

Q: How can I stay anonymous on the (deep/dark) webzz???....

A: By learning how to de-anonymize everyone else; if you can do that, then you know what to do for yourself.

 

Q: What Linux distro is best for x y z?

A: Lubuntu for things with little processing power, Ubuntu for normal PCs, and if you need to do anything else then it's best if you do the research yourself.

 

Q: Why is my Linux giving me x y z error?

A: Have you not googled it? Are you sure StackOverflow doesn't have an answer? Does the error tell you what's wrong? If the answer is no to all of those, message me.

 

Link to comment
https://linustechtips.com/topic/910476-c-program-excepting/
Share on other sites

Link to post
Share on other sites

What is the exact output or error it's giving? C doesn't have exception handling so I'm curious what this is.

 

As far as the whole "code reviewing" bit goes:

  • Try not to use "magic numbers" for array size assignment. Use a #define or a const value, then use that to define the size of the array.
  • For the parameters that take the array, you can drop the number in the first []. It's the second [] that needs to have its size defined. Though you can also pass in a double pointer which is preferable so you can use any array size (see https://stackoverflow.com/questions/10003270/gcc-array-type-has-incomplete-element-type)
  • Use nested loops. It's easier for people to read than try to figure out how to get a row from i-(i%10))/10.
Link to comment
https://linustechtips.com/topic/910476-c-program-excepting/#findComment-11184054
Share on other sites

Link to post
Share on other sites

please see below post

Join the Appleitionist cause! See spoiler below for answers to common questions that shouldn't be common!

Spoiler

Q: Do I have a virus?!
A: If you didn't click a sketchy email, haven't left your computer physically open to attack, haven't downloaded anything sketchy/free, know that your software hasn't been exploited in a new hack, then the answer is: probably not.

 

Q: What email/VPN should I use?
A: Proton mail and VPN are the best for email and VPNs respectively. (They're free in a good way)

 

Q: How can I stay anonymous on the (deep/dark) webzz???....

A: By learning how to de-anonymize everyone else; if you can do that, then you know what to do for yourself.

 

Q: What Linux distro is best for x y z?

A: Lubuntu for things with little processing power, Ubuntu for normal PCs, and if you need to do anything else then it's best if you do the research yourself.

 

Q: Why is my Linux giving me x y z error?

A: Have you not googled it? Are you sure StackOverflow doesn't have an answer? Does the error tell you what's wrong? If the answer is no to all of those, message me.

 

Link to comment
https://linustechtips.com/topic/910476-c-program-excepting/#findComment-11184231
Share on other sites

Link to post
Share on other sites

1 hour ago, M.Yurizaki said:

What is the exact output or error it's giving? C doesn't have exception handling so I'm curious what this is.

Sorry for forgetting to include this in the original post. Originally I was getting a Cygwin open exception error, with no intelligible details. I'm comping with Cygwin on Windows 10. Here is a complete output:

Spoiler

C:\Users\[redacted]\Bot>cygcheck --version
cygcheck (cygwin) 2.9.0
System Checker for Cygwin
Copyright (C) 1998 - 2017 Cygwin Authors
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

C:\Users\[redacted]\Bot>gcc --version
gcc (GCC) 6.4.0
Copyright (C) 2017 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.


C:\Users\[redacted]\Bot>gcc battleshipper_annotated.c -o battleshipper_annotated -O3

C:\Users\[redacted]\Bot>.\battleshipper_annotated.exe
Shooting at 0 0
Shooting at 0 1
Shooting at 0 2
Shooting at 0 3
Shooting at 0 4
Shooting at 0 5
Shooting at 0 6
Shooting at 0 7
Shooting at 0 8
Shooting at 0 9
Shooting at 1 0
Shooting at 1 1
Shooting at 1 2
Shooting at 1 3
Hit 1
Shooting at 1 4
Hit 2
Shooting at 1 5
Hit 3
Shooting at 1 6
Shooting at 1 7
Shooting at 1 8
Shooting at 1 9
Shooting at 2 0
Shooting at 2 1
Shooting at 2 2
Shooting at 2 3
Shooting at 2 4
Shooting at 2 5
Shooting at 2 6
Shooting at 2 7
Shooting at 2 8
Shooting at 2 9
Shooting at 3 0
Shooting at 3 1
Shooting at 3 2
Shooting at 3 3
Shooting at 3 4
Shooting at 3 5
Shooting at 3 6
Shooting at 3 7
Shooting at 3 8
Shooting at 3 9
Shooting at 4 0
Shooting at 4 1
Shooting at 4 2
Shooting at 4 3
Shooting at 4 4
Shooting at 4 5
Shooting at 4 6
Shooting at 4 7
Shooting at 4 8
Shooting at 4 9
Shooting at 5 0
Shooting at 5 1
Shooting at 5 2
Shooting at 5 3
Shooting at 5 4
Shooting at 5 5
Shooting at 5 6
Shooting at 5 7
Shooting at 5 8
Shooting at 5 9
Shooting at 6 0
Shooting at 6 1
Shooting at 6 2
Hit 4
Shooting at 6 3
Hit 5
Shooting at 6 4
Hit 6

C:\Users\[redacted]\Bot>

 

1 hour ago, M.Yurizaki said:

As far as the whole "code reviewing" bit goes:

  • Try not to use "magic numbers" for array size assignment. Use a #define or a const value, then use that to define the size of the array.
  • For the parameters that take the array, you can drop the number in the first []. It's the second [] that needs to have its size defined. Though you can also pass in a double pointer which is preferable so you can use any array size (see https://stackoverflow.com/questions/10003270/gcc-array-type-has-incomplete-element-type)
  • Use nested loops. It's easier for people to read than try to figure out how to get a row from i-(i%10))/10.
  • Referring to my [10][10] in defining my array?
  • Okay, thanks!
  • I made a comment about that, will be testing to see difference in speed

Join the Appleitionist cause! See spoiler below for answers to common questions that shouldn't be common!

Spoiler

Q: Do I have a virus?!
A: If you didn't click a sketchy email, haven't left your computer physically open to attack, haven't downloaded anything sketchy/free, know that your software hasn't been exploited in a new hack, then the answer is: probably not.

 

Q: What email/VPN should I use?
A: Proton mail and VPN are the best for email and VPNs respectively. (They're free in a good way)

 

Q: How can I stay anonymous on the (deep/dark) webzz???....

A: By learning how to de-anonymize everyone else; if you can do that, then you know what to do for yourself.

 

Q: What Linux distro is best for x y z?

A: Lubuntu for things with little processing power, Ubuntu for normal PCs, and if you need to do anything else then it's best if you do the research yourself.

 

Q: Why is my Linux giving me x y z error?

A: Have you not googled it? Are you sure StackOverflow doesn't have an answer? Does the error tell you what's wrong? If the answer is no to all of those, message me.

 

Link to comment
https://linustechtips.com/topic/910476-c-program-excepting/#findComment-11184278
Share on other sites

Link to post
Share on other sites

12 minutes ago, LtStaffel said:

Sorry for forgetting to include this in the original post. Originally I was getting a Cygwin open exception error, with no intelligible details. I'm comping with Cygwin on Windows 10. Here is a complete output:

While I don't see any errors outputted in that, I did find one thing that is a problem and it slipped my mind:

printf("Shots fired: %s", i);

That should be:

printf("Shots fired: %d", i);

And it's missing a newline if you care about that.

 

Quote
  • Referring to my [10][10] in defining my array?

Yes. Well, in the functions that use the array, you don't need to do array[10][10] as a parameter. You can do array[][10], e.g.:

 

Talked about the wrong thing. But yes, when defining your array, use a symbol name either from a #define or constant and use that for any other limits or sizes you need to use. That way also, if you need to change the array size, it's all in one place.

Quote
  • I made a comment about that, will be testing to see difference in speed

It's likely not going to result in an appreciable increase in speed for the trick that you used. Unless performance is absolutely paramount or it saves a significant amount of time (like going from an O(n) to O(log(n)), it's better to have code most people can understand.

Edited by M.Yurizaki
Link to comment
https://linustechtips.com/topic/910476-c-program-excepting/#findComment-11184297
Share on other sites

Link to post
Share on other sites

3 minutes ago, M.Yurizaki said:

While I don't see any errors outputted in that, I did find one thing that is a problem and it slipped my mind:


printf("Shots fired: %s", i);

That should be:


printf("Shots fired: %d", i);

And it's missing a newline if you care about that.

....Can't believe I did that. Thanks lol

 

4 minutes ago, M.Yurizaki said:

Yes. Well, in the functions that use the array, you don't need to do array[10][10] as a parameter. You can do array[][10], e.g.:


int fire(int row, int col, int board[][10])

It's likely not going to result in an appreciable increase in speed for the trick that you used. Unless performance is absolutely paramount or it saves a significant amount of time (like going from an O(n) to O(log(n)), it's better to have code most people can understand.

Thanks!

Performance is very important, especially in future iterations of this program, and I will be the only reading/using it. It will also be competing in real time against other bots trying to shoot my ships, so there's that aspect. Oh, if you want, you could also make a bot for this. I'd be really interested to see how well my strategies fair against other people's stuff

Join the Appleitionist cause! See spoiler below for answers to common questions that shouldn't be common!

Spoiler

Q: Do I have a virus?!
A: If you didn't click a sketchy email, haven't left your computer physically open to attack, haven't downloaded anything sketchy/free, know that your software hasn't been exploited in a new hack, then the answer is: probably not.

 

Q: What email/VPN should I use?
A: Proton mail and VPN are the best for email and VPNs respectively. (They're free in a good way)

 

Q: How can I stay anonymous on the (deep/dark) webzz???....

A: By learning how to de-anonymize everyone else; if you can do that, then you know what to do for yourself.

 

Q: What Linux distro is best for x y z?

A: Lubuntu for things with little processing power, Ubuntu for normal PCs, and if you need to do anything else then it's best if you do the research yourself.

 

Q: Why is my Linux giving me x y z error?

A: Have you not googled it? Are you sure StackOverflow doesn't have an answer? Does the error tell you what's wrong? If the answer is no to all of those, message me.

 

Link to comment
https://linustechtips.com/topic/910476-c-program-excepting/#findComment-11184336
Share on other sites

Link to post
Share on other sites

13 minutes ago, LtStaffel said:

Performance is very important, especially in future iterations of this program, and I will be the only reading/using it.

Even if you're the only one using it, you will likely forget what it's supposed to do a few months after you've stopped looking at it. It sucks to have to relearn what your code is trying to do.

Quote

It will also be competing in real time against other bots trying to shoot my ships, so there's that aspect. Oh, if you want, you could also make a bot for this. I'd be really interested to see how well my strategies fair against other people's stuff

What kind of strategy you should use depends on the input parameters and what else is known. Like for instance, if all the ship sizes are 3x1, then you can reduce the brute force algorithm by testing every column every third row and every third column for the other rows. This is the minimum coverage you need to hit at least one spot of a ship no matter where it is or its orientation.

Link to comment
https://linustechtips.com/topic/910476-c-program-excepting/#findComment-11184387
Share on other sites

Link to post
Share on other sites

2 minutes ago, M.Yurizaki said:

What kind of strategy you should use depends on the input parameters and what else is known. Like for instance, if all the ship sizes are 3x1, then you can reduce the brute force algorithm by testing every column every third row and every third column for the other rows. This is the minimum coverage you need to hit at least one spot of a ship no matter where it is or its orientation.

Yep. I've been doing some research (reading some papers written by professors studying this, reading about optimizations methods, etc)

This program just simply brute forces every square, I'm trying to establish a baseline to work from.

(In case you were interested, my single for loop instead of nested loops has a very very slight advantage in speed over the nested loops, on an average of averages)

Join the Appleitionist cause! See spoiler below for answers to common questions that shouldn't be common!

Spoiler

Q: Do I have a virus?!
A: If you didn't click a sketchy email, haven't left your computer physically open to attack, haven't downloaded anything sketchy/free, know that your software hasn't been exploited in a new hack, then the answer is: probably not.

 

Q: What email/VPN should I use?
A: Proton mail and VPN are the best for email and VPNs respectively. (They're free in a good way)

 

Q: How can I stay anonymous on the (deep/dark) webzz???....

A: By learning how to de-anonymize everyone else; if you can do that, then you know what to do for yourself.

 

Q: What Linux distro is best for x y z?

A: Lubuntu for things with little processing power, Ubuntu for normal PCs, and if you need to do anything else then it's best if you do the research yourself.

 

Q: Why is my Linux giving me x y z error?

A: Have you not googled it? Are you sure StackOverflow doesn't have an answer? Does the error tell you what's wrong? If the answer is no to all of those, message me.

 

Link to comment
https://linustechtips.com/topic/910476-c-program-excepting/#findComment-11184396
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×