class - Trouble with C++ Classes and Functions - Function to print out a 2D array -
i'm trying make battleship game in c++ , i've been having huge nightmare trying classes set way wanted them be. bellow i've included code more information how i'm trying program game.
there 3 classes , header files: game.cpp, ships.cpp , player.cpp corresponding header files game.h, ships.h , player.h. ships , player there store information regarding items want bulk of functions associated game class.
currently way can display rudimentary game board initializing in main() , using local variable. want put "print" function game.cpp.
i've had several goes @ why you'll see random bits of code on place. feel once down once i'll have more confidence , able replicate other classes , function.
the functions i've identified need game far follows: clear //clears game board show //shows game board countnumberofships //counts how many ships there ie when 0 ships remain lose or win depending on @ 0. setships // allows place ships , randomly places computer's ships. attack // controls attack sequences, responsible shoot,hit miss, sink.
main.cpp
#include "game.h" #include <iostream> #include <string> using namespace std; int board3[10][10] = { {1,2,3,4,5,6,7,8,9,10}, {1,2,3,4,5,6,7,8,9,10}, {1,2,3,4,5,6,7,8,9,10}, {1,2,3,4,5,6,7,8,9,10}, {1,2,3,4,5,6,7,8,9,10}, {1,2,3,4,5,6,7,8,9,10}, {1,2,3,4,5,6,7,8,9,10}, {1,2,3,4,5,6,7,8,9,10}, {1,2,3,4,5,6,7,8,9,10}, {1,2,3,4,5,6,7,8,9,10} }; string name; int option1; bool playerwin = false; string type; int main() { cout << "hello, welcome battleship" << endl; game mygame; //mygame.displayboard(); for(int i=0; < 10; i++) { for(int j=0; j < 10; j++) { cout << board3[i][j] << " "; } cout << endl; } /**game go; cout << "welcome, please enter name: "; cin >> name; cout <<"are player or computer?"; cin >> type; player newplayer(name, player); go.addplayer(newplayer); cout << "okay " << name << "! ready lose?" << endl; cout << "hello world" << endl; //game function goes here **/ system("pause"); return 0; };
game.h
#ifndef game_h #define game_h #include "player.h" #include "ships.h" #include <vector> class game { public: game(); ~game(); //void addplayer(player newplayer); void displayboard(); //void addship(ships newship); private: }; #endif
game.cpp
#include "game.h" #include <vector> #include <iostream> using namespace std; game::game() { cout << "hello mr rabbit!" << endl; } game::~game() { } void game::displayboard() { int gameboard[10][10]; for(int i=0; < 10; i++) { for(int j=0; j < 10; j++) { cout << gameboard[i][j] << " "; } cout << endl; } }
from brief glimpse on code, seems may want include gameboard array variable within game class. there create 2 separate 'game' objects, 1 player , other computer. have 2 separate gameboard arrays manipulate using . operator so: playergame.gameboard[i][j] or computergame.gameboard[i][j].
hope helps!
edit: since you've clarified question, , based on i've mentioned. rewrite displayboard function operate on gameboard array of game object. loop method using still work, long remove re-declaration of gameboard.
once again, hope helps!
Comments
Post a Comment