Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <cmath>
- #include <cstdio>
- #include <cstdlib>
- #include <ctime>
- using namespace std;
- const int rows = 5, cols = 5;
- int numOfGames, gamesPlayed, shotsTaken;
- short unsigned int boat1X, boat1Y, boat2X[2], boat2Y[2], compBoat1[2], compBoat2X[2], compBoat2Y[2], playerHits, compHits;
- char gameBoard[rows][cols], usersGameBoard[rows][cols], compGameBoard[rows][cols], posOrNeg, axis;
- bool boatPresent = false;
- bool spaceFreeAround = false;
- bool placeSafe = false;
- int boatCoord[2] ={0,0};
- struct coordinates
- {
- int xAxisCoordinate;
- int yAxisCoordinate;
- };
- void initialise()
- {
- for(int i = 0; i < rows; i++)
- {
- for(int index = 0; index < cols; index++)
- {
- gameBoard[i][index] = 'O';
- usersGameBoard[i][index]= 'O';
- compGameBoard[i][index] = 'O';
- }
- }
- }
- void displayBoards()
- {
- cout << "\nThe opponents Board looks like:\n";
- for(int i = 0; i < rows; i++)
- {
- for(int index = 0; index < cols; index++)
- {
- cout << gameBoard[i][index] << " ";
- }
- cout << " \n";
- }
- cout << "\nYour Board looks like:\n";
- for(int i = 0; i < rows; i++)
- {
- for(int index = 0; index < cols; index++)
- {
- cout << usersGameBoard[i][index] << " ";
- }
- cout << " \n";
- }
- }
- bool outOfRange(int boatPosX, int boatPosY)
- {
- bool result;
- if(boatPosX > cols || boatPosX < 1)
- result = true;
- else if(boatPosY > rows || boatPosY < 1)
- result = true;
- else
- result = false;
- return result;
- }
- void playBoatValBoat2Fr()
- {
- /** The front of the 2 long boat **/
- while(boat2X[0] > cols || boat2X[0] <= 0 )
- {
- cout << "Invalid X coordinate. Please enter again.";
- cin >> boat2X[0];
- }
- while(boat2Y[0] > rows || boat2Y[0] <= 0)
- {
- cout << "Invalid Y coordinate. Please enter again.";
- cin >> boat2Y[0];
- }
- while ((boat2X[0] == boat1X) && (boat2Y[0] == boat1Y))
- {
- cout << "\nThis part of the boat overlaps with the other boat.\n";
- cout << "Please re-enter the first part of the boats' x coordinate: ";
- cin >> boat2X[0];
- cout << "Please the enter the first part of the boats' y coordinate: ";
- cin >> boat2Y[0];
- playBoatValBoat2Fr();
- }
- }
- void playBoatValBoat2Bk()
- {
- /** The back of the 2 long boat **/
- if(boat2X[1] >= boat2X[0] )
- while((boat2X[1] - boat2X[0] != 0) && (boat2Y[1] - boat2Y[0] != 0) || ((boat2X[1] == boat2X[0]) && (boat2Y[1] == boat2Y[0]) ) )
- {
- cout << "\nThe two parts of the boat aren't next to each other!\n";
- cout << "What is the x coordinate next to the front of the boat: ";
- cin >> boat2X[1];
- cout << "What is the y coordinate of the back of the boat: ";
- cin >> boat2Y[1];
- }
- else
- while((boat2X[0] - boat2X[1] != 0) && (boat2Y[0] - boat2Y[1] != 0) || ((boat2X[1] == boat2X[0]) && (boat2Y[1] == boat2Y[0])))
- {
- cout << "\nThe two parts of the boat aren't next to each other!\n";
- cout << "What is the x coordinate next to the front of the boat: ";
- cin >> boat2X[1];
- cout << "What is the y coordinate of the back of the boat: ";
- cin >> boat2Y[1];
- }
- }
- void playBoatPlace()
- {
- bool boatRange = false;
- cout << "\n\nYou get 2 boats a one long boat and a two long boat!";
- do{
- cout << "\n\nWhat is the x coordinate of the 1 long boat: ";
- cin >> boat1X;
- cout << "What is the y coordinate of the 1 long boat: ";
- cin >> boat1Y;
- boatRange = outOfRange(boat1X, boat1Y);
- }while(boatRange == true);
- cout << "\nWhat is the x coord of the front of the two long boat: ";
- cin >> boat2X[0];
- cout << "\nWhat is the y coord of the front of the two long boat: ";
- cin >> boat2Y[0];
- playBoatValBoat2Fr();
- cout << "\nWhat is the x coordinate of the back of the boat: ";
- cin >> boat2X[1];
- cout << "\nWhat is the y coordinate of the back of the boat: ";
- cin >> boat2Y[1];
- playBoatValBoat2Bk();
- boat1X--;
- boat1Y--;
- boat2X[0]--;
- boat2Y[0]--;
- boat2X[1]--;
- boat2Y[1]--;
- usersGameBoard[boat1Y][boat1X] = 'B';
- usersGameBoard[boat2Y[0]][boat2X[0]] = 'B';
- usersGameBoard[boat2Y[1]][boat2X[1]] = 'B';
- }
- void compBoat2Place(int direction)
- {
- switch(direction)
- {
- case 1: compBoat2X[1] = compBoat2X[0];
- compBoat2Y[1] = compBoat2Y[0] - 1;
- break;
- case 2: compBoat2X[1] = compBoat2X[0] - 1;
- compBoat2Y[1] = compBoat2Y[0];
- break;
- case 3: compBoat2X[1] = compBoat2X[0];
- compBoat2Y[1] = compBoat2Y[0] + 1;
- break;
- case 4: compBoat2X[1] = compBoat2X[0] + 1;
- compBoat2Y[1] = compBoat2Y[0];
- break;
- }
- }
- void compBoatPlace()
- {
- short int boatDir;
- bool boat2Accept = false;
- compBoat1[0] = (rand() % cols);
- compBoat1[1] = (rand() % rows);
- compBoat2X[0] = (rand() % cols);
- compBoat2Y[0] = (rand() % rows);
- do
- {
- boatDir = (rand() % 4) + 1;
- compBoat2Place(boatDir);
- boat2Accept = outOfRange(compBoat2X[1], compBoat2Y[1]);
- }while(boat2Accept == true);
- }
- bool compShotVal(int xCoord, int yCoord)
- {
- bool notValid;
- if(usersGameBoard[yCoord][xCoord] == 'H' || usersGameBoard[yCoord][xCoord] == 'M')
- notValid = true;
- else
- notValid = false;
- return notValid;
- }
- bool userShotVal(int x, int y)
- {
- bool notValid;
- if(gameBoard[y][x] == 'H' || gameBoard[y][x] == 'M')
- notValid = true;
- else
- notValid = false;
- return notValid;
- }
- void updateUsersBoard(int coordX, int coordY, char let)
- {
- usersGameBoard[coordY][coordX] = let;
- }
- void updateCompsBoard(int posX, int posY, char let)
- {
- gameBoard[posY][posX] = let;
- }
- bool checkForHitUser(int x, int y)
- {
- bool boatHit = false;
- char letter;
- if(x == compBoat1[0] && y == compBoat1[1])
- {
- boatHit = true;
- letter = 'H';
- }
- else if(x == compBoat2X[0] && y == compBoat2Y[0])
- {
- boatHit = true;
- letter = 'H';
- }
- else if(x == compBoat2X[1] && y == compBoat2Y[1])
- {
- boatHit = true;
- letter = 'H';
- }
- else
- {
- boatHit = false;
- letter = 'M';
- }
- updateCompsBoard(x, y, letter);
- return boatHit;
- }
- void compDirectGo()
- {
- int move2D = 0, move1D = 0;
- move2D = (rand() % 2) + 1;
- move1D = (rand() % 2) + 1;
- switch(move2D)
- {
- case 1: axis = 'X';
- break;
- case 2: axis = 'Y';
- break;
- }
- switch(move1D)
- {
- case 1: posOrNeg = 'P';
- break;
- case 2: posOrNeg = 'N';
- break;
- }
- }
- coordinates compSmartMove()
- {
- bool notInRange;
- coordinates notSmartMove = {10,0};
- coordinates coordGo= {0,0};
- int spacePosX, spacePosY, spaceNegY, spaceNegX, boatXCoord, boatYCoord;
- spaceNegY = (boatCoord[1]- 1);
- spacePosY = (boatCoord[1]+ 1);
- spaceNegX = (boatCoord[0]-1);
- spacePosX = (boatCoord[0]+1);
- boatXCoord = boatCoord[0];
- boatYCoord = boatCoord[1];
- if(shotsTaken == 0)
- {
- boatPresent = false;
- spaceFreeAround = false;
- placeSafe = false;
- boatCoord[0] = 0;
- boatCoord[1] = 0;
- }
- if((usersGameBoard[spaceNegY][boatXCoord] !='O' )&& (usersGameBoard[spacePosY][boatXCoord] != 'O') && (usersGameBoard[boatYCoord][spaceNegX] !='O') && (usersGameBoard[boatYCoord][spacePosX] !='O'))
- spaceFreeAround = false;
- if(boatPresent == true && spaceFreeAround == true)
- {
- for(int i = 0; i < rows; i++)
- {
- for(int index = 0; index < cols; index++)
- {
- if(usersGameBoard[i][index] == 'H' && !(usersGameBoard[i-1][index] !='O' && usersGameBoard[i+1][index] != 'O' && usersGameBoard[i][index-1] !='O' && usersGameBoard[i][index+1] !='O'))
- {
- boatCoord[1] = i;
- boatCoord[0] = index;
- boatPresent = true;
- spaceFreeAround = true;
- break;
- }
- else if(usersGameBoard[i][index] == 'H')
- {
- boatPresent = true;
- spaceFreeAround = false;
- }
- }
- if(boatPresent == true && spaceFreeAround == true)
- break;
- }
- }
- if(boatPresent == true && spaceFreeAround == true)
- {
- while(notInRange == true )
- {
- compDirectGo();
- if(posOrNeg == 'P')
- {
- if(axis == 'X')
- {
- coordGo.xAxisCoordinate = boatCoord[0] + 1;
- coordGo.yAxisCoordinate = boatCoord[1];
- }
- else if(axis == 'Y')
- {
- coordGo.xAxisCoordinate = boatCoord[0];
- coordGo.yAxisCoordinate = boatCoord[1] + 1;
- }
- }
- else if(posOrNeg == 'N')
- {
- if(axis == 'X')
- {
- coordGo.xAxisCoordinate = boatCoord[0] - 1;
- coordGo.yAxisCoordinate = boatCoord[1];
- }
- else if(axis == 'Y')
- {
- coordGo.xAxisCoordinate = boatCoord[0];
- coordGo.yAxisCoordinate = boatCoord[1] - 1;
- }
- }
- notInRange == outOfRange(coordGo.xAxisCoordinate, coordGo.yAxisCoordinate);
- if(notInRange == false)
- notInRange = compShotVal(coordGo.xAxisCoordinate, coordGo.yAxisCoordinate);
- }
- return coordGo;
- }
- else return notSmartMove;
- }
- bool checkForHitComp(int coordX, int coordY)
- {
- bool isHit;
- char letter;
- if(usersGameBoard[coordY][coordX] == 'B')
- {
- isHit = true;
- letter = 'H';
- }
- else if(usersGameBoard[coordY][coordX] == 'O')
- {
- isHit = false;
- letter = 'M';
- }
- updateUsersBoard(coordX, coordY, letter);
- return isHit;
- }
- void playerEnter()
- {
- int positionX, positionY;
- bool acceptable = false, hit;
- do{
- cout << "\nWhere would you like to hit(x): ";
- cin >> positionX;
- cout << "\nWhere would you like to hit(y): ";
- cin >> positionY;
- acceptable = outOfRange(positionX, positionY);
- if(acceptable == false)
- acceptable = userShotVal(positionX, positionY);
- }while(acceptable == true);
- hit = checkForHitUser(positionX, positionY);
- if(hit == true)
- playerHits++;
- }
- void computerGo()
- {
- int positionX, positionY;
- bool notUsable, successful;
- coordinates smartMove;
- do
- {
- positionX = rand() % cols;
- positionY = rand() % rows;
- notUsable = outOfRange(positionX, positionY);
- if(notUsable == false)
- notUsable = compShotVal(positionX, positionY);
- }while(notUsable == true);
- smartMove = compSmartMove();
- if(smartMove.xAxisCoordinate == 10)
- successful = checkForHitComp(positionX, positionY);
- else
- successful = checkForHitComp(smartMove.xAxisCoordinate, smartMove.yAxisCoordinate);
- if(successful == true)
- compHits++;
- }
- int main()
- {
- shotsTaken = 0;
- int wins = 0, loses = 0;
- gamesPlayed = 0;
- srand(time(NULL));
- cout << "\n\n\nWelcome to Battleships!!";
- cout << "\nHow many games would you like to play: ";
- cin >> numOfGames;
- do
- {
- playerHits = 0;
- compHits = 0;
- cout << "\n\nWins: " << wins << " Loses: " << loses;
- initialise();
- displayBoards();
- playBoatPlace();
- displayBoards();
- compBoatPlace();
- do
- {
- playerEnter();
- if(playerHits == 3)
- break;
- computerGo();
- displayBoards();
- shotsTaken++;
- }while(shotsTaken == 25 || compHits == 3 || playerHits == 3);
- if(compHits == 3 && playerHits < 3)
- loses++;
- else if(playerHits == 3 && compHits < 3)
- wins++;
- gamesPlayed++;
- }while(gamesPlayed != numOfGames);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement