GithubHelp home page GithubHelp logo

dingpingzhang / game2048 Goto Github PK

View Code? Open in Web Editor NEW
1.0 2.0 0.0 45 KB

A simple 2048 game implementation that supports the generation of the matrix of any order.

License: GNU General Public License v3.0

C# 100.00%

game2048's Introduction

Game2048

1. Game2048Matrix API

var matrix = new Game2048Matrix(matrixOrder: 6); // Creates an instance of the Game2048Matrix with the specified matrix order.

matrix.MoveTo(MoveOrientation.Left); // Slides all cells to the left.

matrix.Moved += (sender, e) => { }; // Subscribes the Moved event.
matrix.Merged += (sender, e) => { }; // Subscribes the Merged event.

var cellValue = matrix[1, 3]; // Gets the value on (1, 3).

2. Build and Play

  • Open the .sln file with VS2017 (or later). Rebuild the Game2048.ConsoleApp project and run it;
  • You will see a console program launched, just like the screenshot below;
  • You can control these cells by using the arrow keys (, , and ), or start over by pressing the space bar;

console-ui

  • You can open the output dialog on Visual Studio, and view the action event record (Moved or Merged), just like below.

operation-events-log

3. Core Code

private void MoveAndMergeArray(Func<int, int> getter, Action<int, int> setter, Action<(int from, int to), int?> reporter)
{
    for (int p = 0, i = 1; i < MatrixOrder; i++)
    {
        int next = getter(i);
        if (next == 0) continue;

        int current = getter(p);
        if (current == next)
        {
            setter(p++, next * 2);
            setter(i, 0);
            reporter?.Invoke((i, p - 1), next * 2);
        }
        else
        {
            int toIndex = current == 0 ? p : ++p;
            if (toIndex != i)
            {
                setter(toIndex, next);
                setter(i, 0);
                reporter?.Invoke((i, toIndex), null);
            }
        }
    }
}

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.