All Tasks File
All Tasks File
import random
def main():
while True:
low = 1
high = 100
number = generate_number(low, high)
if mode == 'm':
print("You've chosen multiplayer mode!")
else:
print("You've chosen to play against AI!")
if mode == 'm':
player = 1
attempts = 10
while attempts > 0:
print(f"Player {player}, guess the number.")
guess = input("Enter your guess: ")
try:
guess = int(guess)
if guess < low or guess > high:
print(f"Please enter a number between {low} and {high}.")
continue
except ValueError:
print("Please enter a valid number.")
continue
if guess == number:
print(f"Congratulations! Player {player} guessed the number!")
break
elif guess < number:
print("Too low! Try a higher number.")
else:
print("Too high! Try a lower number.")
attempts -= 1
print("Attempts left:", attempts)
player = 2 if player == 1 else 1
if guess == number:
print("Congratulations! You've guessed the number!")
break
elif guess < number:
print("Too low! Try a higher number.")
result = "lower"
else:
print("Too high! Try a lower number.")
result = "higher"
attempts -= 1
print("Attempts left:", attempts)
attempts -= 1
print("Attempts left:", attempts)
if _name_ == "_main_":
main()
Task 2:
import pygame
pygame.font.init()
Window = pygame.display.set_mode((500, 500))
pygame.display.set_caption("SUDOKU GAME by DataFlair")
x=0
z=0
diff = 500 / 9
value= 0
defaultgrid =[
[0, 0, 4, 0, 6, 0, 0, 0, 5],
[7, 8, 0, 4, 0, 0, 0, 2, 0],
[0, 0, 2, 6, 0, 1, 0, 7, 8],
[6, 1, 0, 0, 7, 5, 0, 0, 9],
[0, 0, 7, 5, 4, 0, 0, 6, 1],
[0, 0, 1, 7, 5, 0, 9, 3, 0],
[0, 7, 0, 3, 0, 0, 0, 1, 0],
[0, 4, 0, 2, 0, 6, 0, 0, 7],
[0, 2, 0, 0, 0, 7, 4, 0, 0],
]
font = pygame.font.SysFont("comicsans", 40)
font1 = pygame.font.SysFont("comicsans", 20)
def cord(pos):
global x
x = pos[0]//diff
global z
z = pos[1]//diff
def highlightbox():
for k in range(2):
pygame.draw.line(Window, (0, 0, 0), (x * diff-3, (z + k)*diff), (x * diff + diff + 3,
(z + k)*diff), 7)
pygame.draw.line(Window, (0, 0, 0), ( (x + k)* diff, z * diff ), ((x + k) * diff, z *
diff + diff), 7)
def drawlines():
for i in range (9):
for j in range (9):
if defaultgrid[i][j] != 0:
# Change text color to dark gray
text_color = (50, 50, 50)
text1 = font.render(str(defaultgrid[i][j]), 1, text_color)
# Add a light gray background behind the numbers
bg_color = (200, 200, 200)
pygame.draw.rect(Window, bg_color, (i * diff, j * diff, diff + 1, diff + 1))
Window.blit(text1, (i * diff + 15, j * diff + 15))
for l in range(10):
if l % 3 == 0:
thick = 7
else:
thick = 1
pygame.draw.line(Window, (0, 0, 0), (0, l * diff), (500, l * diff), thick)
pygame.draw.line(Window, (0, 0, 0), (l * diff, 0), (l * diff, 500), thick)
def fillvalue(value):
text1 = font.render(str(value), 1, (0, 0, 0))
Window.blit(text1, (x * diff + 15, z * diff + 15))
def raiseerror():
text1 = font.render("Wrong!", 1, (255, 0, 0))
Window.blit(text1, (20, 570))
def raiseerror1():
text1 = font1.render("Wrong! Enter a valid key for the game", 1, (255, 0, 0))
Window.blit(text1, (20, 570))
def gameresult():
text1 = font.render("Game finished", 1, (0, 0, 0))
Window.blit(text1, (20, 570))
flag=True
flag1 = 0
flag2 = 0
rs = 0
error = 0
while flag:
Window.fill((255,182,193))
for event in pygame.event.get():
if event.type == pygame.QUIT:
flag = False
if event.type == pygame.MOUSEBUTTONDOWN:
flag1 = 1
pos = pygame.mouse.get_pos()
cord(pos)
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_1:
value = 1
if event.key == pygame.K_2:
value = 2
if event.key == pygame.K_3:
value = 3
if event.key == pygame.K_4:
value = 4
if event.key == pygame.K_5:
value = 5
if event.key == pygame.K_6:
value = 6
if event.key == pygame.K_7:
value = 7
if event.key == pygame.K_8:
value = 8
if event.key == pygame.K_9:
value = 9
if event.key == pygame.K_RETURN:
flag2 = 1
if event.key == pygame.K_r:
rs = 0
error = 0
flag2 = 0
defaultgrid=[
[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]
]
if event.key == pygame.K_d:
rs = 0
error = 0
flag2 = 0
defaultgrid =[
[0, 0, 4, 0, 6, 0, 0, 0, 5],
[7, 8, 0, 4, 0, 0, 0, 2, 0],
[0, 0, 2, 6, 0, 1, 0, 7, 8],
[6, 1, 0, 0, 7, 5, 0, 0, 9],
[0, 0, 7, 5, 4, 0, 0, 6, 1],
[0, 0, 1, 7, 5, 0, 9, 3, 0],
[0, 7, 0, 3, 0, 0, 0, 1, 0],
[0, 4, 0, 2, 0, 6, 0, 0, 7],
[0, 2, 0, 0, 0, 7, 4, 0, 0],
]
if flag2 == 1:
if solvegame(defaultgrid , 0, 0) == False:
error = 1
else:
rs = 1
flag2 = 0
if value != 0:
fillvalue(value)
if validvalue(defaultgrid , int(x), int(z), value) == True:
defaultgrid[int(x)][int(z)] = value
flag1 = 0
else:
defaultgrid[int(x)][int(z)] = 0
raiseerror1()
value = 0
if error == 1:
raiseerror()
if rs == 1:
gameresult()
drawlines()
if flag1 == 1:
highlightbox()
pygame.display.update()
pygame.quit()
Manual Testing of task 2:
The rules for sudoku are simple. A 9×9
square must be filled in with numbers
from 1-9 with no repeated numbers in
each line, horizontally or vertically. To
challenge you more, there are 3×3
squares marked out in the grid, and
each of these squares can't have any
repeat numbers either.
Test passed
So when entered 3 the game will accept
it otherwise the cell remain blank
Test passed
By checking no number should repeat
in any similar row or any similar column
or even in 3*3 matrix the game will be
completed
Test passed
Automated testing for Task 2:
Code:
import pygame
import time
def simulate_keypress(key):
event = pygame.event.Event(pygame.KEYDOWN, key=key)
pygame.event.post(event)
def test_game():
pygame.font.init()
Window = pygame.display.set_mode((500, 500))
pygame.display.set_caption("SUDOKU GAME by DataFlair")
clock = pygame.time.Clock()
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
clock.tick(30)
pygame.quit()
test_game()
Task 3:
Version Control: A system that records changes to a file or set of files over
time so that specific versions can be recalled later. It allows multiple users to
work on a project simultaneously.
Terms Defined:
Clone: Creating a local copy of a repository from a remote server.
Commit: Recording changes to the repository.
Pull: Fetching and integrating changes from a remote repository to a local
branch.
Push: Sending local branch updates to the remote repository.
Merger: Integrating changes from different branches.
Branch: A divergent version of the repository, used to isolate development
work without affecting other branches.
Pull Request: A request to merge a branch into the main branch, typically
reviewed by other developers before merging.
Benefits of VCS:
Tracking Changes: Keeping a history of who changed what and when, which
can be crucial for understanding why changes were made.
Facilitating Collaboration: Multiple developers can work on different
features simultaneously without interference.
Risk Mitigation: Changes can be tested in branches and reviewed via pull
requests before integration, reducing the risk of disrupting the main project.
Versus Cloud Storage: Unlike cloud-based storage like Google Drive, VCS
provides tools specifically designed for software development, such as branch
management, code merging, and conflict resolution, which are essential for
maintaining a complex codebase with multiple contributors.
Exploring Commit and Issue/Pull Request
Trackers
Reflect on how using Git could have improved your past development efforts. For
example, version control could have provided better backup options, facilitated
easier code reviews, and enabled smoother collaboration among team members.