Skip to content

Commit c056915

Browse files
author
Belvedere Tiburon Library CoderDojo
committed
Update files
1 parent 35f19a8 commit c056915

File tree

8 files changed

+69
-14
lines changed

8 files changed

+69
-14
lines changed

Project templates/u1_battleship/1.py

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
board = []

Project templates/u1_battleship/2.py

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
width = 5
2+
height = 5
3+
board = [["O"]*height]*width

Project templates/u1_battleship/3.py

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
width = 5
2+
height = 5
3+
board = [["O"]*height]*width
4+
5+
def print_board(board):
6+
for row in board:
7+
print(" ".join(row))
8+
9+
10+
print_board(board)

Project templates/u1_battleship/4.py

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
from random import randint
2+
3+
width = 5
4+
height = 20
5+
board = [["O"]*height]*width
6+
7+
def print_board(board):
8+
for row in board:
9+
for char in row:
10+
print(char, end=" ")
11+
print()
12+
13+
row = randint(0, len(board)-1)
14+
col = randint(0, len(board[0])-1)
15+
16+
board[row][col] = "X"
17+
18+
print_board(board)

__pycache__/screens.cpython-35.pyc

1.85 KB
Binary file not shown.

mainloop.py

+16-6
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,19 @@
33
from tkinter import Tk
44
from screens import Screens
55

6-
master = Tk()
7-
master.geometry("500x250")
8-
app = Screens(master=master)
9-
master.mainloop()
10-
11-
input("Press enter to quit")
6+
q = False
7+
while not q:
8+
master = Tk()
9+
app = Screens(master=master)
10+
master.mainloop()
11+
done = False
12+
inp = input("Are you sure you want to quit (y/n)")
13+
while not done:
14+
if inp == 'y':
15+
done = True
16+
q = True
17+
break
18+
elif inp == 'n':
19+
done = True
20+
continue
21+
inp = input("Seriously. Type y or n")

screens.py

+21-7
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,10 @@ def __init__(self, master=None):
7070
ProjectScreen(1, PROJ, "An empty list", self.bs_empty_list),
7171
ProjectScreen(1, PROJ, "Making the board", self.bs_board_list),
7272
ProjectScreen(1, PROJ, "Printing the board", self.bs_print_board),
73-
ProjectScreen(1, PROJ, "Hide", None),
74-
ProjectScreen(1, PROJ, "and go seek!", None),
75-
ProjectScreen(1, PROJ, "#CHEAT-OS!!!", None),
76-
ProjectScreen(1, PROJ, "We have a winner!!!", None),
73+
ProjectScreen(1, PROJ, "Hide", self.bs_hide),
74+
ProjectScreen(1, PROJ, "and go seek!", self.bs_seek),
75+
ProjectScreen(1, PROJ, "#CHEAT-OS!!!", self.bs_debug),
76+
ProjectScreen(1, PROJ, "We have a winner!!!", self.bs_win),
7777
ProjectScreen(1, PROJ, "YOU SAX!!!", None),
7878
ProjectScreen(1, PROJ, "Next time, try to aim *inside* the ocean", None),
7979
ProjectScreen(1, PROJ, "\"Insanity: doing the same thing over and over and expecting different results\" - Albert Einstein", None),
@@ -97,7 +97,6 @@ def s_init(self, unitNum):
9797
def p_init(self, unitNum):
9898
self.new(0)
9999
s = "self.proj_"+str(unitNum)+"()"
100-
print(s)
101100
exec(s)
102101
self.master.geometry(self.homeBounds)
103102

@@ -218,9 +217,24 @@ def bs_empty_list(self):
218217

219218
def bs_board_list(self):
220219
self.new_proj(1, 2)
221-
self.multiLbl("""Now we need to make the list store where the ship is and where you fired and stuff so let's get right to the Chase. Or Wells Fargo. Or whatever bank you use. So, there's this thing I haven't told you. Actually two things. Number 1: If there's a list inside a list, it's called a 2-dimensional list or a 2D list. A list in a list in a list is a 3D list. Etc, etc, etc. The other thing is that you can create lists with multiple copies of the same value by wrapping it in square brackets (as in to create a list of one item) and multiplying it by a certain amount. Let me give you an example. Let's say you wanted a list with 3 "O"s stored in cheerios. You could initialize it like this: cheerios = ["O", "O", "O"] or you could initialize it like this: cheerios = ["O"]*3 and still get the same result. We'll use this when making our board. Instead of doing a manual 2D array and stuff, we're going to do it like this:\nrows = 5\ncols = 5\n board=[["O"]*5]*5 and you will get [['O', 'O', 'O', 'O', 'O'], ['O', 'O', 'O', 'O', 'O'], ['O', 'O', 'O', 'O', 'O'], ['O', 'O', 'O', 'O', 'O'], ['O', 'O', 'O', 'O', 'O']]""")
220+
self.multiLbl("""Now we need to make the list store where the ship is and where you fired and stuff so let's get right to the Chase. Or Wells Fargo. Or whatever bank you use. So, there's this thing I haven't told you. Actually two things. Number 1: If there's a list inside a list, it's called a 2-dimensional list or a 2D list. A list in a list in a list is a 3D list. Etc, etc, etc. The other thing is that you can create lists with multiple copies of the same value by wrapping it in square brackets (as in to create a list of one item) and multiplying it by a certain amount. Let me give you an example. Let's say you wanted a list with 3 "O"s stored in cheerios. You could initialize it like this: cheerios = ["O", "O", "O"] or you could initialize it like this: cheerios = ["O"]*3 and still get the same result. We'll use this when making our board. Instead of doing a manual 2D array and stuff, we're going to do it like this:\nwidth = 5\nheight = 5\n board=[["O"]*height]*width and you will get [['O', 'O', 'O', 'O', 'O'], ['O', 'O', 'O', 'O', 'O'], ['O', 'O', 'O', 'O', 'O'], ['O', 'O', 'O', 'O', 'O'], ['O', 'O', 'O', 'O', 'O']]""")
222221

223222
def bs_print_board(self):
224223
self.new_proj(1, 3)
225224
self.multiLbl("""We need a method to print out the board good because we don't want it looking like [['O', 'O', 'O', 'O', 'O'], ['O', 'O', 'O', 'O', 'O'], ['O', 'O', 'O', 'O', 'O'], ['O', 'O', 'O', 'O', 'O'],['O', 'O', 'O', 'O', 'O']]. We want to join it. So the function called print_board which will take one argument, board and will have a for loop in which we are doing a command *for row in board*. The command is a method I haven't told you about yet. It's called join. The syntax is like this: str.join(list). For example, we want to print O O O O O where it's currently printing ['O', 'O', 'O', 'O', 'O'] so we do " ".join(board) to join the board with spaces. That is our command in the for loop.""")
226-
225+
226+
def bs_hide(self):
227+
self.new_proj(1, 4)
228+
self.multiLbl("""Let's "hide" the ship. To do this, we need to have a few lines of code. First we need to do this thing called import-ing. To import, you can either do import module_name or from module_name import what_you_want_from_the_module (or you can add as what_you_want_to_called). So we need to do a from module_name import what_you_want. We want to import randint from random. Soooo, just use your logic and from random import randint. Then you need to set a variable *col =* to a *randint* from *(0* to *height-1)* and set *row =* to a *randint* from *(0* to *width-1)*. Then you have the locations of the ship""")
229+
230+
def bs_seek(self):
231+
self.new_proj(1, 5)
232+
self.multiLbl("""Let's "seek" the ship. To do this, we need to ask the user for *input()*. Let's set a variable called *row_guess =* to *input("Guess row: ") and set *col_guess =* to *input("Guess column")*. Then we have a guess that literally does nothing (so far).""")
233+
234+
def bs_debug(self):
235+
self.new_proj(1, 6)
236+
self.multiLbl("""It's not cheating. It's debugging. You should really *print* the *row* and *col*.""")
237+
238+
def bs_win(self):
239+
self.new_proj(1, 7)
240+
self.multiLbl("""To make a winning condition you need to test *if guess_row == row and guess_col == col:* and inside the *if* you should *print("something like \\"We have a winner\\"")*.\n\n\nP.S. \\ means escape a character, e.g. \\n means new line, \\\\ means a backslash, \\t means a tab, etc.""")

window.py

-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ def __init__(self, master=None):
77

88
def close_app(self):
99
self.master.destroy()
10-
print("Succesfully killed")
1110

1211
def cenBtn(self, txt, func):
1312
b = Button(self, text=txt, command=func)

0 commit comments

Comments
 (0)