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:
thanks for any help
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:
Code:
#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