Project showcase
Bulls and Cows Game with Arduino

Bulls and Cows Game with Arduino © GPL3+

Simple game which the goal is to find out the 4 digits secret number randomly generated by the arduino.

  • 4,283 views
  • 1 comment
  • 14 respects

Components and supplies

About this project

Also known as Cows and Bulls or Pigs and Bulls or Bulls and Cleots) is an old code-breaking mind or paper and pencil game for two or more players, predating the similar commercially marketed board game Mastermind.

It is a game with numbers that may date back a century or more. It is played by two opponents.

On a sheet of paper, the players each write a 4-digit secret number.

The digits must be all different. Then, in turn, the players try to guess their opponent's number who gives the number of matches. If the matching digits are in their right positions, they are "bulls", if in different positions, they are "cows". Example:

Secret number: 4271Opponent's try: 1234Answer: 1 bull and 2 cows. (The bull is "2", the cows are "4" and "1".)

This time is made for playing against the Arduino, which generates 4 random digits that are not repeated.

The player enters its 4 digits through the keypad and press key D to confirm the digits. The lcd displays the amount of cows (is shown as 'puntos' in the video ) and bulls ( is shown as 'famas' in the video) ( the video and project was made for spanish version) that the player gets. After 5 tries you have not guessed the secret number, you lost and the secret number is shown.

Pressing the key 'A' starts a new game.

Pressing the key 'B' resigns the game and shows the secret number.

Code

Arduino codeArduino
#include <Keypad.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd(7, 8, 9, 10, 11 , 12);

String numero_azar;
char cifra_jugador[4]; //Stores the player's number
String numero_jugador = String();

String a1 = String();
String b1 = String();
String c1 = String();
String d1 = String();
String k = String();
String l = String();
String m = String();
String n = String();

int count=0;
int aleatorio[4];
int i,j,x;
int cows,bulls=0;
int tries=0;
int maximo_tries=5;

const byte ROWS=4;
const byte COLS=4;
char keys[ROWS][COLS] = {
 {'1','2','3','A'},
 {'4','5','6','B'},
 {'7','8','9','C'},
 {'*','0','#','D'}
};
byte rowPins[ROWS] = {13,6,5,4}; //Filas(pines del 9 al 6)
byte colPins[COLS] = {3,2,1,0}; //Columnas (pines del 5 al 2)
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

// Checks there are not duplicate
boolean check_duplicate(String palabra)
{
  for(i=0;i<=3;i++)
  {
      for(j=0;j<=3;j++)
      {
        if(i !=j)
        {
          if(palabra[i]==palabra[j])
          {
            return false;
            break;
          }
        }

      }
  }
        return true;  
}
//**************************************************************************************************
void start()
{
  lcd.clear();
  
  do
  {
    set_random();
    
  } while(check_duplicate(numero_azar)!=true);
  
    lcd.setCursor(0,0);
    lcd.print("Press 4 digits");
    tries=0;
    cows=0;
    bulls=0;
}

//************************************************************************************************
void set_random()
{
  
  for(x=0;x<=3;x++)
  {
    randomSeed(analogRead(analogRead(x)));
    
    aleatorio[x]=random(9);  //genera un número aleatorio entre 0 y 9
  }
   
   numero_azar = String(aleatorio[0])+String(aleatorio[1])+String(aleatorio[2]+String(aleatorio[3])); 
   
}
//************************************************************************************************
void enter()
{
  if(count==4)  //Si tiene 4 dígitos
  {
     k=String(cifra_jugador[0]);
     l=String(cifra_jugador[1]);
     m=String(cifra_jugador[2]);
     n=String(cifra_jugador[3]);
      
    numero_jugador = k+l+m+n;
    
    if(check_duplicate(numero_jugador))  //Si no contiene duplicados llamo al procedimiento que compara los dígitos
    {
      check();
    }
  
  }
  
  else
  
  {
    lcd.setCursor(0,0);
    lcd.print("Cifras incompletas");
    
  }
    
  count=0;  
  
}
//********************************************************************
void check()
 {
   a1=String(aleatorio[0]);
   b1=String(aleatorio[1]);
   c1=String(aleatorio[2]);
   d1=String(aleatorio[3]);
    
    if(a1==k)   {bulls++;}
     
    if(b1==l)   {bulls++;}
    
    if(c1==m)   {bulls++;}
    
    if(d1==n)   {bulls++;}
    
    if(a1==l || a1==m || a1==n) { cows++;}
    if(b1==k || b1==m || b1==n) { cows++;}
    if(c1==k || c1==l || c1==n) { cows++;}
    if(d1==k || d1==l || d1==m) { cows++;}
    
    
    tries++;
    
        
    if(bulls==4)
    {
      
      lcd.clear();
      lcd.print("You won in: ");
      lcd.print(tries);
      lcd.setCursor(0,1);
      lcd.print( "tries");
      delay(2000);
      start();
      
    }
    
    update_display();  //Muestra la información en pantalla
    
    if(tries==maximo_tries)
    {
      lcd.clear();
      lcd.setCursor(0,0);
      lcd.write("You lost, the");
      lcd.setCursor(0,1);
      lcd.print("number was: ");
      lcd.print(a1);
      lcd.print(b1);
      lcd.print(c1);     
      lcd.print(d1);    
    }
    
    count=0;
    cows=0;
    bulls=0;
    
  }

//*******************************************
void update_display()
{
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("Cows:");
    lcd.print(cows);
    
    lcd.setCursor(8,0);
    lcd.print(" Bulls:");
    lcd.print(bulls);
    
    lcd.setCursor(9,1);
    lcd.print("Try:");
    lcd.print(tries);
    lcd.print("/5");
    
    lcd.setCursor(0,1);
    lcd.blink();
}
//*******************************************
void setup() 
{
  lcd.begin(16, 2);
  start();
}

//*******************************************
void loop()
{
char key = keypad.getKey();

  if(key) 
  {
    switch (key)
    {
      case 'A':
      start();  //Starts a new game
      break;
      
      case 'B':
      lcd.clear();
      lcd.setCursor(0,0);
      lcd.print("Bye, then number ");
      lcd.setCursor(0,1);
      lcd.print("was the: ");
      lcd.print(numero_azar);
      break;
      
      case 'D':
      enter();  //Enter
      break;
      
    }
  }

  if(key) 
  {
    cifra_jugador[count] = key;
    
    //Si la tecla presionada es un número (ignore las letras) 
    if(key!='A' && key!='B' && key!='C' && key!='D')
    {
      lcd.setCursor(0+count,1);
      lcd.print(cifra_jugador[count]);
     
      count++;
    }
       
     
  }

}

Schematics

Squematic diagram
Circuito esquematico
Mounting in the protoboard
Esquema bb en

Comments

Similar projects you might like

LCD Game

Project showcase by Team Iron_SalsaStudio

  • 8,476 views
  • 8 comments
  • 15 respects

Arduino LCD Video Game

Project tutorial by xXarduino_11Xx

  • 1,597 views
  • 0 comments
  • 8 respects

8X8 Matrix LED Snake Game (HTML5 Web Socket)

Project showcase by hmkim

  • 5,396 views
  • 1 comment
  • 14 respects

Ninja Dollar - LCD game

Project tutorial by anime

  • 2,354 views
  • 2 comments
  • 4 respects

Make an Arduino Memory Game

Project tutorial by Jeremie

  • 9,110 views
  • 27 comments
  • 22 respects

8x8 Matrix LED Snake Game (Smartphone Motion)

Project tutorial by hmkim

  • 4,578 views
  • 2 comments
  • 17 respects
Add projectSign up / Login