0% found this document useful (0 votes)
14 views3 pages

Minesweeper0 65

This document contains the code for a minesweeper game. It defines variables for the size of the minefield (10x10 cells), number of mines (20), and initializes an empty field and table. Functions are defined to start the game in a safe cell, randomly place mines, label surrounding cells with mine counts, uncover cells, and allow flagging of suspected mine locations. The main part of the code runs the game loop, placing the initial safe cell and allowing the user to uncover or flag cells until all non-mine cells are revealed.

Uploaded by

fawejer870
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views3 pages

Minesweeper0 65

This document contains the code for a minesweeper game. It defines variables for the size of the minefield (10x10 cells), number of mines (20), and initializes an empty field and table. Functions are defined to start the game in a safe cell, randomly place mines, label surrounding cells with mine counts, uncover cells, and allow flagging of suspected mine locations. The main part of the code runs the game loop, placing the initial safe cell and allowing the user to uncover or flag cells until all non-mine cells are revealed.

Uploaded by

fawejer870
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

Minesweeper0.65.

py
import random
#size of minefield
Y = 10
X = 10
#mines
M = 20
#sets mine counter
mines = 0
#places where mines dont generate
Places=[[-69 for x in range(2)]for y in range(X*Y-M)]
plcs = 0
#creates the empty field
Table = [[0 for y in range(X)] for x in range(Y)]
Field = [['@' for y in range(X)] for x in range(Y)]
unknown = X*Y

#eliminates starter fields from being mined


def Start(sy,sx):
#lsy=2
#sx=2
global plcs
#print(Places)
#print(plcs)

for y in range(sy-1,sy+2):
for x in range(sx-1,sx+2):
if(y > -1 and y < Y and x > -1 and x <X):
Places[plcs][0] = int(y)
Places[plcs][1] = int(x)
plcs+=1
#print(Places)
#print(plcs)

def InPlaces(y,x,plcs):
for i in range(plcs):
if(y == Places[i][0] and x == Places[i][1]):
return False
return True

#adds mines
def Gen(mines):
global plcs
while(mines < M):
y = random.randint(0,Y-1)
x = random.randint(0,X-1)
#print(mines, M)
if(InPlaces(y,x,plcs)==True):
Places[plcs][0] = y
Places[plcs][1] = x
plcs +=1
print(y,x)
Table[y][x] = 9
mines+=1

def Otocz(y,x):
#print("0: y:",y,"x:",x)
for y1 in range(y-1,y+2):
for x1 in range(x-1,x+2):
#print("1: y1:",y1,"x1:",x1)
if(x1>=0 and x1<X and y1>=0 and y1<Y):
if(x1!=x or y1!= y):
#print("2: y1:",y1,"x1:",x1)
if(Table[y1][x1] != 9):
#print(" 3: y1:",y1,"x1:",x1)
Table[y1][x1] += 1

def Label():
#labels fields
for y in range(Y):
for x in range(X):
if(Table[y][x] == 9):
Otocz(y,x)

#draws the current state of minefield


def draw(Table):
for i in range(Y):
for j in range(X):
print(Table[i][j], end=' ')
print('\n')
print("mines: ", mines,'\n','\n')

def Uncover(y,x):
global unknown
unknown -= 1
if(Table[y][x]==9):
Field[y][x]='X'
print('GAME OVER')
unknown = 0
else:
if(Table[y][x]==0):
Field[y][x]=Table[y][x]
for j in range(y-1,y+2):
for i in range(x-1,x+2):
if(j > -1 and j < Y and i > -1 and i < X and Field[j]
[i] == '@'):
Uncover(j,i)

else:
Field[y][x]=Table[y][x]

def Flag(y,x):
global unknown
global mines
Field[y][x]='P'
unknown -=1
mines -=1

def Play():
while(unknown>0):
flag = False
f = input('f if you want to flag')
y=int(input('y:'))
x=int(input('x:'))
if(f=='f' or f=='F'):
flag = True
Flag(y,x)
else:
Uncover(y,x)
draw(Field)

#main
print("Pole o wysokosci:",Y,"i szerokosci:",X)
#starter field
sy = int(input("input y:"))
sx = int(input("input x:"))

Start(sy,sx)

draw(Table)

Gen(mines)

Label()
#counts mines
for y in range(Y):
for x in range(X):
if(Table[y][x] == 9):
mines+=1

Uncover(sy,sx)
draw(Field)
print(unknown)

Play()

You might also like