test
This commit is contained in:
parent
37776af5db
commit
ab03d5f10c
4045 changed files with 286212 additions and 3 deletions
151
.config/Code/User/History/55ccb45e/0AOl.cpp
Normal file
151
.config/Code/User/History/55ccb45e/0AOl.cpp
Normal file
|
|
@ -0,0 +1,151 @@
|
|||
/*
|
||||
Rafay Ahmad
|
||||
23I-2526
|
||||
*/
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
class Minesweeper{
|
||||
char** grid;
|
||||
int* mineLocations;
|
||||
int size;
|
||||
int mines;
|
||||
int opened;
|
||||
|
||||
public:
|
||||
|
||||
Minesweeper(int size, int mines) : size(size), mines(mines), opened(0){
|
||||
srand(time(0));
|
||||
grid = new char*[size];
|
||||
for(int i=0;i<size;i++){
|
||||
grid[i] = new char[size];
|
||||
for(int j=0;j<size;j++) grid[i][j] = '#';
|
||||
}
|
||||
mineLocations = new int[mines];
|
||||
setMines();
|
||||
}
|
||||
~Minesweeper(){
|
||||
for(int i=0;i<size;i++) delete[] grid[i];
|
||||
delete[] grid;
|
||||
delete[] mineLocations;
|
||||
}
|
||||
|
||||
void reset(){
|
||||
for(int i=0;i<size;i++) for(int j=0;j<size;j++) grid[i][j] = '#';
|
||||
setMines();
|
||||
}
|
||||
void revealAll(){
|
||||
for(int i=0;i<size;i++){
|
||||
for(int j=0;j<size;j++){
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
bool revealCell(int row, int col, bool checkMine){
|
||||
for(int i=0;i<mines;i++){
|
||||
if(mineLocations[i]/size == row && mineLocations[i]%7 == col){
|
||||
grid[row][col] = '*';
|
||||
revealAll();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
grid[row][col] = checkSurrounding(row, col) + '0';
|
||||
opened++;
|
||||
if(grid[row][col] == '0'){
|
||||
return revealCell(row+1, col+1, false);
|
||||
return revealCell(row, col+1, false);
|
||||
return revealCell(row-1, col+1, false);
|
||||
return revealCell(row-1, col, false);
|
||||
return revealCell(row-1, col-1, false);
|
||||
return revealCell(row, col-1, false);
|
||||
return revealCell(row+1, col-1, false);
|
||||
return revealCell(row+1, col, false);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
void flagCell(int row, int col){
|
||||
grid[row][col] = 'P';
|
||||
}
|
||||
int checkSurrounding(int row, int col){
|
||||
int count = 0;
|
||||
for(int i=0;i<mines;i++){
|
||||
if(mineLocations[i]/size == row+1 && mineLocations[i]%7 == col+1) count++;
|
||||
if(mineLocations[i]/size == row && mineLocations[i]%7 == col+1) count++;
|
||||
if(mineLocations[i]/size == row-1 && mineLocations[i]%7 == col+1) count++;
|
||||
if(mineLocations[i]/size == row-1 && mineLocations[i]%7 == col) count++;
|
||||
if(mineLocations[i]/size == row-1 && mineLocations[i]%7 == col-1) count++;
|
||||
if(mineLocations[i]/size == row && mineLocations[i]%7 == col-1) count++;
|
||||
if(mineLocations[i]/size == row+1 && mineLocations[i]%7 == col-1) count++;
|
||||
if(mineLocations[i]/size == row+1 && mineLocations[i]%7 == col) count++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
bool checkWin(){
|
||||
return opened == ((size*size) - mines);
|
||||
}
|
||||
void setMines(){
|
||||
for(int i=0;i<mines;i++){
|
||||
bool valid;
|
||||
do{
|
||||
valid = true;
|
||||
mineLocations[i] = rand()%(size*size);
|
||||
for(int j=0;j<i;j++){
|
||||
if(mineLocations[j] == mineLocations[i]){
|
||||
valid = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}while(!valid);
|
||||
}
|
||||
}
|
||||
void displayGrid(){
|
||||
cout<<" ";
|
||||
for(int i=0;i<size;i++) cout<<i+1<<" ";
|
||||
cout<<endl;
|
||||
for(int i=0;i<size;i++){
|
||||
cout<<i+1<<" ";
|
||||
for(int j=0;j<size;j++) cout<<grid[i][j]<<" ";
|
||||
cout<<endl;
|
||||
}
|
||||
cout<<endl;
|
||||
|
||||
for(int i=0;i<mines;i++){
|
||||
cout<<mineLocations[i]<<" ";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
};
|
||||
|
||||
int main(){
|
||||
|
||||
int row, col;
|
||||
char c;
|
||||
int size, mines;
|
||||
cout<<"Enter size of the game board: ";
|
||||
cin>>size;
|
||||
cout<<"Enter no Of mines: ";
|
||||
cin>>mines;
|
||||
Minesweeper game(size, mines);
|
||||
|
||||
while(!game.checkWin()){
|
||||
game.displayGrid();
|
||||
cout<<"Enter row: ";
|
||||
cin>>row;
|
||||
cout<<"Enter col: ";
|
||||
cin>>col;
|
||||
cout<<"Enter F to flag or R to reveal: ";
|
||||
cin>>c;
|
||||
|
||||
if(row<size && col<size && (c == 'F' || c == 'R')){
|
||||
game.revealCell(row, col, true);
|
||||
}
|
||||
else cout<<"Invalid input\n";
|
||||
|
||||
}
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
147
.config/Code/User/History/55ccb45e/1uka.cpp
Normal file
147
.config/Code/User/History/55ccb45e/1uka.cpp
Normal file
|
|
@ -0,0 +1,147 @@
|
|||
/*
|
||||
Rafay Ahmad
|
||||
23I-2526
|
||||
*/
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
class Minesweeper{
|
||||
char** grid;
|
||||
int* mineLocations;
|
||||
int size;
|
||||
int mines;
|
||||
int opened;
|
||||
|
||||
public:
|
||||
|
||||
Minesweeper(int size, int mines) : size(size), mines(mines), opened(0){
|
||||
srand(time(0));
|
||||
grid = new char*[size];
|
||||
for(int i=0;i<size;i++){
|
||||
grid[i] = new char[size];
|
||||
for(int j=0;j<size;j++) grid[i][j] = '#';
|
||||
}
|
||||
mineLocations = new int[mines];
|
||||
setMines();
|
||||
}
|
||||
~Minesweeper(){
|
||||
for(int i=0;i<size;i++) delete[] grid[i];
|
||||
delete[] grid;
|
||||
delete[] mineLocations;
|
||||
}
|
||||
|
||||
void reset(){
|
||||
for(int i=0;i<size;i++) for(int j=0;j<size;j++) grid[i][j] = '#';
|
||||
setMines();
|
||||
}
|
||||
void revealAll(){
|
||||
for(int i=0;i<size;i++){
|
||||
for(int j=0;j<size;j++){
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
bool revealCell(int row, int col, bool checkMine){
|
||||
for(int i=0;i<mines;i++){
|
||||
if(mineLocations[i]/size == row && mineLocations[i]%7 == col){
|
||||
grid[row][col] = '*';
|
||||
revealAll();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
grid[row][col] = checkSurrounding(row, col) + '0';
|
||||
opened++;
|
||||
if(grid[row][col] == '0'){
|
||||
return revealCell(row+1, col+1, false);
|
||||
return revealCell(row, col+1, false);
|
||||
return revealCell(row-1, col+1, false);
|
||||
return revealCell(row-1, col, false);
|
||||
return revealCell(row-1, col-1, false);
|
||||
return revealCell(row, col-1, false);
|
||||
return revealCell(row+1, col-1, false);
|
||||
return revealCell(row+1, col, false);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
void flagCell(int row, int col){
|
||||
grid[row][col] = 'P';
|
||||
}
|
||||
int checkSurrounding(int row, int col){
|
||||
int count = 0;
|
||||
for(int i=0;i<mines;i++){
|
||||
if(mineLocations[i]/size == row+1 && mineLocations[i]%7 == col+1) count++;
|
||||
if(mineLocations[i]/size == row && mineLocations[i]%7 == col+1) count++;
|
||||
if(mineLocations[i]/size == row-1 && mineLocations[i]%7 == col+1) count++;
|
||||
if(mineLocations[i]/size == row-1 && mineLocations[i]%7 == col) count++;
|
||||
if(mineLocations[i]/size == row-1 && mineLocations[i]%7 == col-1) count++;
|
||||
if(mineLocations[i]/size == row && mineLocations[i]%7 == col-1) count++;
|
||||
if(mineLocations[i]/size == row+1 && mineLocations[i]%7 == col-1) count++;
|
||||
if(mineLocations[i]/size == row+1 && mineLocations[i]%7 == col) count++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
bool checkWin(){
|
||||
return opened == (size*size) - mines;
|
||||
}
|
||||
void setMines(){
|
||||
for(int i=0;i<mines;i++){
|
||||
bool valid;
|
||||
do{
|
||||
valid = true;
|
||||
mineLocations[i] = rand()%(size*size);
|
||||
for(int j=0;j<i;j++){
|
||||
if(mineLocations[j] == mineLocations[i]){
|
||||
valid = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}while(!valid);
|
||||
}
|
||||
}
|
||||
void displayGrid(){
|
||||
cout<<" ";
|
||||
for(int i=0;i<size;i++) cout<<i+1<<" ";
|
||||
cout<<endl;
|
||||
for(int i=0;i<size;i++){
|
||||
cout<<i+1<<" ";
|
||||
for(int j=0;j<size;j++) cout<<grid[i][j]<<" ";
|
||||
cout<<endl;
|
||||
}
|
||||
cout<<endl;
|
||||
|
||||
for(int i=0;i<mines;i++){
|
||||
cout<<mineLocations[i]<<" ";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
};
|
||||
|
||||
int main(){
|
||||
|
||||
int row, col;
|
||||
char c;
|
||||
int size, mines;
|
||||
cout<<"Enter size of the game board: ";
|
||||
cin>>size;
|
||||
cout<<"Enter no Of mines: ";
|
||||
cin>>mines;
|
||||
Minesweeper game(size, mines);
|
||||
while(game.checkWin()){
|
||||
game.displayGrid();
|
||||
cout<<"Enter row: ";
|
||||
cin>>row;
|
||||
cout<<"Enter col: ";
|
||||
cin>>col;
|
||||
cout<<"Enter F to flag or R to reveal: ";
|
||||
cin>>c;
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
156
.config/Code/User/History/55ccb45e/3nsc.cpp
Normal file
156
.config/Code/User/History/55ccb45e/3nsc.cpp
Normal file
|
|
@ -0,0 +1,156 @@
|
|||
/*
|
||||
Rafay Ahmad
|
||||
23I-2526
|
||||
*/
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
class Minesweeper{
|
||||
char** grid;
|
||||
int* mineLocations;
|
||||
int size;
|
||||
int mines;
|
||||
int opened;
|
||||
|
||||
public:
|
||||
|
||||
Minesweeper(int size, int mines) : size(size), mines(mines), opened(0){
|
||||
srand(time(0));
|
||||
grid = new char*[size];
|
||||
for(int i=0;i<size;i++){
|
||||
grid[i] = new char[size];
|
||||
for(int j=0;j<size;j++) grid[i][j] = '#';
|
||||
}
|
||||
mineLocations = new int[mines];
|
||||
setMines();
|
||||
}
|
||||
~Minesweeper(){
|
||||
for(int i=0;i<size;i++) delete[] grid[i];
|
||||
delete[] grid;
|
||||
delete[] mineLocations;
|
||||
}
|
||||
|
||||
void reset(){
|
||||
for(int i=0;i<size;i++) for(int j=0;j<size;j++) grid[i][j] = '#';
|
||||
setMines();
|
||||
}
|
||||
void revealAll(){
|
||||
for(int i=0;i<mines;i++){
|
||||
grid[mineLocations[i]/7][mineLocations[i]%7] = '*';
|
||||
}
|
||||
}
|
||||
bool revealCell(int row, int col, bool checkMine){
|
||||
if(checkMine){
|
||||
for(int i=0;i<mines;i++){
|
||||
if(mineLocations[i]/size == row && mineLocations[i]%size == col){
|
||||
grid[row][col] = '*';
|
||||
revealAll();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
grid[row][col] = checkSurrounding(row, col) + '0';
|
||||
opened++;
|
||||
if(grid[row][col] == '0'){
|
||||
if(row<size-1 && col<size-1 && grid[row+1][col+1] == '#') revealCell(row+1, col+1, false);
|
||||
if(col<size-1 && grid[row][col+1] == '#') revealCell(row, col+1, false);
|
||||
if(row>0 && col<size-1 && grid[row-1][col+1] == '#') revealCell(row-1, col+1, false);
|
||||
if(row>0 && grid[row-1][col] == '#') revealCell(row-1, col, false);
|
||||
if(row>0 && col>0 && grid[row-1][col-1] == '#') revealCell(row-1, col-1, false);
|
||||
if(col>0 && grid[row][col-1] == '#') revealCell(row, col-1, false);
|
||||
if(row<size-1 && col>0 && grid[row+1][col-1] == '#') revealCell(row+1, col-1, false);
|
||||
if(row<size-1 && grid[row+1][col] == '#') revealCell(row+1, col, false);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
void flagCell(int row, int col){
|
||||
grid[row][col] = 'P';
|
||||
}
|
||||
int checkSurrounding(int row, int col){
|
||||
int count = 0;
|
||||
for(int i=0;i<mines;i++){
|
||||
if(row<size && col<size && mineLocations[i]/size == row+1 && mineLocations[i]%size == col+1) count++;
|
||||
if(col<size && mineLocations[i]/size == row && mineLocations[i]%size == col+1) count++;
|
||||
if(row>0 && col<size && mineLocations[i]/size == row-1 && mineLocations[i]%size == col+1) count++;
|
||||
if(row>0 && mineLocations[i]/size == row-1 && mineLocations[i]%size == col) count++;
|
||||
if(row>0 && col>0 && mineLocations[i]/size == row-1 && mineLocations[i]%size == col-1) count++;
|
||||
if(col>0 && mineLocations[i]/size == row && mineLocations[i]%size == col-1) count++;
|
||||
if(row<size && col>0 && mineLocations[i]/size == row+1 && mineLocations[i]%size == col-1) count++;
|
||||
if(row<size && mineLocations[i]/size == row+1 && mineLocations[i]%size == col) count++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
bool checkWin(){
|
||||
return opened == ((size*size) - mines);
|
||||
}
|
||||
void setMines(){
|
||||
for(int i=0;i<mines;i++){
|
||||
bool valid;
|
||||
do{
|
||||
valid = true;
|
||||
mineLocations[i] = rand()%(size*size);
|
||||
for(int j=0;j<i;j++){
|
||||
if(mineLocations[j] == mineLocations[i]){
|
||||
valid = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}while(!valid);
|
||||
}
|
||||
}
|
||||
void displayGrid(){
|
||||
cout<<" ";
|
||||
for(int i=0;i<size;i++) cout<<i<<" ";
|
||||
cout<<endl;
|
||||
for(int i=0;i<size;i++){
|
||||
cout<<i<<" ";
|
||||
for(int j=0;j<size;j++) cout<<grid[i][j]<<" ";
|
||||
cout<<endl;
|
||||
}
|
||||
cout<<endl;
|
||||
|
||||
for(int i=0;i<mines;i++){
|
||||
cout<<mineLocations[i]<<" ";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
};
|
||||
|
||||
int main(){
|
||||
|
||||
int row, col;
|
||||
char c;
|
||||
int size, mines;
|
||||
cout<<"Enter size of the game board: ";
|
||||
cin>>size;
|
||||
cout<<"Enter no Of mines: ";
|
||||
cin>>mines;
|
||||
Minesweeper game(size, mines);
|
||||
|
||||
while(!game.checkWin()){
|
||||
game.displayGrid();
|
||||
cout<<"Enter row: ";
|
||||
cin>>row;
|
||||
cout<<"Enter col: ";
|
||||
cin>>col;
|
||||
cout<<"Enter F to flag or R to reveal: ";
|
||||
cin>>c;
|
||||
|
||||
if(row<size && col<size && (c == 'F' || c == 'R')){
|
||||
if(c == 'R' && !game.revealCell(row, col, true)){
|
||||
game.displayGrid();
|
||||
cout<<"You Lost\n";
|
||||
break;
|
||||
}
|
||||
else if(c == 'F') game.flagCell(row, col);
|
||||
}
|
||||
else cout<<"Invalid input\n";
|
||||
|
||||
}
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
156
.config/Code/User/History/55ccb45e/5N6h.cpp
Normal file
156
.config/Code/User/History/55ccb45e/5N6h.cpp
Normal file
|
|
@ -0,0 +1,156 @@
|
|||
/*
|
||||
Rafay Ahmad
|
||||
23I-2526
|
||||
*/
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
class Minesweeper{
|
||||
char** grid;
|
||||
int* mineLocations;
|
||||
int size;
|
||||
int mines;
|
||||
int opened;
|
||||
|
||||
public:
|
||||
|
||||
Minesweeper(int size, int mines) : size(size), mines(mines), opened(0){
|
||||
srand(time(0));
|
||||
grid = new char*[size];
|
||||
for(int i=0;i<size;i++){
|
||||
grid[i] = new char[size];
|
||||
for(int j=0;j<size;j++) grid[i][j] = '#';
|
||||
}
|
||||
mineLocations = new int[mines];
|
||||
setMines();
|
||||
}
|
||||
~Minesweeper(){
|
||||
for(int i=0;i<size;i++) delete[] grid[i];
|
||||
delete[] grid;
|
||||
delete[] mineLocations;
|
||||
}
|
||||
|
||||
void reset(){
|
||||
for(int i=0;i<size;i++) for(int j=0;j<size;j++) grid[i][j] = '#';
|
||||
setMines();
|
||||
}
|
||||
void revealAll(){
|
||||
for(int i=0;i<mines;i++){
|
||||
grid[mineLocations[i]/7][mineLocations[i]%7] = '*';
|
||||
}
|
||||
}
|
||||
bool revealCell(int row, int col, bool checkMine){
|
||||
if(checkMine){
|
||||
for(int i=0;i<mines;i++){
|
||||
if(mineLocations[i]/size == row && mineLocations[i]%7 == col){
|
||||
grid[row][col] = '*';
|
||||
revealAll();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
grid[row][col] = checkSurrounding(row, col) + '0';
|
||||
opened++;
|
||||
if(grid[row][col] == '0'){
|
||||
if(row>size && col>size) revealCell(row+1, col+1, false);
|
||||
if(row>size && col>size) revealCell(row, col+1, false);
|
||||
if(row>size && col>size) revealCell(row-1, col+1, false);
|
||||
if(row>size && col>size) revealCell(row-1, col, false);
|
||||
if(row>size && col>size) revealCell(row-1, col-1, false);
|
||||
if(row>size && col>size) revealCell(row, col-1, false);
|
||||
if(row>size && col>size) revealCell(row+1, col-1, false);
|
||||
if(row>size && col>size) revealCell(row+1, col, false);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
void flagCell(int row, int col){
|
||||
grid[row][col] = 'P';
|
||||
}
|
||||
int checkSurrounding(int row, int col){
|
||||
int count = 0;
|
||||
for(int i=0;i<mines;i++){
|
||||
if(row<size && col<size && mineLocations[i]/size == row+1 && mineLocations[i]%7 == col+1) count++;
|
||||
if(col<size && mineLocations[i]/size == row && mineLocations[i]%7 == col+1) count++;
|
||||
if(row>0 && col<size && mineLocations[i]/size == row-1 && mineLocations[i]%7 == col+1) count++;
|
||||
if(row>0 && mineLocations[i]/size == row-1 && mineLocations[i]%7 == col) count++;
|
||||
if(row>0 && col>0 && mineLocations[i]/size == row-1 && mineLocations[i]%7 == col-1) count++;
|
||||
if(col>0 && mineLocations[i]/size == row && mineLocations[i]%7 == col-1) count++;
|
||||
if(row<size && col>0 && mineLocations[i]/size == row+1 && mineLocations[i]%7 == col-1) count++;
|
||||
if(row<size && mineLocations[i]/size == row+1 && mineLocations[i]%7 == col) count++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
bool checkWin(){
|
||||
return opened == ((size*size) - mines);
|
||||
}
|
||||
void setMines(){
|
||||
for(int i=0;i<mines;i++){
|
||||
bool valid;
|
||||
do{
|
||||
valid = true;
|
||||
mineLocations[i] = rand()%(size*size);
|
||||
for(int j=0;j<i;j++){
|
||||
if(mineLocations[j] == mineLocations[i]){
|
||||
valid = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}while(!valid);
|
||||
}
|
||||
}
|
||||
void displayGrid(){
|
||||
cout<<" ";
|
||||
for(int i=0;i<size;i++) cout<<i<<" ";
|
||||
cout<<endl;
|
||||
for(int i=0;i<size;i++){
|
||||
cout<<i<<" ";
|
||||
for(int j=0;j<size;j++) cout<<grid[i][j]<<" ";
|
||||
cout<<endl;
|
||||
}
|
||||
cout<<endl;
|
||||
|
||||
for(int i=0;i<mines;i++){
|
||||
cout<<mineLocations[i]<<" ";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
};
|
||||
|
||||
int main(){
|
||||
|
||||
int row, col;
|
||||
char c;
|
||||
int size, mines;
|
||||
cout<<"Enter size of the game board: ";
|
||||
cin>>size;
|
||||
cout<<"Enter no Of mines: ";
|
||||
cin>>mines;
|
||||
Minesweeper game(size, mines);
|
||||
|
||||
while(!game.checkWin()){
|
||||
game.displayGrid();
|
||||
cout<<"Enter row: ";
|
||||
cin>>row;
|
||||
cout<<"Enter col: ";
|
||||
cin>>col;
|
||||
cout<<"Enter F to flag or R to reveal: ";
|
||||
cin>>c;
|
||||
|
||||
if(row<size && col<size && (c == 'F' || c == 'R')){
|
||||
if(c == 'R' && !game.revealCell(row, col, true)){
|
||||
game.displayGrid();
|
||||
cout<<"You Lost\n";
|
||||
break;
|
||||
}
|
||||
else if(c == 'F') game.flagCell(row, col);
|
||||
}
|
||||
else cout<<"Invalid input\n";
|
||||
|
||||
}
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
152
.config/Code/User/History/55ccb45e/5WzR.cpp
Normal file
152
.config/Code/User/History/55ccb45e/5WzR.cpp
Normal file
|
|
@ -0,0 +1,152 @@
|
|||
/*
|
||||
Rafay Ahmad
|
||||
23I-2526
|
||||
*/
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
class Minesweeper{
|
||||
char** grid;
|
||||
int* mineLocations;
|
||||
int size;
|
||||
int mines;
|
||||
int opened;
|
||||
|
||||
public:
|
||||
|
||||
Minesweeper(int size, int mines) : size(size), mines(mines), opened(0){
|
||||
srand(time(0));
|
||||
grid = new char*[size];
|
||||
for(int i=0;i<size;i++){
|
||||
grid[i] = new char[size];
|
||||
for(int j=0;j<size;j++) grid[i][j] = '#';
|
||||
}
|
||||
mineLocations = new int[mines];
|
||||
setMines();
|
||||
}
|
||||
~Minesweeper(){
|
||||
for(int i=0;i<size;i++) delete[] grid[i];
|
||||
delete[] grid;
|
||||
delete[] mineLocations;
|
||||
}
|
||||
|
||||
void reset(){
|
||||
for(int i=0;i<size;i++) for(int j=0;j<size;j++) grid[i][j] = '#';
|
||||
setMines();
|
||||
}
|
||||
void revealAll(){
|
||||
|
||||
}
|
||||
bool revealCell(int row, int col, bool checkMine){
|
||||
for(int i=0;i<mines;i++){
|
||||
if(mineLocations[i]/size == row && mineLocations[i]%7 == col){
|
||||
grid[row][col] = '*';
|
||||
revealAll();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
grid[row][col] = checkSurrounding(row, col) + '0';
|
||||
opened++;
|
||||
if(grid[row][col] == '0'){
|
||||
return revealCell(row+1, col+1, false);
|
||||
return revealCell(row, col+1, false);
|
||||
return revealCell(row-1, col+1, false);
|
||||
return revealCell(row-1, col, false);
|
||||
return revealCell(row-1, col-1, false);
|
||||
return revealCell(row, col-1, false);
|
||||
return revealCell(row+1, col-1, false);
|
||||
return revealCell(row+1, col, false);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
void flagCell(int row, int col){
|
||||
grid[row][col] = 'P';
|
||||
}
|
||||
int checkSurrounding(int row, int col){
|
||||
int count = 0;
|
||||
for(int i=0;i<mines;i++){
|
||||
if(mineLocations[i]/size == row+1 && mineLocations[i]%7 == col+1) count++;
|
||||
if(mineLocations[i]/size == row && mineLocations[i]%7 == col+1) count++;
|
||||
if(mineLocations[i]/size == row-1 && mineLocations[i]%7 == col+1) count++;
|
||||
if(mineLocations[i]/size == row-1 && mineLocations[i]%7 == col) count++;
|
||||
if(mineLocations[i]/size == row-1 && mineLocations[i]%7 == col-1) count++;
|
||||
if(mineLocations[i]/size == row && mineLocations[i]%7 == col-1) count++;
|
||||
if(mineLocations[i]/size == row+1 && mineLocations[i]%7 == col-1) count++;
|
||||
if(mineLocations[i]/size == row+1 && mineLocations[i]%7 == col) count++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
bool checkWin(){
|
||||
return opened == ((size*size) - mines);
|
||||
}
|
||||
void setMines(){
|
||||
for(int i=0;i<mines;i++){
|
||||
bool valid;
|
||||
do{
|
||||
valid = true;
|
||||
mineLocations[i] = rand()%(size*size);
|
||||
for(int j=0;j<i;j++){
|
||||
if(mineLocations[j] == mineLocations[i]){
|
||||
valid = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}while(!valid);
|
||||
}
|
||||
}
|
||||
void displayGrid(){
|
||||
cout<<" ";
|
||||
for(int i=0;i<size;i++) cout<<i+1<<" ";
|
||||
cout<<endl;
|
||||
for(int i=0;i<size;i++){
|
||||
cout<<i+1<<" ";
|
||||
for(int j=0;j<size;j++) cout<<grid[i][j]<<" ";
|
||||
cout<<endl;
|
||||
}
|
||||
cout<<endl;
|
||||
|
||||
for(int i=0;i<mines;i++){
|
||||
cout<<mineLocations[i]<<" ";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
};
|
||||
|
||||
int main(){
|
||||
|
||||
int row, col;
|
||||
char c;
|
||||
int size, mines;
|
||||
cout<<"Enter size of the game board: ";
|
||||
cin>>size;
|
||||
cout<<"Enter no Of mines: ";
|
||||
cin>>mines;
|
||||
Minesweeper game(size, mines);
|
||||
|
||||
while(!game.checkWin()){
|
||||
game.displayGrid();
|
||||
cout<<"Enter row: ";
|
||||
cin>>row;
|
||||
cout<<"Enter col: ";
|
||||
cin>>col;
|
||||
cout<<"Enter F to flag or R to reveal: ";
|
||||
cin>>c;
|
||||
|
||||
if(row<size && col<size && (c == 'F' || c == 'R')){
|
||||
if(c == 'R') if(!game.revealCell(row, col, true)){
|
||||
game.displayGrid();
|
||||
cout<<"You Lost\n";
|
||||
break;
|
||||
}
|
||||
else game.flagCell(row, col);
|
||||
}
|
||||
else cout<<"Invalid input\n";
|
||||
|
||||
}
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
123
.config/Code/User/History/55ccb45e/5YIQ.cpp
Normal file
123
.config/Code/User/History/55ccb45e/5YIQ.cpp
Normal file
|
|
@ -0,0 +1,123 @@
|
|||
/*
|
||||
Rafay Ahmad
|
||||
23I-2526
|
||||
*/
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
class Minesweeper{
|
||||
char** grid;
|
||||
int* mineLocations;
|
||||
int size;
|
||||
int mines;
|
||||
|
||||
public:
|
||||
|
||||
Minesweeper(int size, int mines) : size(size), mines(mines){
|
||||
srand(time(0));
|
||||
grid = new char*[size];
|
||||
for(int i=0;i<size;i++){
|
||||
grid[i] = new char[size];
|
||||
for(int j=0;j<size;j++) grid[i][j] = '#';
|
||||
}
|
||||
mineLocations = new int[mines];
|
||||
setMines();
|
||||
}
|
||||
~Minesweeper(){
|
||||
for(int i=0;i<size;i++) delete[] grid[i];
|
||||
delete[] grid;
|
||||
delete[] mineLocations;
|
||||
}
|
||||
|
||||
void reset(){
|
||||
for(int i=0;i<size;i++) for(int j=0;j<size;j++) grid[i][j] = '#';
|
||||
setMines();
|
||||
}
|
||||
void revealAll(){
|
||||
|
||||
}
|
||||
bool revealCell(int row, int col, bool checkMine){
|
||||
for(int i=0;i<mines;i++){
|
||||
if(mineLocations[i]/size == row && mineLocations[i]%7 == col){
|
||||
revealAll();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
grid[row][col] = checkSurrounding(row, col) + '0';
|
||||
if(grid[row][col] == '0'){
|
||||
revealCell(row+1, col+1, false);
|
||||
revealCell(row, col+1, false);
|
||||
revealCell(row-1, col+1, false);
|
||||
revealCell(row-1, col, false);
|
||||
revealCell(row-1, col-1, false);
|
||||
revealCell(row, col-1, false);
|
||||
revealCell(row+1, col-1, false);
|
||||
revealCell(row+1, col, false);
|
||||
}
|
||||
return true;
|
||||
|
||||
}
|
||||
void flagCell(int row, int col){
|
||||
grid[row][col] = 'P';
|
||||
}
|
||||
int checkSurrounding(int row, int col){
|
||||
int count = 0;
|
||||
for(int i=0;i<mines;i++){
|
||||
if(mineLocations[i]/size == row+1 && mineLocations[i]%7 == col+1) count++;
|
||||
if(mineLocations[i]/size == row && mineLocations[i]%7 == col+1) count++;
|
||||
if(mineLocations[i]/size == row-1 && mineLocations[i]%7 == col+1) count++;
|
||||
if(mineLocations[i]/size == row-1 && mineLocations[i]%7 == col) count++;
|
||||
if(mineLocations[i]/size == row-1 && mineLocations[i]%7 == col-1) count++;
|
||||
if(mineLocations[i]/size == row && mineLocations[i]%7 == col-1) count++;
|
||||
if(mineLocations[i]/size == row+1 && mineLocations[i]%7 == col-1) count++;
|
||||
if(mineLocations[i]/size == row+1 && mineLocations[i]%7 == col) count++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
bool checkWin(){
|
||||
|
||||
}
|
||||
void setMines(){
|
||||
for(int i=0;i<mines;i++){
|
||||
bool valid;
|
||||
do{
|
||||
valid = true;
|
||||
mineLocations[i] = rand()%(size*size);
|
||||
for(int j=0;j<i;j++){
|
||||
if(mineLocations[j] == mineLocations[i]){
|
||||
valid = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}while(!valid);
|
||||
}
|
||||
}
|
||||
void displayGrid(){
|
||||
cout<<" ";
|
||||
for(int i=0;i<size;i++) cout<<i+1<<" ";
|
||||
cout<<endl;
|
||||
for(int i=0;i<size;i++){
|
||||
cout<<i+1<<" ";
|
||||
for(int j=0;j<size;j++) cout<<grid[i][j]<<" ";
|
||||
cout<<endl;
|
||||
}
|
||||
cout<<endl;
|
||||
|
||||
for(int i=0;i<mines;i++){
|
||||
cout<<mineLocations[i]<<" ";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
};
|
||||
|
||||
int main(){
|
||||
|
||||
Minesweeper game(7, 10);
|
||||
game.displayGrid();
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
156
.config/Code/User/History/55ccb45e/5uW9.cpp
Normal file
156
.config/Code/User/History/55ccb45e/5uW9.cpp
Normal file
|
|
@ -0,0 +1,156 @@
|
|||
/*
|
||||
Rafay Ahmad
|
||||
23I-2526
|
||||
*/
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
class Minesweeper{
|
||||
char** grid;
|
||||
int* mineLocations;
|
||||
int size;
|
||||
int mines;
|
||||
int opened;
|
||||
|
||||
public:
|
||||
|
||||
Minesweeper(int size, int mines) : size(size), mines(mines), opened(0){
|
||||
srand(time(0));
|
||||
grid = new char*[size];
|
||||
for(int i=0;i<size;i++){
|
||||
grid[i] = new char[size];
|
||||
for(int j=0;j<size;j++) grid[i][j] = '#';
|
||||
}
|
||||
mineLocations = new int[mines];
|
||||
setMines();
|
||||
}
|
||||
~Minesweeper(){
|
||||
for(int i=0;i<size;i++) delete[] grid[i];
|
||||
delete[] grid;
|
||||
delete[] mineLocations;
|
||||
}
|
||||
|
||||
void reset(){
|
||||
for(int i=0;i<size;i++) for(int j=0;j<size;j++) grid[i][j] = '#';
|
||||
setMines();
|
||||
}
|
||||
void revealAll(){
|
||||
for(int i=0;i<mines;i++){
|
||||
grid[mineLocations[i]/7][mineLocations[i]%7] = '*';
|
||||
}
|
||||
}
|
||||
bool revealCell(int row, int col, bool checkMine){
|
||||
if(checkMine){
|
||||
for(int i=0;i<mines;i++){
|
||||
if(mineLocations[i]/size == row && mineLocations[i]%size == col){
|
||||
grid[row][col] = '*';
|
||||
revealAll();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
grid[row][col] = checkSurrounding(row, col) + '0';
|
||||
opened++;
|
||||
if(grid[row][col] == '0'){
|
||||
if(row<size-1 && col<size-1) revealCell(row+1, col+1, false);
|
||||
if(col<size-1) revealCell(row, col+1, false);
|
||||
if(row>0 && col<size-1) revealCell(row-1, col+1, false);
|
||||
if(row>0) revealCell(row-1, col, false);
|
||||
if(row>0 && col>0) revealCell(row-1, col-1, false);
|
||||
if(col>0) revealCell(row, col-1, false);
|
||||
if(row<size-1 && col>0) revealCell(row+1, col-1, false);
|
||||
if(row<size-1) revealCell(row+1, col, false);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
void flagCell(int row, int col){
|
||||
grid[row][col] = 'P';
|
||||
}
|
||||
int checkSurrounding(int row, int col){
|
||||
int count = 0;
|
||||
for(int i=0;i<mines;i++){
|
||||
if(row<size && col<size && mineLocations[i]/size == row+1 && mineLocations[i]%size == col+1) count++;
|
||||
if(col<size && mineLocations[i]/size == row && mineLocations[i]%size == col+1) count++;
|
||||
if(row>0 && col<size && mineLocations[i]/size == row-1 && mineLocations[i]%size == col+1) count++;
|
||||
if(row>0 && mineLocations[i]/size == row-1 && mineLocations[i]%size == col) count++;
|
||||
if(row>0 && col>0 && mineLocations[i]/size == row-1 && mineLocations[i]%size == col-1) count++;
|
||||
if(col>0 && mineLocations[i]/size == row && mineLocations[i]%size == col-1) count++;
|
||||
if(row<size && col>0 && mineLocations[i]/size == row+1 && mineLocations[i]%size == col-1) count++;
|
||||
if(row<size && mineLocations[i]/size == row+1 && mineLocations[i]%size == col) count++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
bool checkWin(){
|
||||
return opened == ((size*size) - mines);
|
||||
}
|
||||
void setMines(){
|
||||
for(int i=0;i<mines;i++){
|
||||
bool valid;
|
||||
do{
|
||||
valid = true;
|
||||
mineLocations[i] = rand()%(size*size);
|
||||
for(int j=0;j<i;j++){
|
||||
if(mineLocations[j] == mineLocations[i]){
|
||||
valid = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}while(!valid);
|
||||
}
|
||||
}
|
||||
void displayGrid(){
|
||||
cout<<" ";
|
||||
for(int i=0;i<size;i++) cout<<i<<" ";
|
||||
cout<<endl;
|
||||
for(int i=0;i<size;i++){
|
||||
cout<<i<<" ";
|
||||
for(int j=0;j<size;j++) cout<<grid[i][j]<<" ";
|
||||
cout<<endl;
|
||||
}
|
||||
cout<<endl;
|
||||
|
||||
for(int i=0;i<mines;i++){
|
||||
cout<<mineLocations[i]<<" ";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
};
|
||||
|
||||
int main(){
|
||||
|
||||
int row, col;
|
||||
char c;
|
||||
int size, mines;
|
||||
cout<<"Enter size of the game board: ";
|
||||
cin>>size;
|
||||
cout<<"Enter no Of mines: ";
|
||||
cin>>mines;
|
||||
Minesweeper game(size, mines);
|
||||
|
||||
while(!game.checkWin()){
|
||||
game.displayGrid();
|
||||
cout<<"Enter row: ";
|
||||
cin>>row;
|
||||
cout<<"Enter col: ";
|
||||
cin>>col;
|
||||
cout<<"Enter F to flag or R to reveal: ";
|
||||
cin>>c;
|
||||
|
||||
if(row<size && col<size && (c == 'F' || c == 'R')){
|
||||
if(c == 'R' && !game.revealCell(row, col, true)){
|
||||
game.displayGrid();
|
||||
cout<<"You Lost\n";
|
||||
break;
|
||||
}
|
||||
else if(c == 'F') game.flagCell(row, col);
|
||||
}
|
||||
else cout<<"Invalid input\n";
|
||||
|
||||
}
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
156
.config/Code/User/History/55ccb45e/6Qz5.cpp
Normal file
156
.config/Code/User/History/55ccb45e/6Qz5.cpp
Normal file
|
|
@ -0,0 +1,156 @@
|
|||
/*
|
||||
Rafay Ahmad
|
||||
23I-2526
|
||||
*/
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
class Minesweeper{
|
||||
char** grid;
|
||||
int* mineLocations;
|
||||
int size;
|
||||
int mines;
|
||||
int opened;
|
||||
|
||||
public:
|
||||
|
||||
Minesweeper(int size, int mines) : size(size), mines(mines), opened(0){
|
||||
srand(time(0));
|
||||
grid = new char*[size];
|
||||
for(int i=0;i<size;i++){
|
||||
grid[i] = new char[size];
|
||||
for(int j=0;j<size;j++) grid[i][j] = '#';
|
||||
}
|
||||
mineLocations = new int[mines];
|
||||
setMines();
|
||||
}
|
||||
~Minesweeper(){
|
||||
for(int i=0;i<size;i++) delete[] grid[i];
|
||||
delete[] grid;
|
||||
delete[] mineLocations;
|
||||
}
|
||||
|
||||
void reset(){
|
||||
for(int i=0;i<size;i++) for(int j=0;j<size;j++) grid[i][j] = '#';
|
||||
setMines();
|
||||
}
|
||||
void revealAll(){
|
||||
for(int i=0;i<size;i++){
|
||||
for(int j=0;j<size;j++){
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
bool revealCell(int row, int col, bool checkMine){
|
||||
for(int i=0;i<mines;i++){
|
||||
if(mineLocations[i]/size == row && mineLocations[i]%7 == col){
|
||||
grid[row][col] = '*';
|
||||
revealAll();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
grid[row][col] = checkSurrounding(row, col) + '0';
|
||||
opened++;
|
||||
if(grid[row][col] == '0'){
|
||||
return revealCell(row+1, col+1, false);
|
||||
return revealCell(row, col+1, false);
|
||||
return revealCell(row-1, col+1, false);
|
||||
return revealCell(row-1, col, false);
|
||||
return revealCell(row-1, col-1, false);
|
||||
return revealCell(row, col-1, false);
|
||||
return revealCell(row+1, col-1, false);
|
||||
return revealCell(row+1, col, false);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
void flagCell(int row, int col){
|
||||
grid[row][col] = 'P';
|
||||
}
|
||||
int checkSurrounding(int row, int col){
|
||||
int count = 0;
|
||||
for(int i=0;i<mines;i++){
|
||||
if(mineLocations[i]/size == row+1 && mineLocations[i]%7 == col+1) count++;
|
||||
if(mineLocations[i]/size == row && mineLocations[i]%7 == col+1) count++;
|
||||
if(mineLocations[i]/size == row-1 && mineLocations[i]%7 == col+1) count++;
|
||||
if(mineLocations[i]/size == row-1 && mineLocations[i]%7 == col) count++;
|
||||
if(mineLocations[i]/size == row-1 && mineLocations[i]%7 == col-1) count++;
|
||||
if(mineLocations[i]/size == row && mineLocations[i]%7 == col-1) count++;
|
||||
if(mineLocations[i]/size == row+1 && mineLocations[i]%7 == col-1) count++;
|
||||
if(mineLocations[i]/size == row+1 && mineLocations[i]%7 == col) count++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
bool checkWin(){
|
||||
return opened == ((size*size) - mines);
|
||||
}
|
||||
void setMines(){
|
||||
for(int i=0;i<mines;i++){
|
||||
bool valid;
|
||||
do{
|
||||
valid = true;
|
||||
mineLocations[i] = rand()%(size*size);
|
||||
for(int j=0;j<i;j++){
|
||||
if(mineLocations[j] == mineLocations[i]){
|
||||
valid = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}while(!valid);
|
||||
}
|
||||
}
|
||||
void displayGrid(){
|
||||
cout<<" ";
|
||||
for(int i=0;i<size;i++) cout<<i+1<<" ";
|
||||
cout<<endl;
|
||||
for(int i=0;i<size;i++){
|
||||
cout<<i+1<<" ";
|
||||
for(int j=0;j<size;j++) cout<<grid[i][j]<<" ";
|
||||
cout<<endl;
|
||||
}
|
||||
cout<<endl;
|
||||
|
||||
for(int i=0;i<mines;i++){
|
||||
cout<<mineLocations[i]<<" ";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
};
|
||||
|
||||
int main(){
|
||||
|
||||
int row, col;
|
||||
char c;
|
||||
int size, mines;
|
||||
cout<<"Enter size of the game board: ";
|
||||
cin>>size;
|
||||
cout<<"Enter no Of mines: ";
|
||||
cin>>mines;
|
||||
Minesweeper game(size, mines);
|
||||
|
||||
while(!game.checkWin()){
|
||||
game.displayGrid();
|
||||
cout<<"Enter row: ";
|
||||
cin>>row;
|
||||
cout<<"Enter col: ";
|
||||
cin>>col;
|
||||
cout<<"Enter F to flag or R to reveal: ";
|
||||
cin>>c;
|
||||
|
||||
if(row<size && col<size && (c == 'F' || c == 'R')){
|
||||
if(c == 'R') if(!game.revealCell(row, col, true)){
|
||||
game.displayGrid();
|
||||
cout<<"You Lost\n";
|
||||
break;
|
||||
}
|
||||
else game.flagCell(row, col);
|
||||
}
|
||||
else cout<<"Invalid input\n";
|
||||
|
||||
}
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
154
.config/Code/User/History/55ccb45e/6yRR.cpp
Normal file
154
.config/Code/User/History/55ccb45e/6yRR.cpp
Normal file
|
|
@ -0,0 +1,154 @@
|
|||
/*
|
||||
Rafay Ahmad
|
||||
23I-2526
|
||||
*/
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
class Minesweeper{
|
||||
char** grid;
|
||||
int* mineLocations;
|
||||
int size;
|
||||
int mines;
|
||||
int opened;
|
||||
|
||||
public:
|
||||
|
||||
Minesweeper(int size, int mines) : size(size), mines(mines), opened(0){
|
||||
srand(time(0));
|
||||
grid = new char*[size];
|
||||
for(int i=0;i<size;i++){
|
||||
grid[i] = new char[size];
|
||||
for(int j=0;j<size;j++) grid[i][j] = '#';
|
||||
}
|
||||
mineLocations = new int[mines];
|
||||
setMines();
|
||||
}
|
||||
~Minesweeper(){
|
||||
for(int i=0;i<size;i++) delete[] grid[i];
|
||||
delete[] grid;
|
||||
delete[] mineLocations;
|
||||
}
|
||||
|
||||
void reset(){
|
||||
for(int i=0;i<size;i++) for(int j=0;j<size;j++) grid[i][j] = '#';
|
||||
setMines();
|
||||
}
|
||||
void revealAll(){
|
||||
for(int i=0;i<mines;i++){
|
||||
grid[mineLocations[i]/7][mineLocations[i]%7] = '*';
|
||||
}
|
||||
}
|
||||
bool revealCell(int row, int col, bool checkMine){
|
||||
for(int i=0;i<mines;i++){
|
||||
if(mineLocations[i]/size == row && mineLocations[i]%7 == col){
|
||||
grid[row][col] = '*';
|
||||
revealAll();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
grid[row][col] = checkSurrounding(row, col) + '0';
|
||||
opened++;
|
||||
if(grid[row][col] == '0'){
|
||||
return revealCell(row+1, col+1, false);
|
||||
return revealCell(row, col+1, false);
|
||||
return revealCell(row-1, col+1, false);
|
||||
return revealCell(row-1, col, false);
|
||||
return revealCell(row-1, col-1, false);
|
||||
return revealCell(row, col-1, false);
|
||||
return revealCell(row+1, col-1, false);
|
||||
return revealCell(row+1, col, false);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
void flagCell(int row, int col){
|
||||
grid[row][col] = 'P';
|
||||
}
|
||||
int checkSurrounding(int row, int col){
|
||||
int count = 0;
|
||||
for(int i=0;i<mines;i++){
|
||||
if(mineLocations[i]/size == row+1 && mineLocations[i]%7 == col+1) count++;
|
||||
if(mineLocations[i]/size == row && mineLocations[i]%7 == col+1) count++;
|
||||
if(mineLocations[i]/size == row-1 && mineLocations[i]%7 == col+1) count++;
|
||||
if(mineLocations[i]/size == row-1 && mineLocations[i]%7 == col) count++;
|
||||
if(mineLocations[i]/size == row-1 && mineLocations[i]%7 == col-1) count++;
|
||||
if(mineLocations[i]/size == row && mineLocations[i]%7 == col-1) count++;
|
||||
if(mineLocations[i]/size == row+1 && mineLocations[i]%7 == col-1) count++;
|
||||
if(mineLocations[i]/size == row+1 && mineLocations[i]%7 == col) count++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
bool checkWin(){
|
||||
return opened == ((size*size) - mines);
|
||||
}
|
||||
void setMines(){
|
||||
for(int i=0;i<mines;i++){
|
||||
bool valid;
|
||||
do{
|
||||
valid = true;
|
||||
mineLocations[i] = rand()%(size*size);
|
||||
for(int j=0;j<i;j++){
|
||||
if(mineLocations[j] == mineLocations[i]){
|
||||
valid = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}while(!valid);
|
||||
}
|
||||
}
|
||||
void displayGrid(){
|
||||
cout<<" ";
|
||||
for(int i=0;i<size;i++) cout<<i<<" ";
|
||||
cout<<endl;
|
||||
for(int i=0;i<size;i++){
|
||||
cout<<i<<" ";
|
||||
for(int j=0;j<size;j++) cout<<grid[i][j]<<" ";
|
||||
cout<<endl;
|
||||
}
|
||||
cout<<endl;
|
||||
|
||||
for(int i=0;i<mines;i++){
|
||||
cout<<mineLocations[i]<<" ";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
};
|
||||
|
||||
int main(){
|
||||
|
||||
int row, col;
|
||||
char c;
|
||||
int size, mines;
|
||||
cout<<"Enter size of the game board: ";
|
||||
cin>>size;
|
||||
cout<<"Enter no Of mines: ";
|
||||
cin>>mines;
|
||||
Minesweeper game(size, mines);
|
||||
|
||||
while(!game.checkWin()){
|
||||
game.displayGrid();
|
||||
cout<<"Enter row: ";
|
||||
cin>>row;
|
||||
cout<<"Enter col: ";
|
||||
cin>>col;
|
||||
cout<<"Enter F to flag or R to reveal: ";
|
||||
cin>>c;
|
||||
|
||||
if(row<size && col<size && (c == 'F' || c == 'R')){
|
||||
if(c == 'R' && !game.revealCell(row, col, true)){
|
||||
game.displayGrid();
|
||||
cout<<"You Lost\n";
|
||||
break;
|
||||
}
|
||||
else game.flagCell(row, col);
|
||||
}
|
||||
else cout<<"Invalid input\n";
|
||||
|
||||
}
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
156
.config/Code/User/History/55ccb45e/8B1I.cpp
Normal file
156
.config/Code/User/History/55ccb45e/8B1I.cpp
Normal file
|
|
@ -0,0 +1,156 @@
|
|||
/*
|
||||
Rafay Ahmad
|
||||
23I-2526
|
||||
*/
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
class Minesweeper{
|
||||
char** grid;
|
||||
int* mineLocations;
|
||||
int size;
|
||||
int mines;
|
||||
int opened;
|
||||
|
||||
public:
|
||||
|
||||
Minesweeper(int size, int mines) : size(size), mines(mines), opened(0){
|
||||
srand(time(0));
|
||||
grid = new char*[size];
|
||||
for(int i=0;i<size;i++){
|
||||
grid[i] = new char[size];
|
||||
for(int j=0;j<size;j++) grid[i][j] = '#';
|
||||
}
|
||||
mineLocations = new int[mines];
|
||||
setMines();
|
||||
}
|
||||
~Minesweeper(){
|
||||
for(int i=0;i<size;i++) delete[] grid[i];
|
||||
delete[] grid;
|
||||
delete[] mineLocations;
|
||||
}
|
||||
|
||||
void reset(){
|
||||
for(int i=0;i<size;i++) for(int j=0;j<size;j++) grid[i][j] = '#';
|
||||
setMines();
|
||||
}
|
||||
void revealAll(){
|
||||
for(int i=0;i<mines;i++){
|
||||
grid[mineLocations[i]/7][mineLocations[i]%7] = '*';
|
||||
}
|
||||
}
|
||||
bool revealCell(int row, int col, bool checkMine){
|
||||
if(checkMine){
|
||||
for(int i=0;i<mines;i++){
|
||||
if(mineLocations[i]/size == row && mineLocations[i]%7 == col){
|
||||
grid[row][col] = '*';
|
||||
revealAll();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
grid[row][col] = checkSurrounding(row, col) + '0';
|
||||
opened++;
|
||||
if(grid[row][col] == '0'){
|
||||
if(row<size && col<size) revealCell(row+1, col+1, false);
|
||||
if(col<size) revealCell(row, col+1, false);
|
||||
if(row>0 && col<size) revealCell(row-1, col+1, false);
|
||||
if(row>0) revealCell(row-1, col, false);
|
||||
if(row<size && col<size) revealCell(row-1, col-1, false);
|
||||
if(row<size && col<size) revealCell(row, col-1, false);
|
||||
if(row<size && col<size) revealCell(row+1, col-1, false);
|
||||
if(row<size && col<size) revealCell(row+1, col, false);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
void flagCell(int row, int col){
|
||||
grid[row][col] = 'P';
|
||||
}
|
||||
int checkSurrounding(int row, int col){
|
||||
int count = 0;
|
||||
for(int i=0;i<mines;i++){
|
||||
if(row<size && col<size && mineLocations[i]/size == row+1 && mineLocations[i]%7 == col+1) count++;
|
||||
if(col<size && mineLocations[i]/size == row && mineLocations[i]%7 == col+1) count++;
|
||||
if(row>0 && col<size && mineLocations[i]/size == row-1 && mineLocations[i]%7 == col+1) count++;
|
||||
if(row>0 && mineLocations[i]/size == row-1 && mineLocations[i]%7 == col) count++;
|
||||
if(row>0 && col>0 && mineLocations[i]/size == row-1 && mineLocations[i]%7 == col-1) count++;
|
||||
if(col>0 && mineLocations[i]/size == row && mineLocations[i]%7 == col-1) count++;
|
||||
if(row<size && col>0 && mineLocations[i]/size == row+1 && mineLocations[i]%7 == col-1) count++;
|
||||
if(row<size && mineLocations[i]/size == row+1 && mineLocations[i]%7 == col) count++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
bool checkWin(){
|
||||
return opened == ((size*size) - mines);
|
||||
}
|
||||
void setMines(){
|
||||
for(int i=0;i<mines;i++){
|
||||
bool valid;
|
||||
do{
|
||||
valid = true;
|
||||
mineLocations[i] = rand()%(size*size);
|
||||
for(int j=0;j<i;j++){
|
||||
if(mineLocations[j] == mineLocations[i]){
|
||||
valid = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}while(!valid);
|
||||
}
|
||||
}
|
||||
void displayGrid(){
|
||||
cout<<" ";
|
||||
for(int i=0;i<size;i++) cout<<i<<" ";
|
||||
cout<<endl;
|
||||
for(int i=0;i<size;i++){
|
||||
cout<<i<<" ";
|
||||
for(int j=0;j<size;j++) cout<<grid[i][j]<<" ";
|
||||
cout<<endl;
|
||||
}
|
||||
cout<<endl;
|
||||
|
||||
for(int i=0;i<mines;i++){
|
||||
cout<<mineLocations[i]<<" ";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
};
|
||||
|
||||
int main(){
|
||||
|
||||
int row, col;
|
||||
char c;
|
||||
int size, mines;
|
||||
cout<<"Enter size of the game board: ";
|
||||
cin>>size;
|
||||
cout<<"Enter no Of mines: ";
|
||||
cin>>mines;
|
||||
Minesweeper game(size, mines);
|
||||
|
||||
while(!game.checkWin()){
|
||||
game.displayGrid();
|
||||
cout<<"Enter row: ";
|
||||
cin>>row;
|
||||
cout<<"Enter col: ";
|
||||
cin>>col;
|
||||
cout<<"Enter F to flag or R to reveal: ";
|
||||
cin>>c;
|
||||
|
||||
if(row<size && col<size && (c == 'F' || c == 'R')){
|
||||
if(c == 'R' && !game.revealCell(row, col, true)){
|
||||
game.displayGrid();
|
||||
cout<<"You Lost\n";
|
||||
break;
|
||||
}
|
||||
else if(c == 'F') game.flagCell(row, col);
|
||||
}
|
||||
else cout<<"Invalid input\n";
|
||||
|
||||
}
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
156
.config/Code/User/History/55ccb45e/BcJ2.cpp
Normal file
156
.config/Code/User/History/55ccb45e/BcJ2.cpp
Normal file
|
|
@ -0,0 +1,156 @@
|
|||
/*
|
||||
Rafay Ahmad
|
||||
23I-2526
|
||||
*/
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
class Minesweeper{
|
||||
char** grid;
|
||||
int* mineLocations;
|
||||
int size;
|
||||
int mines;
|
||||
int opened;
|
||||
|
||||
public:
|
||||
|
||||
Minesweeper(int size, int mines) : size(size), mines(mines), opened(0){
|
||||
srand(time(0));
|
||||
grid = new char*[size];
|
||||
for(int i=0;i<size;i++){
|
||||
grid[i] = new char[size];
|
||||
for(int j=0;j<size;j++) grid[i][j] = '#';
|
||||
}
|
||||
mineLocations = new int[mines];
|
||||
setMines();
|
||||
}
|
||||
~Minesweeper(){
|
||||
for(int i=0;i<size;i++) delete[] grid[i];
|
||||
delete[] grid;
|
||||
delete[] mineLocations;
|
||||
}
|
||||
|
||||
void reset(){
|
||||
for(int i=0;i<size;i++) for(int j=0;j<size;j++) grid[i][j] = '#';
|
||||
setMines();
|
||||
}
|
||||
void revealAll(){
|
||||
for(int i=0;i<mines;i++){
|
||||
grid[mineLocations[i]/7][mineLocations[i]%7] = '*';
|
||||
}
|
||||
}
|
||||
bool revealCell(int row, int col, bool checkMine){
|
||||
if(checkMine){
|
||||
for(int i=0;i<mines;i++){
|
||||
if(mineLocations[i]/size == row && mineLocations[i]%7 == col){
|
||||
grid[row][col] = '*';
|
||||
revealAll();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
grid[row][col] = checkSurrounding(row, col) + '0';
|
||||
opened++;
|
||||
if(grid[row][col] == '0'){
|
||||
return revealCell(row+1, col+1, false);
|
||||
return revealCell(row, col+1, false);
|
||||
return revealCell(row-1, col+1, false);
|
||||
return revealCell(row-1, col, false);
|
||||
return revealCell(row-1, col-1, false);
|
||||
return revealCell(row, col-1, false);
|
||||
return revealCell(row+1, col-1, false);
|
||||
return revealCell(row+1, col, false);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
void flagCell(int row, int col){
|
||||
grid[row][col] = 'P';
|
||||
}
|
||||
int checkSurrounding(int row, int col){
|
||||
int count = 0;
|
||||
for(int i=0;i<mines;i++){
|
||||
if(row<size && mineLocations[i]/size == row+1 && mineLocations[i]%7 == col+1) count++;
|
||||
if(mineLocations[i]/size == row && mineLocations[i]%7 == col+1) count++;
|
||||
if(mineLocations[i]/size == row-1 && mineLocations[i]%7 == col+1) count++;
|
||||
if(mineLocations[i]/size == row-1 && mineLocations[i]%7 == col) count++;
|
||||
if(mineLocations[i]/size == row-1 && mineLocations[i]%7 == col-1) count++;
|
||||
if(mineLocations[i]/size == row && mineLocations[i]%7 == col-1) count++;
|
||||
if(mineLocations[i]/size == row+1 && mineLocations[i]%7 == col-1) count++;
|
||||
if(mineLocations[i]/size == row+1 && mineLocations[i]%7 == col) count++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
bool checkWin(){
|
||||
return opened == ((size*size) - mines);
|
||||
}
|
||||
void setMines(){
|
||||
for(int i=0;i<mines;i++){
|
||||
bool valid;
|
||||
do{
|
||||
valid = true;
|
||||
mineLocations[i] = rand()%(size*size);
|
||||
for(int j=0;j<i;j++){
|
||||
if(mineLocations[j] == mineLocations[i]){
|
||||
valid = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}while(!valid);
|
||||
}
|
||||
}
|
||||
void displayGrid(){
|
||||
cout<<" ";
|
||||
for(int i=0;i<size;i++) cout<<i<<" ";
|
||||
cout<<endl;
|
||||
for(int i=0;i<size;i++){
|
||||
cout<<i<<" ";
|
||||
for(int j=0;j<size;j++) cout<<grid[i][j]<<" ";
|
||||
cout<<endl;
|
||||
}
|
||||
cout<<endl;
|
||||
|
||||
for(int i=0;i<mines;i++){
|
||||
cout<<mineLocations[i]<<" ";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
};
|
||||
|
||||
int main(){
|
||||
|
||||
int row, col;
|
||||
char c;
|
||||
int size, mines;
|
||||
cout<<"Enter size of the game board: ";
|
||||
cin>>size;
|
||||
cout<<"Enter no Of mines: ";
|
||||
cin>>mines;
|
||||
Minesweeper game(size, mines);
|
||||
|
||||
while(!game.checkWin()){
|
||||
game.displayGrid();
|
||||
cout<<"Enter row: ";
|
||||
cin>>row;
|
||||
cout<<"Enter col: ";
|
||||
cin>>col;
|
||||
cout<<"Enter F to flag or R to reveal: ";
|
||||
cin>>c;
|
||||
|
||||
if(row<size && col<size && (c == 'F' || c == 'R')){
|
||||
if(c == 'R' && !game.revealCell(row, col, true)){
|
||||
game.displayGrid();
|
||||
cout<<"You Lost\n";
|
||||
break;
|
||||
}
|
||||
else if(c == 'F') game.flagCell(row, col);
|
||||
}
|
||||
else cout<<"Invalid input\n";
|
||||
|
||||
}
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
156
.config/Code/User/History/55ccb45e/CSWc.cpp
Normal file
156
.config/Code/User/History/55ccb45e/CSWc.cpp
Normal file
|
|
@ -0,0 +1,156 @@
|
|||
/*
|
||||
Rafay Ahmad
|
||||
23I-2526
|
||||
*/
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
class Minesweeper{
|
||||
char** grid;
|
||||
int* mineLocations;
|
||||
int size;
|
||||
int mines;
|
||||
int opened;
|
||||
|
||||
public:
|
||||
|
||||
Minesweeper(int size, int mines) : size(size), mines(mines), opened(0){
|
||||
srand(time(0));
|
||||
grid = new char*[size];
|
||||
for(int i=0;i<size;i++){
|
||||
grid[i] = new char[size];
|
||||
for(int j=0;j<size;j++) grid[i][j] = '#';
|
||||
}
|
||||
mineLocations = new int[mines];
|
||||
setMines();
|
||||
}
|
||||
~Minesweeper(){
|
||||
for(int i=0;i<size;i++) delete[] grid[i];
|
||||
delete[] grid;
|
||||
delete[] mineLocations;
|
||||
}
|
||||
|
||||
void reset(){
|
||||
for(int i=0;i<size;i++) for(int j=0;j<size;j++) grid[i][j] = '#';
|
||||
setMines();
|
||||
}
|
||||
void revealAll(){
|
||||
for(int i=0;i<mines;i++){
|
||||
grid[mineLocations[i]/7][mineLocations[i]%7] = '*';
|
||||
}
|
||||
}
|
||||
bool revealCell(int row, int col, bool checkMine){
|
||||
if(checkMine){
|
||||
for(int i=0;i<mines;i++){
|
||||
if(mineLocations[i]/size == row && mineLocations[i]%7 == col){
|
||||
grid[row][col] = '*';
|
||||
revealAll();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
grid[row][col] = checkSurrounding(row, col) + '0';
|
||||
opened++;
|
||||
if(grid[row][col] == '0'){
|
||||
if(row<size-1 && col<size-1) revealCell(row+1, col+1, false);
|
||||
if(col<size-1) revealCell(row, col+1, false);
|
||||
if(row>0 && col<size-1) revealCell(row-1, col+1, false);
|
||||
if(row>0) revealCell(row-1, col, false);
|
||||
if(row>0 && col>0) revealCell(row-1, col-1, false);
|
||||
if(col>0) revealCell(row, col-1, false);
|
||||
if(row<size-1 && col>0) revealCell(row+1, col-1, false);
|
||||
if(row<size-1) revealCell(row+1, col, false);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
void flagCell(int row, int col){
|
||||
grid[row][col] = 'P';
|
||||
}
|
||||
int checkSurrounding(int row, int col){
|
||||
int count = 0;
|
||||
for(int i=0;i<mines;i++){
|
||||
if(row<size && col<size && mineLocations[i]/size == row+1 && mineLocations[i]%7 == col+1) count++;
|
||||
if(col<size && mineLocations[i]/size == row && mineLocations[i]%7 == col+1) count++;
|
||||
if(row>0 && col<size && mineLocations[i]/size == row-1 && mineLocations[i]%7 == col+1) count++;
|
||||
if(row>0 && mineLocations[i]/size == row-1 && mineLocations[i]%7 == col) count++;
|
||||
if(row>0 && col>0 && mineLocations[i]/size == row-1 && mineLocations[i]%7 == col-1) count++;
|
||||
if(col>0 && mineLocations[i]/size == row && mineLocations[i]%7 == col-1) count++;
|
||||
if(row<size && col>0 && mineLocations[i]/size == row+1 && mineLocations[i]%7 == col-1) count++;
|
||||
if(row<size && mineLocations[i]/size == row+1 && mineLocations[i]%7 == col) count++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
bool checkWin(){
|
||||
return opened == ((size*size) - mines);
|
||||
}
|
||||
void setMines(){
|
||||
for(int i=0;i<mines;i++){
|
||||
bool valid;
|
||||
do{
|
||||
valid = true;
|
||||
mineLocations[i] = rand()%(size*size);
|
||||
for(int j=0;j<i;j++){
|
||||
if(mineLocations[j] == mineLocations[i]){
|
||||
valid = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}while(!valid);
|
||||
}
|
||||
}
|
||||
void displayGrid(){
|
||||
cout<<" ";
|
||||
for(int i=0;i<size;i++) cout<<i<<" ";
|
||||
cout<<endl;
|
||||
for(int i=0;i<size;i++){
|
||||
cout<<i<<" ";
|
||||
for(int j=0;j<size;j++) cout<<grid[i][j]<<" ";
|
||||
cout<<endl;
|
||||
}
|
||||
cout<<endl;
|
||||
|
||||
for(int i=0;i<mines;i++){
|
||||
cout<<mineLocations[i]<<" ";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
};
|
||||
|
||||
int main(){
|
||||
|
||||
int row, col;
|
||||
char c;
|
||||
int size, mines;
|
||||
cout<<"Enter size of the game board: ";
|
||||
cin>>size;
|
||||
cout<<"Enter no Of mines: ";
|
||||
cin>>mines;
|
||||
Minesweeper game(size, mines);
|
||||
|
||||
while(!game.checkWin()){
|
||||
game.displayGrid();
|
||||
cout<<"Enter row: ";
|
||||
cin>>row;
|
||||
cout<<"Enter col: ";
|
||||
cin>>col;
|
||||
cout<<"Enter F to flag or R to reveal: ";
|
||||
cin>>c;
|
||||
|
||||
if(row<size && col<size && (c == 'F' || c == 'R')){
|
||||
if(c == 'R' && !game.revealCell(row, col, true)){
|
||||
game.displayGrid();
|
||||
cout<<"You Lost\n";
|
||||
break;
|
||||
}
|
||||
else if(c == 'F') game.flagCell(row, col);
|
||||
}
|
||||
else cout<<"Invalid input\n";
|
||||
|
||||
}
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
154
.config/Code/User/History/55ccb45e/D0jW.cpp
Normal file
154
.config/Code/User/History/55ccb45e/D0jW.cpp
Normal file
|
|
@ -0,0 +1,154 @@
|
|||
/*
|
||||
Rafay Ahmad
|
||||
23I-2526
|
||||
*/
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
class Minesweeper{
|
||||
char** grid;
|
||||
int* mineLocations;
|
||||
int size;
|
||||
int mines;
|
||||
int opened;
|
||||
|
||||
public:
|
||||
|
||||
Minesweeper(int size, int mines) : size(size), mines(mines), opened(0){
|
||||
srand(time(0));
|
||||
grid = new char*[size];
|
||||
for(int i=0;i<size;i++){
|
||||
grid[i] = new char[size];
|
||||
for(int j=0;j<size;j++) grid[i][j] = '#';
|
||||
}
|
||||
mineLocations = new int[mines];
|
||||
setMines();
|
||||
}
|
||||
~Minesweeper(){
|
||||
for(int i=0;i<size;i++) delete[] grid[i];
|
||||
delete[] grid;
|
||||
delete[] mineLocations;
|
||||
}
|
||||
|
||||
void reset(){
|
||||
for(int i=0;i<size;i++) for(int j=0;j<size;j++) grid[i][j] = '#';
|
||||
setMines();
|
||||
}
|
||||
void revealAll(){
|
||||
for(int i=0;i<mines;i++){
|
||||
grid[mineLocations[i]/7][mineLocations[i]%7] = '*';
|
||||
}
|
||||
}
|
||||
bool revealCell(int row, int col, bool checkMine){
|
||||
for(int i=0;i<mines;i++){
|
||||
if(mineLocations[i]/size == row && mineLocations[i]%7 == col){
|
||||
grid[row][col] = '*';
|
||||
revealAll();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
grid[row][col] = checkSurrounding(row, col) + '0';
|
||||
opened++;
|
||||
if(grid[row][col] == '0'){
|
||||
return revealCell(row+1, col+1, false);
|
||||
return revealCell(row, col+1, false);
|
||||
return revealCell(row-1, col+1, false);
|
||||
return revealCell(row-1, col, false);
|
||||
return revealCell(row-1, col-1, false);
|
||||
return revealCell(row, col-1, false);
|
||||
return revealCell(row+1, col-1, false);
|
||||
return revealCell(row+1, col, false);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
void flagCell(int row, int col){
|
||||
grid[row][col] = 'P';
|
||||
}
|
||||
int checkSurrounding(int row, int col){
|
||||
int count = 0;
|
||||
for(int i=0;i<mines;i++){
|
||||
if(mineLocations[i]/size == row+1 && mineLocations[i]%7 == col+1) count++;
|
||||
if(mineLocations[i]/size == row && mineLocations[i]%7 == col+1) count++;
|
||||
if(mineLocations[i]/size == row-1 && mineLocations[i]%7 == col+1) count++;
|
||||
if(mineLocations[i]/size == row-1 && mineLocations[i]%7 == col) count++;
|
||||
if(mineLocations[i]/size == row-1 && mineLocations[i]%7 == col-1) count++;
|
||||
if(mineLocations[i]/size == row && mineLocations[i]%7 == col-1) count++;
|
||||
if(mineLocations[i]/size == row+1 && mineLocations[i]%7 == col-1) count++;
|
||||
if(mineLocations[i]/size == row+1 && mineLocations[i]%7 == col) count++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
bool checkWin(){
|
||||
return opened == ((size*size) - mines);
|
||||
}
|
||||
void setMines(){
|
||||
for(int i=0;i<mines;i++){
|
||||
bool valid;
|
||||
do{
|
||||
valid = true;
|
||||
mineLocations[i] = rand()%(size*size);
|
||||
for(int j=0;j<i;j++){
|
||||
if(mineLocations[j] == mineLocations[i]){
|
||||
valid = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}while(!valid);
|
||||
}
|
||||
}
|
||||
void displayGrid(){
|
||||
cout<<" ";
|
||||
for(int i=0;i<size;i++) cout<<i<<" ";
|
||||
cout<<endl;
|
||||
for(int i=0;i<size;i++){
|
||||
cout<<i<<" ";
|
||||
for(int j=0;j<size;j++) cout<<grid[i][j]<<" ";
|
||||
cout<<endl;
|
||||
}
|
||||
cout<<endl;
|
||||
|
||||
for(int i=0;i<mines;i++){
|
||||
cout<<mineLocations[i]<<" ";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
};
|
||||
|
||||
int main(){
|
||||
|
||||
int row, col;
|
||||
char c;
|
||||
int size, mines;
|
||||
cout<<"Enter size of the game board: ";
|
||||
cin>>size;
|
||||
cout<<"Enter no Of mines: ";
|
||||
cin>>mines;
|
||||
Minesweeper game(size, mines);
|
||||
|
||||
while(!game.checkWin()){
|
||||
game.displayGrid();
|
||||
cout<<"Enter row: ";
|
||||
cin>>row;
|
||||
cout<<"Enter col: ";
|
||||
cin>>col;
|
||||
cout<<"Enter F to flag or R to reveal: ";
|
||||
cin>>c;
|
||||
|
||||
if(row<size && col<size && (c == 'F' || c == 'R')){
|
||||
if(c == 'R' && !game.revealCell(row, col, true)){
|
||||
game.displayGrid();
|
||||
cout<<"You Lost\n";
|
||||
break;
|
||||
}
|
||||
else if(c == 'F') game.flagCell(row, col);
|
||||
}
|
||||
else cout<<"Invalid input\n";
|
||||
|
||||
}
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
153
.config/Code/User/History/55ccb45e/FY8l.cpp
Normal file
153
.config/Code/User/History/55ccb45e/FY8l.cpp
Normal file
|
|
@ -0,0 +1,153 @@
|
|||
/*
|
||||
Rafay Ahmad
|
||||
23I-2526
|
||||
*/
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
class Minesweeper{
|
||||
char** grid;
|
||||
int* mineLocations;
|
||||
int size;
|
||||
int mines;
|
||||
int opened;
|
||||
|
||||
public:
|
||||
|
||||
Minesweeper(int size, int mines) : size(size), mines(mines), opened(0){
|
||||
srand(time(0));
|
||||
grid = new char*[size];
|
||||
for(int i=0;i<size;i++){
|
||||
grid[i] = new char[size];
|
||||
for(int j=0;j<size;j++) grid[i][j] = '#';
|
||||
}
|
||||
mineLocations = new int[mines];
|
||||
setMines();
|
||||
}
|
||||
~Minesweeper(){
|
||||
for(int i=0;i<size;i++) delete[] grid[i];
|
||||
delete[] grid;
|
||||
delete[] mineLocations;
|
||||
}
|
||||
|
||||
void reset(){
|
||||
for(int i=0;i<size;i++) for(int j=0;j<size;j++) grid[i][j] = '#';
|
||||
setMines();
|
||||
}
|
||||
void revealAll(){
|
||||
for(int i=0;i<size;i++){
|
||||
for(int j=0;j<size;j++){
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
bool revealCell(int row, int col, bool checkMine){
|
||||
for(int i=0;i<mines;i++){
|
||||
if(mineLocations[i]/size == row && mineLocations[i]%7 == col){
|
||||
grid[row][col] = '*';
|
||||
revealAll();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
grid[row][col] = checkSurrounding(row, col) + '0';
|
||||
opened++;
|
||||
if(grid[row][col] == '0'){
|
||||
return revealCell(row+1, col+1, false);
|
||||
return revealCell(row, col+1, false);
|
||||
return revealCell(row-1, col+1, false);
|
||||
return revealCell(row-1, col, false);
|
||||
return revealCell(row-1, col-1, false);
|
||||
return revealCell(row, col-1, false);
|
||||
return revealCell(row+1, col-1, false);
|
||||
return revealCell(row+1, col, false);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
void flagCell(int row, int col){
|
||||
grid[row][col] = 'P';
|
||||
}
|
||||
int checkSurrounding(int row, int col){
|
||||
int count = 0;
|
||||
for(int i=0;i<mines;i++){
|
||||
if(mineLocations[i]/size == row+1 && mineLocations[i]%7 == col+1) count++;
|
||||
if(mineLocations[i]/size == row && mineLocations[i]%7 == col+1) count++;
|
||||
if(mineLocations[i]/size == row-1 && mineLocations[i]%7 == col+1) count++;
|
||||
if(mineLocations[i]/size == row-1 && mineLocations[i]%7 == col) count++;
|
||||
if(mineLocations[i]/size == row-1 && mineLocations[i]%7 == col-1) count++;
|
||||
if(mineLocations[i]/size == row && mineLocations[i]%7 == col-1) count++;
|
||||
if(mineLocations[i]/size == row+1 && mineLocations[i]%7 == col-1) count++;
|
||||
if(mineLocations[i]/size == row+1 && mineLocations[i]%7 == col) count++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
bool checkWin(){
|
||||
return opened == (size*size) - mines;
|
||||
}
|
||||
void setMines(){
|
||||
for(int i=0;i<mines;i++){
|
||||
bool valid;
|
||||
do{
|
||||
valid = true;
|
||||
mineLocations[i] = rand()%(size*size);
|
||||
for(int j=0;j<i;j++){
|
||||
if(mineLocations[j] == mineLocations[i]){
|
||||
valid = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}while(!valid);
|
||||
}
|
||||
}
|
||||
void displayGrid(){
|
||||
cout<<" ";
|
||||
for(int i=0;i<size;i++) cout<<i+1<<" ";
|
||||
cout<<endl;
|
||||
for(int i=0;i<size;i++){
|
||||
cout<<i+1<<" ";
|
||||
for(int j=0;j<size;j++) cout<<grid[i][j]<<" ";
|
||||
cout<<endl;
|
||||
}
|
||||
cout<<endl;
|
||||
|
||||
for(int i=0;i<mines;i++){
|
||||
cout<<mineLocations[i]<<" ";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
};
|
||||
|
||||
int main(){
|
||||
|
||||
int row, col;
|
||||
char c;
|
||||
int size, mines;
|
||||
cout<<"Enter size of the game board: ";
|
||||
cin>>size;
|
||||
cout<<"Enter no Of mines: ";
|
||||
cin>>mines;
|
||||
Minesweeper game(size, mines);
|
||||
|
||||
while(game.checkWin()){
|
||||
game.displayGrid();
|
||||
cout<<"Enter row: ";
|
||||
cin>>row;
|
||||
cout<<"Enter col: ";
|
||||
cin>>col;
|
||||
cout<<"Enter F to flag or R to reveal: ";
|
||||
cin>>c;
|
||||
|
||||
if(row<size && col<size && (c == 'F' || c == 'R')){
|
||||
game.revealCell(row, col, true);
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
156
.config/Code/User/History/55ccb45e/HDYg.cpp
Normal file
156
.config/Code/User/History/55ccb45e/HDYg.cpp
Normal file
|
|
@ -0,0 +1,156 @@
|
|||
/*
|
||||
Rafay Ahmad
|
||||
23I-2526
|
||||
*/
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
class Minesweeper{
|
||||
char** grid;
|
||||
int* mineLocations;
|
||||
int size;
|
||||
int mines;
|
||||
int opened;
|
||||
|
||||
public:
|
||||
|
||||
Minesweeper(int size, int mines) : size(size), mines(mines), opened(0){
|
||||
srand(time(0));
|
||||
grid = new char*[size];
|
||||
for(int i=0;i<size;i++){
|
||||
grid[i] = new char[size];
|
||||
for(int j=0;j<size;j++) grid[i][j] = '#';
|
||||
}
|
||||
mineLocations = new int[mines];
|
||||
setMines();
|
||||
}
|
||||
~Minesweeper(){
|
||||
for(int i=0;i<size;i++) delete[] grid[i];
|
||||
delete[] grid;
|
||||
delete[] mineLocations;
|
||||
}
|
||||
|
||||
void reset(){
|
||||
for(int i=0;i<size;i++) for(int j=0;j<size;j++) grid[i][j] = '#';
|
||||
setMines();
|
||||
}
|
||||
void revealAll(){
|
||||
for(int i=0;i<mines;i++){
|
||||
grid[mineLocations[i]/7][mineLocations[i]%7] = '*';
|
||||
}
|
||||
}
|
||||
bool revealCell(int row, int col, bool checkMine){
|
||||
if(checkMine){
|
||||
for(int i=0;i<mines;i++){
|
||||
if(mineLocations[i]/size == row && mineLocations[i]%7 == col){
|
||||
grid[row][col] = '*';
|
||||
revealAll();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
grid[row][col] = checkSurrounding(row, col) + '0';
|
||||
opened++;
|
||||
if(grid[row][col] == '0'){
|
||||
if(row<size && col<size) revealCell(row+1, col+1, false);
|
||||
if(col<size) revealCell(row, col+1, false);
|
||||
if(row>0 && col<size) revealCell(row-1, col+1, false);
|
||||
if(row>0) revealCell(row-1, col, false);
|
||||
if(row>0 && col>0) revealCell(row-1, col-1, false);
|
||||
if(col>0) revealCell(row, col-1, false);
|
||||
if(row<size && col>0) revealCell(row+1, col-1, false);
|
||||
if(row<size) revealCell(row+1, col, false);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
void flagCell(int row, int col){
|
||||
grid[row][col] = 'P';
|
||||
}
|
||||
int checkSurrounding(int row, int col){
|
||||
int count = 0;
|
||||
for(int i=0;i<mines;i++){
|
||||
if(row<size && col<size && mineLocations[i]/size == row+1 && mineLocations[i]%7 == col+1) count++;
|
||||
if(col<size && mineLocations[i]/size == row && mineLocations[i]%7 == col+1) count++;
|
||||
if(row>0 && col<size && mineLocations[i]/size == row-1 && mineLocations[i]%7 == col+1) count++;
|
||||
if(row>0 && mineLocations[i]/size == row-1 && mineLocations[i]%7 == col) count++;
|
||||
if(row>0 && col>0 && mineLocations[i]/size == row-1 && mineLocations[i]%7 == col-1) count++;
|
||||
if(col>0 && mineLocations[i]/size == row && mineLocations[i]%7 == col-1) count++;
|
||||
if(row<size && col>0 && mineLocations[i]/size == row+1 && mineLocations[i]%7 == col-1) count++;
|
||||
if(row<size && mineLocations[i]/size == row+1 && mineLocations[i]%7 == col) count++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
bool checkWin(){
|
||||
return opened == ((size*size) - mines);
|
||||
}
|
||||
void setMines(){
|
||||
for(int i=0;i<mines;i++){
|
||||
bool valid;
|
||||
do{
|
||||
valid = true;
|
||||
mineLocations[i] = rand()%(size*size);
|
||||
for(int j=0;j<i;j++){
|
||||
if(mineLocations[j] == mineLocations[i]){
|
||||
valid = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}while(!valid);
|
||||
}
|
||||
}
|
||||
void displayGrid(){
|
||||
cout<<" ";
|
||||
for(int i=0;i<size;i++) cout<<i<<" ";
|
||||
cout<<endl;
|
||||
for(int i=0;i<size;i++){
|
||||
cout<<i<<" ";
|
||||
for(int j=0;j<size;j++) cout<<grid[i][j]<<" ";
|
||||
cout<<endl;
|
||||
}
|
||||
cout<<endl;
|
||||
|
||||
for(int i=0;i<mines;i++){
|
||||
cout<<mineLocations[i]<<" ";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
};
|
||||
|
||||
int main(){
|
||||
|
||||
int row, col;
|
||||
char c;
|
||||
int size, mines;
|
||||
cout<<"Enter size of the game board: ";
|
||||
cin>>size;
|
||||
cout<<"Enter no Of mines: ";
|
||||
cin>>mines;
|
||||
Minesweeper game(size, mines);
|
||||
|
||||
while(!game.checkWin()){
|
||||
game.displayGrid();
|
||||
cout<<"Enter row: ";
|
||||
cin>>row;
|
||||
cout<<"Enter col: ";
|
||||
cin>>col;
|
||||
cout<<"Enter F to flag or R to reveal: ";
|
||||
cin>>c;
|
||||
|
||||
if(row<size && col<size && (c == 'F' || c == 'R')){
|
||||
if(c == 'R' && !game.revealCell(row, col, true)){
|
||||
game.displayGrid();
|
||||
cout<<"You Lost\n";
|
||||
break;
|
||||
}
|
||||
else if(c == 'F') game.flagCell(row, col);
|
||||
}
|
||||
else cout<<"Invalid input\n";
|
||||
|
||||
}
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
164
.config/Code/User/History/55ccb45e/HkvZ.cpp
Normal file
164
.config/Code/User/History/55ccb45e/HkvZ.cpp
Normal file
|
|
@ -0,0 +1,164 @@
|
|||
/*
|
||||
Rafay Ahmad
|
||||
23I-2526
|
||||
*/
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
class Minesweeper{
|
||||
char** grid;
|
||||
int* mineLocations;
|
||||
int size;
|
||||
int mines;
|
||||
int opened;
|
||||
|
||||
public:
|
||||
|
||||
Minesweeper(int size, int mines) : size(size), mines(mines), opened(0){
|
||||
srand(time(0));
|
||||
grid = new char*[size];
|
||||
for(int i=0;i<size;i++){
|
||||
grid[i] = new char[size];
|
||||
for(int j=0;j<size;j++) grid[i][j] = '#';
|
||||
}
|
||||
mineLocations = new int[mines];
|
||||
setMines();
|
||||
}
|
||||
~Minesweeper(){
|
||||
for(int i=0;i<size;i++) delete[] grid[i];
|
||||
delete[] grid;
|
||||
delete[] mineLocations;
|
||||
}
|
||||
|
||||
void reset(){
|
||||
for(int i=0;i<size;i++) for(int j=0;j<size;j++) grid[i][j] = '#';
|
||||
setMines();
|
||||
}
|
||||
void revealAll(){
|
||||
for(int i=0;i<mines;i++){
|
||||
grid[mineLocations[i]/7][mineLocations[i]%7] = '*';
|
||||
}
|
||||
}
|
||||
bool revealCell(int row, int col, bool checkMine){
|
||||
if(checkMine){
|
||||
for(int i=0;i<mines;i++){
|
||||
if(mineLocations[i]/size == row && mineLocations[i]%size == col){
|
||||
grid[row][col] = '*';
|
||||
revealAll();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
grid[row][col] = checkSurrounding(row, col) + '0';
|
||||
opened++;
|
||||
if(grid[row][col] == '0'){
|
||||
if(row<size-1 && col<size-1 && grid[row+1][col+1] == '#') revealCell(row+1, col+1, false);
|
||||
if(col<size-1 && grid[row][col+1] == '#') revealCell(row, col+1, false);
|
||||
if(row>0 && col<size-1 && grid[row-1][col+1] == '#') revealCell(row-1, col+1, false);
|
||||
if(row>0 && grid[row-1][col] == '#') revealCell(row-1, col, false);
|
||||
if(row>0 && col>0 && grid[row-1][col-1] == '#') revealCell(row-1, col-1, false);
|
||||
if(col>0 && grid[row][col-1] == '#') revealCell(row, col-1, false);
|
||||
if(row<size-1 && col>0 && grid[row+1][col-1] == '#') revealCell(row+1, col-1, false);
|
||||
if(row<size-1 && grid[row+1][col] == '#') revealCell(row+1, col, false);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
void flagCell(int row, int col){
|
||||
grid[row][col] = 'P';
|
||||
}
|
||||
int checkSurrounding(int row, int col){
|
||||
int count = 0;
|
||||
for(int i=0;i<mines;i++){
|
||||
if(row<size && col<size && mineLocations[i]/size == row+1 && mineLocations[i]%size == col+1) count++;
|
||||
if(col<size && mineLocations[i]/size == row && mineLocations[i]%size == col+1) count++;
|
||||
if(row>0 && col<size && mineLocations[i]/size == row-1 && mineLocations[i]%size == col+1) count++;
|
||||
if(row>0 && mineLocations[i]/size == row-1 && mineLocations[i]%size == col) count++;
|
||||
if(row>0 && col>0 && mineLocations[i]/size == row-1 && mineLocations[i]%size == col-1) count++;
|
||||
if(col>0 && mineLocations[i]/size == row && mineLocations[i]%size == col-1) count++;
|
||||
if(row<size && col>0 && mineLocations[i]/size == row+1 && mineLocations[i]%size == col-1) count++;
|
||||
if(row<size && mineLocations[i]/size == row+1 && mineLocations[i]%size == col) count++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
bool checkWin(){
|
||||
return opened == ((size*size) - mines);
|
||||
}
|
||||
void setMines(){
|
||||
for(int i=0;i<mines;i++){
|
||||
bool valid;
|
||||
do{
|
||||
valid = true;
|
||||
mineLocations[i] = rand()%(size*size);
|
||||
for(int j=0;j<i;j++){
|
||||
if(mineLocations[j] == mineLocations[i]){
|
||||
valid = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}while(!valid);
|
||||
}
|
||||
}
|
||||
void displayGrid(){
|
||||
cout<<" ";
|
||||
for(int i=0;i<size;i++) cout<<i<<" ";
|
||||
cout<<endl;
|
||||
for(int i=0;i<size;i++){
|
||||
cout<<i<<" ";
|
||||
for(int j=0;j<size;j++) cout<<grid[i][j]<<" ";
|
||||
cout<<endl;
|
||||
}
|
||||
cout<<endl;
|
||||
|
||||
for(int i=0;i<mines;i++){
|
||||
cout<<mineLocations[i]<<" ";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
};
|
||||
|
||||
int main(){
|
||||
|
||||
int row, col;
|
||||
char c;
|
||||
int size, mines;
|
||||
cout<<"Enter size of the game board: ";
|
||||
cin>>size;
|
||||
cout<<"Enter no Of mines: ";
|
||||
cin>>mines;
|
||||
Minesweeper game(size, mines);
|
||||
|
||||
bool win = true;
|
||||
|
||||
while(!game.checkWin()){
|
||||
game.displayGrid();
|
||||
cout<<"Enter row: ";
|
||||
cin>>row;
|
||||
cout<<"Enter col: ";
|
||||
cin>>col;
|
||||
cout<<"Enter F to flag or R to reveal: ";
|
||||
cin>>c;
|
||||
|
||||
if(row<size && col<size && (c == 'F' || c == 'R')){
|
||||
if(c == 'R' && !game.revealCell(row, col, true)){
|
||||
game.displayGrid();
|
||||
win = false;
|
||||
cout<<"You Lost\n";
|
||||
break;
|
||||
}
|
||||
else if(c == 'F') game.flagCell(row, col);
|
||||
}
|
||||
else cout<<"Invalid input\n";
|
||||
|
||||
}
|
||||
|
||||
if(win){
|
||||
game.displayGrid();
|
||||
cout<<"You Won!\n";
|
||||
}
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
156
.config/Code/User/History/55ccb45e/J3Mn.cpp
Normal file
156
.config/Code/User/History/55ccb45e/J3Mn.cpp
Normal file
|
|
@ -0,0 +1,156 @@
|
|||
/*
|
||||
Rafay Ahmad
|
||||
23I-2526
|
||||
*/
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
class Minesweeper{
|
||||
char** grid;
|
||||
int* mineLocations;
|
||||
int size;
|
||||
int mines;
|
||||
int opened;
|
||||
|
||||
public:
|
||||
|
||||
Minesweeper(int size, int mines) : size(size), mines(mines), opened(0){
|
||||
srand(time(0));
|
||||
grid = new char*[size];
|
||||
for(int i=0;i<size;i++){
|
||||
grid[i] = new char[size];
|
||||
for(int j=0;j<size;j++) grid[i][j] = '#';
|
||||
}
|
||||
mineLocations = new int[mines];
|
||||
setMines();
|
||||
}
|
||||
~Minesweeper(){
|
||||
for(int i=0;i<size;i++) delete[] grid[i];
|
||||
delete[] grid;
|
||||
delete[] mineLocations;
|
||||
}
|
||||
|
||||
void reset(){
|
||||
for(int i=0;i<size;i++) for(int j=0;j<size;j++) grid[i][j] = '#';
|
||||
setMines();
|
||||
}
|
||||
void revealAll(){
|
||||
for(int i=0;i<mines;i++){
|
||||
grid[mineLocations[i]/7][mineLocations[i]%7] = '*';
|
||||
}
|
||||
}
|
||||
bool revealCell(int row, int col, bool checkMine){
|
||||
if(checkMine){
|
||||
for(int i=0;i<mines;i++){
|
||||
if(mineLocations[i]/size == row && mineLocations[i]%7 == col){
|
||||
grid[row][col] = '*';
|
||||
revealAll();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
grid[row][col] = checkSurrounding(row, col) + '0';
|
||||
opened++;
|
||||
if(grid[row][col] == '0'){
|
||||
return revealCell(row+1, col+1, false);
|
||||
return revealCell(row, col+1, false);
|
||||
return revealCell(row-1, col+1, false);
|
||||
return revealCell(row-1, col, false);
|
||||
return revealCell(row-1, col-1, false);
|
||||
return revealCell(row, col-1, false);
|
||||
return revealCell(row+1, col-1, false);
|
||||
return revealCell(row+1, col, false);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
void flagCell(int row, int col){
|
||||
grid[row][col] = 'P';
|
||||
}
|
||||
int checkSurrounding(int row, int col){
|
||||
int count = 0;
|
||||
for(int i=0;i<mines;i++){
|
||||
if(mineLocations[i]/size == row+1 && mineLocations[i]%7 == col+1) count++;
|
||||
if(mineLocations[i]/size == row && mineLocations[i]%7 == col+1) count++;
|
||||
if(mineLocations[i]/size == row-1 && mineLocations[i]%7 == col+1) count++;
|
||||
if(mineLocations[i]/size == row-1 && mineLocations[i]%7 == col) count++;
|
||||
if(mineLocations[i]/size == row-1 && mineLocations[i]%7 == col-1) count++;
|
||||
if(mineLocations[i]/size == row && mineLocations[i]%7 == col-1) count++;
|
||||
if(mineLocations[i]/size == row+1 && mineLocations[i]%7 == col-1) count++;
|
||||
if(mineLocations[i]/size == row+1 && mineLocations[i]%7 == col) count++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
bool checkWin(){
|
||||
return opened == ((size*size) - mines);
|
||||
}
|
||||
void setMines(){
|
||||
for(int i=0;i<mines;i++){
|
||||
bool valid;
|
||||
do{
|
||||
valid = true;
|
||||
mineLocations[i] = rand()%(size*size);
|
||||
for(int j=0;j<i;j++){
|
||||
if(mineLocations[j] == mineLocations[i]){
|
||||
valid = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}while(!valid);
|
||||
}
|
||||
}
|
||||
void displayGrid(){
|
||||
cout<<" ";
|
||||
for(int i=0;i<size;i++) cout<<i<<" ";
|
||||
cout<<endl;
|
||||
for(int i=0;i<size;i++){
|
||||
cout<<i<<" ";
|
||||
for(int j=0;j<size;j++) cout<<grid[i][j]<<" ";
|
||||
cout<<endl;
|
||||
}
|
||||
cout<<endl;
|
||||
|
||||
for(int i=0;i<mines;i++){
|
||||
cout<<mineLocations[i]<<" ";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
};
|
||||
|
||||
int main(){
|
||||
|
||||
int row, col;
|
||||
char c;
|
||||
int size, mines;
|
||||
cout<<"Enter size of the game board: ";
|
||||
cin>>size;
|
||||
cout<<"Enter no Of mines: ";
|
||||
cin>>mines;
|
||||
Minesweeper game(size, mines);
|
||||
|
||||
while(!game.checkWin()){
|
||||
game.displayGrid();
|
||||
cout<<"Enter row: ";
|
||||
cin>>row;
|
||||
cout<<"Enter col: ";
|
||||
cin>>col;
|
||||
cout<<"Enter F to flag or R to reveal: ";
|
||||
cin>>c;
|
||||
|
||||
if(row<size && col<size && (c == 'F' || c == 'R')){
|
||||
if(c == 'R' && !game.revealCell(row, col, true)){
|
||||
game.displayGrid();
|
||||
cout<<"You Lost\n";
|
||||
break;
|
||||
}
|
||||
else if(c == 'F') game.flagCell(row, col);
|
||||
}
|
||||
else cout<<"Invalid input\n";
|
||||
|
||||
}
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
155
.config/Code/User/History/55ccb45e/JcxY.cpp
Normal file
155
.config/Code/User/History/55ccb45e/JcxY.cpp
Normal file
|
|
@ -0,0 +1,155 @@
|
|||
/*
|
||||
Rafay Ahmad
|
||||
23I-2526
|
||||
*/
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
class Minesweeper{
|
||||
char** grid;
|
||||
int* mineLocations;
|
||||
int size;
|
||||
int mines;
|
||||
int opened;
|
||||
|
||||
public:
|
||||
|
||||
Minesweeper(int size, int mines) : size(size), mines(mines), opened(0){
|
||||
srand(time(0));
|
||||
grid = new char*[size];
|
||||
for(int i=0;i<size;i++){
|
||||
grid[i] = new char[size];
|
||||
for(int j=0;j<size;j++) grid[i][j] = '#';
|
||||
}
|
||||
mineLocations = new int[mines];
|
||||
setMines();
|
||||
}
|
||||
~Minesweeper(){
|
||||
for(int i=0;i<size;i++) delete[] grid[i];
|
||||
delete[] grid;
|
||||
delete[] mineLocations;
|
||||
}
|
||||
|
||||
void reset(){
|
||||
for(int i=0;i<size;i++) for(int j=0;j<size;j++) grid[i][j] = '#';
|
||||
setMines();
|
||||
}
|
||||
void revealAll(){
|
||||
for(int i=0;i<size;i++){
|
||||
for(int j=0;j<size;j++){
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
bool revealCell(int row, int col, bool checkMine){
|
||||
for(int i=0;i<mines;i++){
|
||||
if(mineLocations[i]/size == row && mineLocations[i]%7 == col){
|
||||
grid[row][col] = '*';
|
||||
revealAll();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
grid[row][col] = checkSurrounding(row, col) + '0';
|
||||
opened++;
|
||||
if(grid[row][col] == '0'){
|
||||
return revealCell(row+1, col+1, false);
|
||||
return revealCell(row, col+1, false);
|
||||
return revealCell(row-1, col+1, false);
|
||||
return revealCell(row-1, col, false);
|
||||
return revealCell(row-1, col-1, false);
|
||||
return revealCell(row, col-1, false);
|
||||
return revealCell(row+1, col-1, false);
|
||||
return revealCell(row+1, col, false);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
void flagCell(int row, int col){
|
||||
grid[row][col] = 'P';
|
||||
}
|
||||
int checkSurrounding(int row, int col){
|
||||
int count = 0;
|
||||
for(int i=0;i<mines;i++){
|
||||
if(mineLocations[i]/size == row+1 && mineLocations[i]%7 == col+1) count++;
|
||||
if(mineLocations[i]/size == row && mineLocations[i]%7 == col+1) count++;
|
||||
if(mineLocations[i]/size == row-1 && mineLocations[i]%7 == col+1) count++;
|
||||
if(mineLocations[i]/size == row-1 && mineLocations[i]%7 == col) count++;
|
||||
if(mineLocations[i]/size == row-1 && mineLocations[i]%7 == col-1) count++;
|
||||
if(mineLocations[i]/size == row && mineLocations[i]%7 == col-1) count++;
|
||||
if(mineLocations[i]/size == row+1 && mineLocations[i]%7 == col-1) count++;
|
||||
if(mineLocations[i]/size == row+1 && mineLocations[i]%7 == col) count++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
bool checkWin(){
|
||||
return opened == ((size*size) - mines);
|
||||
}
|
||||
void setMines(){
|
||||
for(int i=0;i<mines;i++){
|
||||
bool valid;
|
||||
do{
|
||||
valid = true;
|
||||
mineLocations[i] = rand()%(size*size);
|
||||
for(int j=0;j<i;j++){
|
||||
if(mineLocations[j] == mineLocations[i]){
|
||||
valid = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}while(!valid);
|
||||
}
|
||||
}
|
||||
void displayGrid(){
|
||||
cout<<" ";
|
||||
for(int i=0;i<size;i++) cout<<i+1<<" ";
|
||||
cout<<endl;
|
||||
for(int i=0;i<size;i++){
|
||||
cout<<i+1<<" ";
|
||||
for(int j=0;j<size;j++) cout<<grid[i][j]<<" ";
|
||||
cout<<endl;
|
||||
}
|
||||
cout<<endl;
|
||||
|
||||
for(int i=0;i<mines;i++){
|
||||
cout<<mineLocations[i]<<" ";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
};
|
||||
|
||||
int main(){
|
||||
|
||||
int row, col;
|
||||
char c;
|
||||
int size, mines;
|
||||
cout<<"Enter size of the game board: ";
|
||||
cin>>size;
|
||||
cout<<"Enter no Of mines: ";
|
||||
cin>>mines;
|
||||
Minesweeper game(size, mines);
|
||||
|
||||
while(!game.checkWin()){
|
||||
game.displayGrid();
|
||||
cout<<"Enter row: ";
|
||||
cin>>row;
|
||||
cout<<"Enter col: ";
|
||||
cin>>col;
|
||||
cout<<"Enter F to flag or R to reveal: ";
|
||||
cin>>c;
|
||||
|
||||
if(row<size && col<size && (c == 'F' || c == 'R')){
|
||||
if(c == 'R') if(!game.revealCell(row, col, true)){
|
||||
cout<<"You Lost\n";
|
||||
break;
|
||||
}
|
||||
else game.flagCell(row, col);
|
||||
}
|
||||
else cout<<"Invalid input\n";
|
||||
|
||||
}
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
156
.config/Code/User/History/55ccb45e/Lkwy.cpp
Normal file
156
.config/Code/User/History/55ccb45e/Lkwy.cpp
Normal file
|
|
@ -0,0 +1,156 @@
|
|||
/*
|
||||
Rafay Ahmad
|
||||
23I-2526
|
||||
*/
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
class Minesweeper{
|
||||
char** grid;
|
||||
int* mineLocations;
|
||||
int size;
|
||||
int mines;
|
||||
int opened;
|
||||
|
||||
public:
|
||||
|
||||
Minesweeper(int size, int mines) : size(size), mines(mines), opened(0){
|
||||
srand(time(0));
|
||||
grid = new char*[size];
|
||||
for(int i=0;i<size;i++){
|
||||
grid[i] = new char[size];
|
||||
for(int j=0;j<size;j++) grid[i][j] = '#';
|
||||
}
|
||||
mineLocations = new int[mines];
|
||||
setMines();
|
||||
}
|
||||
~Minesweeper(){
|
||||
for(int i=0;i<size;i++) delete[] grid[i];
|
||||
delete[] grid;
|
||||
delete[] mineLocations;
|
||||
}
|
||||
|
||||
void reset(){
|
||||
for(int i=0;i<size;i++) for(int j=0;j<size;j++) grid[i][j] = '#';
|
||||
setMines();
|
||||
}
|
||||
void revealAll(){
|
||||
for(int i=0;i<mines;i++){
|
||||
grid[mineLocations[i]/7][mineLocations[i]%7] = '*';
|
||||
}
|
||||
}
|
||||
bool revealCell(int row, int col, bool checkMine){
|
||||
if(checkMine){
|
||||
for(int i=0;i<mines;i++){
|
||||
if(mineLocations[i]/size == row && mineLocations[i]%7 == col){
|
||||
grid[row][col] = '*';
|
||||
revealAll();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
grid[row][col] = checkSurrounding(row, col) + '0';
|
||||
opened++;
|
||||
if(grid[row][col] == '0'){
|
||||
revealCell(row+1, col+1, false);
|
||||
revealCell(row, col+1, false);
|
||||
revealCell(row-1, col+1, false);
|
||||
revealCell(row-1, col, false);
|
||||
revealCell(row-1, col-1, false);
|
||||
revealCell(row, col-1, false);
|
||||
revealCell(row+1, col-1, false);
|
||||
revealCell(row+1, col, false);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
void flagCell(int row, int col){
|
||||
grid[row][col] = 'P';
|
||||
}
|
||||
int checkSurrounding(int row, int col){
|
||||
int count = 0;
|
||||
for(int i=0;i<mines;i++){
|
||||
if(row<size && col<size && mineLocations[i]/size == row+1 && mineLocations[i]%7 == col+1) count++;
|
||||
if(col<size && mineLocations[i]/size == row && mineLocations[i]%7 == col+1) count++;
|
||||
if(row>0 && col<size && mineLocations[i]/size == row-1 && mineLocations[i]%7 == col+1) count++;
|
||||
if(row>0 && mineLocations[i]/size == row-1 && mineLocations[i]%7 == col) count++;
|
||||
if(row>0 && col>0 && mineLocations[i]/size == row-1 && mineLocations[i]%7 == col-1) count++;
|
||||
if(col>0 && mineLocations[i]/size == row && mineLocations[i]%7 == col-1) count++;
|
||||
if(row<size && col>0 && mineLocations[i]/size == row+1 && mineLocations[i]%7 == col-1) count++;
|
||||
if(row<size && mineLocations[i]/size == row+1 && mineLocations[i]%7 == col) count++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
bool checkWin(){
|
||||
return opened == ((size*size) - mines);
|
||||
}
|
||||
void setMines(){
|
||||
for(int i=0;i<mines;i++){
|
||||
bool valid;
|
||||
do{
|
||||
valid = true;
|
||||
mineLocations[i] = rand()%(size*size);
|
||||
for(int j=0;j<i;j++){
|
||||
if(mineLocations[j] == mineLocations[i]){
|
||||
valid = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}while(!valid);
|
||||
}
|
||||
}
|
||||
void displayGrid(){
|
||||
cout<<" ";
|
||||
for(int i=0;i<size;i++) cout<<i<<" ";
|
||||
cout<<endl;
|
||||
for(int i=0;i<size;i++){
|
||||
cout<<i<<" ";
|
||||
for(int j=0;j<size;j++) cout<<grid[i][j]<<" ";
|
||||
cout<<endl;
|
||||
}
|
||||
cout<<endl;
|
||||
|
||||
for(int i=0;i<mines;i++){
|
||||
cout<<mineLocations[i]<<" ";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
};
|
||||
|
||||
int main(){
|
||||
|
||||
int row, col;
|
||||
char c;
|
||||
int size, mines;
|
||||
cout<<"Enter size of the game board: ";
|
||||
cin>>size;
|
||||
cout<<"Enter no Of mines: ";
|
||||
cin>>mines;
|
||||
Minesweeper game(size, mines);
|
||||
|
||||
while(!game.checkWin()){
|
||||
game.displayGrid();
|
||||
cout<<"Enter row: ";
|
||||
cin>>row;
|
||||
cout<<"Enter col: ";
|
||||
cin>>col;
|
||||
cout<<"Enter F to flag or R to reveal: ";
|
||||
cin>>c;
|
||||
|
||||
if(row<size && col<size && (c == 'F' || c == 'R')){
|
||||
if(c == 'R' && !game.revealCell(row, col, true)){
|
||||
game.displayGrid();
|
||||
cout<<"You Lost\n";
|
||||
break;
|
||||
}
|
||||
else if(c == 'F') game.flagCell(row, col);
|
||||
}
|
||||
else cout<<"Invalid input\n";
|
||||
|
||||
}
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
156
.config/Code/User/History/55ccb45e/MBKB.cpp
Normal file
156
.config/Code/User/History/55ccb45e/MBKB.cpp
Normal file
|
|
@ -0,0 +1,156 @@
|
|||
/*
|
||||
Rafay Ahmad
|
||||
23I-2526
|
||||
*/
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
class Minesweeper{
|
||||
char** grid;
|
||||
int* mineLocations;
|
||||
int size;
|
||||
int mines;
|
||||
int opened;
|
||||
|
||||
public:
|
||||
|
||||
Minesweeper(int size, int mines) : size(size), mines(mines), opened(0){
|
||||
srand(time(0));
|
||||
grid = new char*[size];
|
||||
for(int i=0;i<size;i++){
|
||||
grid[i] = new char[size];
|
||||
for(int j=0;j<size;j++) grid[i][j] = '#';
|
||||
}
|
||||
mineLocations = new int[mines];
|
||||
setMines();
|
||||
}
|
||||
~Minesweeper(){
|
||||
for(int i=0;i<size;i++) delete[] grid[i];
|
||||
delete[] grid;
|
||||
delete[] mineLocations;
|
||||
}
|
||||
|
||||
void reset(){
|
||||
for(int i=0;i<size;i++) for(int j=0;j<size;j++) grid[i][j] = '#';
|
||||
setMines();
|
||||
}
|
||||
void revealAll(){
|
||||
for(int i=0;i<mines;i++){
|
||||
grid[mineLocations[i]/7][mineLocations[i]%7] = '*';
|
||||
}
|
||||
}
|
||||
bool revealCell(int row, int col, bool checkMine){
|
||||
if(checkMine){
|
||||
for(int i=0;i<mines;i++){
|
||||
if(mineLocations[i]/size == row && mineLocations[i]%7 == col){
|
||||
grid[row][col] = '*';
|
||||
revealAll();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
grid[row][col] = checkSurrounding(row, col) + '0';
|
||||
opened++;
|
||||
if(grid[row][col] == '0'){
|
||||
if(row<size && col<size) revealCell(row+1, col+1, false);
|
||||
if(row<size && col<size) revealCell(row, col+1, false);
|
||||
if(row<size && col<size) revealCell(row-1, col+1, false);
|
||||
if(row<size && col<size) revealCell(row-1, col, false);
|
||||
if(row<size && col<size) revealCell(row-1, col-1, false);
|
||||
if(row<size && col<size) revealCell(row, col-1, false);
|
||||
if(row<size && col<size) revealCell(row+1, col-1, false);
|
||||
if(row<size && col<size) revealCell(row+1, col, false);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
void flagCell(int row, int col){
|
||||
grid[row][col] = 'P';
|
||||
}
|
||||
int checkSurrounding(int row, int col){
|
||||
int count = 0;
|
||||
for(int i=0;i<mines;i++){
|
||||
if(row<size && col<size && mineLocations[i]/size == row+1 && mineLocations[i]%7 == col+1) count++;
|
||||
if(col<size && mineLocations[i]/size == row && mineLocations[i]%7 == col+1) count++;
|
||||
if(row>0 && col<size && mineLocations[i]/size == row-1 && mineLocations[i]%7 == col+1) count++;
|
||||
if(row>0 && mineLocations[i]/size == row-1 && mineLocations[i]%7 == col) count++;
|
||||
if(row>0 && col>0 && mineLocations[i]/size == row-1 && mineLocations[i]%7 == col-1) count++;
|
||||
if(col>0 && mineLocations[i]/size == row && mineLocations[i]%7 == col-1) count++;
|
||||
if(row<size && col>0 && mineLocations[i]/size == row+1 && mineLocations[i]%7 == col-1) count++;
|
||||
if(row<size && mineLocations[i]/size == row+1 && mineLocations[i]%7 == col) count++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
bool checkWin(){
|
||||
return opened == ((size*size) - mines);
|
||||
}
|
||||
void setMines(){
|
||||
for(int i=0;i<mines;i++){
|
||||
bool valid;
|
||||
do{
|
||||
valid = true;
|
||||
mineLocations[i] = rand()%(size*size);
|
||||
for(int j=0;j<i;j++){
|
||||
if(mineLocations[j] == mineLocations[i]){
|
||||
valid = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}while(!valid);
|
||||
}
|
||||
}
|
||||
void displayGrid(){
|
||||
cout<<" ";
|
||||
for(int i=0;i<size;i++) cout<<i<<" ";
|
||||
cout<<endl;
|
||||
for(int i=0;i<size;i++){
|
||||
cout<<i<<" ";
|
||||
for(int j=0;j<size;j++) cout<<grid[i][j]<<" ";
|
||||
cout<<endl;
|
||||
}
|
||||
cout<<endl;
|
||||
|
||||
for(int i=0;i<mines;i++){
|
||||
cout<<mineLocations[i]<<" ";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
};
|
||||
|
||||
int main(){
|
||||
|
||||
int row, col;
|
||||
char c;
|
||||
int size, mines;
|
||||
cout<<"Enter size of the game board: ";
|
||||
cin>>size;
|
||||
cout<<"Enter no Of mines: ";
|
||||
cin>>mines;
|
||||
Minesweeper game(size, mines);
|
||||
|
||||
while(!game.checkWin()){
|
||||
game.displayGrid();
|
||||
cout<<"Enter row: ";
|
||||
cin>>row;
|
||||
cout<<"Enter col: ";
|
||||
cin>>col;
|
||||
cout<<"Enter F to flag or R to reveal: ";
|
||||
cin>>c;
|
||||
|
||||
if(row<size && col<size && (c == 'F' || c == 'R')){
|
||||
if(c == 'R' && !game.revealCell(row, col, true)){
|
||||
game.displayGrid();
|
||||
cout<<"You Lost\n";
|
||||
break;
|
||||
}
|
||||
else if(c == 'F') game.flagCell(row, col);
|
||||
}
|
||||
else cout<<"Invalid input\n";
|
||||
|
||||
}
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
156
.config/Code/User/History/55ccb45e/MPWO.cpp
Normal file
156
.config/Code/User/History/55ccb45e/MPWO.cpp
Normal file
|
|
@ -0,0 +1,156 @@
|
|||
/*
|
||||
Rafay Ahmad
|
||||
23I-2526
|
||||
*/
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
class Minesweeper{
|
||||
char** grid;
|
||||
int* mineLocations;
|
||||
int size;
|
||||
int mines;
|
||||
int opened;
|
||||
|
||||
public:
|
||||
|
||||
Minesweeper(int size, int mines) : size(size), mines(mines), opened(0){
|
||||
srand(time(0));
|
||||
grid = new char*[size];
|
||||
for(int i=0;i<size;i++){
|
||||
grid[i] = new char[size];
|
||||
for(int j=0;j<size;j++) grid[i][j] = '#';
|
||||
}
|
||||
mineLocations = new int[mines];
|
||||
setMines();
|
||||
}
|
||||
~Minesweeper(){
|
||||
for(int i=0;i<size;i++) delete[] grid[i];
|
||||
delete[] grid;
|
||||
delete[] mineLocations;
|
||||
}
|
||||
|
||||
void reset(){
|
||||
for(int i=0;i<size;i++) for(int j=0;j<size;j++) grid[i][j] = '#';
|
||||
setMines();
|
||||
}
|
||||
void revealAll(){
|
||||
for(int i=0;i<mines;i++){
|
||||
grid[mineLocations[i]/7][mineLocations[i]%7] = '*';
|
||||
}
|
||||
}
|
||||
bool revealCell(int row, int col, bool checkMine){
|
||||
if(checkMine){
|
||||
for(int i=0;i<mines;i++){
|
||||
if(mineLocations[i]/size == row && mineLocations[i]%7 == col){
|
||||
grid[row][col] = '*';
|
||||
revealAll();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
grid[row][col] = checkSurrounding(row, col) + '0';
|
||||
opened++;
|
||||
if(grid[row][col] == '0'){
|
||||
if(row<size && col<size) revealCell(row+1, col+1, false);
|
||||
if(col<size) revealCell(row, col+1, false);
|
||||
if(row<size && col<size) revealCell(row-1, col+1, false);
|
||||
if(row<size && col<size) revealCell(row-1, col, false);
|
||||
if(row<size && col<size) revealCell(row-1, col-1, false);
|
||||
if(row<size && col<size) revealCell(row, col-1, false);
|
||||
if(row<size && col<size) revealCell(row+1, col-1, false);
|
||||
if(row<size && col<size) revealCell(row+1, col, false);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
void flagCell(int row, int col){
|
||||
grid[row][col] = 'P';
|
||||
}
|
||||
int checkSurrounding(int row, int col){
|
||||
int count = 0;
|
||||
for(int i=0;i<mines;i++){
|
||||
if(row<size && col<size && mineLocations[i]/size == row+1 && mineLocations[i]%7 == col+1) count++;
|
||||
if(col<size && mineLocations[i]/size == row && mineLocations[i]%7 == col+1) count++;
|
||||
if(row>0 && col<size && mineLocations[i]/size == row-1 && mineLocations[i]%7 == col+1) count++;
|
||||
if(row>0 && mineLocations[i]/size == row-1 && mineLocations[i]%7 == col) count++;
|
||||
if(row>0 && col>0 && mineLocations[i]/size == row-1 && mineLocations[i]%7 == col-1) count++;
|
||||
if(col>0 && mineLocations[i]/size == row && mineLocations[i]%7 == col-1) count++;
|
||||
if(row<size && col>0 && mineLocations[i]/size == row+1 && mineLocations[i]%7 == col-1) count++;
|
||||
if(row<size && mineLocations[i]/size == row+1 && mineLocations[i]%7 == col) count++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
bool checkWin(){
|
||||
return opened == ((size*size) - mines);
|
||||
}
|
||||
void setMines(){
|
||||
for(int i=0;i<mines;i++){
|
||||
bool valid;
|
||||
do{
|
||||
valid = true;
|
||||
mineLocations[i] = rand()%(size*size);
|
||||
for(int j=0;j<i;j++){
|
||||
if(mineLocations[j] == mineLocations[i]){
|
||||
valid = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}while(!valid);
|
||||
}
|
||||
}
|
||||
void displayGrid(){
|
||||
cout<<" ";
|
||||
for(int i=0;i<size;i++) cout<<i<<" ";
|
||||
cout<<endl;
|
||||
for(int i=0;i<size;i++){
|
||||
cout<<i<<" ";
|
||||
for(int j=0;j<size;j++) cout<<grid[i][j]<<" ";
|
||||
cout<<endl;
|
||||
}
|
||||
cout<<endl;
|
||||
|
||||
for(int i=0;i<mines;i++){
|
||||
cout<<mineLocations[i]<<" ";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
};
|
||||
|
||||
int main(){
|
||||
|
||||
int row, col;
|
||||
char c;
|
||||
int size, mines;
|
||||
cout<<"Enter size of the game board: ";
|
||||
cin>>size;
|
||||
cout<<"Enter no Of mines: ";
|
||||
cin>>mines;
|
||||
Minesweeper game(size, mines);
|
||||
|
||||
while(!game.checkWin()){
|
||||
game.displayGrid();
|
||||
cout<<"Enter row: ";
|
||||
cin>>row;
|
||||
cout<<"Enter col: ";
|
||||
cin>>col;
|
||||
cout<<"Enter F to flag or R to reveal: ";
|
||||
cin>>c;
|
||||
|
||||
if(row<size && col<size && (c == 'F' || c == 'R')){
|
||||
if(c == 'R' && !game.revealCell(row, col, true)){
|
||||
game.displayGrid();
|
||||
cout<<"You Lost\n";
|
||||
break;
|
||||
}
|
||||
else if(c == 'F') game.flagCell(row, col);
|
||||
}
|
||||
else cout<<"Invalid input\n";
|
||||
|
||||
}
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
127
.config/Code/User/History/55ccb45e/OdcP.cpp
Normal file
127
.config/Code/User/History/55ccb45e/OdcP.cpp
Normal file
|
|
@ -0,0 +1,127 @@
|
|||
/*
|
||||
Rafay Ahmad
|
||||
23I-2526
|
||||
*/
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
class Minesweeper{
|
||||
char** grid;
|
||||
int* mineLocations;
|
||||
int size;
|
||||
int mines;
|
||||
|
||||
public:
|
||||
|
||||
Minesweeper(int size, int mines) : size(size), mines(mines){
|
||||
srand(time(0));
|
||||
grid = new char*[size];
|
||||
for(int i=0;i<size;i++){
|
||||
grid[i] = new char[size];
|
||||
for(int j=0;j<size;j++) grid[i][j] = '#';
|
||||
}
|
||||
mineLocations = new int[mines];
|
||||
setMines();
|
||||
}
|
||||
~Minesweeper(){
|
||||
for(int i=0;i<size;i++) delete[] grid[i];
|
||||
delete[] grid;
|
||||
delete[] mineLocations;
|
||||
}
|
||||
|
||||
void reset(){
|
||||
for(int i=0;i<size;i++) for(int j=0;j<size;j++) grid[i][j] = '#';
|
||||
setMines();
|
||||
}
|
||||
void revealAll(){
|
||||
for(int i=0;i<size;i++){
|
||||
for(int j=0;j<size;j++){
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
bool revealCell(int row, int col, bool checkMine){
|
||||
for(int i=0;i<mines;i++){
|
||||
if(mineLocations[i]/size == row && mineLocations[i]%7 == col){
|
||||
grid[row][col] = '*';
|
||||
revealAll();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
grid[row][col] = checkSurrounding(row, col) + '0';
|
||||
if(grid[row][col] == '0'){
|
||||
return revealCell(row+1, col+1, false);
|
||||
return revealCell(row, col+1, false);
|
||||
return revealCell(row-1, col+1, false);
|
||||
return revealCell(row-1, col, false);
|
||||
return revealCell(row-1, col-1, false);
|
||||
return revealCell(row, col-1, false);
|
||||
return revealCell(row+1, col-1, false);
|
||||
return revealCell(row+1, col, false);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
void flagCell(int row, int col){
|
||||
grid[row][col] = 'P';
|
||||
}
|
||||
int checkSurrounding(int row, int col){
|
||||
int count = 0;
|
||||
for(int i=0;i<mines;i++){
|
||||
if(mineLocations[i]/size == row+1 && mineLocations[i]%7 == col+1) count++;
|
||||
if(mineLocations[i]/size == row && mineLocations[i]%7 == col+1) count++;
|
||||
if(mineLocations[i]/size == row-1 && mineLocations[i]%7 == col+1) count++;
|
||||
if(mineLocations[i]/size == row-1 && mineLocations[i]%7 == col) count++;
|
||||
if(mineLocations[i]/size == row-1 && mineLocations[i]%7 == col-1) count++;
|
||||
if(mineLocations[i]/size == row && mineLocations[i]%7 == col-1) count++;
|
||||
if(mineLocations[i]/size == row+1 && mineLocations[i]%7 == col-1) count++;
|
||||
if(mineLocations[i]/size == row+1 && mineLocations[i]%7 == col) count++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
bool checkWin(){
|
||||
|
||||
}
|
||||
void setMines(){
|
||||
for(int i=0;i<mines;i++){
|
||||
bool valid;
|
||||
do{
|
||||
valid = true;
|
||||
mineLocations[i] = rand()%(size*size);
|
||||
for(int j=0;j<i;j++){
|
||||
if(mineLocations[j] == mineLocations[i]){
|
||||
valid = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}while(!valid);
|
||||
}
|
||||
}
|
||||
void displayGrid(){
|
||||
cout<<" ";
|
||||
for(int i=0;i<size;i++) cout<<i+1<<" ";
|
||||
cout<<endl;
|
||||
for(int i=0;i<size;i++){
|
||||
cout<<i+1<<" ";
|
||||
for(int j=0;j<size;j++) cout<<grid[i][j]<<" ";
|
||||
cout<<endl;
|
||||
}
|
||||
cout<<endl;
|
||||
|
||||
for(int i=0;i<mines;i++){
|
||||
cout<<mineLocations[i]<<" ";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
};
|
||||
|
||||
int main(){
|
||||
|
||||
Minesweeper game(7, 10);
|
||||
game.displayGrid();
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
152
.config/Code/User/History/55ccb45e/PxZX.cpp
Normal file
152
.config/Code/User/History/55ccb45e/PxZX.cpp
Normal file
|
|
@ -0,0 +1,152 @@
|
|||
/*
|
||||
Rafay Ahmad
|
||||
23I-2526
|
||||
*/
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
class Minesweeper{
|
||||
char** grid;
|
||||
int* mineLocations;
|
||||
int size;
|
||||
int mines;
|
||||
int opened;
|
||||
|
||||
public:
|
||||
|
||||
Minesweeper(int size, int mines) : size(size), mines(mines), opened(0){
|
||||
srand(time(0));
|
||||
grid = new char*[size];
|
||||
for(int i=0;i<size;i++){
|
||||
grid[i] = new char[size];
|
||||
for(int j=0;j<size;j++) grid[i][j] = '#';
|
||||
}
|
||||
mineLocations = new int[mines];
|
||||
setMines();
|
||||
}
|
||||
~Minesweeper(){
|
||||
for(int i=0;i<size;i++) delete[] grid[i];
|
||||
delete[] grid;
|
||||
delete[] mineLocations;
|
||||
}
|
||||
|
||||
void reset(){
|
||||
for(int i=0;i<size;i++) for(int j=0;j<size;j++) grid[i][j] = '#';
|
||||
setMines();
|
||||
}
|
||||
void revealAll(){
|
||||
for(int i=0;i<size;i++){
|
||||
for(int j=0;j<size;j++){
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
bool revealCell(int row, int col, bool checkMine){
|
||||
for(int i=0;i<mines;i++){
|
||||
if(mineLocations[i]/size == row && mineLocations[i]%7 == col){
|
||||
grid[row][col] = '*';
|
||||
revealAll();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
grid[row][col] = checkSurrounding(row, col) + '0';
|
||||
opened++;
|
||||
if(grid[row][col] == '0'){
|
||||
return revealCell(row+1, col+1, false);
|
||||
return revealCell(row, col+1, false);
|
||||
return revealCell(row-1, col+1, false);
|
||||
return revealCell(row-1, col, false);
|
||||
return revealCell(row-1, col-1, false);
|
||||
return revealCell(row, col-1, false);
|
||||
return revealCell(row+1, col-1, false);
|
||||
return revealCell(row+1, col, false);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
void flagCell(int row, int col){
|
||||
grid[row][col] = 'P';
|
||||
}
|
||||
int checkSurrounding(int row, int col){
|
||||
int count = 0;
|
||||
for(int i=0;i<mines;i++){
|
||||
if(mineLocations[i]/size == row+1 && mineLocations[i]%7 == col+1) count++;
|
||||
if(mineLocations[i]/size == row && mineLocations[i]%7 == col+1) count++;
|
||||
if(mineLocations[i]/size == row-1 && mineLocations[i]%7 == col+1) count++;
|
||||
if(mineLocations[i]/size == row-1 && mineLocations[i]%7 == col) count++;
|
||||
if(mineLocations[i]/size == row-1 && mineLocations[i]%7 == col-1) count++;
|
||||
if(mineLocations[i]/size == row && mineLocations[i]%7 == col-1) count++;
|
||||
if(mineLocations[i]/size == row+1 && mineLocations[i]%7 == col-1) count++;
|
||||
if(mineLocations[i]/size == row+1 && mineLocations[i]%7 == col) count++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
bool checkWin(){
|
||||
return opened == (size*size) - mines;
|
||||
}
|
||||
void setMines(){
|
||||
for(int i=0;i<mines;i++){
|
||||
bool valid;
|
||||
do{
|
||||
valid = true;
|
||||
mineLocations[i] = rand()%(size*size);
|
||||
for(int j=0;j<i;j++){
|
||||
if(mineLocations[j] == mineLocations[i]){
|
||||
valid = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}while(!valid);
|
||||
}
|
||||
}
|
||||
void displayGrid(){
|
||||
cout<<" ";
|
||||
for(int i=0;i<size;i++) cout<<i+1<<" ";
|
||||
cout<<endl;
|
||||
for(int i=0;i<size;i++){
|
||||
cout<<i+1<<" ";
|
||||
for(int j=0;j<size;j++) cout<<grid[i][j]<<" ";
|
||||
cout<<endl;
|
||||
}
|
||||
cout<<endl;
|
||||
|
||||
for(int i=0;i<mines;i++){
|
||||
cout<<mineLocations[i]<<" ";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
};
|
||||
|
||||
int main(){
|
||||
|
||||
int row, col;
|
||||
char c;
|
||||
int size, mines;
|
||||
cout<<"Enter size of the game board: ";
|
||||
cin>>size;
|
||||
cout<<"Enter no Of mines: ";
|
||||
cin>>mines;
|
||||
Minesweeper game(size, mines);
|
||||
|
||||
while(game.checkWin()){
|
||||
game.displayGrid();
|
||||
cout<<"Enter row: ";
|
||||
cin>>row;
|
||||
cout<<"Enter col: ";
|
||||
cin>>col;
|
||||
cout<<"Enter F to flag or R to reveal: ";
|
||||
cin>>c;
|
||||
|
||||
if(row<size && col<size && (c == 'F' || c == 'R')){
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
165
.config/Code/User/History/55ccb45e/RY1w.cpp
Normal file
165
.config/Code/User/History/55ccb45e/RY1w.cpp
Normal file
|
|
@ -0,0 +1,165 @@
|
|||
/*
|
||||
Rafay Ahmad
|
||||
23I-2526
|
||||
Assignment 3
|
||||
*/
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
class Minesweeper{
|
||||
char** grid;
|
||||
int* mineLocations;
|
||||
int size;
|
||||
int mines;
|
||||
int opened;
|
||||
|
||||
public:
|
||||
|
||||
Minesweeper(int size, int mines) : size(size), mines(mines), opened(0){
|
||||
srand(time(0));
|
||||
grid = new char*[size];
|
||||
for(int i=0;i<size;i++){
|
||||
grid[i] = new char[size];
|
||||
for(int j=0;j<size;j++) grid[i][j] = '#';
|
||||
}
|
||||
mineLocations = new int[mines];
|
||||
setMines();
|
||||
}
|
||||
~Minesweeper(){
|
||||
for(int i=0;i<size;i++) delete[] grid[i];
|
||||
delete[] grid;
|
||||
delete[] mineLocations;
|
||||
}
|
||||
|
||||
void reset(){
|
||||
for(int i=0;i<size;i++) for(int j=0;j<size;j++) grid[i][j] = '#';
|
||||
setMines();
|
||||
}
|
||||
void revealAll(){
|
||||
for(int i=0;i<mines;i++){
|
||||
grid[mineLocations[i]/7][mineLocations[i]%7] = '*';
|
||||
}
|
||||
}
|
||||
bool revealCell(int row, int col, bool checkMine){
|
||||
if(checkMine){
|
||||
for(int i=0;i<mines;i++){
|
||||
if(mineLocations[i]/size == row && mineLocations[i]%size == col){
|
||||
grid[row][col] = '*';
|
||||
revealAll();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
grid[row][col] = checkSurrounding(row, col) + '0';
|
||||
opened++;
|
||||
if(grid[row][col] == '0'){
|
||||
if(row<size-1 && col<size-1 && grid[row+1][col+1] == '#') revealCell(row+1, col+1, false);
|
||||
if(col<size-1 && grid[row][col+1] == '#') revealCell(row, col+1, false);
|
||||
if(row>0 && col<size-1 && grid[row-1][col+1] == '#') revealCell(row-1, col+1, false);
|
||||
if(row>0 && grid[row-1][col] == '#') revealCell(row-1, col, false);
|
||||
if(row>0 && col>0 && grid[row-1][col-1] == '#') revealCell(row-1, col-1, false);
|
||||
if(col>0 && grid[row][col-1] == '#') revealCell(row, col-1, false);
|
||||
if(row<size-1 && col>0 && grid[row+1][col-1] == '#') revealCell(row+1, col-1, false);
|
||||
if(row<size-1 && grid[row+1][col] == '#') revealCell(row+1, col, false);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
void flagCell(int row, int col){
|
||||
grid[row][col] = 'P';
|
||||
}
|
||||
int checkSurrounding(int row, int col){
|
||||
int count = 0;
|
||||
for(int i=0;i<mines;i++){
|
||||
if(row<size-1 && col<size-1 && mineLocations[i]/size == row+1 && mineLocations[i]%size == col+1) count++;
|
||||
if(col<size-1 && mineLocations[i]/size == row && mineLocations[i]%size == col+1) count++;
|
||||
if(row>0 && col<size-1 && mineLocations[i]/size == row-1 && mineLocations[i]%size == col+1) count++;
|
||||
if(row>0 && mineLocations[i]/size == row-1 && mineLocations[i]%size == col) count++;
|
||||
if(row>0 && col>0 && mineLocations[i]/size == row-1 && mineLocations[i]%size == col-1) count++;
|
||||
if(col>0 && mineLocations[i]/size == row && mineLocations[i]%size == col-1) count++;
|
||||
if(row<size-1 && col>0 && mineLocations[i]/size == row+1 && mineLocations[i]%size == col-1) count++;
|
||||
if(row<size-1 && mineLocations[i]/size == row+1 && mineLocations[i]%size == col) count++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
bool checkWin(){
|
||||
return opened == ((size*size) - mines);
|
||||
}
|
||||
void setMines(){
|
||||
for(int i=0;i<mines;i++){
|
||||
bool valid;
|
||||
do{
|
||||
valid = true;
|
||||
mineLocations[i] = rand()%(size*size);
|
||||
for(int j=0;j<i;j++){
|
||||
if(mineLocations[j] == mineLocations[i]){
|
||||
valid = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}while(!valid);
|
||||
}
|
||||
}
|
||||
void displayGrid(){
|
||||
cout<<" ";
|
||||
for(int i=0;i<size;i++) cout<<i<<" ";
|
||||
cout<<endl;
|
||||
for(int i=0;i<size;i++){
|
||||
cout<<i<<" ";
|
||||
for(int j=0;j<size;j++) cout<<grid[i][j]<<" ";
|
||||
cout<<endl;
|
||||
}
|
||||
cout<<endl;
|
||||
|
||||
for(int i=0;i<mines;i++){
|
||||
cout<<mineLocations[i]<<" ";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
};
|
||||
|
||||
int main(){
|
||||
|
||||
int row, col;
|
||||
char c;
|
||||
int size, mines;
|
||||
cout<<"Enter size of the game board: ";
|
||||
cin>>size;
|
||||
cout<<"Enter no Of mines: ";
|
||||
cin>>mines;
|
||||
Minesweeper game(size, mines);
|
||||
|
||||
bool win = true;
|
||||
|
||||
while(!game.checkWin()){
|
||||
game.displayGrid();
|
||||
cout<<"Enter row: ";
|
||||
cin>>row;
|
||||
cout<<"Enter col: ";
|
||||
cin>>col;
|
||||
cout<<"Enter F to flag or R to reveal: ";
|
||||
cin>>c;
|
||||
|
||||
if(row<size && col<size && (c == 'F' || c == 'R')){
|
||||
if(c == 'R' && !game.revealCell(row, col, true)){
|
||||
game.displayGrid();
|
||||
win = false;
|
||||
cout<<"You Lost\n";
|
||||
break;
|
||||
}
|
||||
else if(c == 'F') game.flagCell(row, col);
|
||||
}
|
||||
else cout<<"Invalid input\n";
|
||||
|
||||
}
|
||||
|
||||
if(win){
|
||||
game.displayGrid();
|
||||
cout<<"You Won!\n";
|
||||
}
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
122
.config/Code/User/History/55ccb45e/S23Y.cpp
Normal file
122
.config/Code/User/History/55ccb45e/S23Y.cpp
Normal file
|
|
@ -0,0 +1,122 @@
|
|||
/*
|
||||
Rafay Ahmad
|
||||
23I-2526
|
||||
*/
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
class Minesweeper{
|
||||
char** grid;
|
||||
int* mineLocations;
|
||||
int size;
|
||||
int mines;
|
||||
|
||||
public:
|
||||
|
||||
Minesweeper(int size, int mines) : size(size), mines(mines){
|
||||
srand(time(0));
|
||||
grid = new char*[size];
|
||||
for(int i=0;i<size;i++){
|
||||
grid[i] = new char[size];
|
||||
for(int j=0;j<size;j++) grid[i][j] = '#';
|
||||
}
|
||||
mineLocations = new int[mines];
|
||||
setMines();
|
||||
}
|
||||
~Minesweeper(){
|
||||
for(int i=0;i<size;i++) delete[] grid[i];
|
||||
delete[] grid;
|
||||
delete[] mineLocations;
|
||||
}
|
||||
|
||||
void reset(){
|
||||
for(int i=0;i<size;i++) for(int j=0;j<size;j++) grid[i][j] = '#';
|
||||
setMines();
|
||||
}
|
||||
void revealAll(){
|
||||
|
||||
}
|
||||
bool revealCell(int row, int col, bool checkMine){
|
||||
for(int i=0;i<mines;i++){
|
||||
if(mineLocations[i]/size == row && mineLocations[i]%7 == col){
|
||||
revealAll();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
grid[row][col] = checkSurrounding(row, col) + '0';
|
||||
if(grid[row][col] == '0'){
|
||||
return revealCell(row+1, col+1, false);
|
||||
return revealCell(row, col+1, false);
|
||||
return revealCell(row-1, col+1, false);
|
||||
return revealCell(row-1, col, false);
|
||||
return revealCell(row-1, col-1, false);
|
||||
return revealCell(row, col-1, false);
|
||||
return revealCell(row+1, col-1, false);
|
||||
return revealCell(row+1, col, false);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
void flagCell(int row, int col){
|
||||
grid[row][col] = 'P';
|
||||
}
|
||||
int checkSurrounding(int row, int col){
|
||||
int count = 0;
|
||||
for(int i=0;i<mines;i++){
|
||||
if(mineLocations[i]/size == row+1 && mineLocations[i]%7 == col+1) count++;
|
||||
if(mineLocations[i]/size == row && mineLocations[i]%7 == col+1) count++;
|
||||
if(mineLocations[i]/size == row-1 && mineLocations[i]%7 == col+1) count++;
|
||||
if(mineLocations[i]/size == row-1 && mineLocations[i]%7 == col) count++;
|
||||
if(mineLocations[i]/size == row-1 && mineLocations[i]%7 == col-1) count++;
|
||||
if(mineLocations[i]/size == row && mineLocations[i]%7 == col-1) count++;
|
||||
if(mineLocations[i]/size == row+1 && mineLocations[i]%7 == col-1) count++;
|
||||
if(mineLocations[i]/size == row+1 && mineLocations[i]%7 == col) count++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
bool checkWin(){
|
||||
|
||||
}
|
||||
void setMines(){
|
||||
for(int i=0;i<mines;i++){
|
||||
bool valid;
|
||||
do{
|
||||
valid = true;
|
||||
mineLocations[i] = rand()%(size*size);
|
||||
for(int j=0;j<i;j++){
|
||||
if(mineLocations[j] == mineLocations[i]){
|
||||
valid = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}while(!valid);
|
||||
}
|
||||
}
|
||||
void displayGrid(){
|
||||
cout<<" ";
|
||||
for(int i=0;i<size;i++) cout<<i+1<<" ";
|
||||
cout<<endl;
|
||||
for(int i=0;i<size;i++){
|
||||
cout<<i+1<<" ";
|
||||
for(int j=0;j<size;j++) cout<<grid[i][j]<<" ";
|
||||
cout<<endl;
|
||||
}
|
||||
cout<<endl;
|
||||
|
||||
for(int i=0;i<mines;i++){
|
||||
cout<<mineLocations[i]<<" ";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
};
|
||||
|
||||
int main(){
|
||||
|
||||
Minesweeper game(7, 10);
|
||||
game.displayGrid();
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
142
.config/Code/User/History/55ccb45e/VkWk.cpp
Normal file
142
.config/Code/User/History/55ccb45e/VkWk.cpp
Normal file
|
|
@ -0,0 +1,142 @@
|
|||
/*
|
||||
Rafay Ahmad
|
||||
23I-2526
|
||||
*/
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
class Minesweeper{
|
||||
char** grid;
|
||||
int* mineLocations;
|
||||
int size;
|
||||
int mines;
|
||||
int opened;
|
||||
|
||||
public:
|
||||
|
||||
Minesweeper(int size, int mines) : size(size), mines(mines), opened(0){
|
||||
srand(time(0));
|
||||
grid = new char*[size];
|
||||
for(int i=0;i<size;i++){
|
||||
grid[i] = new char[size];
|
||||
for(int j=0;j<size;j++) grid[i][j] = '#';
|
||||
}
|
||||
mineLocations = new int[mines];
|
||||
setMines();
|
||||
}
|
||||
~Minesweeper(){
|
||||
for(int i=0;i<size;i++) delete[] grid[i];
|
||||
delete[] grid;
|
||||
delete[] mineLocations;
|
||||
}
|
||||
|
||||
void reset(){
|
||||
for(int i=0;i<size;i++) for(int j=0;j<size;j++) grid[i][j] = '#';
|
||||
setMines();
|
||||
}
|
||||
void revealAll(){
|
||||
for(int i=0;i<size;i++){
|
||||
for(int j=0;j<size;j++){
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
bool revealCell(int row, int col, bool checkMine){
|
||||
for(int i=0;i<mines;i++){
|
||||
if(mineLocations[i]/size == row && mineLocations[i]%7 == col){
|
||||
grid[row][col] = '*';
|
||||
revealAll();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
grid[row][col] = checkSurrounding(row, col) + '0';
|
||||
opened++;
|
||||
if(grid[row][col] == '0'){
|
||||
return revealCell(row+1, col+1, false);
|
||||
return revealCell(row, col+1, false);
|
||||
return revealCell(row-1, col+1, false);
|
||||
return revealCell(row-1, col, false);
|
||||
return revealCell(row-1, col-1, false);
|
||||
return revealCell(row, col-1, false);
|
||||
return revealCell(row+1, col-1, false);
|
||||
return revealCell(row+1, col, false);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
void flagCell(int row, int col){
|
||||
grid[row][col] = 'P';
|
||||
}
|
||||
int checkSurrounding(int row, int col){
|
||||
int count = 0;
|
||||
for(int i=0;i<mines;i++){
|
||||
if(mineLocations[i]/size == row+1 && mineLocations[i]%7 == col+1) count++;
|
||||
if(mineLocations[i]/size == row && mineLocations[i]%7 == col+1) count++;
|
||||
if(mineLocations[i]/size == row-1 && mineLocations[i]%7 == col+1) count++;
|
||||
if(mineLocations[i]/size == row-1 && mineLocations[i]%7 == col) count++;
|
||||
if(mineLocations[i]/size == row-1 && mineLocations[i]%7 == col-1) count++;
|
||||
if(mineLocations[i]/size == row && mineLocations[i]%7 == col-1) count++;
|
||||
if(mineLocations[i]/size == row+1 && mineLocations[i]%7 == col-1) count++;
|
||||
if(mineLocations[i]/size == row+1 && mineLocations[i]%7 == col) count++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
bool checkWin(){
|
||||
return opened == (size*size) - mines;
|
||||
}
|
||||
void setMines(){
|
||||
for(int i=0;i<mines;i++){
|
||||
bool valid;
|
||||
do{
|
||||
valid = true;
|
||||
mineLocations[i] = rand()%(size*size);
|
||||
for(int j=0;j<i;j++){
|
||||
if(mineLocations[j] == mineLocations[i]){
|
||||
valid = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}while(!valid);
|
||||
}
|
||||
}
|
||||
void displayGrid(){
|
||||
cout<<" ";
|
||||
for(int i=0;i<size;i++) cout<<i+1<<" ";
|
||||
cout<<endl;
|
||||
for(int i=0;i<size;i++){
|
||||
cout<<i+1<<" ";
|
||||
for(int j=0;j<size;j++) cout<<grid[i][j]<<" ";
|
||||
cout<<endl;
|
||||
}
|
||||
cout<<endl;
|
||||
|
||||
for(int i=0;i<mines;i++){
|
||||
cout<<mineLocations[i]<<" ";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
};
|
||||
|
||||
int main(){
|
||||
|
||||
int row, col;
|
||||
char c;
|
||||
Minesweeper game(7, 10);
|
||||
while(game.checkWin()){
|
||||
game.displayGrid();
|
||||
cout<<"Enter row: ";
|
||||
cin>>row;
|
||||
cout<<"Enter col: ";
|
||||
cin>>col;
|
||||
cout<<"Enter F to flag or R to reveal: ";
|
||||
cin>>c;
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
122
.config/Code/User/History/55ccb45e/XEO9.cpp
Normal file
122
.config/Code/User/History/55ccb45e/XEO9.cpp
Normal file
|
|
@ -0,0 +1,122 @@
|
|||
/*
|
||||
Rafay Ahmad
|
||||
23I-2526
|
||||
*/
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
class Minesweeper{
|
||||
char** grid;
|
||||
int* mineLocations;
|
||||
int size;
|
||||
int mines;
|
||||
|
||||
public:
|
||||
|
||||
Minesweeper(int size, int mines) : size(size), mines(mines){
|
||||
srand(time(0));
|
||||
grid = new char*[size];
|
||||
for(int i=0;i<size;i++){
|
||||
grid[i] = new char[size];
|
||||
for(int j=0;j<size;j++) grid[i][j] = '#';
|
||||
}
|
||||
mineLocations = new int[mines];
|
||||
setMines();
|
||||
}
|
||||
~Minesweeper(){
|
||||
for(int i=0;i<size;i++) delete[] grid[i];
|
||||
delete[] grid;
|
||||
delete[] mineLocations;
|
||||
}
|
||||
|
||||
void reset(){
|
||||
for(int i=0;i<size;i++) for(int j=0;j<size;j++) grid[i][j] = '#';
|
||||
setMines();
|
||||
}
|
||||
void revealAll(){
|
||||
|
||||
}
|
||||
bool revealCell(int row, int col, bool checkMine){
|
||||
for(int i=0;i<mines;i++){
|
||||
if(mineLocations[i]/size == row && mineLocations[i]%7 == col){
|
||||
revealAll();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
grid[row][col] = checkSurrounding(row, col) + '0';
|
||||
if(grid[row][col] == '0'){
|
||||
return revealCell(row+1, col+1, false);
|
||||
return revealCell(row, col+1, false);
|
||||
return revealCell(row-1, col+1, false);
|
||||
return revealCell(row-1, col, false);
|
||||
return revealCell(row-1, col-1, false);
|
||||
return revealCell(row, col-1, false);
|
||||
return revealCell(row+1, col-1, false);
|
||||
return revealCell(row+1, col, false);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
void flagCell(int row, int col){
|
||||
grid[row][col] = 'P';
|
||||
}
|
||||
int checkSurrounding(int row, int col){
|
||||
int count = 0;
|
||||
for(int i=0;i<mines;i++){
|
||||
if(mineLocations[i]/size == row+1 && mineLocations[i]%7 == col+1) count++;
|
||||
if(mineLocations[i]/size == row && mineLocations[i]%7 == col+1) count++;
|
||||
if(mineLocations[i]/size == row-1 && mineLocations[i]%7 == col+1) count++;
|
||||
if(mineLocations[i]/size == row-1 && mineLocations[i]%7 == col) count++;
|
||||
if(mineLocations[i]/size == row-1 && mineLocations[i]%7 == col-1) count++;
|
||||
if(mineLocations[i]/size == row && mineLocations[i]%7 == col-1) count++;
|
||||
if(mineLocations[i]/size == row+1 && mineLocations[i]%7 == col-1) count++;
|
||||
if(mineLocations[i]/size == row+1 && mineLocations[i]%7 == col) count++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
bool checkWin(){
|
||||
|
||||
}
|
||||
void setMines(){
|
||||
for(int i=0;i<mines;i++){
|
||||
bool valid;
|
||||
do{
|
||||
valid = true;
|
||||
mineLocations[i] = rand()%(size*size);
|
||||
for(int j=0;j<i;j++){
|
||||
if(mineLocations[j] == mineLocations[i]){
|
||||
valid = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}while(!valid);
|
||||
}
|
||||
}
|
||||
void displayGrid(){
|
||||
cout<<" ";
|
||||
for(int i=0;i<size;i++) cout<<i+1<<" ";
|
||||
cout<<endl;
|
||||
for(int i=0;i<size;i++){
|
||||
cout<<i+1<<" ";
|
||||
for(int j=0;j<size;j++) cout<<grid[i][j]<<" ";
|
||||
cout<<endl;
|
||||
}
|
||||
cout<<endl;
|
||||
|
||||
for(int i=0;i<mines;i++){
|
||||
cout<<mineLocations[i]<<" ";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
};
|
||||
|
||||
int main(){
|
||||
|
||||
Minesweeper game(7, 10);
|
||||
game.displayGrid();
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
156
.config/Code/User/History/55ccb45e/Z7Qq.cpp
Normal file
156
.config/Code/User/History/55ccb45e/Z7Qq.cpp
Normal file
|
|
@ -0,0 +1,156 @@
|
|||
/*
|
||||
Rafay Ahmad
|
||||
23I-2526
|
||||
*/
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
class Minesweeper{
|
||||
char** grid;
|
||||
int* mineLocations;
|
||||
int size;
|
||||
int mines;
|
||||
int opened;
|
||||
|
||||
public:
|
||||
|
||||
Minesweeper(int size, int mines) : size(size), mines(mines), opened(0){
|
||||
srand(time(0));
|
||||
grid = new char*[size];
|
||||
for(int i=0;i<size;i++){
|
||||
grid[i] = new char[size];
|
||||
for(int j=0;j<size;j++) grid[i][j] = '#';
|
||||
}
|
||||
mineLocations = new int[mines];
|
||||
setMines();
|
||||
}
|
||||
~Minesweeper(){
|
||||
for(int i=0;i<size;i++) delete[] grid[i];
|
||||
delete[] grid;
|
||||
delete[] mineLocations;
|
||||
}
|
||||
|
||||
void reset(){
|
||||
for(int i=0;i<size;i++) for(int j=0;j<size;j++) grid[i][j] = '#';
|
||||
setMines();
|
||||
}
|
||||
void revealAll(){
|
||||
for(int i=0;i<mines;i++){
|
||||
grid[mineLocations[i]/7][mineLocations[i]%7] = '*';
|
||||
}
|
||||
}
|
||||
bool revealCell(int row, int col, bool checkMine){
|
||||
if(checkMine){
|
||||
for(int i=0;i<mines;i++){
|
||||
if(mineLocations[i]/size == row && mineLocations[i]%7 == col){
|
||||
grid[row][col] = '*';
|
||||
revealAll();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
grid[row][col] = checkSurrounding(row, col) + '0';
|
||||
opened++;
|
||||
if(grid[row][col] == '0'){
|
||||
if(row<size && col<size) revealCell(row+1, col+1, false);
|
||||
if(col<size) revealCell(row, col+1, false);
|
||||
if(row>0 && col<size) revealCell(row-1, col+1, false);
|
||||
if(row>0) revealCell(row-1, col, false);
|
||||
if(row>0 && col>0) revealCell(row-1, col-1, false);
|
||||
if(row<size && col<size) revealCell(row, col-1, false);
|
||||
if(row<size && col<size) revealCell(row+1, col-1, false);
|
||||
if(row<size && col<size) revealCell(row+1, col, false);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
void flagCell(int row, int col){
|
||||
grid[row][col] = 'P';
|
||||
}
|
||||
int checkSurrounding(int row, int col){
|
||||
int count = 0;
|
||||
for(int i=0;i<mines;i++){
|
||||
if(row<size && col<size && mineLocations[i]/size == row+1 && mineLocations[i]%7 == col+1) count++;
|
||||
if(col<size && mineLocations[i]/size == row && mineLocations[i]%7 == col+1) count++;
|
||||
if(row>0 && col<size && mineLocations[i]/size == row-1 && mineLocations[i]%7 == col+1) count++;
|
||||
if(row>0 && mineLocations[i]/size == row-1 && mineLocations[i]%7 == col) count++;
|
||||
if(row>0 && col>0 && mineLocations[i]/size == row-1 && mineLocations[i]%7 == col-1) count++;
|
||||
if(col>0 && mineLocations[i]/size == row && mineLocations[i]%7 == col-1) count++;
|
||||
if(row<size && col>0 && mineLocations[i]/size == row+1 && mineLocations[i]%7 == col-1) count++;
|
||||
if(row<size && mineLocations[i]/size == row+1 && mineLocations[i]%7 == col) count++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
bool checkWin(){
|
||||
return opened == ((size*size) - mines);
|
||||
}
|
||||
void setMines(){
|
||||
for(int i=0;i<mines;i++){
|
||||
bool valid;
|
||||
do{
|
||||
valid = true;
|
||||
mineLocations[i] = rand()%(size*size);
|
||||
for(int j=0;j<i;j++){
|
||||
if(mineLocations[j] == mineLocations[i]){
|
||||
valid = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}while(!valid);
|
||||
}
|
||||
}
|
||||
void displayGrid(){
|
||||
cout<<" ";
|
||||
for(int i=0;i<size;i++) cout<<i<<" ";
|
||||
cout<<endl;
|
||||
for(int i=0;i<size;i++){
|
||||
cout<<i<<" ";
|
||||
for(int j=0;j<size;j++) cout<<grid[i][j]<<" ";
|
||||
cout<<endl;
|
||||
}
|
||||
cout<<endl;
|
||||
|
||||
for(int i=0;i<mines;i++){
|
||||
cout<<mineLocations[i]<<" ";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
};
|
||||
|
||||
int main(){
|
||||
|
||||
int row, col;
|
||||
char c;
|
||||
int size, mines;
|
||||
cout<<"Enter size of the game board: ";
|
||||
cin>>size;
|
||||
cout<<"Enter no Of mines: ";
|
||||
cin>>mines;
|
||||
Minesweeper game(size, mines);
|
||||
|
||||
while(!game.checkWin()){
|
||||
game.displayGrid();
|
||||
cout<<"Enter row: ";
|
||||
cin>>row;
|
||||
cout<<"Enter col: ";
|
||||
cin>>col;
|
||||
cout<<"Enter F to flag or R to reveal: ";
|
||||
cin>>c;
|
||||
|
||||
if(row<size && col<size && (c == 'F' || c == 'R')){
|
||||
if(c == 'R' && !game.revealCell(row, col, true)){
|
||||
game.displayGrid();
|
||||
cout<<"You Lost\n";
|
||||
break;
|
||||
}
|
||||
else if(c == 'F') game.flagCell(row, col);
|
||||
}
|
||||
else cout<<"Invalid input\n";
|
||||
|
||||
}
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
141
.config/Code/User/History/55ccb45e/c2Ob.cpp
Normal file
141
.config/Code/User/History/55ccb45e/c2Ob.cpp
Normal file
|
|
@ -0,0 +1,141 @@
|
|||
/*
|
||||
Rafay Ahmad
|
||||
23I-2526
|
||||
*/
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
class Minesweeper{
|
||||
char** grid;
|
||||
int* mineLocations;
|
||||
int size;
|
||||
int mines;
|
||||
int opened;
|
||||
|
||||
public:
|
||||
|
||||
Minesweeper(int size, int mines) : size(size), mines(mines), opened(0){
|
||||
srand(time(0));
|
||||
grid = new char*[size];
|
||||
for(int i=0;i<size;i++){
|
||||
grid[i] = new char[size];
|
||||
for(int j=0;j<size;j++) grid[i][j] = '#';
|
||||
}
|
||||
mineLocations = new int[mines];
|
||||
setMines();
|
||||
}
|
||||
~Minesweeper(){
|
||||
for(int i=0;i<size;i++) delete[] grid[i];
|
||||
delete[] grid;
|
||||
delete[] mineLocations;
|
||||
}
|
||||
|
||||
void reset(){
|
||||
for(int i=0;i<size;i++) for(int j=0;j<size;j++) grid[i][j] = '#';
|
||||
setMines();
|
||||
}
|
||||
void revealAll(){
|
||||
for(int i=0;i<size;i++){
|
||||
for(int j=0;j<size;j++){
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
bool revealCell(int row, int col, bool checkMine){
|
||||
for(int i=0;i<mines;i++){
|
||||
if(mineLocations[i]/size == row && mineLocations[i]%7 == col){
|
||||
grid[row][col] = '*';
|
||||
revealAll();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
grid[row][col] = checkSurrounding(row, col) + '0';
|
||||
opened++;
|
||||
if(grid[row][col] == '0'){
|
||||
return revealCell(row+1, col+1, false);
|
||||
return revealCell(row, col+1, false);
|
||||
return revealCell(row-1, col+1, false);
|
||||
return revealCell(row-1, col, false);
|
||||
return revealCell(row-1, col-1, false);
|
||||
return revealCell(row, col-1, false);
|
||||
return revealCell(row+1, col-1, false);
|
||||
return revealCell(row+1, col, false);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
void flagCell(int row, int col){
|
||||
grid[row][col] = 'P';
|
||||
}
|
||||
int checkSurrounding(int row, int col){
|
||||
int count = 0;
|
||||
for(int i=0;i<mines;i++){
|
||||
if(mineLocations[i]/size == row+1 && mineLocations[i]%7 == col+1) count++;
|
||||
if(mineLocations[i]/size == row && mineLocations[i]%7 == col+1) count++;
|
||||
if(mineLocations[i]/size == row-1 && mineLocations[i]%7 == col+1) count++;
|
||||
if(mineLocations[i]/size == row-1 && mineLocations[i]%7 == col) count++;
|
||||
if(mineLocations[i]/size == row-1 && mineLocations[i]%7 == col-1) count++;
|
||||
if(mineLocations[i]/size == row && mineLocations[i]%7 == col-1) count++;
|
||||
if(mineLocations[i]/size == row+1 && mineLocations[i]%7 == col-1) count++;
|
||||
if(mineLocations[i]/size == row+1 && mineLocations[i]%7 == col) count++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
bool checkWin(){
|
||||
return opened == (size*size) - mines;
|
||||
}
|
||||
void setMines(){
|
||||
for(int i=0;i<mines;i++){
|
||||
bool valid;
|
||||
do{
|
||||
valid = true;
|
||||
mineLocations[i] = rand()%(size*size);
|
||||
for(int j=0;j<i;j++){
|
||||
if(mineLocations[j] == mineLocations[i]){
|
||||
valid = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}while(!valid);
|
||||
}
|
||||
}
|
||||
void displayGrid(){
|
||||
cout<<" ";
|
||||
for(int i=0;i<size;i++) cout<<i+1<<" ";
|
||||
cout<<endl;
|
||||
for(int i=0;i<size;i++){
|
||||
cout<<i+1<<" ";
|
||||
for(int j=0;j<size;j++) cout<<grid[i][j]<<" ";
|
||||
cout<<endl;
|
||||
}
|
||||
cout<<endl;
|
||||
|
||||
for(int i=0;i<mines;i++){
|
||||
cout<<mineLocations[i]<<" ";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
};
|
||||
|
||||
int main(){
|
||||
|
||||
int row, col;
|
||||
char c;
|
||||
Minesweeper game(7, 10);
|
||||
while(game.checkWin()){
|
||||
game.displayGrid();
|
||||
cout<<"Enter row: ";
|
||||
cin>>row;
|
||||
cout<<"Enter col: ";
|
||||
cin>>col;
|
||||
cout<<"Enter F to flag or R to reveal: ";
|
||||
cin>>c;
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
123
.config/Code/User/History/55ccb45e/cpy0.cpp
Normal file
123
.config/Code/User/History/55ccb45e/cpy0.cpp
Normal file
|
|
@ -0,0 +1,123 @@
|
|||
/*
|
||||
Rafay Ahmad
|
||||
23I-2526
|
||||
*/
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
class Minesweeper{
|
||||
char** grid;
|
||||
int* mineLocations;
|
||||
int size;
|
||||
int mines;
|
||||
|
||||
public:
|
||||
|
||||
Minesweeper(int size, int mines) : size(size), mines(mines){
|
||||
srand(time(0));
|
||||
grid = new char*[size];
|
||||
for(int i=0;i<size;i++){
|
||||
grid[i] = new char[size];
|
||||
for(int j=0;j<size;j++) grid[i][j] = '#';
|
||||
}
|
||||
mineLocations = new int[mines];
|
||||
setMines();
|
||||
}
|
||||
~Minesweeper(){
|
||||
for(int i=0;i<size;i++) delete[] grid[i];
|
||||
delete[] grid;
|
||||
delete[] mineLocations;
|
||||
}
|
||||
|
||||
void reset(){
|
||||
for(int i=0;i<size;i++) for(int j=0;j<size;j++) grid[i][j] = '#';
|
||||
setMines();
|
||||
}
|
||||
void revealAll(){
|
||||
|
||||
}
|
||||
bool revealCell(int row, int col, bool checkMine){
|
||||
for(int i=0;i<mines;i++){
|
||||
if(mineLocations[i]/size == row && mineLocations[i]%7 == col){
|
||||
revealAll();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
grid[row][col] = checkSurrounding(row, col) + '0';
|
||||
if(grid[row][col] == '0'){
|
||||
return revealCell(row+1, col+1, false);
|
||||
return revealCell(row, col+1, false);
|
||||
return revealCell(row-1, col+1, false);
|
||||
return revealCell(row-1, col, false);
|
||||
return revealCell(row-1, col-1, false);
|
||||
return revealCell(row, col-1, false);
|
||||
return revealCell(row+1, col-1, false);
|
||||
return revealCell(row+1, col, false);
|
||||
}
|
||||
return true;
|
||||
|
||||
}
|
||||
void flagCell(int row, int col){
|
||||
grid[row][col] = 'P';
|
||||
}
|
||||
int checkSurrounding(int row, int col){
|
||||
int count = 0;
|
||||
for(int i=0;i<mines;i++){
|
||||
if(mineLocations[i]/size == row+1 && mineLocations[i]%7 == col+1) count++;
|
||||
if(mineLocations[i]/size == row && mineLocations[i]%7 == col+1) count++;
|
||||
if(mineLocations[i]/size == row-1 && mineLocations[i]%7 == col+1) count++;
|
||||
if(mineLocations[i]/size == row-1 && mineLocations[i]%7 == col) count++;
|
||||
if(mineLocations[i]/size == row-1 && mineLocations[i]%7 == col-1) count++;
|
||||
if(mineLocations[i]/size == row && mineLocations[i]%7 == col-1) count++;
|
||||
if(mineLocations[i]/size == row+1 && mineLocations[i]%7 == col-1) count++;
|
||||
if(mineLocations[i]/size == row+1 && mineLocations[i]%7 == col) count++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
bool checkWin(){
|
||||
|
||||
}
|
||||
void setMines(){
|
||||
for(int i=0;i<mines;i++){
|
||||
bool valid;
|
||||
do{
|
||||
valid = true;
|
||||
mineLocations[i] = rand()%(size*size);
|
||||
for(int j=0;j<i;j++){
|
||||
if(mineLocations[j] == mineLocations[i]){
|
||||
valid = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}while(!valid);
|
||||
}
|
||||
}
|
||||
void displayGrid(){
|
||||
cout<<" ";
|
||||
for(int i=0;i<size;i++) cout<<i+1<<" ";
|
||||
cout<<endl;
|
||||
for(int i=0;i<size;i++){
|
||||
cout<<i+1<<" ";
|
||||
for(int j=0;j<size;j++) cout<<grid[i][j]<<" ";
|
||||
cout<<endl;
|
||||
}
|
||||
cout<<endl;
|
||||
|
||||
for(int i=0;i<mines;i++){
|
||||
cout<<mineLocations[i]<<" ";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
};
|
||||
|
||||
int main(){
|
||||
|
||||
Minesweeper game(7, 10);
|
||||
game.displayGrid();
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
156
.config/Code/User/History/55ccb45e/dScQ.cpp
Normal file
156
.config/Code/User/History/55ccb45e/dScQ.cpp
Normal file
|
|
@ -0,0 +1,156 @@
|
|||
/*
|
||||
Rafay Ahmad
|
||||
23I-2526
|
||||
*/
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
class Minesweeper{
|
||||
char** grid;
|
||||
int* mineLocations;
|
||||
int size;
|
||||
int mines;
|
||||
int opened;
|
||||
|
||||
public:
|
||||
|
||||
Minesweeper(int size, int mines) : size(size), mines(mines), opened(0){
|
||||
srand(time(0));
|
||||
grid = new char*[size];
|
||||
for(int i=0;i<size;i++){
|
||||
grid[i] = new char[size];
|
||||
for(int j=0;j<size;j++) grid[i][j] = '#';
|
||||
}
|
||||
mineLocations = new int[mines];
|
||||
setMines();
|
||||
}
|
||||
~Minesweeper(){
|
||||
for(int i=0;i<size;i++) delete[] grid[i];
|
||||
delete[] grid;
|
||||
delete[] mineLocations;
|
||||
}
|
||||
|
||||
void reset(){
|
||||
for(int i=0;i<size;i++) for(int j=0;j<size;j++) grid[i][j] = '#';
|
||||
setMines();
|
||||
}
|
||||
void revealAll(){
|
||||
for(int i=0;i<mines;i++){
|
||||
grid[mineLocations[i]/7][mineLocations[i]%7] = '*';
|
||||
}
|
||||
}
|
||||
bool revealCell(int row, int col, bool checkMine){
|
||||
if(checkMine){
|
||||
for(int i=0;i<mines;i++){
|
||||
if(mineLocations[i]/size == row && mineLocations[i]%size == col){
|
||||
grid[row][col] = '*';
|
||||
revealAll();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
grid[row][col] = checkSurrounding(row, col) + '0';
|
||||
opened++;
|
||||
if(grid[row][col] == '0'){
|
||||
if(row<size-1 && col<size-1) revealCell(row+1, col+1, false);
|
||||
if(col<size-1) revealCell(row, col+1, false);
|
||||
if(row>0 && col<size-1) revealCell(row-1, col+1, false);
|
||||
if(row>0) revealCell(row-1, col, false);
|
||||
if(row>0 && col>0) revealCell(row-1, col-1, false);
|
||||
if(col>0) revealCell(row, col-1, false);
|
||||
if(row<size-1 && col>0) revealCell(row+1, col-1, false);
|
||||
if(row<size-1) revealCell(row+1, col, false);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
void flagCell(int row, int col){
|
||||
grid[row][col] = 'P';
|
||||
}
|
||||
int checkSurrounding(int row, int col){
|
||||
int count = 0;
|
||||
for(int i=0;i<mines;i++){
|
||||
if(row<size && col<size && mineLocations[i]/size == row+1 && mineLocations[i]%7 == col+1) count++;
|
||||
if(col<size && mineLocations[i]/size == row && mineLocations[i]%7 == col+1) count++;
|
||||
if(row>0 && col<size && mineLocations[i]/size == row-1 && mineLocations[i]%7 == col+1) count++;
|
||||
if(row>0 && mineLocations[i]/size == row-1 && mineLocations[i]%7 == col) count++;
|
||||
if(row>0 && col>0 && mineLocations[i]/size == row-1 && mineLocations[i]%7 == col-1) count++;
|
||||
if(col>0 && mineLocations[i]/size == row && mineLocations[i]%7 == col-1) count++;
|
||||
if(row<size && col>0 && mineLocations[i]/size == row+1 && mineLocations[i]%7 == col-1) count++;
|
||||
if(row<size && mineLocations[i]/size == row+1 && mineLocations[i]%7 == col) count++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
bool checkWin(){
|
||||
return opened == ((size*size) - mines);
|
||||
}
|
||||
void setMines(){
|
||||
for(int i=0;i<mines;i++){
|
||||
bool valid;
|
||||
do{
|
||||
valid = true;
|
||||
mineLocations[i] = rand()%(size*size);
|
||||
for(int j=0;j<i;j++){
|
||||
if(mineLocations[j] == mineLocations[i]){
|
||||
valid = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}while(!valid);
|
||||
}
|
||||
}
|
||||
void displayGrid(){
|
||||
cout<<" ";
|
||||
for(int i=0;i<size;i++) cout<<i<<" ";
|
||||
cout<<endl;
|
||||
for(int i=0;i<size;i++){
|
||||
cout<<i<<" ";
|
||||
for(int j=0;j<size;j++) cout<<grid[i][j]<<" ";
|
||||
cout<<endl;
|
||||
}
|
||||
cout<<endl;
|
||||
|
||||
for(int i=0;i<mines;i++){
|
||||
cout<<mineLocations[i]<<" ";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
};
|
||||
|
||||
int main(){
|
||||
|
||||
int row, col;
|
||||
char c;
|
||||
int size, mines;
|
||||
cout<<"Enter size of the game board: ";
|
||||
cin>>size;
|
||||
cout<<"Enter no Of mines: ";
|
||||
cin>>mines;
|
||||
Minesweeper game(size, mines);
|
||||
|
||||
while(!game.checkWin()){
|
||||
game.displayGrid();
|
||||
cout<<"Enter row: ";
|
||||
cin>>row;
|
||||
cout<<"Enter col: ";
|
||||
cin>>col;
|
||||
cout<<"Enter F to flag or R to reveal: ";
|
||||
cin>>c;
|
||||
|
||||
if(row<size && col<size && (c == 'F' || c == 'R')){
|
||||
if(c == 'R' && !game.revealCell(row, col, true)){
|
||||
game.displayGrid();
|
||||
cout<<"You Lost\n";
|
||||
break;
|
||||
}
|
||||
else if(c == 'F') game.flagCell(row, col);
|
||||
}
|
||||
else cout<<"Invalid input\n";
|
||||
|
||||
}
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
156
.config/Code/User/History/55ccb45e/dcTS.cpp
Normal file
156
.config/Code/User/History/55ccb45e/dcTS.cpp
Normal file
|
|
@ -0,0 +1,156 @@
|
|||
/*
|
||||
Rafay Ahmad
|
||||
23I-2526
|
||||
*/
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
class Minesweeper{
|
||||
char** grid;
|
||||
int* mineLocations;
|
||||
int size;
|
||||
int mines;
|
||||
int opened;
|
||||
|
||||
public:
|
||||
|
||||
Minesweeper(int size, int mines) : size(size), mines(mines), opened(0){
|
||||
srand(time(0));
|
||||
grid = new char*[size];
|
||||
for(int i=0;i<size;i++){
|
||||
grid[i] = new char[size];
|
||||
for(int j=0;j<size;j++) grid[i][j] = '#';
|
||||
}
|
||||
mineLocations = new int[mines];
|
||||
setMines();
|
||||
}
|
||||
~Minesweeper(){
|
||||
for(int i=0;i<size;i++) delete[] grid[i];
|
||||
delete[] grid;
|
||||
delete[] mineLocations;
|
||||
}
|
||||
|
||||
void reset(){
|
||||
for(int i=0;i<size;i++) for(int j=0;j<size;j++) grid[i][j] = '#';
|
||||
setMines();
|
||||
}
|
||||
void revealAll(){
|
||||
for(int i=0;i<mines;i++){
|
||||
grid[mineLocations[i]/7][mineLocations[i]%7] = '*';
|
||||
}
|
||||
}
|
||||
bool revealCell(int row, int col, bool checkMine){
|
||||
if(checkMine){
|
||||
for(int i=0;i<mines;i++){
|
||||
if(mineLocations[i]/size == row && mineLocations[i]%7 == col){
|
||||
grid[row][col] = '*';
|
||||
revealAll();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
grid[row][col] = checkSurrounding(row, col) + '0';
|
||||
opened++;
|
||||
if(grid[row][col] == '0'){
|
||||
return revealCell(row+1, col+1, false);
|
||||
return revealCell(row, col+1, false);
|
||||
return revealCell(row-1, col+1, false);
|
||||
return revealCell(row-1, col, false);
|
||||
return revealCell(row-1, col-1, false);
|
||||
return revealCell(row, col-1, false);
|
||||
return revealCell(row+1, col-1, false);
|
||||
return revealCell(row+1, col, false);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
void flagCell(int row, int col){
|
||||
grid[row][col] = 'P';
|
||||
}
|
||||
int checkSurrounding(int row, int col){
|
||||
int count = 0;
|
||||
for(int i=0;i<mines;i++){
|
||||
if(row<size && col<size && mineLocations[i]/size == row+1 && mineLocations[i]%7 == col+1) count++;
|
||||
if(col<size && mineLocations[i]/size == row && mineLocations[i]%7 == col+1) count++;
|
||||
if(row>0 && mineLocations[i]/size == row-1 && mineLocations[i]%7 == col+1) count++;
|
||||
if(row<size && mineLocations[i]/size == row-1 && mineLocations[i]%7 == col) count++;
|
||||
if(row<size && mineLocations[i]/size == row-1 && mineLocations[i]%7 == col-1) count++;
|
||||
if(row<size && mineLocations[i]/size == row && mineLocations[i]%7 == col-1) count++;
|
||||
if(row<size && mineLocations[i]/size == row+1 && mineLocations[i]%7 == col-1) count++;
|
||||
if(row<size && mineLocations[i]/size == row+1 && mineLocations[i]%7 == col) count++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
bool checkWin(){
|
||||
return opened == ((size*size) - mines);
|
||||
}
|
||||
void setMines(){
|
||||
for(int i=0;i<mines;i++){
|
||||
bool valid;
|
||||
do{
|
||||
valid = true;
|
||||
mineLocations[i] = rand()%(size*size);
|
||||
for(int j=0;j<i;j++){
|
||||
if(mineLocations[j] == mineLocations[i]){
|
||||
valid = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}while(!valid);
|
||||
}
|
||||
}
|
||||
void displayGrid(){
|
||||
cout<<" ";
|
||||
for(int i=0;i<size;i++) cout<<i<<" ";
|
||||
cout<<endl;
|
||||
for(int i=0;i<size;i++){
|
||||
cout<<i<<" ";
|
||||
for(int j=0;j<size;j++) cout<<grid[i][j]<<" ";
|
||||
cout<<endl;
|
||||
}
|
||||
cout<<endl;
|
||||
|
||||
for(int i=0;i<mines;i++){
|
||||
cout<<mineLocations[i]<<" ";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
};
|
||||
|
||||
int main(){
|
||||
|
||||
int row, col;
|
||||
char c;
|
||||
int size, mines;
|
||||
cout<<"Enter size of the game board: ";
|
||||
cin>>size;
|
||||
cout<<"Enter no Of mines: ";
|
||||
cin>>mines;
|
||||
Minesweeper game(size, mines);
|
||||
|
||||
while(!game.checkWin()){
|
||||
game.displayGrid();
|
||||
cout<<"Enter row: ";
|
||||
cin>>row;
|
||||
cout<<"Enter col: ";
|
||||
cin>>col;
|
||||
cout<<"Enter F to flag or R to reveal: ";
|
||||
cin>>c;
|
||||
|
||||
if(row<size && col<size && (c == 'F' || c == 'R')){
|
||||
if(c == 'R' && !game.revealCell(row, col, true)){
|
||||
game.displayGrid();
|
||||
cout<<"You Lost\n";
|
||||
break;
|
||||
}
|
||||
else if(c == 'F') game.flagCell(row, col);
|
||||
}
|
||||
else cout<<"Invalid input\n";
|
||||
|
||||
}
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
129
.config/Code/User/History/55ccb45e/dzLt.cpp
Normal file
129
.config/Code/User/History/55ccb45e/dzLt.cpp
Normal file
|
|
@ -0,0 +1,129 @@
|
|||
/*
|
||||
Rafay Ahmad
|
||||
23I-2526
|
||||
*/
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
class Minesweeper{
|
||||
char** grid;
|
||||
int* mineLocations;
|
||||
int size;
|
||||
int mines;
|
||||
int opened;
|
||||
|
||||
public:
|
||||
|
||||
Minesweeper(int size, int mines) : size(size), mines(mines), opened(0){
|
||||
srand(time(0));
|
||||
grid = new char*[size];
|
||||
for(int i=0;i<size;i++){
|
||||
grid[i] = new char[size];
|
||||
for(int j=0;j<size;j++) grid[i][j] = '#';
|
||||
}
|
||||
mineLocations = new int[mines];
|
||||
setMines();
|
||||
}
|
||||
~Minesweeper(){
|
||||
for(int i=0;i<size;i++) delete[] grid[i];
|
||||
delete[] grid;
|
||||
delete[] mineLocations;
|
||||
}
|
||||
|
||||
void reset(){
|
||||
for(int i=0;i<size;i++) for(int j=0;j<size;j++) grid[i][j] = '#';
|
||||
setMines();
|
||||
}
|
||||
void revealAll(){
|
||||
for(int i=0;i<size;i++){
|
||||
for(int j=0;j<size;j++){
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
bool revealCell(int row, int col, bool checkMine){
|
||||
for(int i=0;i<mines;i++){
|
||||
if(mineLocations[i]/size == row && mineLocations[i]%7 == col){
|
||||
grid[row][col] = '*';
|
||||
revealAll();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
grid[row][col] = checkSurrounding(row, col) + '0';
|
||||
opened++;
|
||||
if(grid[row][col] == '0'){
|
||||
return revealCell(row+1, col+1, false);
|
||||
return revealCell(row, col+1, false);
|
||||
return revealCell(row-1, col+1, false);
|
||||
return revealCell(row-1, col, false);
|
||||
return revealCell(row-1, col-1, false);
|
||||
return revealCell(row, col-1, false);
|
||||
return revealCell(row+1, col-1, false);
|
||||
return revealCell(row+1, col, false);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
void flagCell(int row, int col){
|
||||
grid[row][col] = 'P';
|
||||
}
|
||||
int checkSurrounding(int row, int col){
|
||||
int count = 0;
|
||||
for(int i=0;i<mines;i++){
|
||||
if(mineLocations[i]/size == row+1 && mineLocations[i]%7 == col+1) count++;
|
||||
if(mineLocations[i]/size == row && mineLocations[i]%7 == col+1) count++;
|
||||
if(mineLocations[i]/size == row-1 && mineLocations[i]%7 == col+1) count++;
|
||||
if(mineLocations[i]/size == row-1 && mineLocations[i]%7 == col) count++;
|
||||
if(mineLocations[i]/size == row-1 && mineLocations[i]%7 == col-1) count++;
|
||||
if(mineLocations[i]/size == row && mineLocations[i]%7 == col-1) count++;
|
||||
if(mineLocations[i]/size == row+1 && mineLocations[i]%7 == col-1) count++;
|
||||
if(mineLocations[i]/size == row+1 && mineLocations[i]%7 == col) count++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
bool checkWin(){
|
||||
|
||||
}
|
||||
void setMines(){
|
||||
for(int i=0;i<mines;i++){
|
||||
bool valid;
|
||||
do{
|
||||
valid = true;
|
||||
mineLocations[i] = rand()%(size*size);
|
||||
for(int j=0;j<i;j++){
|
||||
if(mineLocations[j] == mineLocations[i]){
|
||||
valid = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}while(!valid);
|
||||
}
|
||||
}
|
||||
void displayGrid(){
|
||||
cout<<" ";
|
||||
for(int i=0;i<size;i++) cout<<i+1<<" ";
|
||||
cout<<endl;
|
||||
for(int i=0;i<size;i++){
|
||||
cout<<i+1<<" ";
|
||||
for(int j=0;j<size;j++) cout<<grid[i][j]<<" ";
|
||||
cout<<endl;
|
||||
}
|
||||
cout<<endl;
|
||||
|
||||
for(int i=0;i<mines;i++){
|
||||
cout<<mineLocations[i]<<" ";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
};
|
||||
|
||||
int main(){
|
||||
|
||||
Minesweeper game(7, 10);
|
||||
game.displayGrid();
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
133
.config/Code/User/History/55ccb45e/eg0I.cpp
Normal file
133
.config/Code/User/History/55ccb45e/eg0I.cpp
Normal file
|
|
@ -0,0 +1,133 @@
|
|||
/*
|
||||
Rafay Ahmad
|
||||
23I-2526
|
||||
*/
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
class Minesweeper{
|
||||
char** grid;
|
||||
int* mineLocations;
|
||||
int size;
|
||||
int mines;
|
||||
int opened;
|
||||
|
||||
public:
|
||||
|
||||
Minesweeper(int size, int mines) : size(size), mines(mines), opened(0){
|
||||
srand(time(0));
|
||||
grid = new char*[size];
|
||||
for(int i=0;i<size;i++){
|
||||
grid[i] = new char[size];
|
||||
for(int j=0;j<size;j++) grid[i][j] = '#';
|
||||
}
|
||||
mineLocations = new int[mines];
|
||||
setMines();
|
||||
}
|
||||
~Minesweeper(){
|
||||
for(int i=0;i<size;i++) delete[] grid[i];
|
||||
delete[] grid;
|
||||
delete[] mineLocations;
|
||||
}
|
||||
|
||||
void reset(){
|
||||
for(int i=0;i<size;i++) for(int j=0;j<size;j++) grid[i][j] = '#';
|
||||
setMines();
|
||||
}
|
||||
void revealAll(){
|
||||
for(int i=0;i<size;i++){
|
||||
for(int j=0;j<size;j++){
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
bool revealCell(int row, int col, bool checkMine){
|
||||
for(int i=0;i<mines;i++){
|
||||
if(mineLocations[i]/size == row && mineLocations[i]%7 == col){
|
||||
grid[row][col] = '*';
|
||||
revealAll();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
grid[row][col] = checkSurrounding(row, col) + '0';
|
||||
opened++;
|
||||
if(grid[row][col] == '0'){
|
||||
return revealCell(row+1, col+1, false);
|
||||
return revealCell(row, col+1, false);
|
||||
return revealCell(row-1, col+1, false);
|
||||
return revealCell(row-1, col, false);
|
||||
return revealCell(row-1, col-1, false);
|
||||
return revealCell(row, col-1, false);
|
||||
return revealCell(row+1, col-1, false);
|
||||
return revealCell(row+1, col, false);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
void flagCell(int row, int col){
|
||||
grid[row][col] = 'P';
|
||||
}
|
||||
int checkSurrounding(int row, int col){
|
||||
int count = 0;
|
||||
for(int i=0;i<mines;i++){
|
||||
if(mineLocations[i]/size == row+1 && mineLocations[i]%7 == col+1) count++;
|
||||
if(mineLocations[i]/size == row && mineLocations[i]%7 == col+1) count++;
|
||||
if(mineLocations[i]/size == row-1 && mineLocations[i]%7 == col+1) count++;
|
||||
if(mineLocations[i]/size == row-1 && mineLocations[i]%7 == col) count++;
|
||||
if(mineLocations[i]/size == row-1 && mineLocations[i]%7 == col-1) count++;
|
||||
if(mineLocations[i]/size == row && mineLocations[i]%7 == col-1) count++;
|
||||
if(mineLocations[i]/size == row+1 && mineLocations[i]%7 == col-1) count++;
|
||||
if(mineLocations[i]/size == row+1 && mineLocations[i]%7 == col) count++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
bool checkWin(){
|
||||
return opened == (size*size) - mines;
|
||||
}
|
||||
void setMines(){
|
||||
for(int i=0;i<mines;i++){
|
||||
bool valid;
|
||||
do{
|
||||
valid = true;
|
||||
mineLocations[i] = rand()%(size*size);
|
||||
for(int j=0;j<i;j++){
|
||||
if(mineLocations[j] == mineLocations[i]){
|
||||
valid = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}while(!valid);
|
||||
}
|
||||
}
|
||||
void displayGrid(){
|
||||
cout<<" ";
|
||||
for(int i=0;i<size;i++) cout<<i+1<<" ";
|
||||
cout<<endl;
|
||||
for(int i=0;i<size;i++){
|
||||
cout<<i+1<<" ";
|
||||
for(int j=0;j<size;j++) cout<<grid[i][j]<<" ";
|
||||
cout<<endl;
|
||||
}
|
||||
cout<<endl;
|
||||
|
||||
for(int i=0;i<mines;i++){
|
||||
cout<<mineLocations[i]<<" ";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
};
|
||||
|
||||
int main(){
|
||||
|
||||
Minesweeper game(7, 10);
|
||||
while(game.checkWin()){
|
||||
game.displayGrid();
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
1
.config/Code/User/History/55ccb45e/entries.json
Normal file
1
.config/Code/User/History/55ccb45e/entries.json
Normal file
|
|
@ -0,0 +1 @@
|
|||
{"version":1,"resource":"file:///home/rafayahmad/Stuff/a3/q7.cpp","entries":[{"id":"jlR0.cpp","timestamp":1711254043438},{"id":"goa4.cpp","timestamp":1711254073138},{"id":"jos8.cpp","timestamp":1711254122888},{"id":"hd3m.cpp","timestamp":1711254138018},{"id":"q3qi.cpp","timestamp":1711254202175},{"id":"5YIQ.cpp","timestamp":1711254228675},{"id":"cpy0.cpp","timestamp":1711254249408},{"id":"XEO9.cpp","timestamp":1711254264071},{"id":"S23Y.cpp","timestamp":1711254303258},{"id":"OdcP.cpp","timestamp":1711254341331},{"id":"iUOH.cpp","timestamp":1711254419041},{"id":"dzLt.cpp","timestamp":1711254441851},{"id":"wqG2.cpp","timestamp":1711254468024},{"id":"eg0I.cpp","timestamp":1711254512514},{"id":"c2Ob.cpp","timestamp":1711254591781},{"id":"VkWk.cpp","timestamp":1711254610938},{"id":"1uka.cpp","timestamp":1711254650391},{"id":"PxZX.cpp","timestamp":1711254685544},{"id":"FY8l.cpp","timestamp":1711254708964},{"id":"voH8.cpp","timestamp":1711254737201},{"id":"0AOl.cpp","timestamp":1711254791481},{"id":"sgz5.cpp","timestamp":1711254867884},{"id":"JcxY.cpp","timestamp":1711255180846},{"id":"6Qz5.cpp","timestamp":1711255248953},{"id":"5WzR.cpp","timestamp":1711255264363},{"id":"nTnM.cpp","timestamp":1711255291733},{"id":"pO5V.cpp","timestamp":1711255396356},{"id":"6yRR.cpp","timestamp":1711255831198},{"id":"D0jW.cpp","timestamp":1711255879905},{"id":"J3Mn.cpp","timestamp":1711256097138},{"id":"BcJ2.cpp","timestamp":1711256163278},{"id":"dcTS.cpp","timestamp":1711256232378},{"id":"yGA5.cpp","timestamp":1711256269631},{"id":"eo7k.cpp","timestamp":1711256297061},{"id":"yR2O.cpp","timestamp":1711256308954},{"id":"Lkwy.cpp","timestamp":1711256374747},{"id":"5N6h.cpp","timestamp":1711256430827},{"id":"vIy4.cpp","timestamp":1711256462141},{"id":"MBKB.cpp","timestamp":1711256490954},{"id":"MPWO.cpp","timestamp":1711256553474},{"id":"8B1I.cpp","timestamp":1711256640730},{"id":"Z7Qq.cpp","timestamp":1711256658917},{"id":"HDYg.cpp","timestamp":1711256685604},{"id":"CSWc.cpp","timestamp":1711256987383},{"id":"dScQ.cpp","timestamp":1711257375133},{"id":"5uW9.cpp","timestamp":1711257394746},{"id":"3nsc.cpp","timestamp":1711257572859},{"id":"HkvZ.cpp","timestamp":1711257674969},{"id":"ySrA.cpp","timestamp":1711257968055},{"id":"RY1w.cpp","timestamp":1711869537780}]}
|
||||
156
.config/Code/User/History/55ccb45e/eo7k.cpp
Normal file
156
.config/Code/User/History/55ccb45e/eo7k.cpp
Normal file
|
|
@ -0,0 +1,156 @@
|
|||
/*
|
||||
Rafay Ahmad
|
||||
23I-2526
|
||||
*/
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
class Minesweeper{
|
||||
char** grid;
|
||||
int* mineLocations;
|
||||
int size;
|
||||
int mines;
|
||||
int opened;
|
||||
|
||||
public:
|
||||
|
||||
Minesweeper(int size, int mines) : size(size), mines(mines), opened(0){
|
||||
srand(time(0));
|
||||
grid = new char*[size];
|
||||
for(int i=0;i<size;i++){
|
||||
grid[i] = new char[size];
|
||||
for(int j=0;j<size;j++) grid[i][j] = '#';
|
||||
}
|
||||
mineLocations = new int[mines];
|
||||
setMines();
|
||||
}
|
||||
~Minesweeper(){
|
||||
for(int i=0;i<size;i++) delete[] grid[i];
|
||||
delete[] grid;
|
||||
delete[] mineLocations;
|
||||
}
|
||||
|
||||
void reset(){
|
||||
for(int i=0;i<size;i++) for(int j=0;j<size;j++) grid[i][j] = '#';
|
||||
setMines();
|
||||
}
|
||||
void revealAll(){
|
||||
for(int i=0;i<mines;i++){
|
||||
grid[mineLocations[i]/7][mineLocations[i]%7] = '*';
|
||||
}
|
||||
}
|
||||
bool revealCell(int row, int col, bool checkMine){
|
||||
if(checkMine){
|
||||
for(int i=0;i<mines;i++){
|
||||
if(mineLocations[i]/size == row && mineLocations[i]%7 == col){
|
||||
grid[row][col] = '*';
|
||||
revealAll();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
grid[row][col] = checkSurrounding(row, col) + '0';
|
||||
opened++;
|
||||
if(grid[row][col] == '0'){
|
||||
return revealCell(row+1, col+1, false);
|
||||
return revealCell(row, col+1, false);
|
||||
return revealCell(row-1, col+1, false);
|
||||
return revealCell(row-1, col, false);
|
||||
return revealCell(row-1, col-1, false);
|
||||
return revealCell(row, col-1, false);
|
||||
return revealCell(row+1, col-1, false);
|
||||
return revealCell(row+1, col, false);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
void flagCell(int row, int col){
|
||||
grid[row][col] = 'P';
|
||||
}
|
||||
int checkSurrounding(int row, int col){
|
||||
int count = 0;
|
||||
for(int i=0;i<mines;i++){
|
||||
if(row<size && col<size && mineLocations[i]/size == row+1 && mineLocations[i]%7 == col+1) count++;
|
||||
if(col<size && mineLocations[i]/size == row && mineLocations[i]%7 == col+1) count++;
|
||||
if(row>0 && col<size && mineLocations[i]/size == row-1 && mineLocations[i]%7 == col+1) count++;
|
||||
if(row>0 && mineLocations[i]/size == row-1 && mineLocations[i]%7 == col) count++;
|
||||
if(row>0 && col>0 && mineLocations[i]/size == row-1 && mineLocations[i]%7 == col-1) count++;
|
||||
if(col>0 && mineLocations[i]/size == row && mineLocations[i]%7 == col-1) count++;
|
||||
if(row<size && mineLocations[i]/size == row+1 && mineLocations[i]%7 == col-1) count++;
|
||||
if(row<size && mineLocations[i]/size == row+1 && mineLocations[i]%7 == col) count++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
bool checkWin(){
|
||||
return opened == ((size*size) - mines);
|
||||
}
|
||||
void setMines(){
|
||||
for(int i=0;i<mines;i++){
|
||||
bool valid;
|
||||
do{
|
||||
valid = true;
|
||||
mineLocations[i] = rand()%(size*size);
|
||||
for(int j=0;j<i;j++){
|
||||
if(mineLocations[j] == mineLocations[i]){
|
||||
valid = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}while(!valid);
|
||||
}
|
||||
}
|
||||
void displayGrid(){
|
||||
cout<<" ";
|
||||
for(int i=0;i<size;i++) cout<<i<<" ";
|
||||
cout<<endl;
|
||||
for(int i=0;i<size;i++){
|
||||
cout<<i<<" ";
|
||||
for(int j=0;j<size;j++) cout<<grid[i][j]<<" ";
|
||||
cout<<endl;
|
||||
}
|
||||
cout<<endl;
|
||||
|
||||
for(int i=0;i<mines;i++){
|
||||
cout<<mineLocations[i]<<" ";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
};
|
||||
|
||||
int main(){
|
||||
|
||||
int row, col;
|
||||
char c;
|
||||
int size, mines;
|
||||
cout<<"Enter size of the game board: ";
|
||||
cin>>size;
|
||||
cout<<"Enter no Of mines: ";
|
||||
cin>>mines;
|
||||
Minesweeper game(size, mines);
|
||||
|
||||
while(!game.checkWin()){
|
||||
game.displayGrid();
|
||||
cout<<"Enter row: ";
|
||||
cin>>row;
|
||||
cout<<"Enter col: ";
|
||||
cin>>col;
|
||||
cout<<"Enter F to flag or R to reveal: ";
|
||||
cin>>c;
|
||||
|
||||
if(row<size && col<size && (c == 'F' || c == 'R')){
|
||||
if(c == 'R' && !game.revealCell(row, col, true)){
|
||||
game.displayGrid();
|
||||
cout<<"You Lost\n";
|
||||
break;
|
||||
}
|
||||
else if(c == 'F') game.flagCell(row, col);
|
||||
}
|
||||
else cout<<"Invalid input\n";
|
||||
|
||||
}
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
122
.config/Code/User/History/55ccb45e/goa4.cpp
Normal file
122
.config/Code/User/History/55ccb45e/goa4.cpp
Normal file
|
|
@ -0,0 +1,122 @@
|
|||
/*
|
||||
Rafay Ahmad
|
||||
23I-2526
|
||||
*/
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
class Minesweeper{
|
||||
char** grid;
|
||||
int* mineLocations;
|
||||
int size;
|
||||
int mines;
|
||||
|
||||
public:
|
||||
|
||||
Minesweeper(int size, int mines) : size(size), mines(mines){
|
||||
srand(time(0));
|
||||
grid = new char*[size];
|
||||
for(int i=0;i<size;i++){
|
||||
grid[i] = new char[size];
|
||||
for(int j=0;j<size;j++) grid[i][j] = '#';
|
||||
}
|
||||
mineLocations = new int[mines];
|
||||
setMines();
|
||||
}
|
||||
~Minesweeper(){
|
||||
for(int i=0;i<size;i++) delete[] grid[i];
|
||||
delete[] grid;
|
||||
delete[] mineLocations;
|
||||
}
|
||||
|
||||
void reset(){
|
||||
for(int i=0;i<size;i++) for(int j=0;j<size;j++) grid[i][j] = '#';
|
||||
setMines();
|
||||
}
|
||||
void revealAll(){
|
||||
|
||||
}
|
||||
bool revealCell(int row, int col){
|
||||
for(int i=0;i<mines;i++){
|
||||
if(mineLocations[i]/size == row && mineLocations[i]%7 == col){
|
||||
revealAll();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
grid[row][col] = checkSurrounding(row, col) + '0';
|
||||
if(grid[row][col] == '0'){
|
||||
revealCell(row+1, col+1);
|
||||
revealCell(row+1, col+1);
|
||||
revealCell(row+1, col+1);
|
||||
revealCell(row+1, col+1);
|
||||
revealCell(row+1, col+1);
|
||||
revealCell(row+1, col+1);
|
||||
revealCell(row+1, col+1);
|
||||
revealCell(row+1, col+1);
|
||||
}
|
||||
|
||||
}
|
||||
void flagCell(int row, int col){
|
||||
grid[row][col] = 'P';
|
||||
}
|
||||
int checkSurrounding(int row, int col){
|
||||
int count = 0;
|
||||
for(int i=0;i<mines;i++){
|
||||
if(mineLocations[i]/size == row+1 && mineLocations[i]%7 == col+1) count++;
|
||||
if(mineLocations[i]/size == row && mineLocations[i]%7 == col+1) count++;
|
||||
if(mineLocations[i]/size == row-1 && mineLocations[i]%7 == col+1) count++;
|
||||
if(mineLocations[i]/size == row-1 && mineLocations[i]%7 == col) count++;
|
||||
if(mineLocations[i]/size == row-1 && mineLocations[i]%7 == col-1) count++;
|
||||
if(mineLocations[i]/size == row && mineLocations[i]%7 == col-1) count++;
|
||||
if(mineLocations[i]/size == row+1 && mineLocations[i]%7 == col-1) count++;
|
||||
if(mineLocations[i]/size == row+1 && mineLocations[i]%7 == col) count++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
bool checkWin(){
|
||||
|
||||
}
|
||||
void setMines(){
|
||||
for(int i=0;i<mines;i++){
|
||||
bool valid;
|
||||
do{
|
||||
valid = true;
|
||||
mineLocations[i] = rand()%(size*size);
|
||||
for(int j=0;j<i;j++){
|
||||
if(mineLocations[j] == mineLocations[i]){
|
||||
valid = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}while(!valid);
|
||||
}
|
||||
}
|
||||
void displayGrid(){
|
||||
cout<<" ";
|
||||
for(int i=0;i<size;i++) cout<<i+1<<" ";
|
||||
cout<<endl;
|
||||
for(int i=0;i<size;i++){
|
||||
cout<<i+1<<" ";
|
||||
for(int j=0;j<size;j++) cout<<grid[i][j]<<" ";
|
||||
cout<<endl;
|
||||
}
|
||||
cout<<endl;
|
||||
|
||||
for(int i=0;i<mines;i++){
|
||||
cout<<mineLocations[i]<<" ";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
};
|
||||
|
||||
int main(){
|
||||
|
||||
Minesweeper game(7, 10);
|
||||
game.displayGrid();
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
123
.config/Code/User/History/55ccb45e/hd3m.cpp
Normal file
123
.config/Code/User/History/55ccb45e/hd3m.cpp
Normal file
|
|
@ -0,0 +1,123 @@
|
|||
/*
|
||||
Rafay Ahmad
|
||||
23I-2526
|
||||
*/
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
class Minesweeper{
|
||||
char** grid;
|
||||
int* mineLocations;
|
||||
int size;
|
||||
int mines;
|
||||
|
||||
public:
|
||||
|
||||
Minesweeper(int size, int mines) : size(size), mines(mines){
|
||||
srand(time(0));
|
||||
grid = new char*[size];
|
||||
for(int i=0;i<size;i++){
|
||||
grid[i] = new char[size];
|
||||
for(int j=0;j<size;j++) grid[i][j] = '#';
|
||||
}
|
||||
mineLocations = new int[mines];
|
||||
setMines();
|
||||
}
|
||||
~Minesweeper(){
|
||||
for(int i=0;i<size;i++) delete[] grid[i];
|
||||
delete[] grid;
|
||||
delete[] mineLocations;
|
||||
}
|
||||
|
||||
void reset(){
|
||||
for(int i=0;i<size;i++) for(int j=0;j<size;j++) grid[i][j] = '#';
|
||||
setMines();
|
||||
}
|
||||
void revealAll(){
|
||||
|
||||
}
|
||||
bool revealCell(int row, int col){
|
||||
for(int i=0;i<mines;i++){
|
||||
if(mineLocations[i]/size == row && mineLocations[i]%7 == col){
|
||||
revealAll();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
grid[row][col] = checkSurrounding(row, col) + '0';
|
||||
if(grid[row][col] == '0'){
|
||||
revealCell(row+1, col+1);
|
||||
revealCell(row, col+1);
|
||||
revealCell(row-1, col+1);
|
||||
revealCell(row-1, col);
|
||||
revealCell(row-1, col-1);
|
||||
revealCell(row, col-1);
|
||||
revealCell(row+1, col-1);
|
||||
revealCell(row+1, col);
|
||||
}
|
||||
return true;
|
||||
|
||||
}
|
||||
void flagCell(int row, int col){
|
||||
grid[row][col] = 'P';
|
||||
}
|
||||
int checkSurrounding(int row, int col){
|
||||
int count = 0;
|
||||
for(int i=0;i<mines;i++){
|
||||
if(mineLocations[i]/size == row+1 && mineLocations[i]%7 == col+1) count++;
|
||||
if(mineLocations[i]/size == row && mineLocations[i]%7 == col+1) count++;
|
||||
if(mineLocations[i]/size == row-1 && mineLocations[i]%7 == col+1) count++;
|
||||
if(mineLocations[i]/size == row-1 && mineLocations[i]%7 == col) count++;
|
||||
if(mineLocations[i]/size == row-1 && mineLocations[i]%7 == col-1) count++;
|
||||
if(mineLocations[i]/size == row && mineLocations[i]%7 == col-1) count++;
|
||||
if(mineLocations[i]/size == row+1 && mineLocations[i]%7 == col-1) count++;
|
||||
if(mineLocations[i]/size == row+1 && mineLocations[i]%7 == col) count++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
bool checkWin(){
|
||||
|
||||
}
|
||||
void setMines(){
|
||||
for(int i=0;i<mines;i++){
|
||||
bool valid;
|
||||
do{
|
||||
valid = true;
|
||||
mineLocations[i] = rand()%(size*size);
|
||||
for(int j=0;j<i;j++){
|
||||
if(mineLocations[j] == mineLocations[i]){
|
||||
valid = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}while(!valid);
|
||||
}
|
||||
}
|
||||
void displayGrid(){
|
||||
cout<<" ";
|
||||
for(int i=0;i<size;i++) cout<<i+1<<" ";
|
||||
cout<<endl;
|
||||
for(int i=0;i<size;i++){
|
||||
cout<<i+1<<" ";
|
||||
for(int j=0;j<size;j++) cout<<grid[i][j]<<" ";
|
||||
cout<<endl;
|
||||
}
|
||||
cout<<endl;
|
||||
|
||||
for(int i=0;i<mines;i++){
|
||||
cout<<mineLocations[i]<<" ";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
};
|
||||
|
||||
int main(){
|
||||
|
||||
Minesweeper game(7, 10);
|
||||
game.displayGrid();
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
128
.config/Code/User/History/55ccb45e/iUOH.cpp
Normal file
128
.config/Code/User/History/55ccb45e/iUOH.cpp
Normal file
|
|
@ -0,0 +1,128 @@
|
|||
/*
|
||||
Rafay Ahmad
|
||||
23I-2526
|
||||
*/
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
class Minesweeper{
|
||||
char** grid;
|
||||
int* mineLocations;
|
||||
int size;
|
||||
int mines;
|
||||
int opened;
|
||||
|
||||
public:
|
||||
|
||||
Minesweeper(int size, int mines) : size(size), mines(mines), opened(0){
|
||||
srand(time(0));
|
||||
grid = new char*[size];
|
||||
for(int i=0;i<size;i++){
|
||||
grid[i] = new char[size];
|
||||
for(int j=0;j<size;j++) grid[i][j] = '#';
|
||||
}
|
||||
mineLocations = new int[mines];
|
||||
setMines();
|
||||
}
|
||||
~Minesweeper(){
|
||||
for(int i=0;i<size;i++) delete[] grid[i];
|
||||
delete[] grid;
|
||||
delete[] mineLocations;
|
||||
}
|
||||
|
||||
void reset(){
|
||||
for(int i=0;i<size;i++) for(int j=0;j<size;j++) grid[i][j] = '#';
|
||||
setMines();
|
||||
}
|
||||
void revealAll(){
|
||||
for(int i=0;i<size;i++){
|
||||
for(int j=0;j<size;j++){
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
bool revealCell(int row, int col, bool checkMine){
|
||||
for(int i=0;i<mines;i++){
|
||||
if(mineLocations[i]/size == row && mineLocations[i]%7 == col){
|
||||
grid[row][col] = '*';
|
||||
revealAll();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
grid[row][col] = checkSurrounding(row, col) + '0';
|
||||
if(grid[row][col] == '0'){
|
||||
return revealCell(row+1, col+1, false);
|
||||
return revealCell(row, col+1, false);
|
||||
return revealCell(row-1, col+1, false);
|
||||
return revealCell(row-1, col, false);
|
||||
return revealCell(row-1, col-1, false);
|
||||
return revealCell(row, col-1, false);
|
||||
return revealCell(row+1, col-1, false);
|
||||
return revealCell(row+1, col, false);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
void flagCell(int row, int col){
|
||||
grid[row][col] = 'P';
|
||||
}
|
||||
int checkSurrounding(int row, int col){
|
||||
int count = 0;
|
||||
for(int i=0;i<mines;i++){
|
||||
if(mineLocations[i]/size == row+1 && mineLocations[i]%7 == col+1) count++;
|
||||
if(mineLocations[i]/size == row && mineLocations[i]%7 == col+1) count++;
|
||||
if(mineLocations[i]/size == row-1 && mineLocations[i]%7 == col+1) count++;
|
||||
if(mineLocations[i]/size == row-1 && mineLocations[i]%7 == col) count++;
|
||||
if(mineLocations[i]/size == row-1 && mineLocations[i]%7 == col-1) count++;
|
||||
if(mineLocations[i]/size == row && mineLocations[i]%7 == col-1) count++;
|
||||
if(mineLocations[i]/size == row+1 && mineLocations[i]%7 == col-1) count++;
|
||||
if(mineLocations[i]/size == row+1 && mineLocations[i]%7 == col) count++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
bool checkWin(){
|
||||
|
||||
}
|
||||
void setMines(){
|
||||
for(int i=0;i<mines;i++){
|
||||
bool valid;
|
||||
do{
|
||||
valid = true;
|
||||
mineLocations[i] = rand()%(size*size);
|
||||
for(int j=0;j<i;j++){
|
||||
if(mineLocations[j] == mineLocations[i]){
|
||||
valid = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}while(!valid);
|
||||
}
|
||||
}
|
||||
void displayGrid(){
|
||||
cout<<" ";
|
||||
for(int i=0;i<size;i++) cout<<i+1<<" ";
|
||||
cout<<endl;
|
||||
for(int i=0;i<size;i++){
|
||||
cout<<i+1<<" ";
|
||||
for(int j=0;j<size;j++) cout<<grid[i][j]<<" ";
|
||||
cout<<endl;
|
||||
}
|
||||
cout<<endl;
|
||||
|
||||
for(int i=0;i<mines;i++){
|
||||
cout<<mineLocations[i]<<" ";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
};
|
||||
|
||||
int main(){
|
||||
|
||||
Minesweeper game(7, 10);
|
||||
game.displayGrid();
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
115
.config/Code/User/History/55ccb45e/jlR0.cpp
Normal file
115
.config/Code/User/History/55ccb45e/jlR0.cpp
Normal file
|
|
@ -0,0 +1,115 @@
|
|||
/*
|
||||
Rafay Ahmad
|
||||
23I-2526
|
||||
*/
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
class Minesweeper{
|
||||
char** grid;
|
||||
int* mineLocations;
|
||||
int size;
|
||||
int mines;
|
||||
|
||||
public:
|
||||
|
||||
Minesweeper(int size, int mines) : size(size), mines(mines){
|
||||
srand(time(0));
|
||||
grid = new char*[size];
|
||||
for(int i=0;i<size;i++){
|
||||
grid[i] = new char[size];
|
||||
for(int j=0;j<size;j++) grid[i][j] = '#';
|
||||
}
|
||||
mineLocations = new int[mines];
|
||||
setMines();
|
||||
}
|
||||
~Minesweeper(){
|
||||
for(int i=0;i<size;i++) delete[] grid[i];
|
||||
delete[] grid;
|
||||
delete[] mineLocations;
|
||||
}
|
||||
|
||||
void reset(){
|
||||
for(int i=0;i<size;i++) for(int j=0;j<size;j++) grid[i][j] = '#';
|
||||
setMines();
|
||||
}
|
||||
void revealAll(){
|
||||
|
||||
}
|
||||
bool revealCell(int row, int col){
|
||||
for(int i=0;i<mines;i++){
|
||||
if(mineLocations[i]/size == row && mineLocations[i]%7 == col){
|
||||
revealAll();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
grid[row][col] = checkSurrounding(row, col) + '0';
|
||||
if(grid[row][col] == '0'){
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
void flagCell(int row, int col){
|
||||
grid[row][col] = 'P';
|
||||
}
|
||||
int checkSurrounding(int row, int col){
|
||||
int count = 0;
|
||||
for(int i=0;i<mines;i++){
|
||||
if(mineLocations[i]/size == row+1 && mineLocations[i]%7 == col+1) count++;
|
||||
if(mineLocations[i]/size == row && mineLocations[i]%7 == col+1) count++;
|
||||
if(mineLocations[i]/size == row-1 && mineLocations[i]%7 == col+1) count++;
|
||||
if(mineLocations[i]/size == row-1 && mineLocations[i]%7 == col) count++;
|
||||
if(mineLocations[i]/size == row-1 && mineLocations[i]%7 == col-1) count++;
|
||||
if(mineLocations[i]/size == row && mineLocations[i]%7 == col-1) count++;
|
||||
if(mineLocations[i]/size == row+1 && mineLocations[i]%7 == col-1) count++;
|
||||
if(mineLocations[i]/size == row+1 && mineLocations[i]%7 == col) count++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
bool checkWin(){
|
||||
|
||||
}
|
||||
void setMines(){
|
||||
for(int i=0;i<mines;i++){
|
||||
bool valid;
|
||||
do{
|
||||
valid = true;
|
||||
mineLocations[i] = rand()%(size*size);
|
||||
for(int j=0;j<i;j++){
|
||||
if(mineLocations[j] == mineLocations[i]){
|
||||
valid = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}while(!valid);
|
||||
}
|
||||
}
|
||||
void displayGrid(){
|
||||
cout<<" ";
|
||||
for(int i=0;i<size;i++) cout<<i+1<<" ";
|
||||
cout<<endl;
|
||||
for(int i=0;i<size;i++){
|
||||
cout<<i+1<<" ";
|
||||
for(int j=0;j<size;j++) cout<<grid[i][j]<<" ";
|
||||
cout<<endl;
|
||||
}
|
||||
cout<<endl;
|
||||
|
||||
for(int i=0;i<mines;i++){
|
||||
cout<<mineLocations[i]<<" ";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
};
|
||||
|
||||
int main(){
|
||||
|
||||
Minesweeper game(7, 10);
|
||||
game.displayGrid();
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
122
.config/Code/User/History/55ccb45e/jos8.cpp
Normal file
122
.config/Code/User/History/55ccb45e/jos8.cpp
Normal file
|
|
@ -0,0 +1,122 @@
|
|||
/*
|
||||
Rafay Ahmad
|
||||
23I-2526
|
||||
*/
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
class Minesweeper{
|
||||
char** grid;
|
||||
int* mineLocations;
|
||||
int size;
|
||||
int mines;
|
||||
|
||||
public:
|
||||
|
||||
Minesweeper(int size, int mines) : size(size), mines(mines){
|
||||
srand(time(0));
|
||||
grid = new char*[size];
|
||||
for(int i=0;i<size;i++){
|
||||
grid[i] = new char[size];
|
||||
for(int j=0;j<size;j++) grid[i][j] = '#';
|
||||
}
|
||||
mineLocations = new int[mines];
|
||||
setMines();
|
||||
}
|
||||
~Minesweeper(){
|
||||
for(int i=0;i<size;i++) delete[] grid[i];
|
||||
delete[] grid;
|
||||
delete[] mineLocations;
|
||||
}
|
||||
|
||||
void reset(){
|
||||
for(int i=0;i<size;i++) for(int j=0;j<size;j++) grid[i][j] = '#';
|
||||
setMines();
|
||||
}
|
||||
void revealAll(){
|
||||
|
||||
}
|
||||
bool revealCell(int row, int col){
|
||||
for(int i=0;i<mines;i++){
|
||||
if(mineLocations[i]/size == row && mineLocations[i]%7 == col){
|
||||
revealAll();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
grid[row][col] = checkSurrounding(row, col) + '0';
|
||||
if(grid[row][col] == '0'){
|
||||
revealCell(row+1, col+1);
|
||||
revealCell(row, col+1);
|
||||
revealCell(row-1, col+1);
|
||||
revealCell(row-1, col);
|
||||
revealCell(row-1, col-1);
|
||||
revealCell(row, col-1);
|
||||
revealCell(row+1, col-1);
|
||||
revealCell(row+1, col);
|
||||
}
|
||||
|
||||
}
|
||||
void flagCell(int row, int col){
|
||||
grid[row][col] = 'P';
|
||||
}
|
||||
int checkSurrounding(int row, int col){
|
||||
int count = 0;
|
||||
for(int i=0;i<mines;i++){
|
||||
if(mineLocations[i]/size == row+1 && mineLocations[i]%7 == col+1) count++;
|
||||
if(mineLocations[i]/size == row && mineLocations[i]%7 == col+1) count++;
|
||||
if(mineLocations[i]/size == row-1 && mineLocations[i]%7 == col+1) count++;
|
||||
if(mineLocations[i]/size == row-1 && mineLocations[i]%7 == col) count++;
|
||||
if(mineLocations[i]/size == row-1 && mineLocations[i]%7 == col-1) count++;
|
||||
if(mineLocations[i]/size == row && mineLocations[i]%7 == col-1) count++;
|
||||
if(mineLocations[i]/size == row+1 && mineLocations[i]%7 == col-1) count++;
|
||||
if(mineLocations[i]/size == row+1 && mineLocations[i]%7 == col) count++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
bool checkWin(){
|
||||
|
||||
}
|
||||
void setMines(){
|
||||
for(int i=0;i<mines;i++){
|
||||
bool valid;
|
||||
do{
|
||||
valid = true;
|
||||
mineLocations[i] = rand()%(size*size);
|
||||
for(int j=0;j<i;j++){
|
||||
if(mineLocations[j] == mineLocations[i]){
|
||||
valid = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}while(!valid);
|
||||
}
|
||||
}
|
||||
void displayGrid(){
|
||||
cout<<" ";
|
||||
for(int i=0;i<size;i++) cout<<i+1<<" ";
|
||||
cout<<endl;
|
||||
for(int i=0;i<size;i++){
|
||||
cout<<i+1<<" ";
|
||||
for(int j=0;j<size;j++) cout<<grid[i][j]<<" ";
|
||||
cout<<endl;
|
||||
}
|
||||
cout<<endl;
|
||||
|
||||
for(int i=0;i<mines;i++){
|
||||
cout<<mineLocations[i]<<" ";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
};
|
||||
|
||||
int main(){
|
||||
|
||||
Minesweeper game(7, 10);
|
||||
game.displayGrid();
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
154
.config/Code/User/History/55ccb45e/nTnM.cpp
Normal file
154
.config/Code/User/History/55ccb45e/nTnM.cpp
Normal file
|
|
@ -0,0 +1,154 @@
|
|||
/*
|
||||
Rafay Ahmad
|
||||
23I-2526
|
||||
*/
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
class Minesweeper{
|
||||
char** grid;
|
||||
int* mineLocations;
|
||||
int size;
|
||||
int mines;
|
||||
int opened;
|
||||
|
||||
public:
|
||||
|
||||
Minesweeper(int size, int mines) : size(size), mines(mines), opened(0){
|
||||
srand(time(0));
|
||||
grid = new char*[size];
|
||||
for(int i=0;i<size;i++){
|
||||
grid[i] = new char[size];
|
||||
for(int j=0;j<size;j++) grid[i][j] = '#';
|
||||
}
|
||||
mineLocations = new int[mines];
|
||||
setMines();
|
||||
}
|
||||
~Minesweeper(){
|
||||
for(int i=0;i<size;i++) delete[] grid[i];
|
||||
delete[] grid;
|
||||
delete[] mineLocations;
|
||||
}
|
||||
|
||||
void reset(){
|
||||
for(int i=0;i<size;i++) for(int j=0;j<size;j++) grid[i][j] = '#';
|
||||
setMines();
|
||||
}
|
||||
void revealAll(){
|
||||
for(int i=0;i<mines;i++){
|
||||
grid[mineLocations[i]/7][mineLocations[i]%7] = '*';
|
||||
}
|
||||
}
|
||||
bool revealCell(int row, int col, bool checkMine){
|
||||
for(int i=0;i<mines;i++){
|
||||
if(mineLocations[i]/size == row && mineLocations[i]%7 == col){
|
||||
grid[row][col] = '*';
|
||||
revealAll();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
grid[row][col] = checkSurrounding(row, col) + '0';
|
||||
opened++;
|
||||
if(grid[row][col] == '0'){
|
||||
return revealCell(row+1, col+1, false);
|
||||
return revealCell(row, col+1, false);
|
||||
return revealCell(row-1, col+1, false);
|
||||
return revealCell(row-1, col, false);
|
||||
return revealCell(row-1, col-1, false);
|
||||
return revealCell(row, col-1, false);
|
||||
return revealCell(row+1, col-1, false);
|
||||
return revealCell(row+1, col, false);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
void flagCell(int row, int col){
|
||||
grid[row][col] = 'P';
|
||||
}
|
||||
int checkSurrounding(int row, int col){
|
||||
int count = 0;
|
||||
for(int i=0;i<mines;i++){
|
||||
if(mineLocations[i]/size == row+1 && mineLocations[i]%7 == col+1) count++;
|
||||
if(mineLocations[i]/size == row && mineLocations[i]%7 == col+1) count++;
|
||||
if(mineLocations[i]/size == row-1 && mineLocations[i]%7 == col+1) count++;
|
||||
if(mineLocations[i]/size == row-1 && mineLocations[i]%7 == col) count++;
|
||||
if(mineLocations[i]/size == row-1 && mineLocations[i]%7 == col-1) count++;
|
||||
if(mineLocations[i]/size == row && mineLocations[i]%7 == col-1) count++;
|
||||
if(mineLocations[i]/size == row+1 && mineLocations[i]%7 == col-1) count++;
|
||||
if(mineLocations[i]/size == row+1 && mineLocations[i]%7 == col) count++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
bool checkWin(){
|
||||
return opened == ((size*size) - mines);
|
||||
}
|
||||
void setMines(){
|
||||
for(int i=0;i<mines;i++){
|
||||
bool valid;
|
||||
do{
|
||||
valid = true;
|
||||
mineLocations[i] = rand()%(size*size);
|
||||
for(int j=0;j<i;j++){
|
||||
if(mineLocations[j] == mineLocations[i]){
|
||||
valid = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}while(!valid);
|
||||
}
|
||||
}
|
||||
void displayGrid(){
|
||||
cout<<" ";
|
||||
for(int i=0;i<size;i++) cout<<i+1<<" ";
|
||||
cout<<endl;
|
||||
for(int i=0;i<size;i++){
|
||||
cout<<i+1<<" ";
|
||||
for(int j=0;j<size;j++) cout<<grid[i][j]<<" ";
|
||||
cout<<endl;
|
||||
}
|
||||
cout<<endl;
|
||||
|
||||
for(int i=0;i<mines;i++){
|
||||
cout<<mineLocations[i]<<" ";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
};
|
||||
|
||||
int main(){
|
||||
|
||||
int row, col;
|
||||
char c;
|
||||
int size, mines;
|
||||
cout<<"Enter size of the game board: ";
|
||||
cin>>size;
|
||||
cout<<"Enter no Of mines: ";
|
||||
cin>>mines;
|
||||
Minesweeper game(size, mines);
|
||||
|
||||
while(!game.checkWin()){
|
||||
game.displayGrid();
|
||||
cout<<"Enter row: ";
|
||||
cin>>row;
|
||||
cout<<"Enter col: ";
|
||||
cin>>col;
|
||||
cout<<"Enter F to flag or R to reveal: ";
|
||||
cin>>c;
|
||||
|
||||
if(row<size && col<size && (c == 'F' || c == 'R')){
|
||||
if(c == 'R') if(!game.revealCell(row, col, true)){
|
||||
game.displayGrid();
|
||||
cout<<"You Lost\n";
|
||||
break;
|
||||
}
|
||||
else game.flagCell(row, col);
|
||||
}
|
||||
else cout<<"Invalid input\n";
|
||||
|
||||
}
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
154
.config/Code/User/History/55ccb45e/pO5V.cpp
Normal file
154
.config/Code/User/History/55ccb45e/pO5V.cpp
Normal file
|
|
@ -0,0 +1,154 @@
|
|||
/*
|
||||
Rafay Ahmad
|
||||
23I-2526
|
||||
*/
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
class Minesweeper{
|
||||
char** grid;
|
||||
int* mineLocations;
|
||||
int size;
|
||||
int mines;
|
||||
int opened;
|
||||
|
||||
public:
|
||||
|
||||
Minesweeper(int size, int mines) : size(size), mines(mines), opened(0){
|
||||
srand(time(0));
|
||||
grid = new char*[size];
|
||||
for(int i=0;i<size;i++){
|
||||
grid[i] = new char[size];
|
||||
for(int j=0;j<size;j++) grid[i][j] = '#';
|
||||
}
|
||||
mineLocations = new int[mines];
|
||||
setMines();
|
||||
}
|
||||
~Minesweeper(){
|
||||
for(int i=0;i<size;i++) delete[] grid[i];
|
||||
delete[] grid;
|
||||
delete[] mineLocations;
|
||||
}
|
||||
|
||||
void reset(){
|
||||
for(int i=0;i<size;i++) for(int j=0;j<size;j++) grid[i][j] = '#';
|
||||
setMines();
|
||||
}
|
||||
void revealAll(){
|
||||
for(int i=0;i<mines;i++){
|
||||
grid[mineLocations[i]/7][mineLocations[i]%7] = '*';
|
||||
}
|
||||
}
|
||||
bool revealCell(int row, int col, bool checkMine){
|
||||
for(int i=0;i<mines;i++){
|
||||
if(mineLocations[i]/size == row && mineLocations[i]%7 == col){
|
||||
grid[row][col] = '*';
|
||||
revealAll();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
grid[row][col] = checkSurrounding(row, col) + '0';
|
||||
opened++;
|
||||
if(grid[row][col] == '0'){
|
||||
return revealCell(row+1, col+1, false);
|
||||
return revealCell(row, col+1, false);
|
||||
return revealCell(row-1, col+1, false);
|
||||
return revealCell(row-1, col, false);
|
||||
return revealCell(row-1, col-1, false);
|
||||
return revealCell(row, col-1, false);
|
||||
return revealCell(row+1, col-1, false);
|
||||
return revealCell(row+1, col, false);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
void flagCell(int row, int col){
|
||||
grid[row][col] = 'P';
|
||||
}
|
||||
int checkSurrounding(int row, int col){
|
||||
int count = 0;
|
||||
for(int i=0;i<mines;i++){
|
||||
if(mineLocations[i]/size == row+1 && mineLocations[i]%7 == col+1) count++;
|
||||
if(mineLocations[i]/size == row && mineLocations[i]%7 == col+1) count++;
|
||||
if(mineLocations[i]/size == row-1 && mineLocations[i]%7 == col+1) count++;
|
||||
if(mineLocations[i]/size == row-1 && mineLocations[i]%7 == col) count++;
|
||||
if(mineLocations[i]/size == row-1 && mineLocations[i]%7 == col-1) count++;
|
||||
if(mineLocations[i]/size == row && mineLocations[i]%7 == col-1) count++;
|
||||
if(mineLocations[i]/size == row+1 && mineLocations[i]%7 == col-1) count++;
|
||||
if(mineLocations[i]/size == row+1 && mineLocations[i]%7 == col) count++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
bool checkWin(){
|
||||
return opened == ((size*size) - mines);
|
||||
}
|
||||
void setMines(){
|
||||
for(int i=0;i<mines;i++){
|
||||
bool valid;
|
||||
do{
|
||||
valid = true;
|
||||
mineLocations[i] = rand()%(size*size);
|
||||
for(int j=0;j<i;j++){
|
||||
if(mineLocations[j] == mineLocations[i]){
|
||||
valid = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}while(!valid);
|
||||
}
|
||||
}
|
||||
void displayGrid(){
|
||||
cout<<" ";
|
||||
for(int i=0;i<size;i++) cout<<i<<" ";
|
||||
cout<<endl;
|
||||
for(int i=0;i<size;i++){
|
||||
cout<<i<<" ";
|
||||
for(int j=0;j<size;j++) cout<<grid[i][j]<<" ";
|
||||
cout<<endl;
|
||||
}
|
||||
cout<<endl;
|
||||
|
||||
for(int i=0;i<mines;i++){
|
||||
cout<<mineLocations[i]<<" ";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
};
|
||||
|
||||
int main(){
|
||||
|
||||
int row, col;
|
||||
char c;
|
||||
int size, mines;
|
||||
cout<<"Enter size of the game board: ";
|
||||
cin>>size;
|
||||
cout<<"Enter no Of mines: ";
|
||||
cin>>mines;
|
||||
Minesweeper game(size, mines);
|
||||
|
||||
while(!game.checkWin()){
|
||||
game.displayGrid();
|
||||
cout<<"Enter row: ";
|
||||
cin>>row;
|
||||
cout<<"Enter col: ";
|
||||
cin>>col;
|
||||
cout<<"Enter F to flag or R to reveal: ";
|
||||
cin>>c;
|
||||
|
||||
if(row<size && col<size && (c == 'F' || c == 'R')){
|
||||
if(c == 'R') if(!game.revealCell(row, col, true)){
|
||||
game.displayGrid();
|
||||
cout<<"You Lost\n";
|
||||
break;
|
||||
}
|
||||
else game.flagCell(row, col);
|
||||
}
|
||||
else cout<<"Invalid input\n";
|
||||
|
||||
}
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
123
.config/Code/User/History/55ccb45e/q3qi.cpp
Normal file
123
.config/Code/User/History/55ccb45e/q3qi.cpp
Normal file
|
|
@ -0,0 +1,123 @@
|
|||
/*
|
||||
Rafay Ahmad
|
||||
23I-2526
|
||||
*/
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
class Minesweeper{
|
||||
char** grid;
|
||||
int* mineLocations;
|
||||
int size;
|
||||
int mines;
|
||||
|
||||
public:
|
||||
|
||||
Minesweeper(int size, int mines) : size(size), mines(mines){
|
||||
srand(time(0));
|
||||
grid = new char*[size];
|
||||
for(int i=0;i<size;i++){
|
||||
grid[i] = new char[size];
|
||||
for(int j=0;j<size;j++) grid[i][j] = '#';
|
||||
}
|
||||
mineLocations = new int[mines];
|
||||
setMines();
|
||||
}
|
||||
~Minesweeper(){
|
||||
for(int i=0;i<size;i++) delete[] grid[i];
|
||||
delete[] grid;
|
||||
delete[] mineLocations;
|
||||
}
|
||||
|
||||
void reset(){
|
||||
for(int i=0;i<size;i++) for(int j=0;j<size;j++) grid[i][j] = '#';
|
||||
setMines();
|
||||
}
|
||||
void revealAll(){
|
||||
|
||||
}
|
||||
bool revealCell(int row, int col, bool checkMine){
|
||||
for(int i=0;i<mines;i++){
|
||||
if(mineLocations[i]/size == row && mineLocations[i]%7 == col){
|
||||
revealAll();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
grid[row][col] = checkSurrounding(row, col) + '0';
|
||||
if(grid[row][col] == '0'){
|
||||
revealCell(row+1, col+1);
|
||||
revealCell(row, col+1);
|
||||
revealCell(row-1, col+1);
|
||||
revealCell(row-1, col);
|
||||
revealCell(row-1, col-1);
|
||||
revealCell(row, col-1);
|
||||
revealCell(row+1, col-1);
|
||||
revealCell(row+1, col);
|
||||
}
|
||||
return true;
|
||||
|
||||
}
|
||||
void flagCell(int row, int col){
|
||||
grid[row][col] = 'P';
|
||||
}
|
||||
int checkSurrounding(int row, int col){
|
||||
int count = 0;
|
||||
for(int i=0;i<mines;i++){
|
||||
if(mineLocations[i]/size == row+1 && mineLocations[i]%7 == col+1) count++;
|
||||
if(mineLocations[i]/size == row && mineLocations[i]%7 == col+1) count++;
|
||||
if(mineLocations[i]/size == row-1 && mineLocations[i]%7 == col+1) count++;
|
||||
if(mineLocations[i]/size == row-1 && mineLocations[i]%7 == col) count++;
|
||||
if(mineLocations[i]/size == row-1 && mineLocations[i]%7 == col-1) count++;
|
||||
if(mineLocations[i]/size == row && mineLocations[i]%7 == col-1) count++;
|
||||
if(mineLocations[i]/size == row+1 && mineLocations[i]%7 == col-1) count++;
|
||||
if(mineLocations[i]/size == row+1 && mineLocations[i]%7 == col) count++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
bool checkWin(){
|
||||
|
||||
}
|
||||
void setMines(){
|
||||
for(int i=0;i<mines;i++){
|
||||
bool valid;
|
||||
do{
|
||||
valid = true;
|
||||
mineLocations[i] = rand()%(size*size);
|
||||
for(int j=0;j<i;j++){
|
||||
if(mineLocations[j] == mineLocations[i]){
|
||||
valid = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}while(!valid);
|
||||
}
|
||||
}
|
||||
void displayGrid(){
|
||||
cout<<" ";
|
||||
for(int i=0;i<size;i++) cout<<i+1<<" ";
|
||||
cout<<endl;
|
||||
for(int i=0;i<size;i++){
|
||||
cout<<i+1<<" ";
|
||||
for(int j=0;j<size;j++) cout<<grid[i][j]<<" ";
|
||||
cout<<endl;
|
||||
}
|
||||
cout<<endl;
|
||||
|
||||
for(int i=0;i<mines;i++){
|
||||
cout<<mineLocations[i]<<" ";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
};
|
||||
|
||||
int main(){
|
||||
|
||||
Minesweeper game(7, 10);
|
||||
game.displayGrid();
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
152
.config/Code/User/History/55ccb45e/sgz5.cpp
Normal file
152
.config/Code/User/History/55ccb45e/sgz5.cpp
Normal file
|
|
@ -0,0 +1,152 @@
|
|||
/*
|
||||
Rafay Ahmad
|
||||
23I-2526
|
||||
*/
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
class Minesweeper{
|
||||
char** grid;
|
||||
int* mineLocations;
|
||||
int size;
|
||||
int mines;
|
||||
int opened;
|
||||
|
||||
public:
|
||||
|
||||
Minesweeper(int size, int mines) : size(size), mines(mines), opened(0){
|
||||
srand(time(0));
|
||||
grid = new char*[size];
|
||||
for(int i=0;i<size;i++){
|
||||
grid[i] = new char[size];
|
||||
for(int j=0;j<size;j++) grid[i][j] = '#';
|
||||
}
|
||||
mineLocations = new int[mines];
|
||||
setMines();
|
||||
}
|
||||
~Minesweeper(){
|
||||
for(int i=0;i<size;i++) delete[] grid[i];
|
||||
delete[] grid;
|
||||
delete[] mineLocations;
|
||||
}
|
||||
|
||||
void reset(){
|
||||
for(int i=0;i<size;i++) for(int j=0;j<size;j++) grid[i][j] = '#';
|
||||
setMines();
|
||||
}
|
||||
void revealAll(){
|
||||
for(int i=0;i<size;i++){
|
||||
for(int j=0;j<size;j++){
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
bool revealCell(int row, int col, bool checkMine){
|
||||
for(int i=0;i<mines;i++){
|
||||
if(mineLocations[i]/size == row && mineLocations[i]%7 == col){
|
||||
grid[row][col] = '*';
|
||||
revealAll();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
grid[row][col] = checkSurrounding(row, col) + '0';
|
||||
opened++;
|
||||
if(grid[row][col] == '0'){
|
||||
return revealCell(row+1, col+1, false);
|
||||
return revealCell(row, col+1, false);
|
||||
return revealCell(row-1, col+1, false);
|
||||
return revealCell(row-1, col, false);
|
||||
return revealCell(row-1, col-1, false);
|
||||
return revealCell(row, col-1, false);
|
||||
return revealCell(row+1, col-1, false);
|
||||
return revealCell(row+1, col, false);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
void flagCell(int row, int col){
|
||||
grid[row][col] = 'P';
|
||||
}
|
||||
int checkSurrounding(int row, int col){
|
||||
int count = 0;
|
||||
for(int i=0;i<mines;i++){
|
||||
if(mineLocations[i]/size == row+1 && mineLocations[i]%7 == col+1) count++;
|
||||
if(mineLocations[i]/size == row && mineLocations[i]%7 == col+1) count++;
|
||||
if(mineLocations[i]/size == row-1 && mineLocations[i]%7 == col+1) count++;
|
||||
if(mineLocations[i]/size == row-1 && mineLocations[i]%7 == col) count++;
|
||||
if(mineLocations[i]/size == row-1 && mineLocations[i]%7 == col-1) count++;
|
||||
if(mineLocations[i]/size == row && mineLocations[i]%7 == col-1) count++;
|
||||
if(mineLocations[i]/size == row+1 && mineLocations[i]%7 == col-1) count++;
|
||||
if(mineLocations[i]/size == row+1 && mineLocations[i]%7 == col) count++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
bool checkWin(){
|
||||
return opened == ((size*size) - mines);
|
||||
}
|
||||
void setMines(){
|
||||
for(int i=0;i<mines;i++){
|
||||
bool valid;
|
||||
do{
|
||||
valid = true;
|
||||
mineLocations[i] = rand()%(size*size);
|
||||
for(int j=0;j<i;j++){
|
||||
if(mineLocations[j] == mineLocations[i]){
|
||||
valid = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}while(!valid);
|
||||
}
|
||||
}
|
||||
void displayGrid(){
|
||||
cout<<" ";
|
||||
for(int i=0;i<size;i++) cout<<i+1<<" ";
|
||||
cout<<endl;
|
||||
for(int i=0;i<size;i++){
|
||||
cout<<i+1<<" ";
|
||||
for(int j=0;j<size;j++) cout<<grid[i][j]<<" ";
|
||||
cout<<endl;
|
||||
}
|
||||
cout<<endl;
|
||||
|
||||
for(int i=0;i<mines;i++){
|
||||
cout<<mineLocations[i]<<" ";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
};
|
||||
|
||||
int main(){
|
||||
|
||||
int row, col;
|
||||
char c;
|
||||
int size, mines;
|
||||
cout<<"Enter size of the game board: ";
|
||||
cin>>size;
|
||||
cout<<"Enter no Of mines: ";
|
||||
cin>>mines;
|
||||
Minesweeper game(size, mines);
|
||||
|
||||
while(!game.checkWin()){
|
||||
game.displayGrid();
|
||||
cout<<"Enter row: ";
|
||||
cin>>row;
|
||||
cout<<"Enter col: ";
|
||||
cin>>col;
|
||||
cout<<"Enter F to flag or R to reveal: ";
|
||||
cin>>c;
|
||||
|
||||
if(row<size && col<size && (c == 'F' || c == 'R')){
|
||||
if(c == 'R') game.revealCell(row, col, true);
|
||||
else game.flagCell(row, col);
|
||||
}
|
||||
else cout<<"Invalid input\n";
|
||||
|
||||
}
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
156
.config/Code/User/History/55ccb45e/vIy4.cpp
Normal file
156
.config/Code/User/History/55ccb45e/vIy4.cpp
Normal file
|
|
@ -0,0 +1,156 @@
|
|||
/*
|
||||
Rafay Ahmad
|
||||
23I-2526
|
||||
*/
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
class Minesweeper{
|
||||
char** grid;
|
||||
int* mineLocations;
|
||||
int size;
|
||||
int mines;
|
||||
int opened;
|
||||
|
||||
public:
|
||||
|
||||
Minesweeper(int size, int mines) : size(size), mines(mines), opened(0){
|
||||
srand(time(0));
|
||||
grid = new char*[size];
|
||||
for(int i=0;i<size;i++){
|
||||
grid[i] = new char[size];
|
||||
for(int j=0;j<size;j++) grid[i][j] = '#';
|
||||
}
|
||||
mineLocations = new int[mines];
|
||||
setMines();
|
||||
}
|
||||
~Minesweeper(){
|
||||
for(int i=0;i<size;i++) delete[] grid[i];
|
||||
delete[] grid;
|
||||
delete[] mineLocations;
|
||||
}
|
||||
|
||||
void reset(){
|
||||
for(int i=0;i<size;i++) for(int j=0;j<size;j++) grid[i][j] = '#';
|
||||
setMines();
|
||||
}
|
||||
void revealAll(){
|
||||
for(int i=0;i<mines;i++){
|
||||
grid[mineLocations[i]/7][mineLocations[i]%7] = '*';
|
||||
}
|
||||
}
|
||||
bool revealCell(int row, int col, bool checkMine){
|
||||
if(checkMine){
|
||||
for(int i=0;i<mines;i++){
|
||||
if(mineLocations[i]/size == row && mineLocations[i]%7 == col){
|
||||
grid[row][col] = '*';
|
||||
revealAll();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
grid[row][col] = checkSurrounding(row, col) + '0';
|
||||
opened++;
|
||||
if(grid[row][col] == '0'){
|
||||
if(row>size && col>size) revealCell(row+1, col+1, false);
|
||||
if(col>size) revealCell(row, col+1, false);
|
||||
if(row>0 && col>size) revealCell(row-1, col+1, false);
|
||||
if(row>size && col>size) revealCell(row-1, col, false);
|
||||
if(row>size && col>size) revealCell(row-1, col-1, false);
|
||||
if(row>size && col>size) revealCell(row, col-1, false);
|
||||
if(row>size && col>size) revealCell(row+1, col-1, false);
|
||||
if(row>size && col>size) revealCell(row+1, col, false);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
void flagCell(int row, int col){
|
||||
grid[row][col] = 'P';
|
||||
}
|
||||
int checkSurrounding(int row, int col){
|
||||
int count = 0;
|
||||
for(int i=0;i<mines;i++){
|
||||
if(row<size && col<size && mineLocations[i]/size == row+1 && mineLocations[i]%7 == col+1) count++;
|
||||
if(col<size && mineLocations[i]/size == row && mineLocations[i]%7 == col+1) count++;
|
||||
if(row>0 && col<size && mineLocations[i]/size == row-1 && mineLocations[i]%7 == col+1) count++;
|
||||
if(row>0 && mineLocations[i]/size == row-1 && mineLocations[i]%7 == col) count++;
|
||||
if(row>0 && col>0 && mineLocations[i]/size == row-1 && mineLocations[i]%7 == col-1) count++;
|
||||
if(col>0 && mineLocations[i]/size == row && mineLocations[i]%7 == col-1) count++;
|
||||
if(row<size && col>0 && mineLocations[i]/size == row+1 && mineLocations[i]%7 == col-1) count++;
|
||||
if(row<size && mineLocations[i]/size == row+1 && mineLocations[i]%7 == col) count++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
bool checkWin(){
|
||||
return opened == ((size*size) - mines);
|
||||
}
|
||||
void setMines(){
|
||||
for(int i=0;i<mines;i++){
|
||||
bool valid;
|
||||
do{
|
||||
valid = true;
|
||||
mineLocations[i] = rand()%(size*size);
|
||||
for(int j=0;j<i;j++){
|
||||
if(mineLocations[j] == mineLocations[i]){
|
||||
valid = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}while(!valid);
|
||||
}
|
||||
}
|
||||
void displayGrid(){
|
||||
cout<<" ";
|
||||
for(int i=0;i<size;i++) cout<<i<<" ";
|
||||
cout<<endl;
|
||||
for(int i=0;i<size;i++){
|
||||
cout<<i<<" ";
|
||||
for(int j=0;j<size;j++) cout<<grid[i][j]<<" ";
|
||||
cout<<endl;
|
||||
}
|
||||
cout<<endl;
|
||||
|
||||
for(int i=0;i<mines;i++){
|
||||
cout<<mineLocations[i]<<" ";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
};
|
||||
|
||||
int main(){
|
||||
|
||||
int row, col;
|
||||
char c;
|
||||
int size, mines;
|
||||
cout<<"Enter size of the game board: ";
|
||||
cin>>size;
|
||||
cout<<"Enter no Of mines: ";
|
||||
cin>>mines;
|
||||
Minesweeper game(size, mines);
|
||||
|
||||
while(!game.checkWin()){
|
||||
game.displayGrid();
|
||||
cout<<"Enter row: ";
|
||||
cin>>row;
|
||||
cout<<"Enter col: ";
|
||||
cin>>col;
|
||||
cout<<"Enter F to flag or R to reveal: ";
|
||||
cin>>c;
|
||||
|
||||
if(row<size && col<size && (c == 'F' || c == 'R')){
|
||||
if(c == 'R' && !game.revealCell(row, col, true)){
|
||||
game.displayGrid();
|
||||
cout<<"You Lost\n";
|
||||
break;
|
||||
}
|
||||
else if(c == 'F') game.flagCell(row, col);
|
||||
}
|
||||
else cout<<"Invalid input\n";
|
||||
|
||||
}
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
151
.config/Code/User/History/55ccb45e/voH8.cpp
Normal file
151
.config/Code/User/History/55ccb45e/voH8.cpp
Normal file
|
|
@ -0,0 +1,151 @@
|
|||
/*
|
||||
Rafay Ahmad
|
||||
23I-2526
|
||||
*/
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
class Minesweeper{
|
||||
char** grid;
|
||||
int* mineLocations;
|
||||
int size;
|
||||
int mines;
|
||||
int opened;
|
||||
|
||||
public:
|
||||
|
||||
Minesweeper(int size, int mines) : size(size), mines(mines), opened(0){
|
||||
srand(time(0));
|
||||
grid = new char*[size];
|
||||
for(int i=0;i<size;i++){
|
||||
grid[i] = new char[size];
|
||||
for(int j=0;j<size;j++) grid[i][j] = '#';
|
||||
}
|
||||
mineLocations = new int[mines];
|
||||
setMines();
|
||||
}
|
||||
~Minesweeper(){
|
||||
for(int i=0;i<size;i++) delete[] grid[i];
|
||||
delete[] grid;
|
||||
delete[] mineLocations;
|
||||
}
|
||||
|
||||
void reset(){
|
||||
for(int i=0;i<size;i++) for(int j=0;j<size;j++) grid[i][j] = '#';
|
||||
setMines();
|
||||
}
|
||||
void revealAll(){
|
||||
for(int i=0;i<size;i++){
|
||||
for(int j=0;j<size;j++){
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
bool revealCell(int row, int col, bool checkMine){
|
||||
for(int i=0;i<mines;i++){
|
||||
if(mineLocations[i]/size == row && mineLocations[i]%7 == col){
|
||||
grid[row][col] = '*';
|
||||
revealAll();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
grid[row][col] = checkSurrounding(row, col) + '0';
|
||||
opened++;
|
||||
if(grid[row][col] == '0'){
|
||||
return revealCell(row+1, col+1, false);
|
||||
return revealCell(row, col+1, false);
|
||||
return revealCell(row-1, col+1, false);
|
||||
return revealCell(row-1, col, false);
|
||||
return revealCell(row-1, col-1, false);
|
||||
return revealCell(row, col-1, false);
|
||||
return revealCell(row+1, col-1, false);
|
||||
return revealCell(row+1, col, false);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
void flagCell(int row, int col){
|
||||
grid[row][col] = 'P';
|
||||
}
|
||||
int checkSurrounding(int row, int col){
|
||||
int count = 0;
|
||||
for(int i=0;i<mines;i++){
|
||||
if(mineLocations[i]/size == row+1 && mineLocations[i]%7 == col+1) count++;
|
||||
if(mineLocations[i]/size == row && mineLocations[i]%7 == col+1) count++;
|
||||
if(mineLocations[i]/size == row-1 && mineLocations[i]%7 == col+1) count++;
|
||||
if(mineLocations[i]/size == row-1 && mineLocations[i]%7 == col) count++;
|
||||
if(mineLocations[i]/size == row-1 && mineLocations[i]%7 == col-1) count++;
|
||||
if(mineLocations[i]/size == row && mineLocations[i]%7 == col-1) count++;
|
||||
if(mineLocations[i]/size == row+1 && mineLocations[i]%7 == col-1) count++;
|
||||
if(mineLocations[i]/size == row+1 && mineLocations[i]%7 == col) count++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
bool checkWin(){
|
||||
return opened == (size*size) - mines;
|
||||
}
|
||||
void setMines(){
|
||||
for(int i=0;i<mines;i++){
|
||||
bool valid;
|
||||
do{
|
||||
valid = true;
|
||||
mineLocations[i] = rand()%(size*size);
|
||||
for(int j=0;j<i;j++){
|
||||
if(mineLocations[j] == mineLocations[i]){
|
||||
valid = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}while(!valid);
|
||||
}
|
||||
}
|
||||
void displayGrid(){
|
||||
cout<<" ";
|
||||
for(int i=0;i<size;i++) cout<<i+1<<" ";
|
||||
cout<<endl;
|
||||
for(int i=0;i<size;i++){
|
||||
cout<<i+1<<" ";
|
||||
for(int j=0;j<size;j++) cout<<grid[i][j]<<" ";
|
||||
cout<<endl;
|
||||
}
|
||||
cout<<endl;
|
||||
|
||||
for(int i=0;i<mines;i++){
|
||||
cout<<mineLocations[i]<<" ";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
};
|
||||
|
||||
int main(){
|
||||
|
||||
int row, col;
|
||||
char c;
|
||||
int size, mines;
|
||||
cout<<"Enter size of the game board: ";
|
||||
cin>>size;
|
||||
cout<<"Enter no Of mines: ";
|
||||
cin>>mines;
|
||||
Minesweeper game(size, mines);
|
||||
|
||||
while(game.checkWin()){
|
||||
game.displayGrid();
|
||||
cout<<"Enter row: ";
|
||||
cin>>row;
|
||||
cout<<"Enter col: ";
|
||||
cin>>col;
|
||||
cout<<"Enter F to flag or R to reveal: ";
|
||||
cin>>c;
|
||||
|
||||
if(row<size && col<size && (c == 'F' || c == 'R')){
|
||||
game.revealCell(row, col, true);
|
||||
}
|
||||
else cout<<"Invalid input\n";
|
||||
|
||||
}
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
129
.config/Code/User/History/55ccb45e/wqG2.cpp
Normal file
129
.config/Code/User/History/55ccb45e/wqG2.cpp
Normal file
|
|
@ -0,0 +1,129 @@
|
|||
/*
|
||||
Rafay Ahmad
|
||||
23I-2526
|
||||
*/
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
class Minesweeper{
|
||||
char** grid;
|
||||
int* mineLocations;
|
||||
int size;
|
||||
int mines;
|
||||
int opened;
|
||||
|
||||
public:
|
||||
|
||||
Minesweeper(int size, int mines) : size(size), mines(mines), opened(0){
|
||||
srand(time(0));
|
||||
grid = new char*[size];
|
||||
for(int i=0;i<size;i++){
|
||||
grid[i] = new char[size];
|
||||
for(int j=0;j<size;j++) grid[i][j] = '#';
|
||||
}
|
||||
mineLocations = new int[mines];
|
||||
setMines();
|
||||
}
|
||||
~Minesweeper(){
|
||||
for(int i=0;i<size;i++) delete[] grid[i];
|
||||
delete[] grid;
|
||||
delete[] mineLocations;
|
||||
}
|
||||
|
||||
void reset(){
|
||||
for(int i=0;i<size;i++) for(int j=0;j<size;j++) grid[i][j] = '#';
|
||||
setMines();
|
||||
}
|
||||
void revealAll(){
|
||||
for(int i=0;i<size;i++){
|
||||
for(int j=0;j<size;j++){
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
bool revealCell(int row, int col, bool checkMine){
|
||||
for(int i=0;i<mines;i++){
|
||||
if(mineLocations[i]/size == row && mineLocations[i]%7 == col){
|
||||
grid[row][col] = '*';
|
||||
revealAll();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
grid[row][col] = checkSurrounding(row, col) + '0';
|
||||
opened++;
|
||||
if(grid[row][col] == '0'){
|
||||
return revealCell(row+1, col+1, false);
|
||||
return revealCell(row, col+1, false);
|
||||
return revealCell(row-1, col+1, false);
|
||||
return revealCell(row-1, col, false);
|
||||
return revealCell(row-1, col-1, false);
|
||||
return revealCell(row, col-1, false);
|
||||
return revealCell(row+1, col-1, false);
|
||||
return revealCell(row+1, col, false);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
void flagCell(int row, int col){
|
||||
grid[row][col] = 'P';
|
||||
}
|
||||
int checkSurrounding(int row, int col){
|
||||
int count = 0;
|
||||
for(int i=0;i<mines;i++){
|
||||
if(mineLocations[i]/size == row+1 && mineLocations[i]%7 == col+1) count++;
|
||||
if(mineLocations[i]/size == row && mineLocations[i]%7 == col+1) count++;
|
||||
if(mineLocations[i]/size == row-1 && mineLocations[i]%7 == col+1) count++;
|
||||
if(mineLocations[i]/size == row-1 && mineLocations[i]%7 == col) count++;
|
||||
if(mineLocations[i]/size == row-1 && mineLocations[i]%7 == col-1) count++;
|
||||
if(mineLocations[i]/size == row && mineLocations[i]%7 == col-1) count++;
|
||||
if(mineLocations[i]/size == row+1 && mineLocations[i]%7 == col-1) count++;
|
||||
if(mineLocations[i]/size == row+1 && mineLocations[i]%7 == col) count++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
bool checkWin(){
|
||||
return opened == (size*size) - mines;
|
||||
}
|
||||
void setMines(){
|
||||
for(int i=0;i<mines;i++){
|
||||
bool valid;
|
||||
do{
|
||||
valid = true;
|
||||
mineLocations[i] = rand()%(size*size);
|
||||
for(int j=0;j<i;j++){
|
||||
if(mineLocations[j] == mineLocations[i]){
|
||||
valid = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}while(!valid);
|
||||
}
|
||||
}
|
||||
void displayGrid(){
|
||||
cout<<" ";
|
||||
for(int i=0;i<size;i++) cout<<i+1<<" ";
|
||||
cout<<endl;
|
||||
for(int i=0;i<size;i++){
|
||||
cout<<i+1<<" ";
|
||||
for(int j=0;j<size;j++) cout<<grid[i][j]<<" ";
|
||||
cout<<endl;
|
||||
}
|
||||
cout<<endl;
|
||||
|
||||
for(int i=0;i<mines;i++){
|
||||
cout<<mineLocations[i]<<" ";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
};
|
||||
|
||||
int main(){
|
||||
|
||||
Minesweeper game(7, 10);
|
||||
game.displayGrid();
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
156
.config/Code/User/History/55ccb45e/yGA5.cpp
Normal file
156
.config/Code/User/History/55ccb45e/yGA5.cpp
Normal file
|
|
@ -0,0 +1,156 @@
|
|||
/*
|
||||
Rafay Ahmad
|
||||
23I-2526
|
||||
*/
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
class Minesweeper{
|
||||
char** grid;
|
||||
int* mineLocations;
|
||||
int size;
|
||||
int mines;
|
||||
int opened;
|
||||
|
||||
public:
|
||||
|
||||
Minesweeper(int size, int mines) : size(size), mines(mines), opened(0){
|
||||
srand(time(0));
|
||||
grid = new char*[size];
|
||||
for(int i=0;i<size;i++){
|
||||
grid[i] = new char[size];
|
||||
for(int j=0;j<size;j++) grid[i][j] = '#';
|
||||
}
|
||||
mineLocations = new int[mines];
|
||||
setMines();
|
||||
}
|
||||
~Minesweeper(){
|
||||
for(int i=0;i<size;i++) delete[] grid[i];
|
||||
delete[] grid;
|
||||
delete[] mineLocations;
|
||||
}
|
||||
|
||||
void reset(){
|
||||
for(int i=0;i<size;i++) for(int j=0;j<size;j++) grid[i][j] = '#';
|
||||
setMines();
|
||||
}
|
||||
void revealAll(){
|
||||
for(int i=0;i<mines;i++){
|
||||
grid[mineLocations[i]/7][mineLocations[i]%7] = '*';
|
||||
}
|
||||
}
|
||||
bool revealCell(int row, int col, bool checkMine){
|
||||
if(checkMine){
|
||||
for(int i=0;i<mines;i++){
|
||||
if(mineLocations[i]/size == row && mineLocations[i]%7 == col){
|
||||
grid[row][col] = '*';
|
||||
revealAll();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
grid[row][col] = checkSurrounding(row, col) + '0';
|
||||
opened++;
|
||||
if(grid[row][col] == '0'){
|
||||
return revealCell(row+1, col+1, false);
|
||||
return revealCell(row, col+1, false);
|
||||
return revealCell(row-1, col+1, false);
|
||||
return revealCell(row-1, col, false);
|
||||
return revealCell(row-1, col-1, false);
|
||||
return revealCell(row, col-1, false);
|
||||
return revealCell(row+1, col-1, false);
|
||||
return revealCell(row+1, col, false);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
void flagCell(int row, int col){
|
||||
grid[row][col] = 'P';
|
||||
}
|
||||
int checkSurrounding(int row, int col){
|
||||
int count = 0;
|
||||
for(int i=0;i<mines;i++){
|
||||
if(row<size && col<size && mineLocations[i]/size == row+1 && mineLocations[i]%7 == col+1) count++;
|
||||
if(col<size && mineLocations[i]/size == row && mineLocations[i]%7 == col+1) count++;
|
||||
if(row>0 && col<size && mineLocations[i]/size == row-1 && mineLocations[i]%7 == col+1) count++;
|
||||
if(row>0 && mineLocations[i]/size == row-1 && mineLocations[i]%7 == col) count++;
|
||||
if(row<size && mineLocations[i]/size == row-1 && mineLocations[i]%7 == col-1) count++;
|
||||
if(row<size && mineLocations[i]/size == row && mineLocations[i]%7 == col-1) count++;
|
||||
if(row<size && mineLocations[i]/size == row+1 && mineLocations[i]%7 == col-1) count++;
|
||||
if(row<size && mineLocations[i]/size == row+1 && mineLocations[i]%7 == col) count++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
bool checkWin(){
|
||||
return opened == ((size*size) - mines);
|
||||
}
|
||||
void setMines(){
|
||||
for(int i=0;i<mines;i++){
|
||||
bool valid;
|
||||
do{
|
||||
valid = true;
|
||||
mineLocations[i] = rand()%(size*size);
|
||||
for(int j=0;j<i;j++){
|
||||
if(mineLocations[j] == mineLocations[i]){
|
||||
valid = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}while(!valid);
|
||||
}
|
||||
}
|
||||
void displayGrid(){
|
||||
cout<<" ";
|
||||
for(int i=0;i<size;i++) cout<<i<<" ";
|
||||
cout<<endl;
|
||||
for(int i=0;i<size;i++){
|
||||
cout<<i<<" ";
|
||||
for(int j=0;j<size;j++) cout<<grid[i][j]<<" ";
|
||||
cout<<endl;
|
||||
}
|
||||
cout<<endl;
|
||||
|
||||
for(int i=0;i<mines;i++){
|
||||
cout<<mineLocations[i]<<" ";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
};
|
||||
|
||||
int main(){
|
||||
|
||||
int row, col;
|
||||
char c;
|
||||
int size, mines;
|
||||
cout<<"Enter size of the game board: ";
|
||||
cin>>size;
|
||||
cout<<"Enter no Of mines: ";
|
||||
cin>>mines;
|
||||
Minesweeper game(size, mines);
|
||||
|
||||
while(!game.checkWin()){
|
||||
game.displayGrid();
|
||||
cout<<"Enter row: ";
|
||||
cin>>row;
|
||||
cout<<"Enter col: ";
|
||||
cin>>col;
|
||||
cout<<"Enter F to flag or R to reveal: ";
|
||||
cin>>c;
|
||||
|
||||
if(row<size && col<size && (c == 'F' || c == 'R')){
|
||||
if(c == 'R' && !game.revealCell(row, col, true)){
|
||||
game.displayGrid();
|
||||
cout<<"You Lost\n";
|
||||
break;
|
||||
}
|
||||
else if(c == 'F') game.flagCell(row, col);
|
||||
}
|
||||
else cout<<"Invalid input\n";
|
||||
|
||||
}
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
156
.config/Code/User/History/55ccb45e/yR2O.cpp
Normal file
156
.config/Code/User/History/55ccb45e/yR2O.cpp
Normal file
|
|
@ -0,0 +1,156 @@
|
|||
/*
|
||||
Rafay Ahmad
|
||||
23I-2526
|
||||
*/
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
class Minesweeper{
|
||||
char** grid;
|
||||
int* mineLocations;
|
||||
int size;
|
||||
int mines;
|
||||
int opened;
|
||||
|
||||
public:
|
||||
|
||||
Minesweeper(int size, int mines) : size(size), mines(mines), opened(0){
|
||||
srand(time(0));
|
||||
grid = new char*[size];
|
||||
for(int i=0;i<size;i++){
|
||||
grid[i] = new char[size];
|
||||
for(int j=0;j<size;j++) grid[i][j] = '#';
|
||||
}
|
||||
mineLocations = new int[mines];
|
||||
setMines();
|
||||
}
|
||||
~Minesweeper(){
|
||||
for(int i=0;i<size;i++) delete[] grid[i];
|
||||
delete[] grid;
|
||||
delete[] mineLocations;
|
||||
}
|
||||
|
||||
void reset(){
|
||||
for(int i=0;i<size;i++) for(int j=0;j<size;j++) grid[i][j] = '#';
|
||||
setMines();
|
||||
}
|
||||
void revealAll(){
|
||||
for(int i=0;i<mines;i++){
|
||||
grid[mineLocations[i]/7][mineLocations[i]%7] = '*';
|
||||
}
|
||||
}
|
||||
bool revealCell(int row, int col, bool checkMine){
|
||||
if(checkMine){
|
||||
for(int i=0;i<mines;i++){
|
||||
if(mineLocations[i]/size == row && mineLocations[i]%7 == col){
|
||||
grid[row][col] = '*';
|
||||
revealAll();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
grid[row][col] = checkSurrounding(row, col) + '0';
|
||||
opened++;
|
||||
if(grid[row][col] == '0'){
|
||||
return revealCell(row+1, col+1, false);
|
||||
return revealCell(row, col+1, false);
|
||||
return revealCell(row-1, col+1, false);
|
||||
return revealCell(row-1, col, false);
|
||||
return revealCell(row-1, col-1, false);
|
||||
return revealCell(row, col-1, false);
|
||||
return revealCell(row+1, col-1, false);
|
||||
return revealCell(row+1, col, false);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
void flagCell(int row, int col){
|
||||
grid[row][col] = 'P';
|
||||
}
|
||||
int checkSurrounding(int row, int col){
|
||||
int count = 0;
|
||||
for(int i=0;i<mines;i++){
|
||||
if(row<size && col<size && mineLocations[i]/size == row+1 && mineLocations[i]%7 == col+1) count++;
|
||||
if(col<size && mineLocations[i]/size == row && mineLocations[i]%7 == col+1) count++;
|
||||
if(row>0 && col<size && mineLocations[i]/size == row-1 && mineLocations[i]%7 == col+1) count++;
|
||||
if(row>0 && mineLocations[i]/size == row-1 && mineLocations[i]%7 == col) count++;
|
||||
if(row>0 && col>0 && mineLocations[i]/size == row-1 && mineLocations[i]%7 == col-1) count++;
|
||||
if(col>0 && mineLocations[i]/size == row && mineLocations[i]%7 == col-1) count++;
|
||||
if(row<size && col>0 && mineLocations[i]/size == row+1 && mineLocations[i]%7 == col-1) count++;
|
||||
if(row<size && mineLocations[i]/size == row+1 && mineLocations[i]%7 == col) count++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
bool checkWin(){
|
||||
return opened == ((size*size) - mines);
|
||||
}
|
||||
void setMines(){
|
||||
for(int i=0;i<mines;i++){
|
||||
bool valid;
|
||||
do{
|
||||
valid = true;
|
||||
mineLocations[i] = rand()%(size*size);
|
||||
for(int j=0;j<i;j++){
|
||||
if(mineLocations[j] == mineLocations[i]){
|
||||
valid = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}while(!valid);
|
||||
}
|
||||
}
|
||||
void displayGrid(){
|
||||
cout<<" ";
|
||||
for(int i=0;i<size;i++) cout<<i<<" ";
|
||||
cout<<endl;
|
||||
for(int i=0;i<size;i++){
|
||||
cout<<i<<" ";
|
||||
for(int j=0;j<size;j++) cout<<grid[i][j]<<" ";
|
||||
cout<<endl;
|
||||
}
|
||||
cout<<endl;
|
||||
|
||||
for(int i=0;i<mines;i++){
|
||||
cout<<mineLocations[i]<<" ";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
};
|
||||
|
||||
int main(){
|
||||
|
||||
int row, col;
|
||||
char c;
|
||||
int size, mines;
|
||||
cout<<"Enter size of the game board: ";
|
||||
cin>>size;
|
||||
cout<<"Enter no Of mines: ";
|
||||
cin>>mines;
|
||||
Minesweeper game(size, mines);
|
||||
|
||||
while(!game.checkWin()){
|
||||
game.displayGrid();
|
||||
cout<<"Enter row: ";
|
||||
cin>>row;
|
||||
cout<<"Enter col: ";
|
||||
cin>>col;
|
||||
cout<<"Enter F to flag or R to reveal: ";
|
||||
cin>>c;
|
||||
|
||||
if(row<size && col<size && (c == 'F' || c == 'R')){
|
||||
if(c == 'R' && !game.revealCell(row, col, true)){
|
||||
game.displayGrid();
|
||||
cout<<"You Lost\n";
|
||||
break;
|
||||
}
|
||||
else if(c == 'F') game.flagCell(row, col);
|
||||
}
|
||||
else cout<<"Invalid input\n";
|
||||
|
||||
}
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
164
.config/Code/User/History/55ccb45e/ySrA.cpp
Normal file
164
.config/Code/User/History/55ccb45e/ySrA.cpp
Normal file
|
|
@ -0,0 +1,164 @@
|
|||
/*
|
||||
Rafay Ahmad
|
||||
23I-2526
|
||||
*/
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
class Minesweeper{
|
||||
char** grid;
|
||||
int* mineLocations;
|
||||
int size;
|
||||
int mines;
|
||||
int opened;
|
||||
|
||||
public:
|
||||
|
||||
Minesweeper(int size, int mines) : size(size), mines(mines), opened(0){
|
||||
srand(time(0));
|
||||
grid = new char*[size];
|
||||
for(int i=0;i<size;i++){
|
||||
grid[i] = new char[size];
|
||||
for(int j=0;j<size;j++) grid[i][j] = '#';
|
||||
}
|
||||
mineLocations = new int[mines];
|
||||
setMines();
|
||||
}
|
||||
~Minesweeper(){
|
||||
for(int i=0;i<size;i++) delete[] grid[i];
|
||||
delete[] grid;
|
||||
delete[] mineLocations;
|
||||
}
|
||||
|
||||
void reset(){
|
||||
for(int i=0;i<size;i++) for(int j=0;j<size;j++) grid[i][j] = '#';
|
||||
setMines();
|
||||
}
|
||||
void revealAll(){
|
||||
for(int i=0;i<mines;i++){
|
||||
grid[mineLocations[i]/7][mineLocations[i]%7] = '*';
|
||||
}
|
||||
}
|
||||
bool revealCell(int row, int col, bool checkMine){
|
||||
if(checkMine){
|
||||
for(int i=0;i<mines;i++){
|
||||
if(mineLocations[i]/size == row && mineLocations[i]%size == col){
|
||||
grid[row][col] = '*';
|
||||
revealAll();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
grid[row][col] = checkSurrounding(row, col) + '0';
|
||||
opened++;
|
||||
if(grid[row][col] == '0'){
|
||||
if(row<size-1 && col<size-1 && grid[row+1][col+1] == '#') revealCell(row+1, col+1, false);
|
||||
if(col<size-1 && grid[row][col+1] == '#') revealCell(row, col+1, false);
|
||||
if(row>0 && col<size-1 && grid[row-1][col+1] == '#') revealCell(row-1, col+1, false);
|
||||
if(row>0 && grid[row-1][col] == '#') revealCell(row-1, col, false);
|
||||
if(row>0 && col>0 && grid[row-1][col-1] == '#') revealCell(row-1, col-1, false);
|
||||
if(col>0 && grid[row][col-1] == '#') revealCell(row, col-1, false);
|
||||
if(row<size-1 && col>0 && grid[row+1][col-1] == '#') revealCell(row+1, col-1, false);
|
||||
if(row<size-1 && grid[row+1][col] == '#') revealCell(row+1, col, false);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
void flagCell(int row, int col){
|
||||
grid[row][col] = 'P';
|
||||
}
|
||||
int checkSurrounding(int row, int col){
|
||||
int count = 0;
|
||||
for(int i=0;i<mines;i++){
|
||||
if(row<size-1 && col<size-1 && mineLocations[i]/size == row+1 && mineLocations[i]%size == col+1) count++;
|
||||
if(col<size-1 && mineLocations[i]/size == row && mineLocations[i]%size == col+1) count++;
|
||||
if(row>0 && col<size-1 && mineLocations[i]/size == row-1 && mineLocations[i]%size == col+1) count++;
|
||||
if(row>0 && mineLocations[i]/size == row-1 && mineLocations[i]%size == col) count++;
|
||||
if(row>0 && col>0 && mineLocations[i]/size == row-1 && mineLocations[i]%size == col-1) count++;
|
||||
if(col>0 && mineLocations[i]/size == row && mineLocations[i]%size == col-1) count++;
|
||||
if(row<size-1 && col>0 && mineLocations[i]/size == row+1 && mineLocations[i]%size == col-1) count++;
|
||||
if(row<size-1 && mineLocations[i]/size == row+1 && mineLocations[i]%size == col) count++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
bool checkWin(){
|
||||
return opened == ((size*size) - mines);
|
||||
}
|
||||
void setMines(){
|
||||
for(int i=0;i<mines;i++){
|
||||
bool valid;
|
||||
do{
|
||||
valid = true;
|
||||
mineLocations[i] = rand()%(size*size);
|
||||
for(int j=0;j<i;j++){
|
||||
if(mineLocations[j] == mineLocations[i]){
|
||||
valid = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}while(!valid);
|
||||
}
|
||||
}
|
||||
void displayGrid(){
|
||||
cout<<" ";
|
||||
for(int i=0;i<size;i++) cout<<i<<" ";
|
||||
cout<<endl;
|
||||
for(int i=0;i<size;i++){
|
||||
cout<<i<<" ";
|
||||
for(int j=0;j<size;j++) cout<<grid[i][j]<<" ";
|
||||
cout<<endl;
|
||||
}
|
||||
cout<<endl;
|
||||
|
||||
for(int i=0;i<mines;i++){
|
||||
cout<<mineLocations[i]<<" ";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
};
|
||||
|
||||
int main(){
|
||||
|
||||
int row, col;
|
||||
char c;
|
||||
int size, mines;
|
||||
cout<<"Enter size of the game board: ";
|
||||
cin>>size;
|
||||
cout<<"Enter no Of mines: ";
|
||||
cin>>mines;
|
||||
Minesweeper game(size, mines);
|
||||
|
||||
bool win = true;
|
||||
|
||||
while(!game.checkWin()){
|
||||
game.displayGrid();
|
||||
cout<<"Enter row: ";
|
||||
cin>>row;
|
||||
cout<<"Enter col: ";
|
||||
cin>>col;
|
||||
cout<<"Enter F to flag or R to reveal: ";
|
||||
cin>>c;
|
||||
|
||||
if(row<size && col<size && (c == 'F' || c == 'R')){
|
||||
if(c == 'R' && !game.revealCell(row, col, true)){
|
||||
game.displayGrid();
|
||||
win = false;
|
||||
cout<<"You Lost\n";
|
||||
break;
|
||||
}
|
||||
else if(c == 'F') game.flagCell(row, col);
|
||||
}
|
||||
else cout<<"Invalid input\n";
|
||||
|
||||
}
|
||||
|
||||
if(win){
|
||||
game.displayGrid();
|
||||
cout<<"You Won!\n";
|
||||
}
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue