我写了一个简单的,但是功能都实现勒 欢迎各位拍砖
<script>
window.onload=function(){
document.getElementById('submitCompare').onclick=function(){
CompareMethod.doCompare();
}
}
var CompareMethod={};
CompareMethod.errorChance=6;
CompareMethod.flag=false;
CompareMethod.getRandomNum=function(){
var randomNum=0;
var arr=[0,1,2,3,4,5,6,7,8,9];
var newArr=[];
for(var i=0;i<4;i++){
var randomNum=Math.floor(Math.random()*arr.length);
newArr.push(arr[randomNum]);
arr.splice(randomNum,1);
}
randomNum=newArr.join('');
return randomNum;
}
CompareMethod.randomNum=CompareMethod.getRandomNum();
CompareMethod.getUserNum=function(){
var userNum=[];
var resource=document.getElementsByName('myText');
for(var i=0;i<resource.length;i++){
userNum.push(resource[i].value||0);
}
return userNum;
}
CompareMethod.userNum=[];
CompareMethod.doCompare=function(){
var A=0;var B=0;
CompareMethod.userNum=CompareMethod.getUserNum();
if(CompareMethod.flag){
CompareMethod.errorChance=6;
CompareMethod.randomNum=CompareMethod.getRandomNum();
CompareMethod.flag=false;
}
for(var i=0;i<CompareMethod.userNum.length;i++){
if(CompareMethod.randomNum.indexOf(CompareMethod.userNum[i])!=-1){
if(CompareMethod.randomNum.indexOf(CompareMethod.userNum[i])==i){
A++;
}else{
B++;
}
}
}
if(A==4&&B==0){
alert('Congratulation to you!you guess right!');
CompareMethod.flag=true;
return;
}
CompareMethod.errorChance--;
if(CompareMethod.errorChance==0){
alert('game is over,please restart');
CompareMethod.errorChance=6;
CompareMethod.randomNum=CompareMethod.getRandomNum();
return false;
}
alert(A+'A'+B+'B,you still have '+CompareMethod.errorChance+' times chance')
}
</script>
</head>
<body>
<input type="text" class='text' name="myText"/>-<input type="text" class='text' name="myText"/>-<input type="text" class='text' name="myText" />-<input type="text" class='text' name="myText"/>
<br />
<br />
<input type="button" value='campare now' id="submitCompare"/>
</body>
<script>
window.onload=function(){
document.getElementById('submitCompare').onclick=function(){
CompareMethod.doCompare();
}
}
var CompareMethod={};
CompareMethod.errorChance=6;
CompareMethod.flag=false;
CompareMethod.getRandomNum=function(){
var randomNum=0;
var arr=[0,1,2,3,4,5,6,7,8,9];
var newArr=[];
for(var i=0;i<4;i++){
var randomNum=Math.floor(Math.random()*arr.length);
newArr.push(arr[randomNum]);
arr.splice(randomNum,1);
}
randomNum=newArr.join('');
return randomNum;
}
CompareMethod.randomNum=CompareMethod.getRandomNum();
CompareMethod.getUserNum=function(){
var userNum=[];
var resource=document.getElementsByName('myText');
for(var i=0;i<resource.length;i++){
userNum.push(resource[i].value||0);
}
return userNum;
}
CompareMethod.userNum=[];
CompareMethod.doCompare=function(){
var A=0;var B=0;
CompareMethod.userNum=CompareMethod.getUserNum();
if(CompareMethod.flag){
CompareMethod.errorChance=6;
CompareMethod.randomNum=CompareMethod.getRandomNum();
CompareMethod.flag=false;
}
for(var i=0;i<CompareMethod.userNum.length;i++){
if(CompareMethod.randomNum.indexOf(CompareMethod.userNum[i])!=-1){
if(CompareMethod.randomNum.indexOf(CompareMethod.userNum[i])==i){
A++;
}else{
B++;
}
}
}
if(A==4&&B==0){
alert('Congratulation to you!you guess right!');
CompareMethod.flag=true;
return;
}
CompareMethod.errorChance--;
if(CompareMethod.errorChance==0){
alert('game is over,please restart');
CompareMethod.errorChance=6;
CompareMethod.randomNum=CompareMethod.getRandomNum();
return false;
}
alert(A+'A'+B+'B,you still have '+CompareMethod.errorChance+' times chance')
}
</script>
</head>
<body>
<input type="text" class='text' name="myText"/>-<input type="text" class='text' name="myText"/>-<input type="text" class='text' name="myText" />-<input type="text" class='text' name="myText"/>
<br />
<br />
<input type="button" value='campare now' id="submitCompare"/>
</body>
遍历mapUser判断mapSys,时间复杂度O(1)
userNumber[]与sysNumber[]比对,
userNumber[i]==与sysNumber[j] ->A++,
不同则想查找hash表,hash[userNumber[i]]不为空,则B++
sysNum = [3, 5, 6, 4];
usrNum = [5, 6, 1, 4];
sys = [-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1]; //总共10个-1//先找到相同位置,大小相同的数字,并且输出
foreach(sysNum as i => num) {
if(sysNum[i] == usrNum[i]) {
echo 'A';
unset[sysNum[i]]; //去掉相同的
unset[usrNum[i]]; //去掉相同的
continue; //如果相同无需记录到sys数组中
}
sys[num] = num; //sysNum中第num个数字出现在sys中第num位
}//判断usrNum中每个num是否在sys中第num位出现
foreach (usrNum as i => num) {
if(sys[num] == num) echo 'B';
}
usrNum = [5, 6, 1, 4];
sys = [-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1]; //总共11个-1//先找到相同位置,大小相同的数字,并且输出
foreach(sysNum as i => num) {
if(sysNum[i] == usrNum[i]) {
echo 'A';
unset[sysNum[i]]; //去掉相同的
unset[usrNum[i]]; //去掉相同的
continue; //如果相同无需记录到sys数组中
}
sys[num] = num; //sysNum中第num个数字出现在sys中第num位
}//判断usrNum中每个num是否在sys中第num位出现
foreach (usrNum as i => num) {
if(sys[num] == num) echo 'B';
}
public void test(){
int countA = 0;
int countB = 0;
int[] sysNum = {1,0,2,4};
int[] usrNum = {4,0,2,3};
Set<Integer> set = new HashSet<Integer>(8);
int count = 0;
for(int i=0;i<4;i++){
if(sysNum[i] == usrNum[i]){
countA ++;
}else{
count +=2;
set.add(sysNum[i]);
set.add(usrNum[i]);
}
}
countB = count - set.size();
System.out.println(countA + "A" + countB + "B");
if(countA == 0 && countB == 0){
System.out.println("错误数字");
if(++errorNum == 6){
System.out.println("游戏结束");
}
}
}居然被我发现了这么古老的帖子