Skip to content

Commit 5aa6aab

Browse files
authored
Add files via upload
1 parent b1bcc4d commit 5aa6aab

File tree

4 files changed

+136
-0
lines changed

4 files changed

+136
-0
lines changed

CheckTicTacToe.py

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# given a 3 by 3 list of lists that represents a Tic Tac Toe game board,
2+
# tell me whether anyone has won, and tell me which player won, if any.
3+
# A Tic Tac Toe win is 3 in a row - either in a row, a column, or a
4+
# diagonal. Don’t worry about the case where TWO people have won -
5+
# assume that in every board there will only be one winner.
6+
7+
def checkforwin(matrix): #returns 1 if p1 won, 2 if p2 won, and 0 if no win
8+
for line in range(3):
9+
if matrix[line][0] == matrix[line][1] == matrix[line][2]: #test rows
10+
return matrix[line][0]
11+
elif matrix[0][line] == matrix[1][line] == matrix[2][line]: #test columns
12+
return matrix[0][line]
13+
if matrix[0][0] == matrix[1][1] == matrix[2][2]:
14+
return matrix[0][0]
15+
elif matrix[0][2] == matrix[1][1] == matrix [2][0]:
16+
return matrix[0][2]
17+
else:
18+
return 0
19+
20+
21+
if __name__ == "__main__":
22+
winner_is_2 = [[2, 2, 0],
23+
[2, 1, 0],
24+
[2, 1, 1]]
25+
26+
winner_is_1 = [[1, 2, 0],
27+
[2, 1, 0],
28+
[2, 1, 1]]
29+
30+
winner_is_also_1 = [[0, 1, 0],
31+
[2, 1, 0],
32+
[2, 1, 1]]
33+
34+
no_winner = [[1, 2, 0],
35+
[2, 1, 0],
36+
[2, 1, 2]]
37+
38+
also_no_winner = [[1, 2, 0],
39+
[2, 1, 0],
40+
[2, 1, 0]]
41+
42+
winner = checkforwin(also_no_winner)
43+
44+
if winner == 0:
45+
print("No winner!")
46+
else:
47+
print("Player " + str(winner) + " won!")

DrawAGameBoard.py

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Ask the user what size game board they want to draw, and draw
2+
# it for them to the screen using Python’s print statement.
3+
4+
def build_board(width, height):
5+
'''
6+
Takes in the width and height of the game board
7+
Returns a string containing a faux-graphical board
8+
'''
9+
10+
outputrows = ["" for each in range(height*2+1)]
11+
12+
for row in range(len(outputrows)): #2 parts to each board row: mid and bottom, except first row has top
13+
for column in range(width*4+1):# 4 parts to each board column, midleft, mid, midright, right, except first colomn has left
14+
if column%4 != 0 and row%2 == 0:#if column# is not 0,4,8, etc && row# is even, add "-" (ZERO INDEX)
15+
outputrows[row] += "-"
16+
elif column%4 == 0 and row%2 == 1: # if column# is 0,4,8, etc && row is odd, add a "|"
17+
outputrows[row] += "|"
18+
else:
19+
outputrows[row] += " "
20+
21+
return "\n".join(outputrows)
22+
23+
24+
if __name__ == "__main__":
25+
print(build_board(3,3))

GuessingGameTwo.py

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# You, the user, will have in your head a number between 0 and 100.
2+
# The program will guess a number, and you, the user, will say whether
3+
# it is too high, too low, or your number.
4+
5+
def guess(lastguess, answer, minim = 0, maxim = 100): #binary "search" method
6+
if answer == "l": #lastguess is too low
7+
minim = lastguess+1
8+
else: #lastguess is too high
9+
maxim = lastguess-1
10+
guess = int((minim + maxim)/2)
11+
12+
return [guess, minim, maxim]
13+
14+
15+
def ask(guess):
16+
17+
guess = str(guess)
18+
19+
answer = input("Is your number " + guess + "? (C)orrect, Too (L)ow, Too (H)igh\n").lower()
20+
21+
while answer != "c" and answer != "l" and answer != "h":
22+
answer = input("I guessed " + guess + ". Type C if correct, L if too low, and H if too high.\n").lower()
23+
24+
return answer
25+
26+
27+
if __name__ == "__main__":
28+
print("Write down a whole number between 0 and 100, and I will try to guess it. Press enter when you're ready!")
29+
input("Prepare yourself, mortal!\n")
30+
31+
numguesses = 1
32+
guesslist = [50, 0, 100]
33+
answer = ask(guesslist[0])
34+
cheated = False
35+
36+
while answer != "c":
37+
if guesslist[0] == guesslist[1] and guesslist[0] == guesslist[2]:
38+
cheated = True
39+
break
40+
guesslist = guess(guesslist[0], answer, guesslist[1], guesslist[2])
41+
print(guesslist[0])
42+
print(guesslist[1])
43+
print(guesslist[2])
44+
numguesses += 1
45+
answer = ask(guesslist[0])
46+
47+
if cheated is True:
48+
print("What a cheater! You changed your number.")
49+
else:
50+
print("I guessed that your number is " + str(guesslist[0]) + " in only " + str(numguesses) + " guesses!")

WriteToAFile.py

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# I'm kinda rewriting the directions on this one
2+
# Ask user for name of file
3+
# Ask user what to put in file
4+
# Output info to .txt file
5+
6+
if __name__ == "__main__":
7+
name = input("What would you like your file to be named?\n")
8+
text = input("What would you like written in your file?\n")
9+
10+
with open(name + ".txt", "w") as openfile: #create very localized scope in below block (super cool!)
11+
openfile.write(text)
12+
13+
14+
#"w" = write only, "r" = read only, "r+" = read and write!

0 commit comments

Comments
 (0)