PDA

View Full Version : c++ battleship help




lorductape
Mar 30, 2007, 12:57 PM
so, i'm writing battleship in c++.
write now, i'm only writing the GUI part of the program, trying to make it look cool, because that's what so many console apps lack. right now, I'm using a random number generator to determine whether each grid space is a hit or a miss or has not been occupied yet. this will later be filled with user entered data, but for now, that's the way I did it. The problem is I don't think it's drawing it right. I know it's being filled properly, (i'm using two 140 long int arrays) but it's not being called up right. it might make more sense when you see the code. (the problem has to be in function void draw_moves. draw_game simply draws the grids and all that jazz.)

here it is:
#include <cstdlib>
#include <iostream>
#include <windows.h>
#include <stdlib.h>
#include <conio.h>
#include <fstream>

using namespace std;

/*
colors-
15- while
14-yellow
13-purple
12-red
11-turquoise
10-green
9-blue
8-grey
*/
bool setlocation(int x, int y){
static HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);
COORD c;
c.X = x;
c.Y = y;
SetConsoleCursorPosition(console, c);
}
bool setcolor(int c){
static HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(console, c);
}
void delay(double seconds){
int sec = (int)(seconds*10);
Sleep(sec*100);
}


void draw_game(){
setcolor(14);
cout<<"\t\t __ \n\t\t / _)__/_//_ _ / ' \n\t\t /(_)(// /((-_) /)//) \n\t\t / ";
setcolor(3);
for (int q = 0; q <= 36;){
for(int i = 0; i <= 30; i++){
if (i%2 != 0){
setlocation (q+i,4);
cout<<(char)205;
}else{
setlocation (q+i,4);
cout<<(char)209;
}
}
setlocation(q,4);
cout<<(char)201;
setlocation(q+30,4);
cout<<(char)187;
int j=0;
for(int i = 0; i <= 30; i++){
if (i%2 == 0){
setlocation (i+q,5);
cout<<(char)179;
}else{
if (j>0){
setlocation (i+q,5);
cout<<(char)(j+64);
}else{
setlocation(i+q,5);
cout<<(char)15;
}
j++;
}
}
setlocation(q,5);
cout<<(char)186;
setlocation(q+30,5);
cout<<(char)186;
for (int y = 1;y <= 20; y++){
if(y%2!=0){
for(int i = 0; i <= 30; i++){
if (i%2 != 0){
setlocation (i+q,5+y);
cout<<(char)196;
}else{
setlocation (i+q,5+y);
cout<<(char)197;
}
}
setlocation(q,5+y);
cout<<(char)199;
setlocation(q+30,5+y);
cout<<(char)182;
}else{
for(int i = 0; i <= 30; i++){
if (i%2 == 0){
setlocation (i+q,5+y);
cout<<(char)179;
}
}
setlocation(q,5+y);
cout<<(char)186;
setlocation(q+30,5+y);
cout<<(char)186;
}
}
int space = 7;
for (int i = 0; i != 10; i++){
setlocation(q+1,space);
cout<<i;
space = space+2;
}
for(int i = 0; i <= 30; i++){
if (i%2 != 0){
setlocation (q+i,26);
cout<<(char)205;
}else{
setlocation (q+i,26);
cout<<(char)207;
}
}
setlocation(q,26);
cout<<(char)200;
setlocation(q+30,26);
cout<<(char)188;
setlocation(q+8,26);
cout<<(char)216;
setlocation(q+22,26);
cout<<(char)216;
setlocation(q+8,27);
cout<<(char)179;
setlocation(q+22,27);
cout<<(char)179;
setlocation(q+8,28);
cout<<(char)192;
setlocation(q+22,28);
cout<<(char)217;
for (int i = 1; i <= 13; i++){
setlocation(i+q+8,28);
cout<<(char)196;
}
q+=35;
}
setlocation(9,27);
cout<<" your moves";
setlocation(44,27);
cout<<" opponents";
}


void draw_moves(int opponent[],int player[]){
int y2 = 0;
int i2 = 0;
for (int y = 1;y <= 20; y++){

if(y%2==0){
for(int i = 0; i <= 28; i++){
if (i%2 != 0){
int value = ((y2*28)+i2);
setlocation(i+2,5+y);
if (player[value] == 1){
setcolor(15);
cout<<"X";
}else if(player[value] == 2){
setcolor(12);
cout<<"X";
}
i2++;
}
}
}
y2++;
}
y2 = 0;
i2 = 0;
for (int y = 1;y <= 20; y++){
if(y%2==0){
for(int i = 0; i <= 28; i++){
if (i%2 != 0){
int value = ((y2*14)+i2);
setlocation(i+37,5+y);
if (opponent[value] == 1){
setcolor(15);
cout<<"X";
}else if(opponent[value] == 2){
setcolor(12);
cout<<"X";
}
i2++;
}
}
}
y2++;
}
}
/*
void get_ship_locations(int &ships[]){
setlocation(0,30);
cout<<"Enter the starting coordinates for your aircraft carrier.";
cout<<"\nRow:";
int row;
int column;
cin>>row;
ships[0] =
cout<<"\nColumn:";
cin>>column;


}

*/

int main()
{
srand(time(NULL));
int opponent[140];
for (int i = 0; i <= 140; i++){
opponent[i] = rand()%3;
}
int player[140];
for (int i = 0; i <= 140; i++){
player[i] = rand()%3;
}
draw_game();
draw_moves(opponent,player);
setlocation(0,29);
system("PAUSE");
return EXIT_SUCCESS;
}


thanks for any help



toddburch
Mar 30, 2007, 03:05 PM
Hummm... Didn't seem to compile too well on my Mac. Some weird message about not finding "windows.h" & :conio.h" or something... Could this be my choice of XCode project, or my choice of platform? :p

iSee
Mar 30, 2007, 07:03 PM
I did see one problem, on line ~157:

int value = ((y2*28)+i2);

I think y2 should be multiplied by 14, not 28, if I understand the code correctly.

(Edit) Oh, yeah, I forgot: You've got to post this when you're done :)

exabytes18
Mar 30, 2007, 11:05 PM
http://www.laazy.com/pictures/battleship.png

It broke. :confused:

macman2790
Mar 30, 2007, 11:08 PM
I did see one problem, on line ~157:

int value = ((y2*28)+i2);

I think y2 should be multiplied by 14, not 28, if I understand the code correctly.

(Edit) Oh, yeah, I forgot: You've got to post this when you're done :)

i second that.

lorductape
Mar 31, 2007, 12:05 PM
I did see one problem, on line ~157:

int value = ((y2*28)+i2);

I think y2 should be multiplied by 14, not 28, if I understand the code correctly.

(Edit) Oh, yeah, I forgot: You've got to post this when you're done :)

yeah, that might help, thanks!
i'll update if it works when I go back to school, spring break, and it's on my school login (don't worry, it's not homework, just a thing to do in my spare time.)

toddburch
Mar 31, 2007, 04:06 PM
I moved this over to my windows machine. I got it to compile in Visual Studio Express C++ 6.0 after making a few changes. However, when I run it, I get a command window and a blinking cursor in the top left corner and nothing else. Anyone have an idea what I'm doing wrong? I choose "Windows command line application", created an empty project, and added this code.

Thanks, Todd