473,387 Members | 1,520 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,387 software developers and data experts.

array help in battleship game.

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[ ]);

int main( ) {
char board[391], prevmove[41];
int i, missles = 50, mleft = 0, score = 0;

strcpy(prevmove, ""); /* an empty string */

for(i=0; i<15*26; i++){
board[i] = '~';
}
board[i] = '\0';

draw_game(board, missles, mleft, score, prevmove);

draw_game(board, missles, mleft, score, prevmove);
return 0;
}


void draw_game(char board[ ], int maway, int mleft, int score, char prevmove[ ]) {
int i,input ,j;
char row_letters[16] = "123456789ABCDEF";

printf(" C Battleship...\n");
printf(" ABCDEFGHIJKLMNOPQRSTUVWXYZ\n");

for(i=0; i<15; i++) { /* rows */
printf("%c|", row_letters[i]);

for(j=0; j<26; j++) { /* columns */
printf("%c", board[j]);
}
printf("|\n");
}

printf(" Missiles Away:%.2d Missiles Left:%.2d\n", maway, mleft);
printf(" Current Score:%.3d Last Move: %s\n", score, prevmove);
printf(" Enter Target Coordinates--> ");
scanf("%d",&input);
board[input] ='X';
}

__________________________________________________ ________________

the output it should be like this:

C Battleship...
ABCDEFGHIJKLMNOPQRSTUVWXYZ
1|~~~~~~~~~~~~~~~~~~~~~~~~~~|
2|~~~~~~~~~~~~~~~~~~~~~~~~~~|
3|~~~~~~~~~~~~~~~~~~~~~~~~~~|
4|~~~~~~~~~~~~~~~~~~~~~~~~~~|
5|~~~~~~~~~~~~~~~~~~~~~~~~~~|
6|~~~~~~~~~~~~~~~~~~~~~~~~~~|
7|~~~~~~~~~~~~~~~~~~~~~~~~~~|
8|~~~~~~~~~~~~~~~~~~~~~~~~~~|
9|~~~~~~~~~~~~~~~~~~~~~~~~~~|
A|~~~~~~~~~~~~~~~~~~~~~~~~~~|
B|~~~~~~~~~~~~~~~~~~~~~~~~~~|
C|~~~~~~~~~~~~~~~~~~~~~~~~~~|
D|~~~~~~~~~~~~~~~~~~~~~~~~~~|
E|~~~~~~~~~~~~~~~~~~~~~~~~~~|
F|~~~~~~~~~~~~~~~~~~~~~~~~~~|
Missiles Away:00 Missiles Left:50
Current Score:000 Last Move:
Enter Target Coordinates--> 5T

C Battleship...
ABCDEFGHIJKLMNOPQRSTUVWXYZ
1|~~~~~~~~~~~~~~~~~~~~~~~~~~|
2|~~~~~~~~~~~~~~~~~~~~~~~~~~|
3|~~~~~~~~~~~~~~~~~~~~~~~~~~|
4|~~~~~~~~~~~~~~~~~~~~~~~~~~|
5|~~~~~~~~~~~~~~~~~~~X~~~~~~|
6|~~~~~~~~~~~~~~~~~~~~~~~~~~|
7|~~~~~~~~~~~~~~~~~~~~~~~~~~|
8|~~~~~~~~~~~~~~~~~~~~~~~~~~|
9|~~~~~~~~~~~~~~~~~~~~~~~~~~|
A|~~~~~~~~~~~~~~~~~~~~~~~~~~|
B|~~~~~~~~~~~~~~~~~~~~~~~~~~|
C|~~~~~~~~~~~~~~~~~~~~~~~~~~|
D|~~~~~~~~~~~~~~~~~~~~~~~~~~|
E|~~~~~~~~~~~~~~~~~~~~~~~~~~|
F|~~~~~~~~~~~~~~~~~~~~~~~~~~|
Missiles Away:01 Missiles Left:49
Current Score:000 Last Move: MISS on 5T
Enter Target Coordinates-->
__________________________________________________ ____

I have troubling with coordinates, i don't know how to do it. suppose if users enter 5T, the result should be X in row 5 and col T.

need help
thanks
Nov 25 '06 #1
5 5960
Banfa
9,065 Expert Mod 8TB
May I ask why you have implemented the board as a single dimentioned array. For this program a 2 dimensioned array would seem to be easier

board[15][26];
Nov 25 '06 #2
May I ask why you have implemented the board as a single dimentioned array. For this program a 2 dimensioned array would seem to be easier

board[15][26];

i know but i am troubling with the results plz help me out


thanks
Nov 25 '06 #3
Banfa
9,065 Expert Mod 8TB
Well the reason for my last question is that you have accessed the board array incorrectly in the nested loop

Expand|Select|Wrap|Line Numbers
  1.  for(i=0; i<15; i++) { /* rows */
  2.     printf("%c|", row_letters[i]);
  3.  
  4.     for(j=0; j<26; j++) { /* columns */
  5.         printf("%c", board[j]);
  6.     }
  7.  
  8.     printf("|\n");
  9. }
  10.  
printf("%c", board[j]);

should be

printf("%c", board[i*26+j]);
Nov 25 '06 #4
Well the reason for my last question is that you have accessed the board array incorrectly in the nested loop

Expand|Select|Wrap|Line Numbers
  1.  for(i=0; i<15; i++) { /* rows */
  2.     printf("%c|", row_letters[i]);
  3.  
  4.     for(j=0; j<26; j++) { /* columns */
  5.         printf("%c", board[j]);
  6.     }
  7.  
  8.     printf("|\n");
  9. }
  10.  
printf("%c", board[j]);

should be

printf("%c", board[i*26+j]);
hi,

it works but it only count col. suppose if u enter 4T. it will only count ```x. it doesn't count char.

plz help me out
thanks
Nov 25 '06 #5
Banfa
9,065 Expert Mod 8TB
That is because you have used

scanf("%d",&input);

You are requesting a number where as what you actually want as input is a string, a 1 char row id and a 1 char column id.

Use fgets to read in a line of data, then verify the first character is a valid row and the second character is a valid column and that the 3rd character is the termintor.
Nov 26 '06 #6

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...
3
by: gambler | last post by:
let's say you have: var games = new Array(); games = new GAME(gameNum, rotNum1, rotNum2, ... ); ( so a sparsley populate array which enables me to locate a game usin the game number...
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...
3
by: Bosconian | last post by:
I have an array defined as follows: $scores = 19; $scores = 25; $scores = 23; $scores = 25; .... where the key is the team # and the value is the points. I am outputting the key/values as...
1
by: QryS | last post by:
Hi there I have a class within which i assign 2 dimension dynamic array of pointers which points to variable inside these class (variable is of type other class). Now to get into that variable...
2
by: Icarus27 | last post by:
Hello, I am making a program that runs off of a two dimentional array but I have made it into a game called MouseBot. I am trying to make the game so that you can move the mouse until you get the...
7
by: ianenis.tiryaki | last post by:
well i got this assignment which i dont even have a clue what i am supposed to do. it is about reading me data from the file and load them into a parallel array here is the question: Step (1) ...
5
by: Stephen3776 | last post by:
I am doing an inventory control progam and trying to output a multiple array, I am getting an illegal conversion error java.lang.double !d. Can somebody tell me what I am doing wrong or if there is...
3
telgroc
by: telgroc | last post by:
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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,...
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,...

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.