<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>五子棋</title>
<link rel="stylesheet" type="text/css" href="../../css/api.css" />
<link rel="stylesheet" type="text/css" href="../../css/aui.css" />
<link rel="stylesheet" type="text/css" href="../../css/aui-ext.css" />
<link rel="stylesheet" type="text/css" href="../../css/icon/Rjdaoico.css" />
<style media="screen">
#body{
background: url(../image/123.jpg)no-repeat;
background-size: 100% 100%;
}
#new{
height: 28px;
margin-left: 20%;
margin-top: 5%;
background: url(../image/bb.png)no-repeat;
background-size: 27% 100%;
}
#sz{
margin-left: 5%;
margin-top: 1%;
}
#replay{
height: 28px;
background: url(../image/bb.png)no-repeat;
background-size: 50% 100%;
display:none;
}
#ez{
margin-left: 12%;
margin-top: 4.5%;
}
span{
padding-left: 5%;
}
.aui-padded-t-15{
margin-top: -5%;
}
</style>
<script src="../script/jquery-3.3.1.min.js"></script>
<script type="text/javascript" src="../script/api.js"></script>
<script>
apiready = function(){
var backAuto = document.getElementById('backaudio');
backAuto.play();
$api.css($api.byId('body'), 'height:'+api.winHeight*0.974+'px');
$(function(){
// var canvas = document.getElementById("map");
var canvas = $api.byId('map');
canvas.width = api.winWidth*0.95;
canvas.height = api.winWidth*0.95;
var width = canvas.width;
var height = canvas.height;
var R = parseInt(width/20);
var ctx = canvas.getContext("2d");
function drawLine(){
ctx.beginPath();
for (var y = R;y<=height-R;y+=R) {
ctx.moveTo(R,y);
ctx.lineTo(height-R,y);
ctx.strokeStyle = "#B0B0B0";
ctx.stroke();
}
for (var x = R;x<=width-R;x+=R) {
ctx.moveTo(x,R);
ctx.lineTo(x,width-R);
ctx.strokeStyle = "#B0B0B0";
ctx.stroke();
}
ctx.closePath();
}
drawLine();
readChess();
canvas.addEventListener("click", play);
// var flag = true;
function play(event){
chess(canvas,event);
if(white.length>black.length && !isEnd()){
AI();
}
writeChess();
if(isEnd()){
canvas.removeEventListener("click", play);
}
}
var rec = [];
function getMin(rx,ry){
for (var i = 1;i<(width-R)/R;i++) {
for(var j = 1;j<(height-R)/R;j++){
if(Math.abs((i*R)-rx)<=(R/2) && Math.abs((j*R)-ry)<=R/2){
rx = i*R;
ry = j*R;
if($.inArray((rx+","+ry),rec) == -1){
rec.push(rx+","+ry);
return [rx,ry];
}else{
return -1;
}
}
}
}
}
var white = [];
var black = [];
function chess(canvas,event){
ctx.beginPath();
var rx = event.clientX - canvas.getBoundingClientRect().left;
var ry = event.clientY - canvas.getBoundingClientRect().top;
var l = getMin(rx,ry);
if (l == -1 || l == '' || l == undefined) {
return;
}
var temp = l[0]+","+l[1];
if(l == -1 || white.indexOf(temp) != -1 || black.indexOf(temp) != -1){
return;
}
var myAuto = document.getElementById('myaudio');
myAuto.play();
ctx.arc(l[0],l[1],R/2,0,2*Math.PI,true);
var chessstyle=ctx.createRadialGradient(l[0]+2,l[1]-2,R/2,l[0]+2,l[1]-2,1);
chessstyle.addColorStop(0,"#D1D1D1");
chessstyle.addColorStop(1,"#F9F9F9");
// ctx.fillStyle = "white";
white.push(l[0]+","+l[1]);
ctx.fillStyle = chessstyle;
ctx.fill();
ctx.closePath();
}
var flagXBlack = false;
var flagYBlack = false;
var flagYXBlack = false;
var flagXYBlack = false;
function AI(){
var num;
var maxPoint = [];
var pointScore = [];
for (var i = R;i < width-R;i+=R) {
pointScore[i] = [];
for (var j = R;j < height-R;j+=R) {
pointScore[i][j] = 0
}
}
for (var i = R;i < width-R;i+=R) {
for (var j = R;j < height-R;j+=R) {
if ($.inArray((i+","+j),rec) == -1) {//遍历所有的空
white.push(i+","+j);//每个空假设放一次白棋
num = numRX(white,black,white[(white.length-1)],1,0);//放了之后白棋横向能连的棋子数
if (num < numRY(white,black,white[white.length-1],1,0)) {
num = numRY(white,black,white[white.length-1],1,0);//放了之后白棋竖向能连的棋子数
}
if (num < numRXY(white,black,white[white.length-1],1,0)) {
num = numRXY(white,black,white[white.length-1],1,0);//放了之后白棋右斜能连的棋子数
}
if (num < numRYX(white,black,white[white.length-1],1,0)) {
num = numRYX(white,black,white[white.length-1],1,0);//放了之后白棋左斜能连的棋子数
}
if(num > 2){
if ((numRX(white,black,white[(white.length-1)],1,0) == numRY(white,black,white[(white.length-1)],1,0)) && (!flagXBlack && !flagYBlack)){
num == 7;
}
if ((numRX(white,black,white[(white.length-1)],1,0) == numRYX(white,black,white[(white.length-1)],1,0)) && (!flagXBlack && !flagYXBlack)){
num == 7;
}
if ((numRX(white,black,white[(white.length-1)],1,0) == numRXY(white,black,white[(white.length-1)],1,0)) && (!flagXBlack && !flagXYBlack)){
num == 7;
}
if ((numRY(white,black,white[(white.length-1)],1,0) == numRYX(white,black,white[(white.length-1)],1,0)) && (!flagYBlack && !flagYXBlack)){
num == 7;
}
if ((numRY(white,black,white[(white.length-1)],1,0) == numRXY(white,black,white[(white.length-1)],1,0)) && (!flagYBlack && !flagXYBlack)){
num == 7;
}
}
if (num < 6) {
if (numRX(white,black,white[(white.length-1)],1,0) > 2 && (!flagXBlack)) {
if ((numRY(white,black,white[white.length-1],1,0) > 2 && (!flagYBlack)) || (numRXY(white,black,white[white.length-1],1,0) > 2 && (!flagXYBlack)) || (numRYX(white,black,white[white.length-1],1,0) > 2 && (!flagYXBlack))) {
num = 7;
}
}
if (numRY(white,black,white[(white.length-1)],1,0) > 2 && (!flagYBlack)) {
if ((numRX(white,black,white[white.length-1],1,0) > 2 && (!flagXBlack)) || (numRXY(white,black,white[white.length-1],1,0) > 2 && (!flagXYBlack)) || (numRYX(white,black,white[white.length-1],1,0) > 2 && (!flagYXBlack))) {
num = 7;
}
}
if (numRXY(white,black,white[(white.length-1)],1,0) > 2 && (!flagXYBlack)) {
if ((numRY(white,black,white[white.length-1],1,0) > 2 && (!flagYBlack)) || (numRX(white,black,white[white.length-1],1,0) > 2 && (!flagXBlack)) || (numRYX(white,black,white[white.length-1],1,0) > 2 && (!flagYXBlack))) {
num = 7;
}
}
if (numRYX(white,black,white[(white.length-1)],1,0) > 2 && (!flagYXBlack)) {
if ((numRY(white,black,white[white.length-1],1,0) > 2 && (!flagYBlack)) || (numRX(white,black,white[white.length-1],1,0) > 2 && (!flagXBlack)) || (numRXY(white,black,white[white.length-1],1,0) > 2 && (!flagXYBlack))) {
num = 7;
}
}
}
switch (num){
case 1:
pointScore[i][j] = 50;
break;
case 2:
pointScore[i][j] = 200;
break;
case 3:
pointScore[i][j] = 500;
break;
case 4:
pointScore[i