Skip to content

Instantly share code, notes, and snippets.

@ShawnSWu
Created October 24, 2022 17:15
Show Gist options
  • Save ShawnSWu/b4bf2bafb5896633df2165e62ea6fedb to your computer and use it in GitHub Desktop.
Save ShawnSWu/b4bf2bafb5896633df2165e62ea6fedb to your computer and use it in GitHub Desktop.
Gist範例
#include <iostream>
using namespace std;
int main() {
srand((unsigned)time(NULL));
int p[4];
p[0]=(rand()%9)+1;
p[1]=(rand()%9)+1;
while(p[1] == p[0]){
p[1]=(rand()%9)+1;
}
p[2]=(rand()%9)+1;
while(p[2] == p[0] || p[2] == p[1]){
p[2]=(rand()%9)+1;
}
p[3]=(rand()%9)+1;
while(p[3] == p[0] || p[3] == p[1] || p[3] == p[2]){
p[3]=(rand()%9)+1;
}
int password = (p[0])*1000+(p[1])*100+(p[2])*10+(p[3])*1;
cout<<password<<endl;
int guess,r;
int i=0;
int j=1;
int k=2;
int m=3;
do{
int a=0;
int b=0;
cout<<"please enter your guess: ";
cin>>guess;
int g[4];
g[0]= guess/1000;
g[1]= (guess%1000)/100;
g[2]= (guess%100)/10;
g[3]= (guess%10);
if(guess == password){
cout<<"4A0B";
break;
}
for(r=0;r<=3;r++){
if(g[r] == p[i]){
a++;
}
if(g[r] == p[j]){
b++;
}
if(g[r] == p[k]){
b++;
}
if(g[r] == p[m]){
b++;
}
i++;
j++;
k++;
m++;
if(j>3){
j%=4;
}
if(k>3){
k%=4;
}
if(m>3){
m%=4;
}
}
cout<<a<<'A'<<b<<'B'<<endl;
}while(guess != password);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment