Stilllegung des Forums
Das Forum wurde am 05.06.2023 nach über 20 Jahren stillgelegt (weitere Informationen und ein kleiner Rückblick).
Registrierungen, Anmeldungen und Postings sind nicht mehr möglich. Öffentliche Inhalte sind weiterhin zugänglich.
Das Team von spieleprogrammierer.de bedankt sich bei der Community für die vielen schönen Jahre.
Wenn du eine deutschsprachige Spieleentwickler-Community suchst, schau doch mal im Discord und auf ZFX vorbei!
Werbeanzeige
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 |
#include <iostream> #include <Windows.h> #pragma comment (lib, "winmm.lib") using namespace std; struct s_Spielfeld { bool bBesetzt; bool bBeschossen; }; const int Breite = 12; const int Hoehe = 12; s_Spielfeld Feld[Breite][Hoehe]; void Erstellen(); void Besetzen(); void Anzeigen(); int main() { srand (timeGetTime() ); int Auswahl = 0; do { cout << "Menue:" << endl; cout << "-----------------------" << endl; cout << "1. Erstellen" << endl; cout << "2. Besetzen" << endl; cout << "3. Anzeigen" << endl; cout << "4. Beenden" << endl; cout << "-----------------------" << endl << endl; cin >> Auswahl; switch(Auswahl) { case(1): { Erstellen(); }break; case(2): { Besetzen(); }break; case(3): { Anzeigen(); }break; case(4): { }break; } }while(Auswahl!=4); return 0; } /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// void Erstellen() { int Random; int Random1; int VertHori; //Zufallsgenerator VertHori = (rand()%2); //Schiff horizontal oder vertikal? if(VertHori==0) //Schiff wird VERTIKAL gestellt { do { Random = (rand()%12); Random1 = (rand()%12); }while (Random1 < 3); Feld[Random][Random1].bBesetzt=1; //////////////// for(int i=Random1-3; i<Random1; i++) { Feld[Random][i].bBesetzt=1; } //////////////// } else //Schiff wird HORIZONTAL gestellt { do { Random = (rand()%12); Random1 = (rand()%12); }while (Random < 3); Feld[Random][Random1].bBesetzt=1; for(int j=Random-3; j<Random;j++) { Feld[j][Random1].bBesetzt=1; } } } /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// void Besetzen() { int x, y; do { cout << "X-Koordinate:"; cin >> x; cout << "Y-Koordinate"; cin >> y; if (Feld[x-1][y-1].bBesetzt==1) cout << "Getroffen!!!!!" << endl << endl; else cout << "Daneben!!!!!" << endl << endl; Feld[x-1][y-1].bBeschossen=1; }while(x<1 || x>13); } /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// void Anzeigen() //DEBUG-ANZEIGE { for(int i=0; i<Hoehe; i++) { for (int j=0; j<Breite; j++) { if(Feld[j][i].bBesetzt==1) cout << "X"; else cout << "."; } cout << endl; } } |
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
void Erstellen(){ int Random; int Random1; int VertHori; //Zufallsgenerator VertHori = (rand()%2); //Schiff horizontal oder vertikal? if(VertHori==0) //Schiff wird VERTIKAL gestellt { do { Random = (rand()%12); Random1 = (rand()%12); }while (Random1 < 3); Feld[Random][Random1].bBesetzt=1; //////////////// for(int i=Random1-3; i<Random1; i++) { Feld[Random][i].bBesetzt=1; } //////////////// } else //Schiff wird HORIZONTAL gestellt { do { Random = (rand()%12); Random1 = (rand()%12); }while (Random < 3); Feld[Random][Random1].bBesetzt=1; for(int j=Random-3; j<Random;j++) { Feld[j][Random1].bBesetzt=1; } |
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 8 |
for (int i = Random1 - 3; i<Random1; i++) { if (Feld[Random][i].bBesetzt == 1) { //feld ist besetzt => setze das schiff woanders break; } Feld[Random][i].bBesetzt = 1; } |
Community-Fossil
Beruf: Teamleiter Mobile Applikationen & Senior Software Engineer
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
struct ShipPosition { int x = 0; int y = 0; bool vertical = true; }; ShipPosition getRandomValidPosition(int shipSize) { // befülle zuerst den vector mit valid Positions std::vector<ShipPosition> posVec; for (int x = 0; x < width; ++x) { for (int y = 0; y < height; ++y) { ShipPosition pos(x, y, true); if (validPosition(pos)) posVec.push_back(pos); else { pos.vertical = false; if (validPosition(pos)) posVec.push_back(pos); } } } // wenn keine valide Position vorhanden ist, schmeiße eine exception if (posVec.empty()) throw std::runtime_error("leerer vector"); return posVec.at(randomPosition); } bool validPosition(ShipPosition pos, int shipSize) { if (pos.vertical) { for (int x = 0; pos.x + x < width && x < shipSize; ++x) { if (Feld[pos.y][pos.x + x].bBesetzt==1) return false; } } else { for (int y = 0; pos.y + y < height && y < shipSize; ++y) { if (Feld[pos.y + y][pos.x].bBesetzt==1) return false; } } return true; } |
Es ist ja keine Schande etwas falsch zu machen, als Programmierer tu ich das täglich, [...].
Dieser Beitrag wurde bereits 3 mal editiert, zuletzt von »Sacaldur« (20.03.2015, 17:47)
Werbeanzeige