-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnsew.py
More file actions
97 lines (87 loc) · 2.53 KB
/
nsew.py
File metadata and controls
97 lines (87 loc) · 2.53 KB
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
#!/usr/bin/env python3
# Mission: Coding a classic `Nsew`
# (pronounced NessEwwh ;-)
# Nsew is designed to be as easy to understand as possible, yet still be interesting enough to play.
# Project: https://github.com/Python3-Training/Coding-w-Nagy
'''
The Game of Nsew
================
Objective is to see how many "risks"
(moves into unknown regions) we can
endure before being "zeroed," or
scoring less than zero.
'''
debug = False # False = hide cell values
from random import randrange
def move(w, pos, board_size):
hpos = list(pos)
if w == 'n':
pos[1] -= 1
elif w == 's':
pos[1] += 1
elif w == 'e':
pos[0] += 1
elif w == 'w':
pos[0] -= 1
else:
return False
for ipos in 0, 1:
if pos[ipos] < 0:
pos[ipos] = 0
if pos[ipos] >= board_size:
pos[ipos] = board_size -1
if pos == hpos:
print("bump..")
return False
return True
def show(board, pos):
for yy, row in enumerate(board):
for xx, col in enumerate(row):
if xx == pos[0] and yy == pos[1]:
print('[$]\t', end='')
elif col == 0:
print(f'{col}\t', end='')
else:
if debug:
print(f'{col}\t', end='')
else:
print('?\t', end='')
print()
score = 10
board_size = 8
board = [[int(randrange(-3,3)) for _ in range(board_size)] for _ in range(board_size)]
risks = hi = lo = evil = 0
for row in board:
for col in row:
if col < 0:
lo += col
risks += 1
evil += 1
elif col > 0:
hi += col
risks += 1
print("Welcome to Nsew!")
print(__doc__)
pct = int(100-((board_size * 2/evil)*100))
print(f"Game is {pct}% evil (Net {lo+hi})\n")
moved = 0
pos = [int(board_size/2), int(board_size/2)]
show(board, pos) # Jump
while True:
print(f"Your Score: {score}, Risks: {moved}")
where = input("NSEW? ").strip().lower()
if not where:
break
if move(where[0], pos, board_size):
if board[pos[1]][pos[0]] != 0:
moved +=1
score += board[pos[1]][pos[0]]
board[pos[1]][pos[0]] = 0
if score < 0:
debug = True
show(board, pos)
if score < 0:
print(f"Zeroed in {moved} risks. [{pct}% evil (${lo+hi})]")
break
else:
print('Please enter either N,S,E or W')