Sie sind auf Seite 1von 6

minesweeper

Shayon Sengupta, Sharad Tara Final Computer Science Project XI L Delhi Public School, RK Puram

Introduction
Since one of the topics in our CBSE class XI Computer Science curriculum is Number Systems, we thought it would be best to develop a game that has been modelled using an algebra of binary variables that pose a fairly computationally expensive problem in NP-complete theory. We give you minesweeper, the windows classic. The player is initially presented with a grid of undifferentiated squares. Some randomly selected squares, unknown to the player, are designated to contain mines. Typically, the size of the grid and the number of mines are set in advance by the user, either by entering the numbers or selecting from defined skill levels depending on the implementation (In this program, it is the former) The game is played by revealing squares of the grid, typically by clicking them with a mouse (DOS lets you use coordinates). If a square containing a mine is revealed, the player loses the game. Otherwise, a digit is revealed in the square, indicating the number of adjacent squares (typically, out of the possible eight) that contain mines. In typical implementations, if this number is zero then the square appears blank, and the surrounding squares are automatically also revealed. By using logic, the player can in many instances use this information to deduce that certain other squares are mine-free, in which case they may be safely revealed, or mine-filled, in which they can be marked as such with an x. Another convenience feature present in some implementations is an interface to quickly clear around a revealed square once the correct number of mines have been flagged around it. The game is won when all mine-free squares are revealed, meaning that all mines have been located. It's not often you can win a million dollars by analysing a computer game, but by a curious conjunction of fate, there's a chance that you might (see millennium prize problems in mathematics) Concepts in Computer Science that have been used: Loops, 2D Arrays, Classes, Searching, Recursion

Code
#include <iostream> #include <iomanip> void main(); void replay() { char a; cout << "1) Replay 2) Quit" << endl; cin >> a; switch(a) { case '1': main(); break; case '2': cout << "Quit" << endl; break; default: cout << "Invalid input" << endl; replay(); break; } } void reveal(int x) { if(x == 0) cout << "o|"; else if(x == 9) cout << "x|"; else cout << x << "|"; } int random(int i, int b) { long ran; int t = time(0); int s; srand(t); s = ran; ran = rand(); ran >>= ran / (ran * i) + (i * 1337); ran = ran % b; return ran; } int main() { int b; do { cout << "Enter number of squares per side (2 - 10)" << endl; cin >> b; }while(b < 1 && b >= 10); int board[b][b]; int revealed[b][b]; int i = 0; int j = 0; int x = 0; int y = 0;

int int int int do { ")" << endl;

z; q; t = 0; dead = 0; cout << "How many mines? (1 - " << ((b*b)-1) <<

cin >> z; }while(z <= 0 && z >= ((b*b)-1)); for(i=0;i<b;i++) for(j=0;j<b;j++) board[i][j] = 0; i = random(i, b); j = random(j, b); cout << "Generating board" << endl; do { i+=3; j+=6; x = random(i, b); y = random(j, b); if(board[y][x] != 9) { board[y][x] = 9; z--; if((y-1) >= 0) if(board[y-1][x] != 9) board[y-1][x]++; if((y-1) >= 0 && (x-1) >= 0) if(board[y-1][x-1] != 9) board[y-1][x-1]++; if((x-1) >= 0) if(board[y][x-1] != 9) board[y][x-1]++; if((y+1) < b) if(board[y+1][x] != 9) board[y+1][x]++; if((y+1) < b && (x+1) < b) if(board[y+1][x+1] != 9) board[y+1][x+1]++; if((x+1) < b) if(board[y][x+1] != 9) board[y][x+1]++; if((y-1) >= 0 && (x+1) < b) if(board[y-1][x+1] != 9) board[y-1][x+1]++; if((y+1) < b && (x-1) >= 0) if(board[y+1][x-1] != 9) board[y+1][x-1]++; } }while(z>0); for(i = 0; i < b; i++) for(j=0;j<b;j++) revealed[i][j]=0; setw(10); do { q = 0;

z = 0; cout << "

";

for(i=0;i<b;i++) cout << i << " "; cout << endl; for(i=0;i<b;i++) { for(j=0;j<b;j++) { if(j==0) cout << i << " |"; if(revealed[i][j]==1) reveal(board[i][j]); else cout << "_|"; if(j==(b-1)) cout << endl; if(board[i][j]!=9 && revealed[i][j]==1) q++; if(board[i][j] == 9) z++; } } if(q >= ((b*b) - z)) { cout << "You win!" << endl; dead = 1; } if(dead == 0) { cout << "Enter x and y coordinates separated by a space" << endl; cin >> x; cin >> y; } if(board[y][x] == 9) { cout << "You hit a mine!" << endl; cout << " "; for(i=0;i<b;i++) cout << i << " "; cout << endl; dead = 1; for(i=0; i<b; i++) { for(j=0;j<b;j++) { if(j==0) cout << i << " |"; if(board[i][j]==9) revealed[i][j]=1;

if(revealed[i][j]==1) reveal(board[i][j]); else cout << "_|"; if(j==(b1)) cout << endl; } } } if(board[y][x]==0) { revealed[y][x] = 1; for(i=0;i<b;i++) { for(j=0;j<b;j++) { if(i>(y2)&&i<(y+2)) if(j>(x-2)&&j<(x+2)) if(board[i][j]!=9) revealed[i][j]=1; } } } if(board[y][x]>0 && board[y][x]<9) revealed[y][x] = 1; } while(dead == 0); if (dead == 1) replay(); }

Das könnte Ihnen auch gefallen