473,699 Members | 2,752 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

array help in battleship game.

5 New Member
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] = "123456789ABCDE F";

printf(" C Battleship...\n ");
printf(" ABCDEFGHIJKLMNO PQRSTUVWXYZ\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",&inp ut);
board[input] ='X';
}

_______________ _______________ _______________ _______________ ______

the output it should be like this:

C Battleship...
ABCDEFGHIJKLMNO PQRSTUVWXYZ
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...
ABCDEFGHIJKLMNO PQRSTUVWXYZ
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 5993
Banfa
9,065 Recognized Expert Moderator Expert
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
muna123
5 New Member
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 Recognized Expert Moderator Expert
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
muna123
5 New Member
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 Recognized Expert Moderator Expert
That is because you have used

scanf("%d",&inp ut);

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
8089
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
3
3990
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 without having to implement a "search" function on th games array
0
2174
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...
3
2061
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 follows:
1
3865
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 I have to use ptr-> but my ptrtoobj is pointer to pointer and left hand side of -> has to be pointer to class/structure when I use ptrtoobj->class_variable I'm getting an error of type 'cell' does not have an overloaded member 'operator ->' see...
2
2417
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 cheese. It has four different difficulty levels and you have obsticles in your way (the amount depends on the difficulty level). The problem I am having is that when I move the 'M' (mousebot) into the array space that the cheese ('C') is in it...
7
11782
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) Your first task is to write a program which reads this file into two parallel arrays in memory. One array contains the titles, and the other array contains the authors. The arrays are 'parallel' in the sense that the n-th element of the authors...
5
2502
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 another way? /* * Main.java * * Created on April 29, 2007, 6:57 PM * * To change this template, choose Tools | Template Manager * and open the template in the editor. */
3
3740
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 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...
0
8617
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
9174
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...
1
8914
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,...
1
6534
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
5875
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
4376
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4629
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3057
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
2347
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.