473,714 Members | 2,050 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Battleship

telgroc
3 New Member
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 3741
Fabez
29 New Member
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 Recognized Expert Top Contributor
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 New Member
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
8091
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 pass an int value or something representing the x,y coordinates of each shot. Should I use some type of Socket programming or RMI or what else should be used|? Thanks
0
2176
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 whole project is for educational purposes. Being the initiator of this project, I thought I would write a skeleton for it before we actually start with it. The client thing won't be too hard, what I'm having problems with is the server, maybe...
0
3250
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 repaired the old server, gave it a different name and ip address but leaving the server id the same. I get the following error on the slave:
5
1156
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; but i get these errors:
1
1531
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 the end of my rope...if anyone cal help thatd b awsome. i have a 10x10 picturebox grid initialized into a 2d array to hold them. i just need to have a way to have them pick where they want their ships and keep track of it....thanks to anyone...
24
9382
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 switch it with the characters in my array after they have been picked. This is the code I have so far. Right now all all I want to do is make a game where you have to sink the computers ships in less than 50 turns. Thanks for any help that I get. ...
3
5233
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, -1 for hit int hits; //hits is the number of times its been
5
5996
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
6730
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 started after doing some research #include <iostream> #include <windows.h> using namespace std;
3
1077
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 working as it should, I have no idea what the problem is and would appreciate some help. while (y == 1) { System.out.println(" "); System.out.print("Do you want to set the battleship to North, South, East, or West?"); bshipnsew =...
0
8817
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8715
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
9092
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9037
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7963
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5964
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4734
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3170
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2531
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.