473,748 Members | 10,737 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 3744
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
8093
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
2179
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
3253
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
1158
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
1533
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
9393
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
5997
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
6732
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
8991
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
8830
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,...
0
9541
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9370
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9321
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
8242
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...
1
6796
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6074
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
4874
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.