SQL Battleship

  • It was good, the computer had three ships grouped close together, which helped my score. The other two where near by. The only thought I had was that when we played we did not let the other person know what ship they hit, until it was sunk. I think knowing what ship you hit may make it slightly easier.

    -------------------------------------------------------------
    we travel not to escape life but for life not to escape us
    Don't fear failure, fear regret.

  • Game over. Computer player wins in 44 shots!

    Was head to head with the AI and would have won on the next shot. Nice job - very interesting use of SQL.

  • below86 (5/27/2014)


    It was good, the computer had three ships grouped close together, which helped my score. The other two where near by. The only thought I had was that when we played we did not let the other person know what ship they hit, until it was sunk. I think knowing what ship you hit may make it slightly easier.

    Agreed. There are so many variations on the "official" rules, so I just decided to use that ones that I knew best.

  • SCGJohn (5/27/2014)


    Game over. Computer player wins in 44 shots!

    Was head to head with the AI and would have won on the next shot. Nice job - very interesting use of SQL.

    Thank you. And thanks for posting your score. Hope you enjoyed the game. 🙂

  • Great Script and has a lot of features that new SQL developers can use as a learning tool.

    First Game: Loss in 34 shots. But I was hunting down my last ship (the battleship) and had two hits on it so i came very close to winning.

    John

  • jpk 91966 (6/2/2014)


    Great Script and has a lot of features that new SQL developers can use as a learning tool.

    First Game: Loss in 34 shots. But I was hunting down my last ship (the battleship) and had two hits on it so i came very close to winning.

    John

    Thank you! And isn't that the most frustrating thing?! So close to winning! 🙂

  • I've played about 4 times so far, haven't lost yet. Come close to loosing, been down to my patrol boat but was able to sink the computers remaining two ships before it could find mine. Luckily I had found the computers patrol boat early on.

    Great way to spend my break time. Thanks again.

    -------------------------------------------------------------
    we travel not to escape life but for life not to escape us
    Don't fear failure, fear regret.

  • below86 (6/2/2014)


    I've played about 4 times so far, haven't lost yet. Come close to loosing, been down to my patrol boat but was able to sink the computers remaining two ships before it could find mine. Luckily I had found the computers patrol boat early on.

    Great way to spend my break time. Thanks again.

    I play it on and off during break or lunch time. It's funny because people come by my desk and think I am doing some super-complex analysis or something by just looking at the screen. LOL.

  • Sean Smith-776614 (6/2/2014)


    below86 (6/2/2014)


    I've played about 4 times so far, haven't lost yet. Come close to loosing, been down to my patrol boat but was able to sink the computers remaining two ships before it could find mine. Luckily I had found the computers patrol boat early on.

    Great way to spend my break time. Thanks again.

    I play it on and off during break or lunch time. It's funny because people come by my desk and think I am doing some super-complex analysis or something by just looking at the screen. LOL.

    That is what makes this great, most people will not Know you are playing a game. I bragged too soon, I lost twice in a row since. Today's was a nail biter, I only won because I shoot first. The computer had my destroyer(my last ship) down to one shot, I had the computer sub down to one shot, but it could have been H04 or H07, I chose correct, H04. Won in 43 shots.

    -------------------------------------------------------------
    we travel not to escape life but for life not to escape us
    Don't fear failure, fear regret.

  • below86 (6/4/2014)


    Sean Smith-776614 (6/2/2014)


    below86 (6/2/2014)


    I've played about 4 times so far, haven't lost yet. Come close to loosing, been down to my patrol boat but was able to sink the computers remaining two ships before it could find mine. Luckily I had found the computers patrol boat early on.

    Great way to spend my break time. Thanks again.

    I play it on and off during break or lunch time. It's funny because people come by my desk and think I am doing some super-complex analysis or something by just looking at the screen. LOL.

    That is what makes this great, most people will not Know you are playing a game. I bragged too soon, I lost twice in a row since. Today's was a nail biter, I only won because I shoot first. The computer had my destroyer(my last ship) down to one shot, I had the computer sub down to one shot, but it could have been H04 or H07, I chose correct, H04. Won in 43 shots.

    LOL! Good stuff! I've had a few of those too. It's surprising how involved it can become. 😉

  • Game over. Human player wins in 17 shots!

    It's easy when you cheat.

    Thanks for the fun game. Nice interface.

    I'd really like to see this rewritten in a modular form. I'd like to see if genetic algorithms could battle against each other.

    I've written a corewar program if anyone would like to create bots to battle it out. Just search the web for corewars if you don't know what it is.

  • Bill Talada (6/12/2014)


    Game over. Human player wins in 39 shots!

    I'll quit now while I have zero losses.

    Thanks for the fun game. Nice interface.

    I'd really like to see this rewritten in a modular form. I'd like to see if genetic algorithms could battle against each other.

    I've written a corewar program if anyone would like to create bots to battle it out. Just search the web for corewars if you don't know what it is.

    Thanks for the feedback. Very cool idea BTW. Now I have an idea of what to do when I have some more "free time" on my hands. 😀

  • Hi Sean,

    Great program, I had a lot of fun playing and analyzing it.

    The only problem I found is that ship placement is not random enough which makes it easy to find them. For example ships rarely touch boundaries and quite often touch each other or share the same direction.

    Also rules for this game were significantly simplified.

    Try to adopt program to real rules:

    a) Ships cannot touch each other, even diagonally

    b) Set of ships is different: one Battleship (4 squares), two Destroyers (3 sq each), three Submarines (2 sq each) and four Patrol Boats (1 sq each).

    c) Do not report which ship is hit. Opponent should figure this out. Report only Miss, Hit or Sink.

    d) Extra turn after sinking a ship, but not after hitting

  • andrei_solntsev (6/29/2014)


    Hi Sean,

    Great program, I had a lot of fun playing and analyzing it.

    The only problem I found is that ship placement is not random enough which makes it easy to find them. For example ships rarely touch boundaries and quite often touch each other or share the same direction.

    Also rules for this game were significantly simplified.

    Try to adopt program to real rules:

    a) Ships cannot touch each other, even diagonally

    b) Set of ships is different: one Battleship (4 squares), two Destroyers (3 sq each), three Submarines (2 sq each) and four Patrol Boats (1 sq each).

    c) Do not report which ship is hit. Opponent should figure this out. Report only Miss, Hit or Sink.

    d) Extra turn after sinking a ship, but not after hitting

    Thanks for the feedback. These are in fact real rules though. When developing the code I discovered that there are several variations to the game. Wikipedia actually lists out several of these variations, along with common game sites such as Hasbro. As I mentioned, I went with the rules for which I was most familiar with, but did confirm that at least they were valid in some adaptation of the game. On top of that this was developed more for a personal challenge than anything else. I still prefer to pay the game online. 😉 Glad you liked it though!

  • That's ok. It is possible to rewrite code to use different rules.

    For example, you may define Ship_Definitions table as

    IF OBJECT_ID (N'tempdb.dbo.#temp_Ship_Definitions', N'U') IS NULL

    BEGIN

    CREATE TABLE dbo.#temp_Ship_Definitions

    (

    DefID tinyint NOT NULL PRIMARY KEY,

    Letter char(1) NOT NULL,

    Squares tinyint NOT NULL,

    Qty tinyint NOT NULL,

    Name varchar(20) NOT NULL

    );

    INSERT dbo.#temp_Ship_Definitions (DefID, Letter, Squares, Qty, Name)

    VALUES (1, 'B', 4, 1, 'Battleship'),

    (2, 'D', 3, 2, 'Destroyer'),

    (3, 'S', 2, 3, 'Submarine'),

    (4, 'P', 1, 4, 'Patrol Boat');

    END

    Also I recommend to use PIVOT to display a grid. Try this

    SET @Loop_Player = 0;

    WHILE @Loop_Player <= 1 --

    BEGIN

    SELECT [0] AS [A], [1] AS , [2] AS [C], [3] AS [D], [4] AS [E], [5] AS [F], [6] AS [G], [7] AS [H], [8] AS [ I], [9] AS [ J] -- spaces added as letters I and J are too short

    FROM

    (

    SELECT grid_value_current AS [Value], (row_id-1-@Loop_Player*100)/10 AS [Row], (row_id-1)%10 AS [Col]

    FROM dbo.#temp_SB_Ocean_Grid

    WHERE row_id BETWEEN (@Loop_Player*100) AND (@Loop_Player*100 + 99)

    ) AS p

    PIVOT (MAX([Value]) FOR [Col] IN ([0], [1], [2], [3], [4], [5], [6], [7], [8], [9])) AS pvt

    ORDER BY [Row];

    SET @Loop_Player = @Loop_Player + 1;

    END

    This game is a perfect excersise to improve coding skills

Viewing 15 posts - 16 through 30 (of 74 total)

You must be logged in to reply to this topic. Login to reply