Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- 0 1 2 3 4
- 5 6 7 8 9
- 10 11 12 13 14
- 15 16 17 18 19
- 20 21 22 23 24
- Possible bombs:
- =====================================================
- LEFTMOST CORNERS (3 possible)
- 0:
- 0 + 1 = 1
- 0 + 5 = 5
- 0 + 6 = 6
- 20:
- 20 - 5 = 15
- 20 - 4 = 16
- 20 + 1 = 21
- =====================================================
- RIGHTMOST CORNERS (3 possible)
- 4:
- - 1
- + 4
- + 5
- 24:
- - 5
- - 6
- - 1
- =====================================================
- LEFTMOST SIDES (5 possible)
- FOR 5, 10, 15:
- - 5
- - 4
- + 1
- + 5
- + 6
- =====================================================
- RIGHTMOST SIDES (5 possible)
- FOR 9, 14, 19:
- - 6
- - 5
- - 1
- + 4
- + 5
- =====================================================
- FOR UPPERMOST SIDES (5 possible)
- FOR 1, 2, 3:
- - 1
- + 1
- + 4
- + 5
- + 6
- =====================================================
- FOR LOWERMOST SIDES (5 possible)
- FOR 21, 22, 23:
- - 6
- - 5
- - 4
- - 1
- + 1
- =====================================================
- FOR MIDDLES (8 possible) (max bombs still 5)
- FOR 6, 7, 8, 11, 12, 13, 16, 17, 18
- - 6
- - 5
- - 4
- - 1
- + 1
- + 4
- + 5
- + 6
- =====================================================
- **/
- #include <iostream>
- #include <stdlib.h>
- #include <time.h>
- using namespace std;
- int main()
- {
- int n = 26;
- char board[n];
- char boardDisp[n] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y'};
- char bombCount;
- int bombVicinityCount(int q);
- srand(time(0));
- cout<<"MINESWEEPER\n\n";
- // Assigns bomb locations, bomb = Z
- for (int i = 0; i<5 ; i++)
- {
- int k = rand()% 25;
- board[k] = 'Z';
- }
- //if letter is not a bomb, assign letter for display
- for (int l = 0; l<n; l++)
- {
- if (board[l] != 'Z')
- {
- board[l] = boardDisp[l];
- }
- }
- // displays the letters in a grid
- for (int a=0; a<5; a++)
- {
- cout<<boardDisp[a]<<" ";
- }
- cout<<endl;
- for (int b=5; b<10; b++)
- {
- cout<<boardDisp[b]<<" ";
- }
- cout<<endl;
- for (int c=10; c<15; c++)
- {
- cout<<boardDisp[c]<<" ";
- }
- cout<<endl;
- for (int d=15; d<20; d++)
- {
- cout<<boardDisp[d]<<" ";
- }
- cout<<endl;
- for (int e=20; e<25; e++)
- {
- cout<<boardDisp[e]<<" ";
- }
- cout<<endl;
- // places value for number of bombs in board
- for (int f = 0; f<n; f++)
- {
- if (board[f] != 'Z')
- {
- int bombs = bombVicinityCount(f);
- if (bombs != 0)
- {
- if (bombs == 1)
- {
- bombCount = '1';
- } else if (bombs == 2)
- {
- bombCount = '2';
- } else if (bombs == 3)
- {
- bombCount = '3';
- } else if (bombs == 4)
- {
- bombCount = '4';
- } else if (bombs == 5)
- {
- bombCount = '5';
- }
- board[f] = bombCount;
- } else
- if (bombs == 0)
- {
- board[f] = boardDisp[f];
- }
- } else
- {
- break;
- }
- }
- // for testing; displays bomb locations and number of bombs
- for (int a=0; a<5; a++)
- {
- cout<<board[a]<<" ";
- }
- cout<<endl;
- for (int b=5; b<10; b++)
- {
- cout<<board[b]<<" ";
- }
- cout<<endl;
- for (int c=10; c<15; c++)
- {
- cout<<board[c]<<" ";
- }
- cout<<endl;
- for (int d=15; d<20; d++)
- {
- cout<<board[d]<<" ";
- }
- cout<<endl;
- for (int e=20; e<25; e++)
- {
- cout<<board[e]<<" ";
- }
- cout<<endl;
- return 0;
- }
- // function to find number of bombs
- int bombVicinityCount(int q)
- {
- int bCount;
- int fN6(int w, int r);
- int fN5(int x, int s);
- int fN4(int y, int t);
- int fN1(int z, int u);
- // for pos 0 (upper left corner)
- if (q==0)
- {
- bCount = fN1(2, 0) + fN5(2, 0) + fN6(2, 0);
- }
- // for pos 20 (lower left corner)
- if (q==20)
- {
- bCount = fN1(2, 20) + fN4(1, 20) + fN5(1, 20);
- }
- // for pos 4 (upper right corner)
- if (q==4)
- {
- bCount = fN1(1, 4) + fN4(2, 4) + fN5(2, 4);
- }
- // for pos 24 (lower right corner)
- if (q==24)
- {
- bCount = fN1(1, 24) + fN5(1, 24) + fN6(1, 24);
- }
- // for pos 5, 10, 15 (left sides)
- if ((q==5) || (q==10) || (q==15))
- {
- bCount = fN5(3, q) + fN4(1, q) + fN1(2, q) + fN6(2, q);
- }
- // for pos 9, 14, 19 (right sides)
- if ((q==9) || (q==14) || (q==19))
- {
- bCount = fN5(3, q) + fN4(2, q) + fN1(1, q) + fN6(1, q);
- }
- // for pos 1, 2, 3 (upper sides)
- if ((q==1) || (q==2) || (q==3))
- {
- bCount = fN1(3, q) + fN4(2, q) + fN5(2, q) + fN6(2, q);
- }
- // for pos 21, 22, 23 (lower sides)
- if ((q==21) || (q==22) || (q==23))
- {
- bCount = fN1(3, q) + fN4(1, q) + fN5(1, q) + fN6(1, q);
- }
- // for pos 6, 7, 8, 11, 12, 13, 16, 17, 18 (middles)
- if ((q==6) || (q==7) || (q==8) || (q==11) || (q==12) || (q==13) || (q==16) || (q==17) || (q==18))
- {
- bCount = fN5(3, q) + fN4(3, q) + fN1(3, q) + fN6(3, q);
- }
- return bCount;
- }
- int fN6 (int w, int r)
- {
- char board[26];
- int bomb6 = 0;
- if (w == 1)
- {
- if (board[r]-6 == 'Z')
- {
- bomb6 += 1;
- }
- } else if (w == 2)
- {
- if (board[r]+6 == 'Z' )
- {
- bomb6 += 1;
- }
- } else if (w == 3)
- {
- if (board[r]-6 == 'Z')
- {
- bomb6 += 1;
- }
- if (board[r]+6 == 'Z' )
- {
- bomb6 += 1;
- }
- }
- return bomb6;
- }
- int fN5 (int x, int s)
- {
- char board[26];
- int bomb5 = 0;
- if (x == 1)
- {
- if (board[s]-5 == 'Z')
- {
- bomb5 += 1;
- }
- } else if (x == 2)
- {
- if (board[s]+5 == 'Z' )
- {
- bomb5 += 1;
- }
- } else if (s == 3)
- {
- if (board[s]-5 == 'Z')
- {
- bomb5 += 1;
- }
- if (board[s]+5 == 'Z' )
- {
- bomb5 += 1;
- }
- }
- return bomb5;
- }
- int fN4 (int y, int t)
- {
- char board[26];
- int bomb4 = 0;
- if (y == 1)
- {
- if (board[t]-4 == 'Z')
- {
- bomb4 += 1;
- }
- } else if (y == 2)
- {
- if (board[t]+4 == 'Z' )
- {
- bomb4 += 1;
- }
- } else if (y == 3)
- {
- if (board[t]-4 == 'Z')
- {
- bomb4 += 1;
- }
- if (board[t]+4 == 'Z' )
- {
- bomb4 += 1;
- }
- }
- return bomb4;
- }
- int fN1 (int z, int u)
- {
- char board[26];
- int bomb1 = 0;
- if (z == 1)
- {
- if (board[u]-1 == 'Z')
- {
- bomb1 += 1;
- }
- } else if (z == 2)
- {
- if (board[u]+1 == 'Z' )
- {
- bomb1 += 1;
- }
- } else if (z == 3)
- {
- if (board[u]-1 == 'Z')
- {
- bomb1 += 1;
- }
- if (board[u]+1 == 'Z' )
- {
- bomb1 += 1;
- }
- }
- return bomb1;
- }
- // for fNum functions, x == 2 adds Num, while x == 1 subtracts Num, x == 3 does both
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement