Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <time.h>
- #include <fstream>
- using namespace std;
- void printPremadeBoards()
- {
- char cMatBoard1[10][10], cMatBoard2[10][10], cMatBoard3[10][10];
- ifstream fBoard;
- fBoard.open("board1.txt");
- if (fBoard.is_open())
- {
- for (int iRowCounter = 0; iRowCounter < 10; iRowCounter++)
- {
- for (int iColCounter = 0; iColCounter < 10; iColCounter++)
- {
- fBoard >> cMatBoard1[iRowCounter][iColCounter];
- }
- }
- }
- fBoard.close();
- fBoard.open("board2.txt");
- if (fBoard.is_open())
- {
- for (int iRowCounter = 0; iRowCounter < 10; iRowCounter++)
- {
- for (int iColCounter = 0; iColCounter < 10; iColCounter++)
- {
- fBoard >> cMatBoard2[iRowCounter][iColCounter];
- }
- }
- }
- fBoard.close();
- fBoard.open("board3.txt");
- if (fBoard.is_open())
- {
- for (int iRowCounter = 0; iRowCounter < 10; iRowCounter++)
- {
- for (int iColCounter = 0; iColCounter < 10; iColCounter++)
- {
- fBoard >> cMatBoard3[iRowCounter][iColCounter];
- }
- }
- }
- fBoard.close();
- cout << "Board #1 Board #2 Board #3" << "\n";
- for (int iRowCounter = 0; iRowCounter < 10; iRowCounter++)
- {
- for (int iColCounter = 0; iColCounter < 10; iColCounter++)
- {
- cout << cMatBoard1[iColCounter][iRowCounter] << " ";
- }
- cout << " ";
- for (int iColCounter = 0; iColCounter < 10; iColCounter++)
- {
- cout << cMatBoard2[iColCounter][iRowCounter] << " ";
- }
- cout << " ";
- for (int iColCounter = 0; iColCounter < 10; iColCounter++)
- {
- cout << cMatBoard3[iColCounter][iRowCounter] << " ";
- }
- cout << "\n";
- }
- }
- void processInput()
- {
- char cInput = 0;
- while (true)
- {
- cout << "* To choose one of these boards, enter '1' for board #1, enter '2' for board #2, or enter '3' for board #3." << "\n";
- cout << "* To randomize a board, press 'r'." << "\n";
- cout << "* To create the board yourself, press 'c'." << "\n";
- cin >> cInput;
- switch (cInput)
- {
- case '1':
- goto success;
- break;
- case '2':
- goto success;
- break;
- case '3':
- goto success;
- break;
- case 'h':
- case 'H':
- goto success;
- break;
- case 'r':
- case 'R':
- goto success;
- break;
- case 'c':
- case 'C':
- goto success;
- break;
- default:
- cout << "Invalid answer!" << "\n";
- continue;
- }
- success:
- cout << "Yes";
- break;
- }
- }
- void printIntro()
- {
- cout << "Welcome to BATTLESHIP. If you don't know how to play, press 'h'. Otherwise, you must select your board." << "\n";
- cout << "You can select one of the pre-made ones, create a random one, or arrange the ships by yourself." << "\n";
- cout << "These are the pre-made boards:" << "\n";
- printPremadeBoards();
- processInput();
- }
- /*
- randomizeBoard
- Randomizes a board, providing random ship orientation, X position and Y position.
- Parameters: cMatBoard (the board to randomize)
- Returns: nothing.
- */
- void randomizeBoard(char cMatBoard[10][10])
- {
- int iCounter = 5;
- while (iCounter > 0)
- {
- bool bSuccess = true;
- int iCurrentShip = iCounter;
- // since there are two 3-blocks ships, when iCounter is 1 we will create another one
- if (iCounter == 1)
- {
- iCurrentShip = 3;
- }
- // randomize orientation, x position and y position
- int iOrientation = rand() % 2;
- int iRandRow = rand() % 10;
- int iRandCol = rand() % 10;
- if (iOrientation == 0) // 0 = horizontal
- {
- // check if the ship won't go out of bounds
- if (iRandCol + iCurrentShip < 10)
- {
- // make sure that the ship won't be over any other existing ships
- for (int iCol = iRandCol; iCol < iRandCol + iCurrentShip; iCol++)
- {
- if (cMatBoard[iRandRow][iCol] != '0')
- {
- bSuccess = false;
- break;
- }
- }
- // if we make it to here, then we can proceed to create the ship
- if (bSuccess)
- {
- for (int iCol = iRandCol; iCol < iRandCol + iCurrentShip; iCol++)
- {
- cMatBoard[iRandRow][iCol] = '0' + iCurrentShip;
- }
- }
- }
- else
- {
- continue;
- }
- }
- else // 1 = vertical
- {
- // check if the ship won't go out of bounds
- if (iRandRow + iCurrentShip < 10)
- {
- // make sure that the ship won't be over any other existing ships
- for (int iRow = iRandRow; iRow < iRandRow + iCurrentShip; iRow++)
- {
- if (cMatBoard[iRow][iRandCol] != '0')
- {
- bSuccess = false;
- break;
- }
- }
- // if we make it to here, then we can proceed to create the ship
- if (bSuccess)
- {
- for (int iRow = iRandRow; iRow < iRandRow + iCurrentShip; iRow++)
- {
- cMatBoard[iRow][iRandCol] = '0' + iCurrentShip;
- }
- }
- }
- else
- {
- continue;
- }
- }
- // if we didn't fail on the creation, proceed onto the next ship
- if (bSuccess)
- {
- iCounter--;
- }
- }
- }
- /*
- createBoard
- Fills a matrix with 0, which is an empty battleship board.
- Parameters: cMatBoard (the matrix to fill)
- Returns: nothing.
- */
- void createBoard(char cMatBoard[10][10])
- {
- for (int iRow = 0; iRow < 10; iRow++)
- {
- for (int iCol = 0; iCol < 10; iCol++)
- {
- cMatBoard[iRow][iCol] = '0';
- }
- }
- }
- void printBoard(char cMatBoard[10][10])
- {
- for (int iRow = 0; iRow < 10; iRow++)
- {
- for (int iCol = 0; iCol < 10; iCol++)
- {
- cout << cMatBoard[iRow][iCol] << " ";
- }
- cout << "\n";
- }
- }
- int main()
- {
- char cMatBoard[10][10];
- srand(time(NULL));
- printIntro();
- while (true)
- {
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement