You are playing a simple game with a stack of tiles numbered 1 through 3 and a queue containing a pool of additional tiles. Each turn you look at the tile on the top of the stack and perform the appropriate operation: • If the tile is a 1, you remove that tile plus one more from the top of the stack. • If the tile is a 2, you remove that tile plus two more from the top of the stack. • If the tile is a 3, you remove that tile but take the three next tiles from the queue and add them to the top of the stack. When the player's stack has become empty, the game is over. If this happens in the middle of a turn (for example, if the player is supposed to remove three tiles from the stack but there are only two tiles left), that still counts as a turn. You can assume that the queue will always contain enough tiles to reach the end of the game. Write a class called TileGame that contains a method that takes the stack and queue of integers and returns the number of turns it takes for the player's stack of tiles to become empty. Your method must have the following signature: public static int tileGame (ArrayDeque stack, Queue q)

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

stacks and queues.

program must be able to handle all test cases without causing an exception

Note that this problem does not require recursion to solve (though you can use that if you wish).

Directions:
You are playing a simple game with a stack of tiles numbered 1 through 3 and a queue containing a pool
of additional tiles. Each turn you look at the tile on the top of the stack and perform the appropriate
operation:
• If the tile is a 1, you remove that tile plus one more from the top of the stack.
• If the tile is a 2, you remove that tile plus two more from the top of the stack.
• If the tile is a 3, you remove that tile but take the three next tiles from the queue and add them
to the top of the stack.
When the player's stack has become empty, the game is over. If this happens in the middle of a turn (for
example, if the player is supposed to remove three tiles from the stack but there are only two tiles left),
that still counts as a turn. You can assume that the queue will always contain enough tiles to reach the
end of the game.
Write a class called TileGame that contains a method that takes the stack and queue of integers and
returns the number of turns it takes for the player's stack of tiles to become empty. Your method must
have the following signature:
public static int tileGame (ArrayDeque<Integer> stack, Queue<Integer> q)
Transcribed Image Text:Directions: You are playing a simple game with a stack of tiles numbered 1 through 3 and a queue containing a pool of additional tiles. Each turn you look at the tile on the top of the stack and perform the appropriate operation: • If the tile is a 1, you remove that tile plus one more from the top of the stack. • If the tile is a 2, you remove that tile plus two more from the top of the stack. • If the tile is a 3, you remove that tile but take the three next tiles from the queue and add them to the top of the stack. When the player's stack has become empty, the game is over. If this happens in the middle of a turn (for example, if the player is supposed to remove three tiles from the stack but there are only two tiles left), that still counts as a turn. You can assume that the queue will always contain enough tiles to reach the end of the game. Write a class called TileGame that contains a method that takes the stack and queue of integers and returns the number of turns it takes for the player's stack of tiles to become empty. Your method must have the following signature: public static int tileGame (ArrayDeque<Integer> stack, Queue<Integer> q)
Example output:
If the queue contains the items 1, 2, 2, 1, 3, 1, 2, 1, 2, 3
and the stack contains (from top to bottom) the items 3, 2, 1, 2
then the return value of your method should be 3 because
Turn 1: discard the 3 and add 1, 2, 2 so the stack is now 2, 2, 1, 2, 1, 2
Turn 2: discard the 2 as well as the following 2, 1 so the stack is now 2, 1, 2
Turn 3: discard the 2 and the following 1, 2, so the stack is now empty
Transcribed Image Text:Example output: If the queue contains the items 1, 2, 2, 1, 3, 1, 2, 1, 2, 3 and the stack contains (from top to bottom) the items 3, 2, 1, 2 then the return value of your method should be 3 because Turn 1: discard the 3 and add 1, 2, 2 so the stack is now 2, 2, 1, 2, 1, 2 Turn 2: discard the 2 as well as the following 2, 1 so the stack is now 2, 1, 2 Turn 3: discard the 2 and the following 1, 2, so the stack is now empty
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 4 steps with 2 images

Blurred answer
Knowledge Booster
Stack
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
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