내 C++ 클래스에서 게임을 만들도록 요청 받았기 때문에 잠시 전에 C++ 클래스를 가져 와서 약간 녹슬 었습니다. 배열과 목록을 사용할 수 있다는 것을 기억합니까?목록에서 선택된 항목을 표시하지 않습니까?
게임의 포인트는 군대를 선택하고, 군대 유형을 선택한 다음 누군가를 공격하는 것입니다. 공격 부분에 도달하면 이전 진영에서 선택한 진영을 확인하는 if 문을 여러 개 만드는 대신 프로그램의 시작 부분에서 진영을 배열로 구현할 수 있고 공격 단계에 도달하면 배열을 통해 사용자가 선택한 대륙을 배열의 대륙과 비교하고 일치하는 출력을 보류합니까?
누군가가 약간의 코드 나 링크를 웹 사이트에 올려 놓으면 큰 도움이 될만한 사례를 보여줄 수 있습니다. 직접 해보고 싶습니다. 완전히 타이핑 한 것이 아닙니다.
#include <iostream>
using namespace std;
int main() {
// Declare variables
int menuInput = 0, continentInput = 0, armyInput = 0, actionInput = 0;
// Intro
cout << "WELCOME TO WAR" << endl;
// Display main menu
do {
cout << "1) Rules" << endl;
cout << "2) Play Game" << endl;
cout << "3) Quit" << endl;
cout << "Menu choice: ";
cin >> menuInput;
// if rules is selected
if (menuInput == 1) {
cout << endl;
cout << "RULES: " << endl;
cout << "1) Choose your player." << endl;
cout << "2) Choose your army type." << endl;
cout << "3) Choose to attack." << endl;
cout << endl;
}
// if game is selected
else if (menuInput == 2) {
cout << endl;
cout << "START" << endl;
// first do while loop, continent choice
do {
cout << "1) North America" << endl;
cout << "2) South America" << endl;
cout << "3) Europe" << endl;
cout << "4) Africa" << endl;
cout << "5) Asia" << endl;
cout << "6) Australia" << endl;
cout << "7) Antartica" << endl;
cout << "Choose your player from the list: ";
cin >> continentInput;
cout << endl;
// invalid display if selection not in range
if (continentInput <= 0 ||continentInput > 7) {
cout << "INVALID" << endl;
cout << endl;
}
} while (continentInput <= 0 || continentInput > 8);
// second do while loop, army type choice
do {
cout << "1) Army (Ground type forces)" << endl;
cout << "2) Navy (Sea type forces)" << endl;
cout << "3) Air Force (Air type forces)" << endl;
cout << "Choose your army type from the list: ";
cin >> armyInput;
cout << endl;
if (armyInput <= 0 || armyInput > 3) {
cout << "INVALID" << endl;
}
} while (armyInput <= 0 || armyInput > 3);
// third do while loop, who to attack
}
else if (menuInput == 3) {
cout << endl;
cout << "GAME OVER" << endl;
}
else {
// display invlaid input if number choice is not in given range
cout << "INAVLID INPUT" << endl;
}
} while (menuInput != 3);
return 0;
}
이러한 문제를 해결하는 올바른 도구는 디버거입니다. 스택 오버플로를 묻기 전에 코드를 단계별로 실행해야합니다. 자세한 도움말은 [작은 프로그램 디버깅 방법 (Eric Lippert 작성)] (https://ericlippert.com/2014/03/05/how-to-debug-small-programs/)을 참조하십시오. 문제를 재현하는 [최소, 완료 및 확인 가능] (http://stackoverflow.com/help/mcve) 예제와 함께 해당 질문을 \ [편집]해야합니다. 디버거. –
수업을 아직 배웠습니까? 플레이어와 군대 유형 멤버가 있고, 설정하고, 가져오고, 실행하는 클래스 개체가 있다면 더 좋을 것입니다. https://www.tutorialspoint.com/cplusplus/cpp_classes_objects.htm – DigitalNinja
@DigitalNinja 클래스가 아직 그 시점에 도달하지 못했다면, 첫 번째 "버전"은 더 기본적인 방법으로 코딩되어야한다고 생각하지만, 목표 지향적 인 계획을 세웠다. –