Schach - Pointer zum String
-
Bisher funktioniert mein Programm recht gut,
funktionen:
Schachfeld mit Figuren anzeigen
Problem:
nächste Funktion die erstellt werden soll = Bewegung des Bauern;
ansich hat das schon funktioniert, nur da ich die schleife die dafür zuständig ist in eine außenstehende Funktion einpacken wollte, kam ich auf das Problem.
Das Programm läuft bis auf eine Stelle:einen Pointer des std::string xy[][] in die Funktion zu übertragen
andere Probleme und Unvollständigkeiten sollen nicht beachtet werden.
Danke// // main.cpp // Schach // // Created by Benno Dörr on 09.04.14. // Copyright (c) 2014 Benno Dörr. All rights reserved. // #include <iostream> #include <string> void tout(std::string xy[9][9]); void action(int *yfig, int *xfig, int *turnfig, int xheap, int yheap, std::string *xy[9][9]); int main() { std::string xy[9][9]; std::string figwahl; for (int i = 8; i>0; --i) { for (int y = 0; y<8; ++y) { xy[i][y] = "_ "; } } // ============================================================================= // ============================================================================= // Figure Creation // ============================================================================= // ============================================================================= struct fig { int x = 0, y = 0, turn = 0; }; fig wBauer1, wBauer2, wBauer3, wBauer4, wBauer5, wBauer6, wBauer7, wBauer8; wBauer1.x = 1; wBauer1.y = 2; xy[wBauer1.y][wBauer1.x-1] = "wB1 "; wBauer2.x = 2; wBauer2.y = 2; xy[wBauer2.y][wBauer2.x-1] = "wB2 "; wBauer3.x = 3; wBauer3.y = 2; xy[wBauer3.y][wBauer3.x-1] = "wB3 "; wBauer4.x = 4; wBauer4.y = 2; xy[wBauer4.y][wBauer4.x-1] = "wB4 "; wBauer5.x = 5; wBauer5.y = 2; xy[wBauer5.y][wBauer5.x-1] = "wB5 "; wBauer6.x = 6; wBauer6.y = 2; xy[wBauer6.y][wBauer6.x-1] = "wB6 "; wBauer7.x = 7; wBauer7.y = 2; xy[wBauer7.y][wBauer7.x-1] = "wB7 "; wBauer8.x = 8; wBauer8.y = 2; xy[wBauer8.y][wBauer8.x-1] = "wB8 "; fig wDame, wKing, wlTurm, wrTurm, wlSpring, wrSpring, wlLauf, wrLauf, heap; wDame.x = 4; wDame.y = 1; xy[wDame.y][wDame.x-1] = "wDa "; wKing.x = 5; wKing.y = 1; xy[wKing.y][wKing.x-1] = "wKi "; wlTurm.x = 1; wlTurm.y = 1; xy[wlTurm.y][wlTurm.x-1] = "wlT "; wrTurm.x = 8; wrTurm.y = 1; xy[wrTurm.y][wrTurm.x-1] = "wrT "; wlSpring.x = 2; wlSpring.y = 1; xy[wlSpring.y][wlSpring.x-1] = "wlS "; wrSpring.x = 7; wrSpring.y = 1; xy[wrSpring.y][wrSpring.x-1] = "wrS "; wlLauf.x = 3; wlLauf.y = 1; xy[wlLauf.y][wlLauf.x-1] = "wlL "; wrLauf.x = 6; wrLauf.y = 1; xy[wrLauf.y][wrLauf.x-1] = "wrL "; // ============================================================================= // ============================================================================= tout(xy); std::cout << "Welche Figur soll bewegt werden? "; std::cin >> figwahl; if (figwahl == "wb1"||"wB1") { action(&wBauer1.y, &wBauer1.x, &wBauer1.turn, heap.x, heap.y, &xy); } tout(xy); } void tout(std::string xy[9][9]) { std::cout << " 1 2 3 4 5 6 7 8\n"; for (int i = 8; i>0; --i) { for (int y = 0; y<8; ++y) { if (y == 0) { switch (i) { case 1: std::cout << "A "; break; case 2: std::cout << "B "; break; case 3: std::cout << "C "; break; case 4: std::cout << "D "; break; case 5: std::cout << "E "; break; case 6: std::cout << "F "; break; case 7: std::cout << "G "; break; case 8: std::cout << "H "; break; default: break; } } std::cout << xy[i][y]; } std::cout << std::endl; } std::cout << std::endl; } void action(int *yfig, int *xfig, int *turnfig, int xheap, int yheap, std::string *xy[9][9]) { int mov1 = 0, mov2 = 0, mov3 = 0; int act = 0; std::cout << "Bewegen oder Schlagen? "; std::cin >> act; switch (act) { case 1: switch (*turnfig) { case 0: std::cout << "1 oder 2 Felder bewegen? "; std::cin >> mov1; switch (mov1) { case 1: xheap = *xfig; yheap = *yfig; *yfig = *yfig+1; *xy[yheap][xheap-1] = "_ "; *xy[*yfig][*xfig-1] = "wB1 "; break; default: xheap = *xfig; yheap = *yfig; *yfig = *yfig+2; *xy[yheap][xheap-1] = "_ "; *xy[*yfig][*xfig-1] = "wB1 "; break; } break; case !0: break; } break; case !1: break; } }
-
mach aus std::string[][] einen std::vector<std::vectorstd::string>. Den kannst du dann als Referenz übergeben.
Da die Größe bei dir konstant ist, würde sich auch ein std::array anstelle von std::vector anbieten.
-
Zeile 69:
void tout(std::string (&xy)[9][9])Zeile 113 äquivalent.
Mit std::array geht das aber trotzdem leichter und du bekommst kostenlose Zusatzfunktionen dazu.
-
danke schonmal für die schnellen Antworten,
(die ich sogar verstehe ;))
werde es sobald ich zeit habe direkt mal ausprobieren
und werde wieder schreiben falls ich doch noch Probleme habe sollte.Danke