Coder Social home page Coder Social logo

tetris-engine's Introduction

Tetris Engine

C# Tetris Engine written to be pluggable for both manually play and automatic AI play.
Supporting 3rd party AI algorithms by using the AI engine's IAlgorithm interface.

Build Status

Usage

Manual play

var gameManager = new GameManager(20, 10);

// Game loop
while (!gameManager.GameState.IsGameOver())
{
    gameManager.OnGameLoopStep();

    // Simple user input.
    if (keyboard.Input == Left) {
        gameManager.MoveBlock(Move.Left);
    }
    ...
}

AI Agent

See example console runner at ./Runners/Tetris.Engine.AIConsoleRunner

var gameManager = new GameManager(20, 10);
var ai = new Engine(
    new TetrisAi(
        new TetrisAiWeights
            {
                ColumnTransitions = 0.8024363520000000f,
                LandingHeight = -0.1958289440000000f,
                NumberOfHoles = 5.0289489999999999f,
                RowTransitions = -0.4794300500000000f,
                RowsCleared = -2.0772042300000000f,
                WellSums = 0.4410647000000000f
            }));

IEnumerator moveIterator = null;
var blockNumber = -1;

// Game loop
while (!gameManager.GameState.IsGameOver())
{
    // Print state and sleep wait run loop logic.
    PrintState(gameManager);
    Thread.Sleep(50);

    gameManager.OnGameLoopStep();

    if (moveIterator != null && moveIterator.MoveNext())
    {
        gameManager.MoveBlock((Move)moveIterator.Current);
        continue;
    }

    // Make sure we only calculate best move once per spawned block.
    if (blockNumber != gameManager.GameStats.BlocksSpawned)
    {
        var steps = ai.GetNextMove(gameManager.BoardManager);
        moveIterator = steps.Moves.GetEnumerator();
        blockNumber = gameManager.GameStats.BlocksSpawned;
    }
}

Unity3d

A demo project for usage of the engine in unity with 2d graphics and game stats.

tetris-engine's People

Contributors

cbpetersen avatar

Watchers

James Cloos 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.