<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>牛牛js</title>
<script type="text/javascript">
var huase=['♥','♠','♦','♣'];
var pai=['A','2','3','4','5','6','7','8','9','10','J','Q','K'];
var items=[];
var allindex=[];
var limit=1;//提示一次
for (let i in huase){
for (let j in pai){
items.push(huase[i]+pai[j]);
// console.log(huase[i]+pai[j]);
}
}
for (let i in items){
allindex.push(i);
}
function reset(){
if (allindex.length==0){
for (let i in items){
allindex.push(i);
}
alert("牌重置成功");
limit=1;
}
}
function getrandomnum(){
let num=[];
let count = 5;
while(num.length<count){
var temp=Math.floor(Math.random()*allindex.length);
num.push(parseInt(allindex.splice(temp,1)[0]));
}
console.log(allindex);
if (allindex.length==0 && limit==1){
alert("牌已经发完");
limit+=1;
}
console.log(num);
return num
// let num=[];
// for (let i=0;i<5;i++){
// Ran();
// }
// function Ran(){
// do{
// Random=Math.floor(Math.random()*items.length);
// } while(num.indexOf(Random)!=-1)
// num.push(Random);
// }
// return num;
}
// getrandomnum();
function getfivenum(){
let fivepai=[];
let num=getrandomnum()
for (let i in num){
fivepai.push(items[num[i]]);
}
console.log(fivepai);
return fivepai;
}
function tranum(item){
var fivenum=[];
console.log(item);
for (let j in item){
var number;
let JQK=['0','J','Q','K']
var a=item[j].substr(item[j].length-1,1)
console.log(a);
if (JQK.includes(a)){
number=10;
}
else if (a=='A'){
number=1;
}
else{
number=Number(a);
}
console.log(number);
fivenum.push(number);
}
console.log(fivenum);
return fivenum;
}
function play(){
let result='';
player=getfivenum();
cpu=getfivenum();
poker=player.join("        ");
poker2=cpu.join("        ");
pnum=tranum(player);
pnum2=tranum(cpu);
console.log(poker);
console.log(pnum);
console.log(poker2);
console.log(pnum2);
pniu=iswin(pnum)[1];
pnumber=iswin(pnum)[0];
cniu=iswin(pnum2)[1];
cnumber=iswin(pnum2)[0];
console.log(pniu,pnumber);
console.log(cniu,cnumber);
if (cnumber>pnumber){
result="<font color='green'>"+"你输了"+"</font>";
}
else if (cnumber<pnumber) {
result="<font color=red>"+"你赢了"+"</font>";
}
else{
result="平手";
}
// alert(poker);
document.getElementById("player").innerHTML="玩家:       "+poker;//发牌
document.getElementById("cpu").innerHTML="电脑:       "+poker2;//发牌
document.getElementById("cpuniu").innerHTML="电脑:       "+cniu;
document.getElementById("playerniu").innerHTML="玩家:       "+pniu;
document.getElementById("result").innerHTML=result;
return {poker,poker2};
}
function iswin(arr){
console.log(arr);
for(let i=0;i<arr.length-2;i++){
for(let j=i+1;j<arr.length-1;j++){
for(let k=j+1;k<arr.length;k++){
if((arr[i]+arr[j]+arr[k])%10 == 0){
console.log(i,j,k);
arr.splice(i,1,0);
arr.splice(j,1,0);
arr.splice(k,1,0);
console.log(arr);
if ((arr[0]+arr[1]+arr[2]+arr[3]+arr[4])%10==0){
console.log("满牛");
return [10,"满牛"];
}
else{
niushu=(arr[0]+arr[1]+arr[2]+arr[3]+arr[4])%10;
console.log("牛"+niushu);
return [niushu,"牛"+niushu];
}
}
}
}
}
console.log("没牛");
return [0,"没牛"];
}
</script>
</head>
<body>
<div><p id="cpu" style="text-align: center;"></p></div><br><br>
<p id="player" style="text-align: center;"></p><br><br>
<div style="text-align: center;"><button onclick="play()">发牌</button>       
<button onclick="reset()">重置</button></div><br>
<p id="cpuniu" style="text-align: center;"></p>
<p id="playerniu" style="text-align: center;"></p>
<p id="result" style="text-align: center;"></p>
</body>
</html>