-
Nov 10th, 2006, 10:49 PM
#1
Thread Starter
Junior Member
Battle ship game in C,such a challenge thing :D
Hello guys,I'm new to C programming.My lecturer asked me to do a Battle Ship game in C (DOS mode,none GUI required).This is such a interesting game but quite difficult for me.I appreciate if anyone have experieces to share.
The instruction is : Player and computer place ships in 2 fields (acctually 2 10x10 grids) and the ship positions are look like a1-e1 or b2-b4 (2D position). Player and computer will play in turn,every turn is to guess 5 position .The player can not see the computer ships and vice-versa.The game ends when all of one opponent's ships are destroyed.
This game is adviced to used 2 dimensional arrays and global variables are restricted.
Thanks for helps
Last edited by coldzero; Nov 11th, 2006 at 12:35 AM.
-
Nov 11th, 2006, 06:41 AM
#2
Addicted Member
Re: Battle ship game in C,such a challenge thing :D
Advice: First be a little more familiar with the language.
#Appreciate others by rating good posts !!
#The Software Peter Principle is in operation when unwise developers "improve" and "generalize" the software until they themselves can no longer understand it, then the project slowly dies.
#People who are still ignorant of their ignorance are dangerous.
-
Nov 11th, 2006, 08:26 AM
#3
Re: Battle ship game in C,such a challenge thing :D
@SunnyPalSingh,
Just because someone is new to the forums, it doesnot always imply that the member is a noob.
@ColdZero, Nobody would do your homework here. We can help you ALONG with your project, but you would have to show some effort.
What code do you have till now?? And most importantly, have you looked and searched, or questioned your teacher about the logic involved?
Coding is no problem, but the major factor is to understand the algorithm used to solve this game.
-
Nov 11th, 2006, 09:21 AM
#4
Fanatic Member
Re: Battle ship game in C,such a challenge thing :D
Originally Posted by coldzero
Hello guys,I'm new to C programming.
Originally Posted by Harsh Gupta
Just because someone is new to the forums, it doesnot always imply that the member is a noob.
I think that is good enough evidence to say that coldzero is new to C programming.
-
Nov 11th, 2006, 09:27 AM
#5
Addicted Member
Re: Battle ship game in C,such a challenge thing :D
Originally Posted by Harsh Gupta
Just because someone is new to the forums, it doesnot always imply that the member is a noob.
Certainly it doesn't mean that but if you would have cared to read OP's post with little more concentration then you wouldn't have wasted time to pass useless comments around.
Let me quote OP's post again for you
Originally Posted by coldzero
Hello guys,I'm new to C programming....
#Appreciate others by rating good posts !!
#The Software Peter Principle is in operation when unwise developers "improve" and "generalize" the software until they themselves can no longer understand it, then the project slowly dies.
#People who are still ignorant of their ignorance are dangerous.
-
Nov 11th, 2006, 12:52 PM
#6
Thread Starter
Junior Member
Re: Battle ship game in C,such a challenge thing :D
Originally Posted by x-ice
I think that is good enough evidence to say that coldzero is new to C programming.
Yeah of course I'm new to C programming so this game is a challenge thing for me (If not,I can do with out any question ).Acttually,I can code in C#.Net and I did many applications in C# and some in Java.But in my new semester at University requires me to master C language.It is really hard for you to turn back from OP programming to C .Unfortunately,it is my circumstance now.
Anyways,thanks for comments.
-
Nov 11th, 2006, 01:01 PM
#7
Thread Starter
Junior Member
Re: Battle ship game in C,such a challenge thing :D
Originally Posted by Harsh Gupta
@SunnyPalSingh,
Just because someone is new to the forums, it doesnot always imply that the member is a noob.
@ColdZero, Nobody would do your homework here. We can help you ALONG with your project, but you would have to show some effort.
What code do you have till now?? And most importantly, have you looked and searched, or questioned your teacher about the logic involved?
Coding is no problem, but the major factor is to understand the algorithm used to solve this game.
Thanks for your understanding.Infact,I'm doing some researchs and trying to find solutions for the game.I will show some in the next post.It is unacceptable if I only lean on you but do not do any thing,right ?
-
Nov 11th, 2006, 01:17 PM
#8
Re: Battle ship game in C,such a challenge thing :D
Do you want to have "graphical" output of the board or do you just want commands to place the ships and then commands to fire at fields with feedback whether anything was hit?
The first is considerably harder.
All the buzzt
CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Nov 11th, 2006, 01:20 PM
#9
Thread Starter
Junior Member
Re: Battle ship game in C,such a challenge thing :D
Originally Posted by CornedBee
Do you want to have "graphical" output of the board or do you just want commands to place the ships and then commands to fire at fields with feedback whether anything was hit?
The first is considerably harder.
The game just focuses on algorithm and no graphical interface required,every thing is in command line.Thanks
-
Nov 11th, 2006, 01:28 PM
#10
Re: Battle ship game in C,such a challenge thing :D
Not talking about a graphical interface. Just whether it should output a big rectangle of characters representing the ships and water etc.
Anyway. Basically you need 4 arrays (though if you want, you can use evil bit packing tricks and only actually use two or even one array). For the two players, there's one array each for the placement of the ships and one each for tracking the fields fired at.
Placing, firing and hitting within these arrays is trivial.
For actually finding out which ships are damaged and destroyed, you can either repeatedly scan these arrays, or you can track the health of ships separately. I.e. you hold two arrays, one for each player. The arrays hold one entry for each ship recording its remaining "health" (each health point represents one occupied field). Whenever a ship is hit, you take one hit point away. This way you can track which ships are completely destroyed more easily.
All the buzzt
CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Nov 11th, 2006, 02:57 PM
#11
Re: Battle ship game in C,such a challenge thing :D
I think OP has provided enough evidence for me to re-read his/her post.
And I never said don't give them some feasible advice, but atleast don't consider them fools.
@coldzero, Check this link, it may give you some vague idea about solving the game.
-
Nov 11th, 2006, 07:57 PM
#12
Thread Starter
Junior Member
Re: Battle ship game in C,such a challenge thing :D
Originally Posted by Harsh Gupta
Hi Harsh Gupta,thanks for the link.It's a good example and it's really close to the game.
-
Nov 11th, 2006, 08:25 PM
#13
Thread Starter
Junior Member
Re: Battle ship game in C,such a challenge thing :D
Originally Posted by CornedBee
Anyway. Basically you need 4 arrays (though if you want, you can use evil bit packing tricks and only actually use two or even one array). For the two players, there's one array each for the placement of the ships and one each for tracking the fields fired at.
You're right and understand the solution for the game.In the main function there are 4 functions called :
/* Stores player ship position information secret to opponent. */
char playerHidden[SIZE][SIZE];
/* Stores player ship position information known to opponent. */
char playerReveal[SIZE][SIZE];
/* Stores computer ship position information secret to opponent. */
char computHidden[SIZE][SIZE];
/* Stores computer ship position information known to opponent. */
char computReveal[SIZE][SIZE];
In addition , the game requires me to put shis on the follwing pattern :
/****************************************************************************
* Function placePlayerShips() adds the player ships to the grid in the
* following fixed pattern:
* 1 2 3 4 5 6 7 8 9 0
* +-+-+-+-+-+-+-+-+-+-+
* a |A| | | | | | | | | |
* +-+-+-+-+-+-+-+-+-+-+
* b |A| | | | | |D|D|D| |
* +-+-+-+-+-+-+-+-+-+-+
* c |A| | | | | | |S| | |
* +-+-+-+-+-+-+-+-+-+-+
* d |A| | | | | | |S| | |
* +-+-+-+-+-+-+-+-+-+-+
* e |A| | | | | | | | | |
* +-+-+-+-+-+-+-+-+-+-+
* f | | | | | | | | | | |
* +-+-+-+-+-+-+-+-+-+-+
* g | | | | | | | | | | |
* +-+-+-+-+-+-+-+-+-+-+
* h | | | | | | | | | | |
* +-+-+-+-+-+-+-+-+-+-+
* i | |F|F|F| | | | | | |
* +-+-+-+-+-+-+-+-+-+-+
* j | | | | | | |B|B|B|B|
* +-+-+-+-+-+-+-+-+-+-+
****************************************************************************/
void placePlayerShips(char playerHidden[SIZE][SIZE])
{
}
with A represents Aircraft Carrier (lenght = 5)
B Battleship (lenght = 4)
D Destroyer (lenght = 3)
F Frigate (lenght = 3)
S Submarine (lenght = 2)
and the computer ships will be added ramdomly with an function placeComputerShips(char computerHidden[SIZE][SIZE]) which is similar to placePlayerShips() function.
At the moment,drawing a grid with ships in it makes me confused.I have to use printf() function to print out every line to show the grid and put the ships whenever player specify the co-ordinate (ship's position).And for every action,I have to use a built in function named readRestOfLine() to hadle buffer.
Can you help me to draw a grid like this?Thanks alot.
-
Nov 11th, 2006, 09:17 PM
#14
Re: Battle ship game in C,such a challenge thing :D
Provided that each array is filled with spaces except where the ships are (not the way I'd choose to do it), you can output any of them like this:
Code:
int i;
for(i = 0; i < SIZE; ++i) {
printf("%.*s", SIZE, theArray[i]);
}
This also requires column-major storage, i.e. that the first index is the row, not the column.
All the buzzt
CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Nov 12th, 2006, 05:13 AM
#15
Frenzied Member
Re: Battle ship game in C,such a challenge thing :D
this could basically get as tedious as you want it to be
if you want a quick and dirty way.. you could do something like this..
Code:
#define WATER ' '
#define WATER_SHOT '-'
#define SHIP 'O'
#define SHIP_SHOT 'X'
char player1[SIZE][SIZE]
char player2[SIZE][SIZE]
fire (x, y)
{
if player[x][y] == WATER
player[x][y] = WATER_SHOT
else if player[x][y] == SHIP
player[x][y] = SHIP_SHOT
search the array for any elements that are SHIP, if none then that player lost
}
Last edited by dis1411; Nov 12th, 2006 at 05:19 AM.
-
Nov 12th, 2006, 11:58 AM
#16
Thread Starter
Junior Member
Re: Battle ship game in C,such a challenge thing :D
Originally Posted by CornedBee
Provided that each array is filled with spaces except where the ships are (not the way I'd choose to do it), you can output any of them like this:
Code:
int i;
for(i = 0; i < SIZE; ++i) {
printf("%.*s", SIZE, theArray[i]);
}
This also requires column-major storage, i.e. that the first index is the row, not the column.
Can you explane more clearly what will the code prints out?
I think we have to print every line to make up a grid (hard-coded)and then put the ship in the exact position ( when the player specify the co-ordinate ) by a logic algorimth.Is it right?
Last edited by coldzero; Nov 12th, 2006 at 12:02 PM.
-
Nov 12th, 2006, 12:02 PM
#17
Thread Starter
Junior Member
Re: Battle ship game in C,such a challenge thing :D
Originally Posted by dis1411
this could basically get as tedious as you want it to be
if you want a quick and dirty way.. you could do something like this..
Code:
#define WATER ' '
#define WATER_SHOT '-'
#define SHIP 'O'
#define SHIP_SHOT 'X'
char player1[SIZE][SIZE]
char player2[SIZE][SIZE]
fire (x, y)
{
if player[x][y] == WATER
player[x][y] = WATER_SHOT
else if player[x][y] == SHIP
player[x][y] = SHIP_SHOT
search the array for any elements that are SHIP, if none then that player lost
}
This is exactly the way to check whether the ships are sunk or not.But I'm stuck at the point how to draw a 10x10 grid and eveytime the player place a ship in,the grid then be re-drawn with the ship added in the right position.It's seem required a lot of hard-code.I'm so confused
-
Nov 12th, 2006, 12:13 PM
#18
Thread Starter
Junior Member
Re: Battle ship game in C,such a challenge thing :D
At this time I'm trying to draw a grid like the game requires.C seems a rocky language.I hope I can submit this assignment on time (17th November) , after that I will post the source code here for everybody who need a reference.
-
Nov 12th, 2006, 11:15 PM
#19
Frenzied Member
Re: Battle ship game in C,such a challenge thing :D
ver 1
Code:
#include <iostream.h>
#define WATER ' '
#define WATER_SHOT 'x'
#define SHIP 'O'
#define SHIP_SHOT 'Ø'
#define SIZE 10
#define TOP " 1 2 3 4 5 6 7 8 9 10"
#define LINE " -----------------------------------------\n"
char player1[SIZE][SIZE];
char player2[SIZE][SIZE];
int i,j;
void main()
{
for (i=0;i<SIZE;++i)
for (j=0;j<SIZE;++j)
player1[i][j] = player2[i][j] = WATER;
player1[1][2] = SHIP;
player1[1][3] = SHIP;
player1[6][8] = SHIP;
player1[7][8] = SHIP;
player1[8][8] = SHIP;
//display board
// player 1
cout << TOP << endl << LINE;
for (i=0;i<SIZE;++i)
{
cout << char(65+i) << " | ";
for (j=0;j<SIZE;++j)
cout << player1[i][j] << " | ";
cout << endl << LINE;
}
cout << "\n\n\n";
//player 2
cout << TOP << endl << LINE;
for (i=0;i<SIZE;++i)
{
cout << char(65+i) << " | ";
for (j=0;j<SIZE;++j)
cout << player2[i][j] << " | ";
cout << endl << LINE;
}
}
ver2
Code:
#include <iostream.h>
#define WATER ' '
#define WATER_SHOT 'x'
#define SHIP 'O'
#define SHIP_SHOT 'Ø'
#define SIZE 10
#define TOP " 1 2 3 4 5 6 7 8 9 10"
#define LINE " ---------------------"
char player1[SIZE][SIZE];
char player2[SIZE][SIZE];
int i,j;
void main()
{
for (i=0;i<SIZE;++i)
for (j=0;j<SIZE;++j)
player1[i][j] = player2[i][j] = WATER;
player1[1][2] = SHIP;
player1[1][3] = SHIP;
player1[6][8] = SHIP;
player1[7][8] = SHIP;
player1[8][8] = SHIP;
//display board
// player 1 and 2
cout << TOP << " \t\t" << TOP << endl << LINE << " \t\t" << LINE << endl;
for (i=0;i<SIZE;++i)
{
cout << char(65+i) << " |";
for (j=0;j<SIZE;++j)
cout << player1[i][j] << "|";
cout << "\t\t\t" << char(65+i) << " |";;
for (j=0;j<SIZE;++j)
cout << player2[i][j] << "|";
cout << endl << LINE << " \t\t" << LINE << endl;
}
}
-
Nov 13th, 2006, 04:21 AM
#20
Thread Starter
Junior Member
Re: Battle ship game in C,such a challenge thing :D
These code snipets look great.Now I have the idea to deal with the game.Tonight I will try to finish the grid drawing and ship placing sections.
Thanks alot dis1411
-
Nov 14th, 2006, 09:49 PM
#21
Thread Starter
Junior Member
Re: Battle ship game in C,such a challenge thing :D
Can I use "printf" instead of "cout"?Because the game requirement does not allow me to use iostream.h library ("stdio.h" and "stdlib.h" only)so I can not use the "<<" bit wise operator as well.
I've tried to replace "cout" with "printf" and dismiss "<<" (Infact,I dont understand what "<<" can do.).But the out put likes a mess,the number and the line order is not correct.Can you help ?Thanks.
-
Dec 5th, 2006, 09:35 PM
#22
New Member
Re: Battle ship game in C,such a challenge thing :D
yes, you can use printf() instead.
Code:
cout << "Hello, World."
is the same as saying
Code:
cout.operator<<("Hello, World.")
You can think of << as pushing the right hand side stuff (Hello, World.) onto the output stream.
thus, you can substitute all 'cout <<' with printf()
example:
cout << TOP; becomes printf(TOP);
-
Dec 6th, 2006, 05:01 AM
#23
Re: Battle ship game in C,such a challenge thing :D
Not quite. printf() does its format string and variable arguments thing. You cannot output structs with printf() directly.
All the buzzt
CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|