Post Reply 
 
Thread Rating:
  • 0 Votes - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Mineswepper
08-03-2009, 08:51 PM (This post was last modified: 08-03-2009 09:10 PM by drdebcol.)
Post: #1
Mineswepper
Mineswepper is a common Windows Game, many of people know what i am talking about ! You need to mark mines on a matrix field ! There is an interesting assignment. If you have matrix of integers which represent field of mineswepper, you need to mark all fields that are in contact with mine !
Mines are shown as "-1" in matrix and other fields are "0" zeros ! You need to fill those zeros (where needed) with numbers that show with how many mines is that field in contact (maximum is 8) !
Here is a test example :
Input :
Code:
-1 0 0 0
0 0 0 0
0 -1 0 0
0 0 0 0
Output:
Code:
-1 1 0 0
2 2 1 0
1 -1 1 0
1 1 1 0
And of course here is the solution in TPW :
Code:
program mineswepper;
uses wincrt;
var
i,j,m,n:longint;
a:array[1..100,1..100] of -1..8;
function number(x,y:longint):integer;
var
s:integer;
begin
s:=0;
if a[x-1,y-1]=-1 then
s:=s+1;
if a[x,y-1]=-1 then
s:=s+1;
if a[x+1,y-1]=-1 then
s:=s+1;
if a[x+1,y]=-1 then
s:=s+1;
if a[x+1,y+1]=-1 then
s:=s+1;
if a[x,y+1]=-1 then
s:=s+1;
if a[x-1,y+1]=-1 then
s:=s+1;
if a[x-1,y]=-1 then
s:=s+1;
number:=s;
end;
begin
writeln('Enter length !');
readln(n);
writeln('Enter height !');
readln(m);
writeln('Enter matrix with "0" zeros where is');
writeln('nothing and "-1" minus-ones where mines are !');
for i:=1 to n do
for j:=1 to m do
  read(a[i,j]);
for i:=1 to n do
for j:=1 to m do
  if  a[i,j]=0 then
   a[i,j]:=number(i,j);
writeln('Solution is : ');
for i:=1 to n do
begin
  writeln;
  for j:=1 to m do
    write(a[i,j],' ');
end;
end.
And for those who prefer C++ :
Code:
# include <iostream>
# include <cstdio>

using namespace std;

int i,j,m,n;
int a [100][100];

int number(int x, int y)
{
    int s=0;
if (a[x-1][y-1]==-1)
s++;
if (a[x][y-1]==-1)
s++;
if (a[x+1][y-1]==-1)
s++;
if (a[x+1][y]==-1)
s++;
if (a[x+1][y+1]==-1)
s++;
if (a[x][y+1]==-1)
s++;
if (a[x-1][y+1]==-1)
s++;
if (a[x-1][y]==-1)
s++;
return s;  
}

int main()
{
    cout<<"Enter length of matrix !\n";
    cin>>n;
    cout<<"Enter height of matrix !\n";
    cin>>m;
    cout<<"Enter matrix with '0' zeros where is\n";
    cout<<"nothing and '-1' minus-ones where mines are !\n";
    for (i=0;i<n;i++)
      for (j=0;j<m;j++)
       cin>>a[i][j];
    for (i=0;i<n;i++)
      for (j=0;j<m;j++)
        if (a[i][j]==0)
          a[i][j]=number(i,j);
      cout<<"Solution is \n";    
    for (i=0;i<n;i++)
    {
        cout<<"\n";
      for (j=0;j<m;j++)
      cout<<a[i][j]<<" ";
      }
      cout<<"\n";
    system("pause");
    }

"I dont know with what weapons World War 3 will be fought with, but i know World War 4 will be fought with stones and sticks" - Albert Einstein
Visit this user's website Find all posts by this user
Quote this message in a reply
08-03-2009, 09:22 PM
Post: #2
RE: Mineswepper
Nice,
So this is the the basic algorithm to a mine sweeper like game and i see your C++ skills are improving greatly!

"Character is determined more by the lack of certain experiences than by those one has had."
Friedrich Nietzsche
Visit this user's website Find all posts by this user
Quote this message in a reply
08-03-2009, 09:28 PM (This post was last modified: 08-03-2009 09:30 PM by drdebcol.)
Post: #3
RE: Mineswepper
Yeah, i think mineswepper windows programmers couldn't make it better than this !
Maybe i should make that game ! Maybe the biggest part of job in that would be making like 100 buttons with empty label which represent fields !
But it can be made easier with dynamic component declaration, i think i have that procedure somewhere !
And also randomization of mines on first click (you know that in the game you can not get a mine in the first click) !
Everything else is easy !
And yeah, i have been doing some C++ lately and i have to learn some libraries too, like "map" or "iterators" !

"I dont know with what weapons World War 3 will be fought with, but i know World War 4 will be fought with stones and sticks" - Albert Einstein
Visit this user's website Find all posts by this user
Quote this message in a reply
08-05-2009, 02:47 AM
Post: #4
RE: Mineswepper
lol... i just now discovered that number on field is nu,ber of how much mines field was associated with!!! thanks on info! Tongue

Read rules Smile
[Image: legislator.png]
Find all posts by this user
Quote this message in a reply
08-05-2009, 03:47 AM
Post: #5
RE: Mineswepper
(08-05-2009 02:47 AM)l3g1sl4tor Wrote:  lol... i just now discovered that number on field is nu,ber of how much mines field was associated with!!! thanks on info! Tongue
You really didn't know that ? LOL !

"I dont know with what weapons World War 3 will be fought with, but i know World War 4 will be fought with stones and sticks" - Albert Einstein
Visit this user's website Find all posts by this user
Quote this message in a reply
Post Reply 


Forum Jump:


 Quick Theme: