Giter Club home page Giter Club logo

chess's Introduction

Chess

This fully interactive, pure-ruby chess game runs in the terminal. Color cues in the game not only illuminate the board, but also show players where they can move and what pieces they can take. The game takes advantage of modules, inheritance, and manipulation of user input for a great chess experience.

To play the game, clone this repo, navigate to the project folder, and enter ruby game.rb

Demo

gameplay-gif

Features

Deep Board Duplication

In order to check whether moves would put a player in check, I made a deep duplication of the board, performed the move, and checked the result.

  def valid_moves(color)
    return [] if self.color != color
    valid_moves = []

      self.possible_moves.each do |move|
        new_board = board.dup
        new_board.make_move!(position, move)
        valid_moves << move unless new_board.in_check?(color)
      end
    valid_moves
  end
  def dup
    duped = Board.new(true)
    grid.each_with_index do |row, row_idx|
      row.each_with_index do |square, col_idx|
        if square.class == EmptyPiece
          duped.grid[row_idx][col_idx] = EmptyPiece.new
        else
          duped.grid[row_idx][col_idx] = square.dup_with_board(duped)
        end
      end
    end
    duped
  end

Slideable Piece Inheritance

Object-Oriented Programming--all pieces inherit from a Piece class, and the Bishop, Rook, and Queen all inherit from the Slideable module for their multi-tile movement.

Cursorable

Navigate through the game with simple arrow key commands.

  def get_input
    key = KEYMAP[read_char]
    handle_key(key)
  end

  def handle_key(key)
    case key
    when :ctrl_c
      exit 0
    when :return, :space
      @cursor_pos
    when :left, :right, :up, :down
      update_pos(MOVES[key])
      nil
    else
      puts key
    end
  end

  def read_char
    STDIN.echo = false
    STDIN.raw!

    input = STDIN.getc.chr
    if input == "\e" then
      input << STDIN.read_nonblock(3) rescue nil
      input << STDIN.read_nonblock(2) rescue nil
    end
  ensure
    STDIN.echo = true
    STDIN.cooked!
    return input
  end

chess's People

Contributors

noahwiener avatar

Stargazers

Josep Servat avatar Yangchen Shen avatar Nathan Specht avatar anotherminh avatar nicole devillers 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.