Giter Club home page Giter Club logo

bulls-and-cows's Introduction

๐Ÿ‚๐Ÿ‚๐Ÿ‚ Bulls and Cows ๐Ÿ„๐Ÿ„๐Ÿ„

A ruby implementation of the bulls and cows game, 4-letter word version.

๐Ÿฎ How to play

  1. get the code
git clone [email protected]:ramses-lopez/bulls-and-cows.git
  1. Make sure you have ruby 2.0+ installed. If not, there are many ways to do it

  2. execute the file

ruby bull.rb

๐Ÿฎ Play Modes

  1. Auto Guess mode: The user must enter a four letter word; the algorithm will try and guess that word, using only the feedback from the Chooser.
    The Chooser evaluates every guess by checking the number of letters in correct places (bulls) and correct letters but in the wrong place (cows).

The guessing strategy used in the CLI is replacement; details of the implementation are described below.

  1. Manual mode: This mode allows the user to try to guess a 4-letter word. After every guess from the player, the computer will return the number of cows and bulls present in the current guess.

The game ends when the player successfully guess the word chosen by the computer.

๐Ÿฎ Guessing strategies

๐Ÿฎ Replacement

  1. Given a user-entered word, the algorithm will try to discard all letters not present in it by testing it against a pre-built list of 'words':
  • ETAO
  • INSH
  • RDLC
  • UMWF
  • GYPB
  • VKJX
  • QZJX
  1. This list contains all the letters in the english alphabet, ordered by its frecuency in the english language, from the most frequent at top and the least frequent at the end of the list, and no repeating letters (except for the last word)
  2. When checking the initial guesses, a letter from a guess with score zero is extracted for later use. It is a letter that is absolutely not included on the chosen word.
  3. Each letter of each candidate word is replaced with the unused letter, one at a time. Each new word gets a score and a cow & bull count.
  • If the score lowers, then the replaced letter is part of the chosen word.
  • If the score remains unchanged, the letter is replaced.
  1. As a result of the previous step, now we have a list of words, which contains the letters for the chosen word, with the unused letters removed.
  2. The unused letter extracted in step 3 is removed.
  3. All the permutations of the remaining 4 letters are tested against the chosen word until four bulls are found.

๐Ÿฎ Brute force

  1. Given a user-entered word, the algorithm will try to discard all letters not present in it by testing it against a pre-built list of 'words':
  • ETAO
  • INSH
  • RDLC
  • UMWF
  • GYPB
  • VKJX
  • QZJX
  1. This list contains all the letters in the english alphabet, ordered by its frecuency in the english language, from the most frequent at top and the least frequent at the end of the list, and no repeating letters (except for the last word)
  2. The initial guesses probably won't include the chosen word, but it will yield 4 words with one cow each, in the worst case.
  3. With these 16 letters at most, the algorithm will check the permutations against the chooser, using a scoring system to compare guesses.
  • Each bull in the word adds 5 points to the score, for a maximum of 20
  • Each cow in the word adds 1 point to the score, for a maximum of 4
  1. The algorithm ends when it finds a guess with a score of 20, or runs out of guesses.

๐Ÿฎ Tests

The code comes with a set of tests. Install bundler then run bundle install to install the required dependences.

Then, you can run rspec to run all the tests present in the spec folder.

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.