Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
12/2
#include <iostream> #include <ctime> #include <vector> #include <string> using namespace std const int eRows = 5; const int eColumns = 5; const int mRows = 5; const int mColumns = 5; const int hRows = 10; const int hColumns = 10; int eShipsTotal = 4; int mShipsTotal = 3; int hShipsTotal = 5; int eMatrix[eRows][eColumns]; int mMatrix[mRows][mColumns]; int hMatrix[hRows][hColumns]; //Easy- line26 //Medium- line112 //Hard- line119 //Main- Line286 //EASY CODE void eClear() { for(int i = 0; i < eRows; i++){ for(int j = 0; j < eColumns; j++){ eMatrix[i][j] = 0; } } } void eShow() { for(int i=0; i < eRows; i++) { for(int j=0; j < eColumns; j++) { cout << eMatrix[i][j] << " "; } cout << endl; } } int eNumber() { int c = 0; for(int i=0; i < eRows; i++) { for(int j=0; j < eColumns; j++) { if(eMatrix[i][j] == 1) c++; } } return c; } void eShips() { int eS = 0; while(eS < eShipsTotal) { int x = rand() % eRows; int y = rand() % eColumns; if(eMatrix[x][y] != 1) { eS++; eMatrix[x][y] = 1; } } } bool eAttack(int x,int y) { if(eMatrix[x][y] == 1) { eMatrix[x][y] = 2; return true; } return false; } int eMain() { srand(time(NULL)); eClear(); eShips(); int pos1,pos2; char prompt; while(1) { cout << "Please input target location: "; cin >> pos1 >> pos2; if(eAttack(pos1,pos2)) cout << "It's a Hit! Shoot Again!" << endl; else cout << "You Missed! Try Again!" << endl; cout << "Number of ships left: " << eNumber() << endl << endl; cout << "Do you want to surrender (y/n)? "; cin >> prompt; if(prompt == 'y') break; } cout << "Game over!" << endl; system("pause"); } //MEDIUM CODE void mClear() { for(int i = 0; i < mRows; i++){ for(int j = 0; j < mColumns; j++){ mMatrix[i][j] = 0; } } } void mShow() { for(int i=0; i < mRows; i++) { for(int j=0; j < mColumns; j++) { cout << mMatrix[i][j] << " "; } cout << endl; } } int mNumber() { int c = 0; for(int i=0; i < mRows; i++) { for(int j=0; j < mColumns; j++) { if(mMatrix[i][j] == 1) c++; } } return c; } void mShips() { int mS = 0; while(mS < mShipsTotal) { int x = rand() % mRows; int y = rand() % mColumns; if(mMatrix[x][y] != 1) { mS++; mMatrix[x][y] = 1; } } } bool mAttack(int x,int y) { if(mMatrix[x][y] == 1) { mMatrix[x][y] = 2; return true; } return false; } int mMain() { srand(time(NULL)); mClear(); mShips(); int pos1,pos2; char prompt; while(1) { cout << "Please input target location: "; cin >> pos1 >> pos2; if(mAttack(pos1,pos2)) cout << "It's a Hit! Shoot Again!" << endl; else cout << "You Missed! Try Again!" << endl; cout << "Number of ships left: " << mNumber() << endl << endl; cout << "Do you want to surrender (y/n)? "; cin >> prompt; if(prompt == 'y') break; } cout << "Game over!" << endl; system("pause"); } //HARD CODE void hClear() { for(int i = 0; i < hRows; i++){ for(int j = 0; j < hColumns; j++){ hMatrix[i][j] = 0; } } } void hShow() { for(int i=0; i < hRows; i++) { for(int j=0; j < hColumns; j++) { cout << hMatrix[i][j] << " "; } cout << endl; } } int hNumber() { int c = 0; for(int i=0; i < hRows; i++) { for(int j=0; j < hColumns; j++) { if(hMatrix[i][j] == 1) c++; } } return c; } void hShips() { int hS = 0; while(hS < hShipsTotal) { int x = rand() % hRows; int y = rand() % hColumns; if(hMatrix[x][y] != 1) { hS++; hMatrix[x][y] = 1; } } } bool hAttack(int x,int y) { if(hMatrix[x][y] == 1) { hMatrix[x][y] = 2; return true; } return false; } int hMain() { srand(time(NULL)); hClear(); hShips(); int pos1,pos2; char prompt; while(1) { cout << "Please input target location: "; cin >> pos1 >> pos2; if(hAttack(pos1,pos2)) cout << "It's a Hit! Shoot Again!" << endl; else cout << "You Missed! Try Again!" << endl; cout << "Number of ships left: " << hNumber() << endl << endl; cout << "Do you want to surrender (y/n)? "; cin >> prompt; if(prompt == 'y') break; } cout << "Game over!" << endl; system("pause"); } //MAIN CODE int main () { cout << "Hello what is your name?" << "\n"; string userName; cin >> userName; cout << userName << ", choose your difficulty:" << "\n"; cout << "Easy, Medium, or Hard" << endl; string gDifficulty; cin >> gDifficulty; if ((gDifficulty == "Easy")||(gDifficulty == "easy")) { cout << endl; cout << endl; cout << endl; cout << endl; cout << endl; cout << "--------------------" << endl; eMain(); } if ((gDifficulty == "Medium")||(gDifficulty == "medium")) { cout << endl; cout << endl; cout << endl; cout << endl; cout << endl; cout << "--------------------" << endl; mMain(); } if ((gDifficulty == "Hard")||(gDifficulty == "hard")) { cout << endl; cout << endl; cout << endl; cout << endl; cout << endl; cout << "--------------------" << endl; hMain(); } return 0; }
run
|
edit
|
history
|
help
0
__FUNCTION__ not a preprocessor macro on clang
template specialization inheritance problem
C++ Standard Template Library
Fun with Pointers #2
ljblblljkl
Pure virtual function called!
1337
unordered graphs search
Program to Time Difference
U2