Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
bulls & cows game - the computer will randomize a 4 digit number and the user need to guess it in turns.
digit class - represent a single digit in the game.
number class - represent a number (user/computer randomize)
game class - managing the game.
  • Loading branch information
eyalbi committed May 30, 2020
1 parent ec1b718 commit 9c3d774
Show file tree
Hide file tree
Showing 7 changed files with 260 additions and 0 deletions.
27 changes: 27 additions & 0 deletions Bulls & cows/Digit.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include "Digit.h"


bool Digit::InArray(int num, int index, Digit array[4])
{
for (int i = ZERO; i < index; i++) {
if (num == array[i].value) {
return true;
}
}
return false;
}


bool Digit::DEqual(const Digit & TDigit) const
{
if (value == TDigit.value) {
return true;
}
return false;
}

void Digit::Set_Digit(int num)
{
if(num > ZERO && num <= 9)
value = num;
}
15 changes: 15 additions & 0 deletions Bulls & cows/Digit.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#define ZERO 0
using namespace std;

class Digit { //declaration of digit class
private: //private varible int type
int value;
bool InArray(int num,int index, Digit array[4]); ///private method that check if number is an array allready
public:
Digit():value(ZERO) {}; //default constroctor
bool DEqual(const Digit &)const; //checks if digit typ are equal (their values)
int Get_Digit() { return value; }; // get function to value varible
void Set_Digit(int num); //set function
friend class Number; // friends classes declaration
friend class Game;
};
68 changes: 68 additions & 0 deletions Bulls & cows/Number.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#include "Number.h"
#include <ctime>
Number::Number()
{
for (int i = ZERO; i < 4; i++) {
(number[i]).Set_Digit(ZERO);
}
}

void Number::randomise()
{
int temp;
srand(time(NULL));
(number)[ZERO].value = (rand() % 9) + 1;
for (int i = 1; i < 4; i++) {
do
{
temp = (rand() % 9) + 1;
if (number[i].InArray(temp, i, number) == false) {
number[i].value = temp;
}

} while (number[i].InArray(temp, i, number) == true);
}
}
void Number::EnterValue()
{
int num,ones,tens ,hund,thous;

int i = ZERO;
do
{
if (i == ZERO) {
cout << "please enter a 4 digit integer without returning values or 0" << endl;
cin >> num;
}
if (i > ZERO) {
cout << "invalid input try agian" << endl;
cin >> num;
}
ones = num % 10;
tens = (num / 10) % 10;
hund = (num / 100) % 10;
thous = (num / 1000) % 10;
number[ZERO].value = thous;
number[1].value = hund;
number[2].value = tens;
number[3].value = ones;
i++;
}
while (ones == ZERO || ones == tens || ones == hund || ones == thous || tens == ZERO || tens == hund || tens == thous || hund == ZERO || hund == thous || thous == ZERO || num < 1000 || num > 9999 );
}

int Number::Get_Value(int num)
{
if (num < 0 || num > 3) {
return -1;
}
else {
return (number)[num].value;
}
}

void Number::PrintNumber()
{
int num = 1000 * (number)[ZERO].value + 100 * (number)[1].value + 10 * (number)[2].value + (number)[3].value;
cout << num ;
}
18 changes: 18 additions & 0 deletions Bulls & cows/Number.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#include <iostream>
#include <ctime>
#include "Digit.h"
#define ZERO 0
using namespace std;

class Number //class declaration
{
private:
Digit number[4]; // an array of 4 digits type
public:
Number(); // default constroctor
void randomise(); // a function that randomise number
void EnterValue(); // a function that help user enter a 4 digit number into the array
int Get_Value(int num); // a function that return a chosen value from the array
void PrintNumber(); // a function that prints the array
friend class Game; //declaration of friend class
};
13 changes: 13 additions & 0 deletions Bulls & cows/bullsNcows.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#define ZERO 0
#include "game1.h"
int main() {
cout << "==========================================\n" << "hello, this is the game 'bulls and cows' . " << endl;
cout << "==========================================" << endl;
cout << "the computer concieved 4 - digits number (all digits are different and without 0)." << endl;
cout << "your task - guess it threw number inputs." << endl;
cout << "if the digit is identical, but it's at another location - this is a 'cow'\nif the digit is in the right place - its a 'bull'" << endl;
Game PLAY;

PLAY.play();
return 0;
}
102 changes: 102 additions & 0 deletions Bulls & cows/game1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
#include "game1.h"

int Game::Cows()const
{
int cows = ZERO;
for (int i = ZERO; i < 4; i++) {
for (int j = ZERO; j < 4; j++) {
if (i != j && (computer->number)[i].value == (user->number)[j].value) {
cows++;
}
}
}
return cows;
}

int Game::Bulls() const
{
int bulls = ZERO;
for (int i = ZERO; i < 4; i++) {
if ((computer->number)[i].value == (user->number)[i].value) {
bulls++;
}

}
return bulls;
}

Game::Game()
{
user = new Number;
computer = new Number;
computer->randomise();
user->EnterValue();
}

Game::Game(const Game & obj)
{
if (user == NULL && computer == NULL) {
user = new Number;
computer = new Number;
}
else
{
delete user;
delete computer;
user = new Number;
computer = new Number;
}
for (int i = 0; i < 4; i++)
{
(user->number)[i].Set_Digit((obj.user)->number[i].Get_Digit());
(computer->number)[i].Set_Digit((obj.computer)->number[i].Get_Digit());
}
}

void Game::play()
{
char str;
do
{


cout << "Number B C\n" << "---------------" << endl;
while (this->CheckWin() == false) {
this->PrintUser();
cout << " " << this->Bulls() << " " << this->Cows() << endl;
this->Set_user();
}
cout << "************\nYOU WIN !!!\n************" << endl;
cout << "new game? Y/N" << endl;
cin >> str;
if (str != 'y') {
cout << "have a good day" << endl;
}
else {
this->Set_user();
}
}while (str == 'y');
}

bool Game::CheckWin() const
{
for (int i = ZERO; i < 4; i++) {
if ((computer->number)[i].value != (user->number)[i].value) {
return false;
}
}
return true;
}

void Game::PrintUser() const
{
user->PrintNumber();
}

void Game::Set_user()
{
user->EnterValue();
}



17 changes: 17 additions & 0 deletions Bulls & cows/game1.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include "Number.h"
#define ZERO 0
using namespace std;
class Game { // class declaration
private:
Number *user = NULL, *computer = NULL; //private variable declartion pointers to numbers class
int Cows()const;
int Bulls()const;
public:
Game(); // default constroctor allocates memory
Game(const Game & obj); // copy constroctor
~Game() { delete user; delete computer; }; ///destroctor to free dynamic memory
void play(); // play method that runs and manges the game
bool CheckWin()const; // function that checks if the game is over
void PrintUser()const; // function that prints user number array
void Set_user(); // a function to intialize user number in every turn
};

0 comments on commit 9c3d774

Please sign in to comment.