0% found this document useful (0 votes)
9 views2 pages

Code

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

Code

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

// Start of the game EndFunction

// Randomly place 'X' in a cell (not in Grid(1,1))

Function InitializeGame() // Function to move the player

Set secretRow = random number between 1 and 5 Function MovePlayer(direction)


// Secret row where 'X' is located
If direction = "up" AND playerRow > 1
Set secretCol = random number between 1 and 5 //
playerRow = playerRow - 1
Secret column where 'X' is located
ElseIf direction = "down" AND playerRow < 5
While secretRow = 1 AND secretCol = 1 //
Ensure the secret cell is not (1, 1) playerRow = playerRow + 1

Set secretRow = random number between 1 and ElseIf direction = "left" AND playerCol > 1
5 playerCol = playerCol - 1
Set secretCol = random number between 1 and 5 ElseIf direction = "right" AND playerCol < 5
EndWhile playerCol = playerCol + 1
Set playerRow = 1 // Player starts at top-left Else
corner (1, 1)
Print "Invalid move! Try again."
Set playerCol = 1
EndIf
Set moves = 0 // Initialize move counter
moves = moves + 1 // Increment move counter
Print "Welcome to the game! You have 10 moves to
EndFunction
find the secret cell marked with 'X'."
While moves < 10

// Function to check if the player found the 'X' Print "You are currently at (", playerRow, ", ",
playerCol, ")."
Function CheckWin()
Print "Enter move direction (up, down, left, right):"
If playerRow = secretRow AND playerCol =
secretCol Input direction

Print "You Win! You found the secret cell in", MovePlayer(direction) // Move the player based on
moves, "moves." input

Return True If CheckWin() = True // Check if the game is over

ElseIf moves >= 10 Exit While

Print "You Lose! You used all 10 moves without EndIf


finding the 'X'."
EndWhile
Return True

EndIf
If moves = 10 AND playerRow != secretRow AND
Return False playerCol != secretCol

EndFunction Print "You Lose! The secret cell was at (",


secretRow, ", ", secretCol, ")."

EndIf
// Main game loop

InitializeGame() // Setup the game

You might also like