This document contains the code for a rock paper scissors game. The code randomly generates the computer's choice from a list of options, allows the user to input their choice, compares the choices to determine a winner, and prints the result. It includes formatting to print messages indicating a draw, win, or loss. The game loops continuously to allow repeated plays.
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
New microsoft office word document
1.
2. Name: Rudra pratap singh
Roll NO : Class : Xll
SCI
SCHOOL : A.P.S ACADEMY
This is to cerify that the bonafide work of F.I.T (Practical / Project) has
been done by the student as per CBSE requirement during the
academic session : 2019-2020
Examiner’s signature Teacher – In – charge
Date HEAD OF INSTITUTION
3. I WOULD LIKE TO EXPRESS MY SPECIAL
THANKS OF GRATITUDE TO MY TEACHER
MRS. DEEPIKA MULLICK AS WELL AS OUR
PRINCIPAL MRS. HEMA KALAKOTI WHO
GAVE ME THE GOLDEN OPPORTUNITY TO
DO THIS WONDERFUL PROJECT ON THE
TOPIC GAMEING PROGRAM WHICH ALSO
HELPED ME IN DOING A LOT OF RESEARCH
AND I COME TO KNOW ABOUT SO MANY
NEW THINGS I AM REALLY THANKFUL TO
THEM.
SECONDLY I WOULD ALSO LIKE TO THANK
MY PARENTS AND FRIENDS WHO HELPED
ME A LOT IN FINALIZING THIS PROJECT
WITHIN THE LIMITED TIME FRAME.
4.
5. importrandom
importcollections
random.seed(1)
size = 5
bombs = collections.defaultdict(int)
counts = collections.defaultdict(int)
display = collections.defaultdict(lambda: 'X')
neighbors = sorted(set((i, j) for i in range(size) for j in range(size)) - {(0, 0)})
# Initialize
for x in range(size):
for y in range(size):
bombs[x, y] = 0
counts[x, y] = 0
display[x, y] = '?'
# Set bombs
for _ in range(3):
x = random.randrange(size)
y = random.randrange(size)
bombs[x, y] = 1
# Calculate counts
for x in range(size):
for y in range(size):
total = 0
for i in [-1, 0, 1]:
for j in [-1, 0, 1]:
total += bombs[x + i, y + j]
6. counts[x, y] = total
def show(grid):
for x in range(size):
for y in range(size):
print(grid[x, y], end=', ')
print()
print('Bombs:')
show(bombs)
print('Counts:')
show(counts)
alive = True
def reveal(x, y):
if bombs[x, y]:
return False
display[x, y] = counts[x, y]
if counts[x, y]:
return True
zeros = [ (x, y) ]
while zeros:
x, y = zeros.pop()
if not ((0 <= x < size) and (0 <= y < size)):
continue
if counts[x, y] == 0:
display[x, y] = 0
for i in (-1, 0, 1):
for j in (-1, 0, 1):
7. if i == j == 0:
continue
offset_x = x + i
offset_y = y + j
seen = display[offset_x, offset_y] != '?'
if counts[offset_x, offset_y] == 0 and notseen:
zeros.append((offset_x, offset_y))
return True
while alive:
show(display)
x = int(input('row: '))
y = int(input('column: '))
alive = reveal(x, y)
if not alive or not any(spot== '?' for spotin display.values()):
break
if alive:
print('Congratulations!You win.')
else:
print('Sorry, you failed.')
----------------------------------------------------------------------------------------------------
-------------------------
8.
9.
10.
11. importatexit
importcollections
importitertools
importrandom
importsqlite3
importthreading
importtime
importconsole
original_terminal_state = console.get_terminal_mode()
atexit.register(console.set_terminal_mode, original_terminal_state)
class Game:
"Game state for Tetris."
def __init__(self, width, height, seed=None):
self.random = random.Random(seed)
self.width= width
self.height = height
self.board = collections.defaultdict(lambda: '#')
for x in range(width):
for y in range(height):
self.board[x, y] = ' '
self.active = True
self.speed = 20
self.next_letter = self.random.choice('IJLOSTZ')
self.piece = self.next_piece()
self.score = 0
self.stash = None
def draw(self):
"Draw game state."
print('Score:', self.score, end='rn')
print('Level:', self.score// 4 + 1, end='rn')
print('Nextpiece:', self.next_letter, end='rn')
print('Stash piece:', 'no' if self.stash is Noneelse 'yes', end='rn')
print('*' * (self.width + 2), end='rn')
12. for y in range(self.height):
print('|', end='')
for x in range(self.width):
if (x, y) in self.piece:
print('@', end='')
else:
print(self.board[x, y], end='')
print('|', end='rn')
print('*' * (self.width + 2), end='rn')
def next_piece(self):
"Create a new piece, on collision set active to False."
letter = self.next_letter
self.next_letter = self.random.choice('IJLOSTZ')
if letter == 'I':
piece = {(0, 0), (0, 1), (0, 2), (0, 3)}
elif letter == 'J':
piece = {(1, 0), (1, 1), (1, 2), (0, 2)}
elif letter == 'L':
piece = {(0, 0), (0, 1), (0, 2), (1, 2)}
elif letter == 'O':
piece = {(0, 0), (0, 1), (1, 0), (1, 1)}
elif letter == 'S':
piece = {(0, 1), (1, 0), (1, 1), (2, 0)}
elif letter == 'T':
piece = {(0, 0), (1, 0), (2, 0), (1, 1)}
else:
assert letter == 'Z'
piece = {(0, 0), (1, 0), (1, 1), (2, 1)}
offset = self.width // 2 - 1
piece = {(x + offset, y) for x, y in piece}
if self.collide(piece):
self.end()
return piece
def end(self):
self.active = False
print('Gameover!Pressany key to quit.', end='rn')
13. def tick(self, mark):
"Notify the game of a clock tick."
if mark % self.speed == 0:
moved = self.move_piece(0, 1)
if not moved:
for x, y in self.piece:
self.board[x, y] = '#'
self.collapse()
self.piece = self.next_piece()
self.draw()
def collapse(self):
"Collapse fulllines."
y = self.height - 1
while y >= 0:
full_line= all(self.board[x, y] == '#' for x in range(self.width))
if full_line:
z = y
while z > 0:
for x in range(self.width):
self.board[x, z] = self.board[x, z - 1]
z -= 1
for x in range(self.width):
self.board[x, 0] = ' '
self.score += 1
if self.score % 4 == 0:
self.speed -= 1
else:
y -= 1
def collide(self, piece):
"Check whether piece collides with others on board."
return any(self.board[x, y] != ' ' for x, y in piece)
def move_piece(self, x, y):
"Movepiece by delta x and y."
new_piece= {(a + x, y + b) for a, b in self.piece}
if self.collide(new_piece):
return False
14. self.piece = new_piece
return True
def rotate_piece(self):
"Rotate piece."
min_x = min(xfor x, y in self.piece)
max_x = max(x for x, y in self.piece)
diff_x = max_x - min_x
min_y = min(y for x, y in self.piece)
max_y = max(y for x, y in self.piece)
diff_y = max_y - min_y
size = max(diff_x, diff_y)
new_piece= set()
for x, y in self.piece:
pair = (min_x + size) - (y - min_y), min_y + (x - min_x)
new_piece.add(pair)
if self.collide(new_piece):
return False
self.piece = new_piece
return True
def move(self, key):
"Update game state based on key press."
if key == 'left':
moved = self.move_piece(-1, 0)
elif key == 'right':
moved = self.move_piece(1, 0)
elif key == 'down':
moved = self.move_piece(0, 1)
elif key == 'up':
moved = self.rotate_piece()
elif key == 'swap':
if self.stash is None:
self.stash = self.piece
self.piece = self.next_piece()
else:
self.piece, self.stash = self.stash, self.piece
if self.collide(self.piece):
self.end()
15. moved = True
else:
assert key == 'space'
moved = self.move_piece(0, 1)
while moved:
moved = self.move_piece(0, 1)
moved = True
if moved:
self.draw()
def draw_loop(game):
game.draw()
counter = itertools.count(start=1)
while game.active:
mark = next(counter)
game.tick(mark)
time.sleep(0.1)
def input_loop(game):
"""Input loop.
Handlekeyboard inputin a separate thread.
"""
while game.active:
key = console.get_input()
if key is None:
continue
elif key == 'quit':
game.active = False
else:
assert key in ('left', 'down', 'right', 'up', 'space', 'swap')
game.move(key)
console.set_terminal_mode(original_terminal_state)
print('Enter your namefor leaderboard (blank to ignore):')
name = input()
16. if name:
con = sqlite3.connect('tetris.sqlite3', isolation_level=None)
con.execute('CREATE TABLE IFNOT EXISTS Leaderboard (name, score)')
con.execute('INSERTINTO Leaderboard VALUES (?, ?)', (name,
game.score))
scores = con.execute('SELECT* FROM Leaderboard ORDER BY score
DESC LIMIT 10')
print('{0:<16}| {1:<16}'.format('Name', 'Score'))
for pair in scores:
print('{0:<16}| {1:<16}'.format(*pair))
def main():
"Main entry-pointfor Tetris."
game = Game(10, 10)
draw_thread = threading.Thread(target=draw_loop, args=(game,))
input_thread = threading.Thread(target=input_loop, args=(game,))
draw_thread.start()
input_thread.start()
draw_thread.join()
input_thread.join()
if __name__ == '__main__':
main()
---------------------------------------------------------------------------------------------------
17.
18.
19. import time
from time import sleep
import random
sus="-"*35
depo=["rock","paper","scissors"]
while True:
x=input("rock , paper, scissors: ")
if x not in depo:
print ("Dontcheat!")
continue
pc=random.choice(depo)
sleep(0.5)
print (("Computer picked {}.").format(pc))
if x==pc:
sleep(0.5)
print (("nIt's a draw.n{}").format(sus))
elif x=="rock" and pc=="scissors":
sleep(0.5)
print (("nYou win.rock beats scissorsn{}").format(sus))
elif x=="paper" and pc=="rock":
sleep(0.5)
print (("nYou win.paper beats rockn{}").format(sus))
elif x=="scissors" and pc=="paper":
sleep(0.5)
print (("nYou win.scissors beats papern{}").format(sus))
else:
sleep(0.5)
print (("nYou lose. {} beats {}n{}").format(pc,x,sus))
input()