C#

Moderators: None (Apply to moderate this forum)
Number of threads: 2722
Number of posts: 5749

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Somebody have Simple Minesweeper source code in C# Posted by haticee on 13 Dec 2008 at 10:06 AM
i need source code of Minesweeper in C#. It's just beginner or intermediate type of Windows Minesweeper. Somebody can heLp me?
Report
Re: Somebody have Simple Minesweeper source code in C# Posted by skobin on 5 Feb 2009 at 6:58 AM
using System;
using System.Collections.Generic;
using System.Text;

namespace mineText
{

class Borad
{
public Borad() //default constractor
{
mineNum = 2;
selX = 0;
selY = 0;
sizeX = 10;
sizeY = 10;
shown = new bool[sizeX, sizeY];
hasMine = new bool[sizeX, sizeY];
hasFlag = new bool[sizeX, sizeY];
aroundMines = new int[sizeX, sizeY];
}

private void putFrame(int x, int y, bool del)
{
if (del)
{
Console.SetCursorPosition(x - 1, y);
Console.Write(" ");
Console.SetCursorPosition(x + 1, y);
Console.Write(" ");
Console.SetCursorPosition(x, y - 1);
Console.Write(" ");
Console.SetCursorPosition(x, y + 1);
Console.Write(" ");
}
else
{
Console.SetCursorPosition(x - 1, y);
Console.Write("|");
Console.SetCursorPosition(x + 1, y);
Console.Write("|");
Console.SetCursorPosition(x, y - 1);
Console.Write("-");
Console.SetCursorPosition(x, y + 1);
Console.Write("-");
}
}

public void display()
{
for (int x = 0; x < sizeX; x++)
{
for (int y = 0; y < sizeY; y++)
{
Console.SetCursorPosition(x * 2 + 1, y * 2 + 1);
if (shown[x, y])
{
if (hasMine[x, y])
Console.Write('*');
else
{
if (aroundMines[x, y] != 0)
Console.Write(aroundMines[x, y]);
else
Console.Write(' ');
}
}
else
{
if (hasFlag[x, y])
Console.Write('F');
else
Console.Write('#');

}
}
}
}

private Boolean isXYvalid(int x, int y)
{
return ((x >= 0) && (y >= 0) && (x < sizeX) && (y < sizeY));
}

private Boolean placeMine(int x, int y)
{
if ((isXYvalid(x, y)) && (!hasMine[x, y]))
{
hasMine[x, y] = true;
for (int xx = -1; xx <= 1; xx++)
for (int yy = -1; yy <= 1; yy++)
{
if (((xx != 0) || (yy != 0)) && isXYvalid(x + xx, y + yy))
aroundMines[x + xx, y + yy]++;
}
return true;
}
else
return false;


}

private int rand(int range,int index)
{
return (int)(Math.Cos(index*1000+Math.Sin(index*101))*range);
}

public void makeBoard(int seed)
{
reveledNum = 0;

for (int x = 0; x < sizeX; x++)
for (int y = 0; y < sizeY; y++)
{
shown[x, y] = false;
hasMine[x, y] = false;
hasFlag[x, y] = false;
aroundMines[x, y] = 0;
}

Random rnd = new Random();
int count = 0;
int i = 0;
while (count < mineNum)
{
i++;
if (placeMine(rnd.Next(sizeX), rnd.Next(sizeY)))
count++;
}


}

public void putFlag(int x, int y)
{
if (!shown[x, y])
hasFlag[x, y] = true;
}

public void removeFlag(int x, int y)
{
//if (!shown[x, y])
hasFlag[x, y] = false;
}

public bool revelBlock(int x, int y)
{
if (!hasFlag[x, y])
{
reveledNum++;
shown[x, y] = true;
int newX, newY;
if ((aroundMines[x ,y] == 0) && (!hasMine[x, y]))
{
for (int xx = -1; xx <= 1; xx++)
for (int yy = -1; yy <= 1; yy++)
{
newX = x + xx;
newY = y + yy;
if ((isXYvalid(newX, newY)) && (!shown[newX, newY]) && (!hasFlag[newX, newY]))
revelBlock(newX, newY);
}
}
return hasMine[x, y];
}
else
return false;
}

public bool wonGame()
{
Console.WriteLine(sizeX * sizeY);
Console.WriteLine(reveledNum);
Console.WriteLine((sizeX * sizeY) - (reveledNum + mineNum));
return (reveledNum + mineNum) == (sizeX * sizeY);
}

public bool control(ConsoleKeyInfo cki)
{

putFrame(selX * 2 + 1, selY * 2 + 1,true);

if ((selX < sizeX - 1)&&(cki.Key == ConsoleKey.RightArrow))
selX++;
if ((selX > 0) && (cki.Key == ConsoleKey.LeftArrow))
selX--;
if ((selY < sizeY - 1) && (cki.Key == ConsoleKey.DownArrow))
selY++;
if ((selY > 0) && (cki.Key == ConsoleKey.UpArrow))
selY--;
putFrame(selX * 2 + 1, selY * 2 + 1,false);


if (cki.Key == ConsoleKey.Enter)
putFlag(selX, selY);
if (cki.Key == ConsoleKey.Backspace)
removeFlag(selX, selY);
if (cki.Key == ConsoleKey.Spacebar)
return revelBlock(selX, selY);
else
return false;

}

private bool[,] shown, hasMine, hasFlag;
private int[,] aroundMines;
int sizeX, sizeY;
int selX, selY;
int mineNum, reveledNum;
}

class Program
{


static void Main(string[] args)
{
Borad game;
bool exit = false;
int lostNum = 1;
ConsoleKeyInfo ch;

game = new Borad();
game.makeBoard(10);
game.display();

do
{
ch = Console.ReadKey(true);
//newGame = game.control(ch);

if (game.control(ch))
{
game.makeBoard(10);
Console.SetCursorPosition(25, lostNum);
Console.WriteLine("you lost");
game.display();
lostNum++;

}
//Console.KeyAvailable
game.display();
exit = ch.Key == ConsoleKey.Escape;
}
while (!exit && !game.wonGame());
if (game.wonGame())
Console.WriteLine("you won!");
}
}
}




 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.