1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
|
//
// main.cpp
// Bulls and Cows
//
// Created by user on 2015/7/17.
// Copyright (c) 2015年 Warren Leu. All rights reserved.
//
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
int main ()
{
std::string number, number2, guess1, guess2, guess3, guess4;
int num1, num2, num3, num4, number3, win, guesses, a, b, guess5, guess6, guess7, guess8, count;
win = 0;
guesses = 0;
srand (time(NULL));
num1 = rand() % 9 + 1;
srand (time(NULL));
num2 = rand() % 9 + 1;
while (num2 == num1) {
srand (time(NULL));
num2 = rand() % 9 + 1;
}
srand (time(NULL));
num3 = rand() % 9 + 1;
while(num3 == num2 || num3 == num2){
srand (time(NULL));
num3 = rand() % 9 + 1;
}
srand (time(NULL));
num4 = rand() % 9 + 1;
while(num4 == num3 || num4 == num2 || num4 == num1){
srand (time(NULL));
num4 = rand() % 9 + 1;
}
while(win != 1){
std::cin >> number;
number2 = number;
int number3 = atoi(number2.c_str());
guess1 = number[0];
guess2 = number[1];
guess3 = number[2];
guess4 = number[3];
int guess5 = atoi(guess1.c_str());
int guess6 = atoi(guess2.c_str());
int guess7 = atoi(guess3.c_str());
int guess8 = atoi(guess4.c_str());
if(number3 > 9999){
std::cout << "Too Large!\n";
continue;
}
else if(number3 < 1000) {
std::cout << "Too Small!\n";
continue;
}
if(guess5 == guess6 || guess5 == guess7 || guess5 == guess8){
std::cout << "No Repeats!\n";
continue;
}
else if(guess6 == guess5 || guess6 == guess7 || guess6 == guess8){
std::cout << "No Repeats!\n";
continue;
}
else if(guess7 == guess5 || guess7 == guess6 || guess7 == guess8){
std::cout << "No Repeats!\n";
continue;
}
else if(guess8 == guess5 || guess8 == guess6 || guess8 == guess7){
std::cout << "No Repeats!\n";
continue;
}
if(guess5 == num1){
a += 1;
}
else if(guess5 == num2 || guess5 == num3 || guess5 == num4){
b += 1;
}
if(guess6 == num2){
a += 1;
}
else if(guess6 == num1 || guess6 == num3 || guess6 == num4){
b += 1;
}if(guess7 == num3){
a += 1;
}
else if(guess7 == num1 || guess7 == num2 || guess7 == num4){
b += 1;
}if(guess8 == num4){
a += 1;
}
else if(guess8 == num2 || guess8 == num3 || guess8 == num1){
b += 1;
}
count += 1;
if(a == 4){
win = 1;
std::cout << "YOU WIN!!!\n";
std::cout << "You guessed ";
std::cout << count;
std::cout << " times.";
return 0;
}
std::cout << "\n";
std::cout << a;
std::cout <<"A";
std::cout << b;
std::cout << "B\n\n";
a = 0;
b = 0;
}
return 0;
}
|