Giter VIP home page Giter VIP logo

2048's Introduction

2048

Instructions

Coursera: https://www.coursera.org/learn/kotlin-for-java-developers/supplement/6HrU1/setting-up-programming-assignments

You solve the assignments using JetBrains EduTools plugin for IntelliJ IDEA or Android Studio. Read the tutorial (below) on how to install the plugin, start the assignment and submit your solution to Coursera.

Jetbrains tutorial: https://www.jetbrains.com/help/education/coursera-programming-assignments.html
Edu-tools: https://plugins.jetbrains.com/plugin/10081-edutools

Board

Your task is to implement interfaces SquareBoard and GameBoard.

SquareBoard

SquareBoard stores the information about the square board and all the cells on it. It allows to get a cell by its indexes, get parts of columns and rows on a board, or get a specified neighbour of a cell.

Note that the numbering of cells starts with 1, not with 0. A board of a width 2 consists of the following cells:

(1, 1) (1, 2) (2, 1) (2, 2) For the following examples, we'll use this board of a width 2:

val board = createSquareBoard(2) If you call board.getCellOrNull(3, 3) for such board, you'll get null as the result, because the board doesn't have a cell with such coordinates. The function Board.getCell should throw IllegalArgumentException for incorrect values of i and j.

You can write board.getRow(1, 1..2) or board.getRow(1, 2 downTo 1), and you'll get the lists of cells [(1, 1), (1, 2)] and [(1, 2), (1, 1)] accordingly. Note how using the range 2 downTo 1 returns a row in a reversed order. You can use any range to get a part of a column or a row.

Note that getRow and getColumn should return a list containing only the cells that belong to the board if the range is larger than the board limits and ignore other indexes, thus, board.getRow(1, 1..10) should return [(1, 1), (1, 2)].

The neighbours of a cell (1, 1) depending on the direction should be:

Direction.UP - null
Direction.LEFT - null
Direction.DOWN - (2, 1) Direction.RIGHT - (1, 2) Create only width * width cells; all the functions working with cells should return existing cells instead of creating new ones.

GameBoard

GameBoard lets to store the values in board cells, update them, and ask the general information about the stored values (like any, all etc.) Note that GameBoard extends SquareBoard.

See TestSquareBoard and TestGameBoard for examples.

Game

Your task is to implement two games: Game 2048 and Game of Fifteen. Use your implementation of the GameBoard interface from the previous task.

After implementing the game you can play it yourself running main function in ui/PlayGame2048 or ui/PlayGameOfFifteen.

Game 2048

First, complete the tasks in Game2048Helper.kt (implementing the function moveAndMergeEqual declared in Game2048Helper.kt) and in Game2048Initializer.kt (generating new values randomly). Then, implement the utility functions declared in Game2048.kt. The tests which you can run to check each function are specified in the comments next to the function.

Game of Fifteen

Game of Fifteen is solvable only if the initial permutation of numbers is even. Implement first the function isEven (declared in GameOfFifteenHelper.kt) checking whether a permutation is even or odd, and then use this function to produce only solvable permutations in GameOfFifteenIntiializer.kt.

You can use the following algorithm to check the given permutation. Let P is a permutation function on a range of numbers 1..n. For a pair (i, j) of elements such that i < j , if P(i) > P(j), then the permutation is said to invert the order of (i, j). The number of such inverted pairs is the parity of the permutation. If permutation inverts even number of such pairs it is an even permutation else it is an odd permutation.

2048's People

Contributors

oshai avatar

Watchers

James Cloos avatar Yaakov Shahak avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.