473,399 Members | 3,106 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

Battleship

telgroc
I'm trying to write the code for the game Battleship, in order to count how many turns it will take to sink all 5 ships. I've written it without strategy (it just generates random numbers until 17 hits have occurred. However, I can't figure out how to add strategy, so when a hit is scored, the program will adjust to try and specify the spots around the hit and when it finds which direction the rest of the ship lies, it finishes off the ship. Any suggestions? Code is posted below,
Expand|Select|Wrap|Line Numbers
  1. import random
  2. battleBoard=\
  3.               [['a','a','a','a','a','a','a','a','a','a'],\
  4.                ['a','a','a','a','a','a','a','a','a','a'],\
  5.                ['a','B','a','a','a','a','a','a','a','a'],\
  6.                ['a','B','a','a','B','B','B','B','B','a'],\
  7.                ['a','B','a','a','a','a','a','a','a','a'],\
  8.                ['a','a','a','B','a','a','a','a','a','a'],\
  9.                ['a','a','a','B','a','a','a','a','a','a'],\
  10.                ['a','a','a','B','a','a','a','a','a','a'],\
  11.                ['a','a','a','a','a','a','a','a','a','B'],\
  12.                ['a','B','B','B','B','a','a','a','a','B']]
  13. count=0
  14. hitCount=0
  15. priorMove=[]
  16. while hitCount<17:
  17.     n=random.randint(0,9)
  18.     j=random.randint(0,9)
  19.     b=[n,j]
  20.     dupCount=0
  21.     for a in range(len(priorMove)):
  22.         if b==priorMove[a]:
  23.             dupCount=dupCount+1
  24.         elif b!=priorMove[a]:
  25.             dupCount=dupCount
  26.     if dupCount==0:
  27.         if battleBoard[n][j]=='B':
  28.             priorMove.append(b)
  29.             hitCount=hitCount+1
  30.             count=count+1
  31.         elif battleBoard[n][j]=='a':
  32.             priorMove.append(b)
  33.             count=count+1
  34. print 'The  program took',count,'turns to sink all 5 ships.'
  35.  
Nov 6 '08 #1
3 3710
Fabez
29
If it scores a hit then it should look in the eight squares around it for another hit, if not then it should pick a random place. If the hit is near the edge omit squares as needed.
Nov 7 '08 #2
YarrOfDoom
1,247 Expert 1GB
Don't you mean 4 squares? If I remember it right, ships can't be set up diagonally. Just a detail, but it makes it more efficient.
Nov 8 '08 #3
Fabez
29
Thanks for pointing that out, you are correct, it should be four squares instead of eight.
Nov 24 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: C-man | last post by:
Basically I want to create a battleship game that can be played by having two clients connect to it. I was wonder what the best networking principal would be. Basically I suppose I would have to...
0
by: Michael Goettsche | last post by:
Hi there, for a project in our computer science lessons at school we decided to write a client/server based battleship like game . I know this game could be written without a server, but the...
0
by: webhosting | last post by:
After a failure, we promoted a slave to master. This included renaming the server to the old master's name and ip address, but leaving the server id alone. The new master works fine. We...
5
by: iwdu15 | last post by:
hi, to get the bytes sent by a socket, in VB a simple local variable like below was used Dim recv as Byte() how can i do that in C++...this is what i am trying now Byte * rec = new Bytes; ...
1
by: iwdu15 | last post by:
hi, im trying to program the game battleship but i cant figure out how to code how people can place the ships and keep track of it. Ive been trying and researching for a couple days now and am at...
24
by: gonzo86 | last post by:
Hey, Im kind of new to C++ and I'm working on making a Battleship program. I need to be able to output a screen with just dots on it for when people havent tried those spots and then be able to...
3
by: bgordon0 | last post by:
Here's my class and its constructor: class gBoard { public: gBoard(); private: //cell is like the 4th quadrant with Y first and always positive int cell; //0 for empty, ship code for ship,...
5
by: muna123 | last post by:
hi, I need help in battleship game here is my code: #include <stdio.h> void draw_game(char board, int maway, int mleft, int score, char prevmove);
19
by: Olivia Jaden | last post by:
Hi, I was about to create a 8x8 battleship program. there should be 8 ships in total with 1x1 size. However, I do not know how to place the ships randomly. Below are some of the codes that I...
3
by: noideanoclue | last post by:
I am writing a Battleship 'Game' in Java as part of familiarizing myself with the language, but have run into a problem which I can't solve, for some inexplicable reason one of my if loops isn't...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.