Browse Source

Init

master
NaiJi 12 months ago
commit
5164dfdcc7
  1. 73
      .gitignore
  2. 7
      CowsAndBulls.pro
  3. 24
      LICENSE
  4. 12
      README.md
  5. 131
      main.cpp

73
.gitignore

@ -0,0 +1,73 @@
# This file is used to ignore files which are generated
# ----------------------------------------------------------------------------
*~
*.autosave
*.a
*.core
*.moc
*.o
*.obj
*.orig
*.rej
*.so
*.so.*
*_pch.h.cpp
*_resource.rc
*.qm
.#*
*.*#
core
!core/
tags
.DS_Store
.directory
*.debug
Makefile*
*.prl
*.app
moc_*.cpp
ui_*.h
qrc_*.cpp
Thumbs.db
*.res
*.rc
/.qmake.cache
/.qmake.stash
# qtcreator generated files
*.pro.user*
# xemacs temporary files
*.flc
# Vim temporary files
.*.swp
# Visual Studio generated files
*.ib_pdb_index
*.idb
*.ilk
*.pdb
*.sln
*.suo
*.vcproj
*vcproj.*.*.user
*.ncb
*.sdf
*.opensdf
*.vcxproj
*vcxproj.*
# MinGW generated files
*.Debug
*.Release
# Python byte code
*.pyc
# Binaries
# --------
*.dll
*.exe

7
CowsAndBulls.pro

@ -0,0 +1,7 @@
TEMPLATE = app
CONFIG += console c++11
CONFIG -= app_bundle
CONFIG -= qt
SOURCES += \
main.cpp

24
LICENSE

@ -0,0 +1,24 @@
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.
In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
For more information, please refer to <https://unlicense.org>

12
README.md

@ -0,0 +1,12 @@
# Cows-bulls
Bulls and Cows (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
The numerical version of the game is usually played with 4 digits, but can also be played with 3 or any other number of digits.
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: 4271
- Opponent's try: 1234
- Answer: 1 bull and 2 cows. (The bull is "2", the cows are "4" and "1".)
(c) Wikipedia

131
main.cpp

@ -0,0 +1,131 @@
#include <iostream>
#include <ctime>
#include <cmath>
#define LENGTH 4
using namespace std;
/* adjust
*
* Cuts the digit and changes the repeatable number like this: num++
* */
int adjust(int d)
{
int digit = d;
int buff;
short numi;
bool is_ok;
do
{
buff = digit;
is_ok = true;
for (int i = 0; i < LENGTH; i++)
{
numi = buff % 10;
buff /= 10;
short buffj = buff;
for (int j = i + 1; j < LENGTH; j++)
{
if (buffj % 10 == numi || (j != LENGTH-1 && buffj % 10 == 0))
{
/* When the repeateble digit has been found, it changes the problem number
* and calls the function again
* (or if it's the first digit and it's 0, like " 0453 ")
* */
buffj = digit / pow(10, j);
if (buffj % 10 != 9)
buffj++;
else
buffj--;
digit = (buffj * pow(10, j)) + (digit % int(pow(10, j)));
return adjust(digit);
is_ok = false;
break;
}
buffj /= 10;
}
}
} while (!is_ok);
return digit;
}
/* specrand
* Generates some digit for a match
* */
int specrand()
{
srand(time(NULL));
int digit = (rand() % 9000) + 1000;
return adjust(digit);
}
int main()
{
int digit, input;
while(1)
{
digit = specrand();
cout << "Guess the digit!\nIf you want to give up, input -7";
while (cout << "\n > " && cin >> input && (input != -7 && input != digit))
{
if (!(input > 9876 || input < 1234)) {
short cows = 0, bulls = 0;
int buff;
int buff_bulls = digit;
for (int i = LENGTH; i > 0; i--)
{
short num = buff_bulls % 10;
short inp_num = input % 10;
if (num != inp_num)
{
buff = digit;
for (int j = LENGTH; j > 0; j--)
{
if ((input % 10 == buff % 10) && (j != i))
cows++;
buff /= 10;
}
} else
bulls++;
input /= 10;
buff_bulls /= 10;
}
cout << "Cows: " << cows << "; Bulls: " << bulls;
} else
cout << "Please, be accurate";
}
cout << "\nWhoa, the digit is " << digit << "!\n\n\n";
}
}
Loading…
Cancel
Save