我剛學程式老師給很難的題目,希望高手可以幫我解答,謝謝
請撰寫一個猜數字遊戲。電腦產生之數字(被猜的數字)為隨機產生,且必須三個數字不同,輸入的數(玩家猜測的數字)亦要檢測是否三個數值不同。玩家一回合可以猜五次數字,若輸入-1結束遊戲。
NOTE:
(1) 若電腦當回合產生數字為211,因後面兩數相同,必須重新產生。
(2) 若玩家輸入211,因後面兩數相同,必須重新產生。
(3) 一回合僅可以猜測五次。
(4) 輸入 -1 結束遊戲。
我剛學程式老師給很難的題目,希望高手可以幫我解答,謝謝
請撰寫一個猜數字遊戲。電腦產生之數字(被猜的數字)為隨機產生,且必須三個數字不同,輸入的數(玩家猜測的數字)亦要檢測是否三個數值不同。玩家一回合可以猜五次數字,若輸入-1結束遊戲。
NOTE:
(1) 若電腦當回合產生數字為211,因後面兩數相同,必須重新產生。
(2) 若玩家輸入211,因後面兩數相同,必須重新產生。
(3) 一回合僅可以猜測五次。
(4) 輸入 -1 結束遊戲。
//A game to guess the number which generated by the computer,
//each round user will input a number, if that number match the number generated by computer, user will win.
//If after 5 times user still can't guess the number, user will lose.
//If user input -1 the program will end here.
//user can not input the same numbers like 111,222,...or 211,311,... or 112,113... It is all wrong to input
//If the number generated is the same numbers like 111,222,...or 211,311,... or 112,113... It will auto change different numbers
#include<iostream>
#include<cstdlib>
#include<ctime>
using namespace std;
int main()
{
int ANS,Input,i;
int a,b,c;
int x,y,z;
int A=0;
int B=0;
int times = 0;
while(Input!=-1){ //if user input -1, it will break the while loop and end the program
srand( time(NULL) ); //generate a random number after a round
ANS = rand()%(999-100+1)+100; //range of numbers generated by computer from 100 to 999
do //check the ANS if they have the same number
{
z = ANS % 10; //3rd
y = (ANS % 100 - z) / 10; //2nd
x = ANS / 100; //1st
}
while(x==y || x==z || y==z);{
for(i=1;i<=5;i++){ //it is maximum 5 times for user to guess the number generated by computer
cout << i << " : please input the number: ";
cin >> Input; //user input the number they guess here
c = Input%10; //3rd
b = (Input%100-a)/10; //2nd
a = Input/100; //1st
if( Input == -1 ) //if user input -1, it will break the for loop
{
break;
}
//check the Input number and ANS, if they have the same number with the same position then A+1; else B+1
if(a==x){A++;}
if(a==y){B++;}
if(a==z){B++;}
if(b==x){B++;}
if(b==y){A++;}
if(b==z){B++;}
if(c==x){B++;}
if(c==y){B++;}
if(c==z){A++;}
cout << "Result" << i <<" : "<< A <<"A"<< B <<"B" << endl << endl;
if(a==c||a==b||c==b) //check the Input number and ANS if they the same, output you are wrong
{
cout << "You input a wrong number. Please input again."<<endl<<endl;
}
if( Input == ANS || A==3 ) //if user input the number match the number generated by computer, they will win and the program will show user the answer
{
cout << "You win!! " << endl << endl;
break;
}
if( i == 5) // if user can't input the correct number after 5 times, they will lose and the program will show user the answer
{
cout << "you are a loser " << endl << endl;
break;
}
}
return 0;
}
}
}
請問我這樣寫為什麼不能輸入5次每round, 然後判斷AB是錯嗎, 請高手幫我改一改。
非常感謝!!!