Git Product home page Git Product logo

chess_game's Introduction

#Chess Battles

Link to live App

Main Functionality

Play a game of Chess with a twist! There are different classes in the game, where each class has it's own unique moves. We keep track of how well you're doing by recording your wins and losses.

Game Demo

User actions

  • Signup
  • Login
  • Search for a game against another player

Classes

###Conqueror - King

  • Has one extra move vertically, and horizontally
  • The extra move is considered a hop, and lets the King jump over pieces like the Knight Conqueror

###Knight - Knight

  • The knight can now move in the tile between it's regular moveset Knight

Pieces

Each piece is an object, where its valid moves are saved to an array that corresponds to the engine array.

export class Knight extends Piece {
    constructor(team, type) {
        super(team, type)
        this.team === 1 ? this.name = 'whiteKnight' : this.name = 'blackKnight'
    }
    findValidMoves() {
        if(this.type) {
            let boundsIndex = convertBounds(board[this.position].rankFile)
            let possibleMoves = [boundsIndex+21, boundsIndex+12, boundsIndex-8, boundsIndex-19, boundsIndex-21, boundsIndex-12, boundsIndex+8, boundsIndex+19, boundsIndex+20, boundsIndex-20, boundsIndex-2, boundsIndex+2 ]
            //Remove out of bounds
            possibleMoves = possibleMoves.map(int => {if(!isOffBoard(int)) return convertBoard(int)}).filter(int => int !== undefined)
            //Check for teamate/enemy
            possibleMoves = possibleMoves.map(int => {
                if(!board[int].piece) return int
                else if(!this.isSameTeam(board[int].piece.team)) return int
            }).filter(int => int !== undefined)

            return possibleMoves
        }else {
            let boundsIndex = convertBounds(board[this.position].rankFile)
            let possibleMoves = [boundsIndex+21, boundsIndex+12, boundsIndex-8, boundsIndex-19, boundsIndex-21, boundsIndex-12, boundsIndex+8, boundsIndex+19]
            //Remove out of bounds
            possibleMoves = possibleMoves.map(int => {if(!isOffBoard(int)) return convertBoard(int)}).filter(int => int !== undefined)
            //Check for teamate/enemy
            possibleMoves = possibleMoves.map(int => {
                if(!board[int].piece) return int
                else if(!this.isSameTeam(board[int].piece.team)) return int
            }).filter(int => int !== undefined)

            return possibleMoves
        }
    }
}

Chess Engine

The engine uses two arrays to position the pieces on the board and to detect moves considered out of bounds.

Board = array of length 64 (8x8) OutOfBounds = array of length 120 (12x10)

Board representation

We convert between the two arrays

//converts 64 index based array to 120 index based array
export function convertBounds(rankFile) {
    //rankFile is a string ex: '0,6' => board[6]
    //convert rankFile to an array of integers ex: '1,8' => [1,8]
    let parsed = rankFile.split(',').map(num => parseInt(num))
    let rank = parsed[0]
    let file = parsed[1]
    let boundsIndex = (21 + file) + (rank * 10)
    //return the index of 120 array
    return boundsIndex
}
//converts index from 120 array to 64 array
export function convertBoard(index){
    //index === boundsBoard[index]
    let val
    //convert depending on index inside boundsBoard
    if(index<31) {
        val = index-21
    }else if(index<41) {
        val = index-21-2
    }else if(index<51) {
        val = index-21-4
    }else if(index<61) {
        val = index-21-6
    }else if(index<71) {
        val = index-21-8
    }else if(index<81) {
        val = index-21-10
    }else if(index<91) {
        val = index-21-12
    }else if(index<101) {
        val  = index-21-14
    }
    //return the board index if not out of bounds
    if(!isOffBoard(index)) return val
    //return null if out of bounds
    else return null
}

Each tile is represented as so:

    //push 64 empty the tiles into the board
    for(let i = 0; i < 8; i++) {
        for(let j = 0; j < 8; j++) {
            let tile = {
                rankFile: `${i},${j}`,
                piece: null
            }
            board.push(tile)
        }
    }

chess_game's People

Contributors

asetre avatar

Watchers

 avatar  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.