Python: 2048 games   Board = list[list[str]] # Checks whether a given board has any # possible move left. If no more moves, # return True. Otherwise return False. def isGameOver(board: Board) -> bool: return False # Returns a tuple (changed, new_board) # where: # changed - a boolean indicating if # the board has changed. # new_board - the board after the user # presses the 'Up' key. def doKeyUp(board: Board) -> tuple[bool, Board]: return False, board # Returns a tuple (changed, new_board) # where: # changed - a boolean indicating if # the board has changed. # new_board - the board after the user # presses the 'Down' key. def doKeyDown(board: Board) -> tuple[bool, Board]: return False, board # Returns a tuple (changed, new_board) # where: # changed - a boolean indicating if # the board has changed. # new_board - the board after the user # presses the 'Left' key. def doKeyLeft(board: Board) -> tuple[bool, Board]: return False, board # Returns a tuple (changed, new_board) # where: # changed - a boolean indicating if # the board has changed. # new_board - the board after the user # presses the 'Right' key. def doKeyRight(board: Board) -> tuple[bool, Board]: return False, board # Returns a list of tuples (row, col) # indicating where the empty spots are def emptyPos(board: Board) -> list[t

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

Python: 2048 games

 

Board = list[list[str]]

# Checks whether a given board has any
# possible move left. If no more moves,
# return True. Otherwise return False.


def isGameOver(board: Board) -> bool:
return False

# Returns a tuple (changed, new_board)
# where:
# changed - a boolean indicating if
# the board has changed.
# new_board - the board after the user
# presses the 'Up' key.


def doKeyUp(board: Board) -> tuple[bool, Board]:
return False, board

# Returns a tuple (changed, new_board)
# where:
# changed - a boolean indicating if
# the board has changed.
# new_board - the board after the user
# presses the 'Down' key.


def doKeyDown(board: Board) -> tuple[bool, Board]:
return False, board

# Returns a tuple (changed, new_board)
# where:
# changed - a boolean indicating if
# the board has changed.
# new_board - the board after the user
# presses the 'Left' key.


def doKeyLeft(board: Board) -> tuple[bool, Board]:
return False, board


# Returns a tuple (changed, new_board)
# where:
# changed - a boolean indicating if
# the board has changed.
# new_board - the board after the user
# presses the 'Right' key.
def doKeyRight(board: Board) -> tuple[bool, Board]:
return False, board


# Returns a list of tuples (row, col)
# indicating where the empty spots are
def emptyPos(board: Board) -> list[tuple[int, int]]:
return []

Board Representation: The board (aka. grid) in this game is represented as a list of lists (2d list).
Each tile on the board is represented as string, where
it is a string that is the value of the tile (e.g., '128'). As an example, the figure below show a 2-d list
encoding and its corresponding board:
(a single space) is an empty tile; otherwise,
2 88
boardA
%3D
' 8', '8'],
' 4', '8'],
'4', '4'],
'2', '8'],
'16']]
4 | 8
4 4 4
4 428
16
What're You Implementing? There are a number of functions you are going to implement (see also
the stubs in t2048.py). The first family of functions is concerned with movements:
dokeyUp (board: list[list[str]])
doKeyDown (board: list[list[str]]) -> tuple[bool, list[list[str]]]
doKeyLeft (board: list[list[str]]) -> tuple[bool, list[1ist[str]]]
doKeyRight (board: list[list[str]]) -> tuple[bool, list[list[str]]]
-> tuple[bool, list[list[str]]]
These correspond to the directions of movement up, down, left, and right, respectively. For each of
these functions:
Input: a board/grid whose dimension is as specified by the input.
Output: returns a tuple (changed, new_board), where changed is a Boolean that indicates
whether or not the board has changed; and new_board is the board that results after that partic-
ular movement is made.
You are also to implement a few functions that help operate the game:
emptyPos (board: list[list[str]]) -> list[tuple[int, int]] takes as input a board
(represented as described above) and returns a list of (row, col) tuples of empty spots on
this board. For example, for the board [['', '2'],['4',
[(0,0), (1,1)].
']], the function will return
• isGameOver(board: list[list[str]]) -> bool takes as input a board (represented like
before) and returns the game on that given board is over (see the definition above). That is, it
checks whether the given board has any possible move remaining.
Grading: We allocate 50 points for this problem. For each function we ask you to implement, we'll
test it thoroughly using a script and grade it manually for style and clarity.
Transcribed Image Text:Board Representation: The board (aka. grid) in this game is represented as a list of lists (2d list). Each tile on the board is represented as string, where it is a string that is the value of the tile (e.g., '128'). As an example, the figure below show a 2-d list encoding and its corresponding board: (a single space) is an empty tile; otherwise, 2 88 boardA %3D ' 8', '8'], ' 4', '8'], '4', '4'], '2', '8'], '16']] 4 | 8 4 4 4 4 428 16 What're You Implementing? There are a number of functions you are going to implement (see also the stubs in t2048.py). The first family of functions is concerned with movements: dokeyUp (board: list[list[str]]) doKeyDown (board: list[list[str]]) -> tuple[bool, list[list[str]]] doKeyLeft (board: list[list[str]]) -> tuple[bool, list[1ist[str]]] doKeyRight (board: list[list[str]]) -> tuple[bool, list[list[str]]] -> tuple[bool, list[list[str]]] These correspond to the directions of movement up, down, left, and right, respectively. For each of these functions: Input: a board/grid whose dimension is as specified by the input. Output: returns a tuple (changed, new_board), where changed is a Boolean that indicates whether or not the board has changed; and new_board is the board that results after that partic- ular movement is made. You are also to implement a few functions that help operate the game: emptyPos (board: list[list[str]]) -> list[tuple[int, int]] takes as input a board (represented as described above) and returns a list of (row, col) tuples of empty spots on this board. For example, for the board [['', '2'],['4', [(0,0), (1,1)]. ']], the function will return • isGameOver(board: list[list[str]]) -> bool takes as input a board (represented like before) and returns the game on that given board is over (see the definition above). That is, it checks whether the given board has any possible move remaining. Grading: We allocate 50 points for this problem. For each function we ask you to implement, we'll test it thoroughly using a script and grade it manually for style and clarity.
To explain what happens when the player slides the grid, we describe the effects in three steps (for
pedagogical reasons, your code must implement these steps):
STEP 1: Slide in the Direction of Movement. You
push the tiles as far as possible in the direction going right. STEP 1 transforms the grid on the left
of movement until they cannot be moved in that below to the one on the right.
EXAMPLE. Suppose the direction of movement is
further. Similarly, you can imagine
gravity pulling in the direction of movement, so
all the blocks follow the gravitational force before
stopping at the grid's edge. As an example, we
show on the right a grid and what happens after
STEP 1 when the direction of movement is right.
direction
any
2
4
48
2 44 8
2
2
8
2 |28
2 22
2 4
2 222| 4
4 | 2
2 28
4 222| 8
16
16
EXAMPLE. Below, the left grid was processed by
STEP 2: Compact Similar Blocks. For each row or
column in the direction of movement (DOM), you STEP 1; the right grid shows the effects of STEP 2.
will start at one end of the grid and walk in the Notice how the tiles are combined using this pro-
DOM through the grid to the other end. Along this cess, paying attention especially to the rows that
walk, upon encountering two adjacent tiles of the
same value, the latter is merged into the former,
their point values combined. This means there
will no longer be a tile at the latter location. Then,
the walk continues. Keep in mind that more than
one merges can take place per walk.
more than one merges take place.
2|44 8
28
8
22 8
4
8
212 2+2| 4
4
4
4
4|22 | 2 | 8
4 | 4
2|8
16
16
STEP 3: Slide Once More. You may have created EXAMPLE. Applying STEP 3 to the result of STEP 2
gaps in STEP 2. Identical to STEP 1, Step 3 pushes yields the grid on the right below.
the tiles as far as possible in the direction of move-
ment until they cannot be moved in that direction
28
8
2|88
4
8
4 |8
any further.
4|4 4
4
4
4
4 |4
28
4 |4|28
16
16
Special Circumstances: It is possible that a move will not cause any change to the grid. We call this
type of move a void move. Your program will handle this case specially.
The game is considered over when the current grid is full and (ii) all directions of movement result in
void move.
Transcribed Image Text:To explain what happens when the player slides the grid, we describe the effects in three steps (for pedagogical reasons, your code must implement these steps): STEP 1: Slide in the Direction of Movement. You push the tiles as far as possible in the direction going right. STEP 1 transforms the grid on the left of movement until they cannot be moved in that below to the one on the right. EXAMPLE. Suppose the direction of movement is further. Similarly, you can imagine gravity pulling in the direction of movement, so all the blocks follow the gravitational force before stopping at the grid's edge. As an example, we show on the right a grid and what happens after STEP 1 when the direction of movement is right. direction any 2 4 48 2 44 8 2 2 8 2 |28 2 22 2 4 2 222| 4 4 | 2 2 28 4 222| 8 16 16 EXAMPLE. Below, the left grid was processed by STEP 2: Compact Similar Blocks. For each row or column in the direction of movement (DOM), you STEP 1; the right grid shows the effects of STEP 2. will start at one end of the grid and walk in the Notice how the tiles are combined using this pro- DOM through the grid to the other end. Along this cess, paying attention especially to the rows that walk, upon encountering two adjacent tiles of the same value, the latter is merged into the former, their point values combined. This means there will no longer be a tile at the latter location. Then, the walk continues. Keep in mind that more than one merges can take place per walk. more than one merges take place. 2|44 8 28 8 22 8 4 8 212 2+2| 4 4 4 4 4|22 | 2 | 8 4 | 4 2|8 16 16 STEP 3: Slide Once More. You may have created EXAMPLE. Applying STEP 3 to the result of STEP 2 gaps in STEP 2. Identical to STEP 1, Step 3 pushes yields the grid on the right below. the tiles as far as possible in the direction of move- ment until they cannot be moved in that direction 28 8 2|88 4 8 4 |8 any further. 4|4 4 4 4 4 4 |4 28 4 |4|28 16 16 Special Circumstances: It is possible that a move will not cause any change to the grid. We call this type of move a void move. Your program will handle this case specially. The game is considered over when the current grid is full and (ii) all directions of movement result in void move.
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps with 3 images

Blurred answer
Knowledge Booster
Operations of Linked List
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education