int pos = 0;
int number = 0;
int computernumber = 0;
int generate()
{
int arr[] = new int[4];
int number = 0;
for (int i = 0; i<=3;i++)
{
arr[i] = int(random(1,9));
for (int a = 0; a < i;a++)
{
if (arr[a] == arr[i])
{
i--;
break;
}
}
}
for (int i = 0; i<=3;i++) number = number*10 + arr[i];
return number;
}
void setup()
{
size(250,250);
background(150,150,150);
computernumber = generate();
println(computernumber);
}
void compute1(int num,int number)
{
int counter = 1;
for (int i = 0; i <= 3; i++)
{
if (number%10 == num%10)
{
counter++;
}
num/=10;
number/=10;
}
if (counter == 1)
{
text("Ноль быков",0,100);
}
if (counter == 2)
{
text("Один бык",0,100);
}
if (counter == 3)
{
text("Два быка",0,100);
}
if (counter == 4)
{
text("Три быка",0,100);
}
if (counter == 5)
{
text("Четыре быка",0,100);
}
}
void compute2(int num,int number)
{
int d1[] = new int[4];
int d2[] = new int[4];
for (int i = 0 ; i <=3;i++)
{
d1[i] = num%10;
num/=10;
d2[i] = number%10;
number/=10;
}
int counter = 1;
for (int i = 0; i <= 3; i++)
{
for (int j = 0; j <= 3; j++)
{
if (d1[i] == d2[j] && i != j && d1[i] != - 1 && d2[j]!= - 1)
{
counter++;
d1[i] = - 1;
d2[j] = - 1;
}
}
}
if (counter == 1)
{
text("Ноль коров",0,150);
}
if (counter == 2)
{
text("Одна корова",0,150);
}
if (counter == 3)
{
text("Две коровы",0,150);
}
if (counter == 4)
{
text("Три коровы",0,150);
}
if (counter == 5)
{
text("Четыре коровы",0,150);
}
}
void draw()
{
if (computernumber == number)
{
text("Число угадано",0,200);
}
}
void keyPressed() {
if (keyCode == UP)
{
computernumber = generate();
}
String n = "0123456789";
if (pos == 4)
{
background(150,150,150);
compute1(computernumber,number);
compute2(computernumber,number);
pos = 0;
number = 0;
}
for (int i = 0; i <= 9;i++)
{
if (keyCode == n.charAt(i))
{
number = number*10 + (n.charAt(i) - 48);
fill(0,0,0);
textSize(25);
text(n.charAt(i),pos*50,50);
pos++;
}
}
}
|