Project 3 - Play Tetris

profileNick2020
Tetris_Details.pdf

CS 5044 Object-Oriented Programming with Java

Q&A Session

Midterm Exam • Exam is split into three parts, all due Monday (7/3) at noon ET

– Each part has a 30 minute time limit for 20 multiple-choice questions – Each part covers a cross-section of all materials from Module 1 through Module 5 – See also Piazza @166 (and posts linked from there) for a few more details

Project 3 - Play Tetris • Tetris!

– We've been tasked to develop an artificial intelligence (AI) that can play Tetris • An "interface" has been provided; you will implement the interface

– We'll learn more about interfaces in several weeks, but for now: • An interface is just a way in Java to specify required methods, without any code • To implement the interface, we develop a class with actual code in these methods

– Eclipse can generate the placeholder methods for us automatically • Once that's done, it becomes just like our previous projects • Plus, the compiler will ensure we don't accidentally change the method headers

Meet the Cast of Players • enum Shape

– One value for each of the seven geometric pieces involved:

– Each Shape provides a set of distinct Rotation values (default orientations shown above) – You can also request the width of the shape (in blocks) after applying any valid Rotation

• enum Rotation – One value for each 90° rotation:

• NONE • CCW_90 • CCW_180 • CCW_270

Game On • class Placement

– A Placement just holds a Rotation value and a column number together – This is what our AI needs to return to the game engine, for any Shape:

• First the Rotation will be applied to the Shape • Then the rotated Shape will be moved horizontally

– The left-most block will be placed in the specified column – Invalid Placement objects will be rejected (and ignored) by the game engine

• class Board – Represents the state of the playing board at any given time – Provides static int values: WIDTH, HEIGHT, and HEIGHT_LIMIT – Contains a collection of fixed blocks; allows us to query via isBlockAt(col, row) – We can ask the board to show us the hypothetical result of placing another piece

• The piece will be placed and dropped, then any full rows will be cleared • Creates a new Board object; does not mutate the existing Board

– We can also construct new Board objects with arbitrary blocks for testing • ShapeStream and RandomMode are only used for the optional challenge • Tetris5044 is for internal use only, by the game engine itself

Mind Games • interface AI

– This is what we need to implement! – public Placement findBestPlacement(Board currentBoard, Shape shape)

• This is how the game engine asks our AI what to do – The remaining methods need to compute "cost" factors for a given board:

• int getColumnHeightVariability(Board board) • int getMaximumBlockHeight(Board board) • int getTotalBlockCount(Board board) • int getTotalGapCount(Board board) • Academic Note: Normally these would be private, and not required by the interface

– The individual cost factor methods should be developed (and tested!) first – Once you've completed at least one cost factor method, to find the best placement:

• For every possible placement of this shape – Get the board that would result from this placement – Calculate cost factors for result board and combine with weights – If this is the lowest overall cost so far, consider this placement as the new best

• Return the best placement to the game engine – Adjust the weights to meet minimum average performance requirements in TEST modes