repl.it
@kieranlocke98/

MineSweeper

Python

No description

fork
loading
Files
  • main.py
  • instructions.txt
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
#these are the external libraries that wi;l be used inside the game.
import random, time, copy

#Introduction
time.sleep(2)
print('Welcome to my version of MineSweeper')


# this is where the reset function will be defined and the user can restart the game or view the instructions

def reset():
    print('''

MENU SCREEN


If you wish to read the instruction press 'I'

To play the game press 'P'


''')
    #this section is where the user is asked to enter their input of I or P in upper case
    choice = input('type here: ').lower()
    #this is the strt of the if statement and the what will happen if I is selected
    if choice == 'I':
        

        #this piece of code will open the instructions text file
        print(open('instructions.txt', 'r').read())
        #the player then needs to press enter before continuing
        input('press [enter] when ready to play. ')
    # if the player does not press P then the terminal will reset
    elif choice != 'P':
        
        reset()

#this is what will place the bombs in rndom locations  
def placeBomb(b):
    #this will determin which row (r) and column (c) to place the bomb randomly
    r = random.randint(0, 9)
    c = random.randint(0, 9)

    currentRow = b[r]
    if not currentRow[c] == '*':
        currentRow[c] = '*'
    else:
        placeBomb(b)

b = [[0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0],
     [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0],
     [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0],
     [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0],
     [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0]]

for n in range (0,10):
    placeBomb(b)


for r in range (0,9):
    for c in range (0,9):
        value = 1(r, c, b)
        if value == '*':
            updateValues(r, c, b)

#Sets the variable k to a grid of blank spaces, because nothing is yet known about the grid.
k = [[' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '], [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ']
     , [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '], [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ']
     , [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '], [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ']
     , [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '], [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ']
     , [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '], [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ']]


printBoard(k)

#Start timer
startTime = time.time()

play(b, k, startTime)

#Gets the value of a coordinate on the grid.
def l (r, c, b):
    return b [r] [c]



#Adds 1 to all of the squares around a bomb.
def updateValues(rn, c, b):

    #Row above.
    if rn-1 > -1:
        r = b[rn-1]
        
        if c-1 > -1:
            if not r[c-1] == '*':
                r[c-1] += 1

        if not r[c] == '*':
            r[c] += 1

        if 10 > c+1:
            if not r[c+1] == '*':
                r[c+1] += 1

    #Same row.    
    r = b[rn]

    if c-1 > -1:
        if not r[c-1] == '*':
            r[c-1] += 1

    if 10 > c+1:
        if not r[c+1] == '*':
            r[c+1] += 1

    #Row below.
    if 10 > rn+1:
        r = b[rn+1]

        if c-1 > -1:
            if not r[c-1] == '*':
                r[c-1] += 1

        if not r[c] == '*':
            r[c] += 1

        if 10 > c+1:
            if not r[c+1] == '*':
                r[c+1] += 1

def zeroProcedure(r, c, k, b):

    #Row above
    if r-1 > -1:
        row = k[r-1]
        if c-1 > -1: row[c-1] = l(r-1, c-1, b)
        row[c] = l(r-1, c, b)
        if 10 > c+1: row[c+1] = l(r-1, c+1, b)

    #Same row
    row = k[r]
    if c-1 > -1: row[c-1] = l(r, c-1, b)
    if 10 > c+1: row[c+1] = l(r, c+1, b)

    #Row below
    if 10 > r+1:
        row = k[r+1]
        if c-1 > -1: row[c-1] = l(r+1, c-1, b)
        row[c] = l(r+1, c, b)
        if 10 > c+1: row[c+1] = l(r+1, c+1, b)

#Checks known grid for 0s.
def checkZeros(k, b, r, c):
    oldGrid = copy.deepcopy(k)
    zeroProcedure(r, c, k, b)
    if oldGrid == k:
        return
    while True:
        oldGrid = copy.deepcopy(k)
        for x in range (10):
            for y in range (10):
                if l(x, y, k) == 0:
                    zeroProcedure(x, y, k, b)
        if oldGrid == k:
            return


#Places a marker in the given location.
def marker(r, c, k):
    k[r][c] = '⚐'
    printBoard(k)


#this is what will print the playing board
def printBoard(b):
    replit.clear()
    print()
    print('    A   B   C   D   E   F   G   H   I   J')
    print('  ╔═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╗')

for r in range (0, 9):
    print(r,'║',l(r,0,b),'║',l(r,1,b),'║',l(r,2,b),'║',l(r,3,b),'║',l(r,4,b),'║',l(r,5,b),'║',l(r,6,b),'║',l(r,7,b),'║',l(r,8,b),'║',l(r,9,b),'║')
    if not r == 8:
        print('  ╠═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╣')
print('  ╚═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╝')


#The player chooses a location.
def choose(b, k, startTime):
    #Variables 'n stuff.
    letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h' ,'i', 'j']
    numbers = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
    #Loop in case of invalid entry.
    while True:
        chosen = input('Please choose a square (eg. D1) or place a marker (eg. mD4): ').lower()
        #Checks for valid square.
        if len(chosen) == 3 and chosen[0] == 'm' and chosen[1] in letters and chosen[2] in numbers:
            c, r = (ord(chosen[1]))-97, int(chosen[2])
            marker(r, c, k)
            play(b, k, startTime)
            break
        elif len(chosen) == 2 and chosen[0] in letters and chosen[1] in numbers: return (ord(chosen[0]))-97, int(chosen[1])
        else: choose(b, k, startTime)         



def play (b, k, startTime):
    
#Player chooses square.
    c, r = choose(b, k, startTime)       
#Gets the value at that location.
    v = l(r, c, b)
#If you hit a bomb, it ends the game.
if v == '*':
    printBoard(b)
    print('You Lose!')
    #Print timer result.
    print('Time: ' + str(round(time.time() - startTime)) + 's')
    #Offer to play again.
    playAgain = input('Play again? (Y/N): ').lower()
    if playAgain == 'y':
        replit.clear()
        reset()
    else:
        quit()
#Puts that value into the known grid (k).
k[r][c] = v
#Runs checkZeros() if that value is a 0.
if v == 0:
    checkZeros(k, b, r, c)
printBoard(k)
#Checks to see if you have won.
squaresLeft = 0
for x in range (0, 10):
    row = k[x]
    squaresLeft += row.count(' ')
    squaresLeft += row.count('⚐')

if squaresLeft == 10:
    printBoard(b)
    print('You win!')
    #Print timer result.
    print('Time: ' + str(round(time.time() - startTime)) + 's')
    #Offer to play again.
    playAgain = input('Play again? (Y/N): ')
    playAgain = playAgain.lower()
    if playAgain == 'y':
        replit.clear()
        reset()
    else:
            quit()

#Repeats!
play(b, k, startTime)

reset()