0% found this document useful (0 votes)
3 views19 pages

All Tasks File

The document contains code for two games: a Number Guessing Game and a Sudoku Game, both implemented in Python. The Number Guessing Game allows players to guess a randomly generated number either against another player or an AI, while the Sudoku Game involves filling a 9x9 grid with numbers following specific rules. Additionally, the document includes manual and automated testing procedures for the Sudoku Game, as well as a section discussing the benefits of providing programming documentation for contributors.

Uploaded by

tali66261
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views19 pages

All Tasks File

The document contains code for two games: a Number Guessing Game and a Sudoku Game, both implemented in Python. The Number Guessing Game allows players to guess a randomly generated number either against another player or an AI, while the Sudoku Game involves filling a 9x9 grid with numbers following specific rules. Additionally, the document includes manual and automated testing procedures for the Sudoku Game, as well as a section discussing the benefits of providing programming documentation for contributors.

Uploaded by

tali66261
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 19

Task 1:

import random

def generate_number(low, high):


return random.randint(low, high)

def ai_guess(low, high, last_guess, last_result):


if last_result == "lower":
return random.randint(last_guess + 1, high)
elif last_result == "higher":
return random.randint(low, last_guess - 1)
else:
return random.randint(low, high)

def main():
while True:
low = 1
high = 100
number = generate_number(low, high)

print("Welcome to Number Guessing Game!")


mode = input("Choose mode: 'm' for multiplayer, 'a' for against AI: ").lower()
while mode not in ['m', 'a']:
print("Invalid mode. Please choose again.")
mode = input("Choose mode: 'm' for multiplayer, 'a' for against AI: ").lower()

if mode == 'm':
print("You've chosen multiplayer mode!")
else:
print("You've chosen to play against AI!")

print(f"I'm thinking of a number between {low} and {high}.")

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

else: # Against AI mode


attempts = 7
while attempts > 0:
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("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)

ai_guess_num = ai_guess(low, high, guess, result)


print(f"The AI guesses: {ai_guess_num}")
if ai_guess_num == number:
print("The AI guessed the number!")
break
elif ai_guess_num < number:
print("AI's guess was too low.")
else:
print("AI's guess was too high.")

attempts -= 1
print("Attempts left:", attempts)

play_again = input("Do you want to play again? (yes/no): ").lower()


if play_again != 'yes':
print("Thanks for playing! Goodbye!")
break

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 validvalue(m, k, l, value):


for it in range(9):
if m[k][it] == value:
return False
if m[it][l] == value:
return False
it = k//3
jt = l//3
for k in range(it * 3, it * 3 + 3):
for l in range (jt * 3, jt * 3 + 3):
if m[k][l] == value:
return False
return True

def solvegame(defaultgrid, i, j):


while defaultgrid[i][j] != 0:
if i < 8:
i += 1
elif i == 8 and j < 8:
i=0
j += 1
elif i == 8 and j == 8:
return True
pygame.event.pump()
for it in range(1, 10):
if validvalue(defaultgrid, i, j, it) == True:
defaultgrid[i][j] = it
global x, z
x=i
z=j
Window.fill((255, 255, 255))
drawlines()
highlightbox()
pygame.display.update()
pygame.time.delay(20)
if solvegame(defaultgrid, i, j) == 1:
return True
else:
defaultgrid[i][j] = 0
Window.fill((0, 0, 0))
drawlines()
highlightbox()
pygame.display.update()
pygame.time.delay(50)
return False

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_click(x, y):


event = pygame.event.Event(pygame.MOUSEBUTTONDOWN, pos=(x, y))
pygame.event.post(event)

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

# Test Case 1: Game Initialization


print("Test Case 1: Game Initialization - Passed")

# Test Case 2: Mouse Click to Select a Cell


simulate_click(50, 50) # Click on the first cell
print("Test Case 2: Mouse Click to Select a Cell - Passed")

# Test Case 3: Keyboard Input to Fill a Cell


simulate_keypress(pygame.K_5) # Press 5
time.sleep(0.5)
print("Test Case 3: Keyboard Input to Fill a Cell - Passed")

# Test Case 4: Solving the Sudoku Puzzle


simulate_keypress(pygame.K_RETURN) # Press Enter to solve
time.sleep(0.5)
print("Test Case 4: Solving the Sudoku Puzzle - Passed")

# Test Case 5: Resetting the Puzzle


simulate_keypress(pygame.K_r) # Press R to reset
time.sleep(0.5)
print("Test Case 5: Resetting the Puzzle - Passed")

# Test Case 6: Restoring the Default Puzzle


simulate_keypress(pygame.K_d) # Press D to restore default
time.sleep(0.5)
print("Test Case 6: Restoring the Default Puzzle - Passed")

# Test Case 7: Invalid Key Press Handling


simulate_keypress(pygame.K_a) # Press an invalid key
time.sleep(0.5)
print("Test Case 7: Invalid Key Press Handling - Passed")

# Test Case 8: Quitting the Game


pygame.event.post(pygame.event.Event(pygame.QUIT))
print("Test Case 8: Quitting the Game - Passed")

clock.tick(30)
pygame.quit()

test_game()

Task 3:

T4 Tasks (General Questions across T4-3 to


T4-7)
 Benefits of Providing Programming
Documentation for Contributors
 Enhanced Onboarding: Comprehensive documentation helps new
contributors get up to speed quickly, reducing the learning curve associated
with understanding the project’s codebase.
 Improved Code Quality: Documentation that includes coding standards,
architecture descriptions, and API usage can guide contributors to maintain
consistency and quality in the code they write.
 Efficient Collaboration: Good documentation serves as a reference point
that teams can rely on, reducing the need for repeated clarification and
communication among team members, thus speeding up the development
process.

 Understanding and Value of Version Control


Systems (VCS) like Git

 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

 Commit History Examination: Look at the repository’s commit log to


identify significant changes, who made them, and the discussion around
these changes.
 Issue Tracker Analysis: Explore both open and closed issues to understand
ongoing challenges and how they have been addressed.
 Pull Request Review: Examine both open and closed pull requests to see
what changes have been proposed and integrated into the project.
 Application of Git in Previous Development
Tasks

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.

You might also like