有大神将这个人机五子棋改成双人对战,没有AI的,自己弄蒙了,谢谢
有大神将这个人机五子棋改成双人对战,没有AI的,自己弄蒙了,谢谢
有大神将这个人机五子棋改成双人对战,没有AI的,自己弄蒙了,谢谢
2017-11-09
就把computerAI这个函数去掉,在chess.onclick里面分别对黑棋和白旗进行赢法统计。
chess.onclick=function(e){
if(gameOver){
return;
}
var x=e.offsetX;
var y=e.offsetY;
var i=Math.floor(x/30);
var j=Math.floor(y/30);
if(empty[i][j]==0){
//保证黑白棋子交替
oneStep(i,j,flag);
empty[i][j]=1;
if(flag){
empty[i][j]=1;
for(var k=0;k<count;k++){
if(wins[i][j][k]){
myWin[k]++;
computerWin[k]=6;
if(myWin[k]==5){
setTimeout(()=>{
alert("黑棋赢了!");
},700);
gameOver=true;
}
}
}
if(!gameOver){
flag=!flag;
}
}else{
empty[i][j]=2;
for(var k=0;k<count;k++){
if(wins[i][j][k]){
computerWin[k]++;
myWin[k]=6;
if(computerWin[k]==5){
setTimeout(()=>{
alert("白棋赢了!");
},700);
gameOver=true;
}
}
}
if(!gameOver){
flag=!flag;
}
}
}
}
var chess = document.getElementById("chess");
var context = chess.getContext('2d');
var over = false;
var me = true; //我
var chressBord = [];//棋盘
for(var i = 0; i < 15; i++){
chressBord[i] = [];
for(var j = 0; j < 15; j++){
chressBord[i][j] = 0;
}
}
//赢法的统计数组
var myWin = [];
var yourWin = [];
//赢法数组
var wins = [];
for(var i = 0; i < 15; i++){
wins[i] = [];
for(var j = 0; j < 15; j++){
wins[i][j] = [];
}
}
var count = 0; //赢法总数
//横线赢法
for(var i = 0; i < 15; i++){
for(var j = 0; j < 11; j++){
for(var k = 0; k < 5; k++){
wins[i][j+k][count] = true;
}
count++;
}
}
//竖线赢法
for(var i = 0; i < 15; i++){
for(var j = 0; j < 11; j++){
for(var k = 0; k < 5; k++){
wins[j+k][i][count] = true;
}
count++;
}
}
//正斜线赢法
for(var i = 0; i < 11; i++){
for(var j = 0; j < 11; j++){
for(var k = 0; k < 5; k++){
wins[i+k][j+k][count] = true;
}
count++;
}
}
//反斜线赢法
for(var i = 0; i < 11; i++){
for(var j = 14; j > 3; j--){
for(var k = 0; k < 5; k++){
wins[i+k][j-k][count] = true;
}
count++;
}
}
for(var i = 0; i < count; i++){
myWin[i] = 0;
yourWin[i] = 0;
}
var chessBoard=[];
var me=true;
for(var i=0;i<15;i++){
chessBoard[i]=[];
for(var j=0;j<15;j++){
chessBoard[i][j]=0;
}
}
chess.onclick = function(e){
if(over){
return;
}
if(!me){
return;
}
var x = e.offsetX;
var y = e.offsetY;
var i = Math.floor(x / 30);
var j = Math.floor(y / 30);
if(chressBord[i][j] == 0){
oneStep(i,j,me);
chressBord[i][j] = 1;//我
for(var k = 0; k < count; k++){
if(wins[i][j][k]){
myWin[k]++;
yourWin[k] = 6;//这个位置对方不可能赢了
if(myWin[k] == 5){
window.alert('我赢了');
over = true;
}
}
}
if(!over){
me = !me;
for(var k = 0; k < count; k++){
if(wins[i][j][k]){
yourWin[k]++;
myWin[k] = 6;//这个位置对方不可能赢了
if(yourWin[k] == 5){
window.alert('你赢了');
over = false;
}
}
}
}
}
}
var chess = document.getElementById('chess');
var context = chess.getContext('2d');
context.strokeStyle="#bfbfbf";
var logo=new Image();
logo.src="image/logo.png";
logo.onload=function(){
context.drawImage(logo,0,0,450,450);
drawChessBoard();
}
var drawChessBoard=function(){
for(var i=0;i<15;i++){
context.moveTo(15+i*30,15);
context.lineTo(15+i*30,435);
context.stroke();
context.moveTo(15,15+i*30);
context.lineTo(435,15+i*30);
context.stroke();
}
}
var oneStep=function(i,j,me){
context.beginPath();
context.arc(15+i*30,15+j*30,13,0,2*Math.PI);
context.closePath;
var gradient=context.createRadialGradient(15+i*30+2,15+j*30-2,13,15+i*30+2,15+j*30-2,0);
if(me){
gradient.addColorStop(0,"#0a0a0a");
gradient.addColorStop(1,"#636766");
}else{
gradient.addColorStop(0,"#d1d1d1");
gradient.addColorStop(1,"#f9f9f9");
}
context.fillStyle=gradient;
context.fill();
}
chess.onclick=function (e) {
var x=e.offsetX;
var y=e.offsetY;
var i=Math.floor(x/30);
var j=Math.floor(y/30);
if(chessBoard[i][j]==0){
oneStep(i,j,me);
if(me){
chessBoard[i][j]=1;
}else{
chessBoard[i][j]=2;
}
me=!me;
}
}
document.getElementById("restart").onclick = function(){
window.location.reload();
}
我这个完成了ui,,,实现了落子,怎样可以实现双人对战???,是不是还需要两个人的分数计算。
举报