概述
先说先五子棋的思想就是练成5个就胜出
用了博弈树但是由于是JS语言的关系程序才2层而已,心思都放在了估值函数上。
各个情况的权有些是参照网上的不过自己改了下而且感觉自己写的估值函数不错发上来跟大家分享下,本人是小白程序还写的不太好,js只学了几天。
下面放出服务器代码,客户端代码就不发了(没什么参考价值).
var server = require('http').createServer();
var io = require('socket.io')(server);
var users = {};
var rooms = {};
var TypeRecord = null;//位置评分
/*
TypeRecord = [
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0],
[0, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 0],
[0, 1, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 2, 1, 0],
[0, 1, 2, 3, 4, 4, 4, 4, 4, 4, 4, 3, 2, 1, 0],
[0, 1, 2, 3, 4, 5, 5, 5, 5, 5, 4, 3, 2, 1, 0],
[0, 1, 2, 3, 4, 5, 6, 6, 6, 5, 4, 3, 2, 1, 0],
[0, 1, 2, 3, 4, 5, 6, 7, 6, 5, 4, 3, 2, 1, 0],
[0, 1, 2, 3, 4, 5, 6, 6, 6, 5, 4, 3, 2, 1, 0],
[0, 1, 2, 3, 4, 5, 5, 5, 5, 5, 4, 3, 2, 1, 0],
[0, 1, 2, 3, 4, 4, 4, 4, 4, 4, 4, 3, 2, 1, 0],
[0, 1, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 2, 1, 0],
[0, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 0],
[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]; //初始化权值表
*/
TypeRecord = [
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]; //初始化权值表
io.on('connection', function (socket) {
//console.log("有一个客户段连接!");
console.log(socket.id);
socket.join("public");
//用户登录
socket.on('user.online', function (user) {
user.roomname = "public";
users[user.id] = user;
//console.log(socket.id);
//广播用户列表(包括自己)
io.sockets.emit("user.online", getUsers());
//发送房间信息给自己
socket.emit("room.rooms", getRooms());
});
//发送对话
socket.on('chat.send', function (data) {
var user = users[socket.id.replace("/#", "")];
//转发给同一房间(除了自己)其他客户端处理
socket.in(user.roomname).emit("chat.newchat", data);
});
//创建房间
socket.on('room.create', function (roomname) {
if (rooms[roomname]) {//查看是否有同名的房间
socket.emit("room.failure");
} else {
var user = users[socket.id.replace("/#", "")];
if (user.roomname != "public") {
socket.emit("room.failure");
return;
}
socket.leave(user.roomname);
user.roomname = roomname;
socket.join(user.roomname);
user.status = 2;
var room = {
roomname: roomname,
play1: user,
play2: null,
history: []//记录下棋历史
}
rooms[roomname] = room;
socket.emit("room.createOk", room);
//广播房间
socket.in("public").emit("room.rooms", getRooms());
//广播用户列表(包括自己)
io.sockets.emit("user.online", getUsers());
}
});
//加入房间
socket.on('room.join', function (roomname) {
var room = rooms[roomname];
if (room.play2) {//查看房间是否已经满了
socket.emit("joinFaild");
return;
}
var user = users[socket.id.replace("/#", "")];
socket.leave(user.roomname);//离开房间
user.roomname = roomname;//修改信息
socket.join(user.roomname);//加入房间
room.play2 = user;//play2指向user
user.status = 2;
//返回房间创建成功
socket.emit("room.joinOk", room);
//广播信息(除了自己)告诉房主我加入了
socket.in(roomname).emit("room.createOk", room);
//广播用户列表(包括自己)
io.sockets.emit("user.online", getUsers());
});
//离开房间
socket.on('room.out', function () {
var user = users[socket.id.replace("/#", "")];
var room = rooms[user.roomname];
if (user.status == 2) {//在房间的时候离开房间
socket.leave(room.roomname);
socket.join("public");
user.status = 1;
user.roomname = "public";
if (room.play1.id == user.id) {//房主
if (room.play2) {
var pl2 = users[room.play2.id];
io.sockets.sockets["/#" + room.play2.id].leave(room.roomname);
io.sockets.sockets["/#" + room.play2.id].join("public");
pl2.status = 1;
pl2.roomname = "public";
}
delete rooms[room.roomname];
//广播房间
io.sockets.in("public").emit("room.rooms", getRooms());
} else {//对手
room.play2 = null;
//告诉房主自己走了
socket.in(room.roomname).emit("room.createOk", room);
//自己获取房间列表
socket.emit("room.rooms", getRooms());
}
}
if (user.status == 3) {//在游戏中离开房间
socket.leave(room.roomname);
socket.join("public");
user.status = 1;
user.roomname = "public";
if (room.play1.id == user.id) {//房主
if (room.play2) {
var pl2 = users[room.play2.id];
pl2.status = 1;
pl2.win += 1;
pl2.total += 1;
pl2.roomname = "public";
room.play1 = null;
//发送胜利消息
io.sockets.in(room.roomname).emit("game.overBydisConnect", {
winID: room.play2.id,
room: room
});
socket.emit("game.overBydisConnect", {
winID: room.play2.id,
room: room
});
io.sockets.sockets["/#" + room.play2.id].leave(room.roomname);
io.sockets.sockets["/#" + room.play2.id].join("public");
} else {
socket.emit("game.overByComputer", {
win: false,
roomname: room.roomname,
play1: null,
play2: null
});
}
delete rooms[room.roomname];
//广播房间
io.sockets.in("public").emit("room.rooms", getRooms());
} else {//对手
room.play1.win += 1;
room.play1.total += 1;
room.play1.status = 2;
room.play2 = null;
//发送胜利消息
io.sockets.in(room.roomname).emit("game.overBydisConnect", {
winID: room.play1.id,
room: room
});
socket.emit("game.overBydisConnect", {
winID: room.play1.id,
room: room
});
socket.emit("room.rooms", getRooms());
}
}
//广播用户列表(包括自己)
io.sockets.emit("user.online", getUsers());
});
//游戏开始
socket.on('game.start', function () {
var user = users[socket.id.replace("/#", "")];
var room = rooms[user.roomname];
room.play1.status = 3;
if (room.play2) {
room.play2.status = 3;
} else {
rooms[user.roomname] = {
roomname: room.roomname,
play1: user,
play2: null,
history: [],//记录下棋历史
currentBoardSituation: null,//记录当前局面
computer: {
row: null,
col: null
}
}
room = rooms[user.roomname];
room.currentBoardSituation = [];
for (var i = 0; i < 15; i++) {
var arr = [];
for (var j = 0; j < 15; j++) {
arr[j] = -1;
}
room.currentBoardSituation.push(arr);
}
}
//广播用户列表(包括自己)
io.sockets.emit("user.online", getUsers());
//告诉自己游戏创建成功
socket.emit("game.start", 0);
//告诉同房间的对手游戏开始了 flag为1;
socket.in(user.roomname).emit("game.start", 1);
});
//游戏数据交互
socket.on('game.change', function (data) {
var user = users[socket.id.replace("/#", "")];
//添加下棋历史
var room = rooms[user.roomname];
var step = {
row: data.row,
col: data.col,
flag: data.flag
};
room.history.push(step);
if (!room.play2) {//判断是否是人机
room.currentBoardSituation[data.row][data.col] = data.flag;
MainDealFunction(room);//人机处理函数
step = {
row: room.computer.row,
col: room.computer.col,
flag: 1
};
room.currentBoardSituation[room.computer.row][room.computer.col] = 1;
room.history.push(step);
if (isGameOver(room.computer.row, room.computer.col, 1, room.currentBoardSituation)) {
socket.emit("game.change", {
row: room.computer.row,
col: room.computer.col,
flag: 1
});
room.play1.status = 2;
socket.emit("user.online", getUsers());//告诉自己游戏结束了
socket.emit("game.overByComputer", {
win: false,
roomname: room.roomname,
play1: room.play1,
play2: null
});
} else {
socket.emit("game.change", {
row: room.computer.row,
col: room.computer.col,
flag: 1
});
}
} else {
//告诉同房间的对手已经下一步棋了
socket.in(user.roomname).emit("game.change", data);
}
});
//游戏结束
socket.on('game.over', function () {
var user = users[socket.id.replace("/#", "")];
var room = rooms[user.roomname];
user.win += 1;
room.play1.total += 1;
room.play1.status = 2;
if (room.play2) {
room.play2.status = 2;
room.play2.total += 1;
//广播用户列表(包括自己)
io.sockets.emit("user.online", getUsers());
//发送同房间的对手游戏结束了
io.sockets.in(user.roomname).emit("game.over", {
winID: socket.id.replace("/#", ""),
room: room
});
} else {
socket.emit("user.online", getUsers());//告诉自己游戏结束了
socket.emit("game.overByComputer", {
win: true,
roomname: room.roomname,
play1: room.play1,
play2: null
});
}
});
//悔棋
socket.on('game.rollback', function () {
var user = users[socket.id.replace("/#", "")];
//删除一次下棋历史
var room = rooms[user.roomname];
if (room.history.length < 2) return;
io.sockets.in(room.roomname).emit("game.rollback", {
myStep: room.history[room.history.length - 1],
outerStep: room.history[room.history.length - 2]
});
delete room.history[room.history.length - 1];
delete room.history[room.history.length - 2];
room.history.length -= 2;
});
//
socket.on('event', function (data) {
});
//掉线处理
socket.on('disconnect', function () {
var user = users[socket.id.replace("/#", "")];
var room = rooms[user.roomname];
if (user.status == 2) {//在房间的时候掉线
if (room.play1.id == user.id) {//房主
if (room.play2) {
var pl2 = users[room.play2.id];
pl2.status = 1;
pl2.roomname = "public";
io.sockets.sockets["/#" + room.play2.id].leave(room.roomname);
io.sockets.sockets["/#" + room.play2.id].join("public");
}
delete rooms[user.roomname];
//广播房间
socket.in("public").emit("room.rooms", getRooms());
} else {//对手
room.play2 = null;
//告诉房主自己走了
socket.in(user.roomname).emit("room.createOk", room);
}
}
if (user.status == 3) {//在游戏中掉线
if (room.play1.id == user.id) {//房主
if (room.play2) {
var pl2 = users[room.play2.id];
pl2.status = 1;
pl2.win += 1;
pl2.total += 1;
pl2.roomname = "public";
room.play1 = null;
//发送胜利消息
io.sockets.in(room.roomname).emit("game.overBydisConnect", {
winID: room.play2.id,
room: room
});
io.sockets.sockets["/#" + room.play2.id].leave(room.roomname);
io.sockets.sockets["/#" + room.play2.id].join("public");
}
delete rooms[room.roomname];
//广播房间
socket.in("public").emit("room.rooms", getRooms());
} else {//对手
room.play1.win += 1;
room.play1.total += 1;
room.play1.status = 2;
room.play2 = null;
//发送胜利消息
io.sockets.in(room.roomname).emit("game.overBydisConnect", {
winID: room.play1.id,
room: room
});
}
}
delete users[socket.id.replace("/#", "")];
io.sockets.emit("user.online", getUsers());
});
});
function getUsers() {
var arr = [];
for (var key in users) {
arr.push(users[key]);
}
return arr;
}
function getRooms() {
var arr = [];
for (var key in rooms) {
arr.push(rooms[key]);
}
return arr;
}
function MainDealFunction(room) {
if (room.history.length == 1) {
var pi = room.history[room.history.length - 1].row;
var pj = room.history[room.history.length - 1].col;
room.computer.row = pi > 6 ? pi - 1 : pi + 1;
room.computer.col = pj > 6 ? pj - 1 : pj + 1;
} else {
AlphaBeta(room.currentBoardSituation, { row: room.history[room.history.length - 1].row, col: room.history[room.history.length - 1].col, flag: 0 }, 2, Number.NEGATIVE_INFINITY, Number.POSITIVE_INFINITY, room.computer);
}
/*
var nAlpha = Number.NEGATIVE_INFINITY;//记录权重先初始化为一个很小的数 等等比较选最大的
for (var i = 1; i < 15; i++) {
for (var j = 1; j < 15; j++) {
if (-1 == room.currentBoardSituation[i][j]) {
room.currentBoardSituation[i][j] = 1;
var score = AlphaBeta(room.currentBoardSituation,{ row: i, col: j, flag: 1 }, 1, Number.NEGATIVE_INFINITY, Number.POSITIVE_INFINITY) + TypeRecord[i][j];
room.currentBoardSituation[i][j] = -1;
if (score > nAlpha) {
nAlpha = score;
room.computer.row = i;
room.computer.col = j;
}
}
}
}
*/
}
function AlphaBeta(currentBoardSituation, point, nPly, nAlpha, nBeta, computer) {
if (isGameOver(point.row, point.col, point.flag, currentBoardSituation)) {
return 100000;
}
if (nPly == 0) {//到叶子了
return Eveluation(currentBoardSituation);
}
//var iMAX = point.row + 5 < 15 ? point.row + 5 : 15;
//var jMAX = point.col + 5 < 15 ? point.col + 5 : 15;
if (point.flag == 1) {//上一步是人还是机器,0是人,1是机器
//到人走min
//for (var i = (point.row - 5 > 0 ? point.row - 5 : 1); i < iMAX; i++) {//减少遍历次数
//for (var j = (point.col - 5 > 0 ? point.col - 5 : 1); j < jMAX; j++) {//减少遍历次数
for (var i = 1; i < 15; i++) {
for (var j = 1; j < 15; j++) {
if (-1 == currentBoardSituation[i][j]) {
currentBoardSituation[i][j] = 0;
var score = AlphaBeta(currentBoardSituation, { row: i, col: j, flag: 0 }, nPly - 1, nAlpha, nBeta, computer);
currentBoardSituation[i][j] = -1;
if (score < nBeta) {
nBeta = score;
if (nAlpha >= nBeta) {
return nAlpha;
}
}
}
}
}
return nBeta;
} else {//到机器走max
//for (var i = (point.row - 5 > 0 ? point.row - 5 : 1); i < iMAX; i++) {//减少遍历次数
//for (var j = (point.col - 5 > 0 ? point.col - 5 : 1); j < jMAX; j++) {//减少遍历次数
for (var i = 1; i < 15; i++) {
for (var j = 1; j < 15; j++) {
if (-1 == currentBoardSituation[i][j]) {
currentBoardSituation[i][j] = 1;
var score = AlphaBeta(currentBoardSituation, { row: i, col: j, flag: 1 }, nPly - 1, nAlpha, nBeta, computer);
TypeRecord[i][j] = score;
currentBoardSituation[i][j] = -1;
if (score > nAlpha) {
nAlpha = score;
computer.row = i;
computer.col = j;
if (nAlpha >= nBeta) {
return nBeta;
}
}
}
}
}
return nAlpha;
}
}
function Eveluation(currentBoardSituation) {
return CountValue(currentBoardSituation, 1) - CountValue(currentBoardSituation, 0);
}
function CountValue(currentBoardSituation, flag) {
var result = 0;
//判断在横上的权
for (var i = 1; i < 15; i++) {
var begindown;//表示前面是不是死棋
var enddown;//表示相连棋子后面是不是死棋
var count = 0;
var oldcount = 0;
var kong = 0;
var j = 1;
while ((j < 15) && (currentBoardSituation[i][j] != flag)) {//找到第一个同类的棋子
j++;
}
while (j < 15) {//看有没有超越界限
begindown = true;
enddown = true;
count = 0;
if ((j > 1) && (-1 == currentBoardSituation[i][j - 1])) {
begindown = false;//表示棋子没死
}
while ((j < 15) && (currentBoardSituation[i][j] == flag)) {
j++;
count++;
}
if ((j < 15) && (-1 == currentBoardSituation[i][j])) {
enddown = false;
}
if (count > 0) {
result += CheckValue(begindown, enddown, count,oldcount, kong);
if (!enddown) kong = 1;
oldcount = count;
} else {
if (!enddown) kong++;
}
j++;
}
}
//判断在竖线上的权
for (var j = 1; j < 15; j++) {
var begindown;//表示前面是不是死棋
var enddown;//表示相连棋子后面是不是死棋
var count = 0;
var oldcount = 0;
var kong = 0;
var i = 1;
while ((i < 15) && (currentBoardSituation[i][j] != flag)) {//找到第一个黑子
i++;
}
while (i < 15) {//看有没有超越界限
begindown = true;
enddown = true;
count = 0;
if ((i > 1) && (-1 == currentBoardSituation[i - 1][j])) {
begindown = false;//表示棋子没死
}
while ((i < 15) && (currentBoardSituation[i][j] == flag)) {
i++;
count++;
}
if ((i < 15) && (-1 == currentBoardSituation[i][j])) {
enddown = false;
}
if (count > 0) {
result += CheckValue(begindown, enddown, count,oldcount, kong);
if (!enddown) kong = 1;
oldcount = count;
} else {
if (!enddown) kong++;
}
i++;
}
}
//判断在向左斜的斜线上?0
for (var k = 1; k < 15; k++) {
var begindown;//表示前面是不是死棋
var enddown;//表示相连棋子后面是不是死棋
var count = 0;
var oldcount = 0;
var kong = 0;
var i = k;
var j = 14;
while ((i < 15) && (j > 0) && (currentBoardSituation[i][j] != flag)) {//找到第一个黑子
i++;
j--;
}
while ((i < 15) && (j > 0)) {//看有没有超越界限
begindown = true;
enddown = true;
count = 0;
if ((j < 14) && (i > 1) && (-1 == currentBoardSituation[i - 1][j + 1])) {
begindown = false;//表示棋子没死
}
while ((i < 15) && (j > 0) && (currentBoardSituation[i][j] == flag)) {
i++;
j--;
count++;
}
if ((i < 15) && (j > 0) && (currentBoardSituation[i][j] == -1)) {
enddown = false;
}
if (count > 0) {
result += CheckValue(begindown, enddown, count,oldcount, kong);
if (!enddown) kong = 1;
oldcount = count;
} else {
if (!enddown) kong++;
}
i++;
j--;
}
kong = 0;
oldcount = 0;
if (k != 1) {
i = 1;
j = 15 - k;
while ((i < 15) && (j > 0) && (currentBoardSituation[i][j] != flag)) {//找到第一个黑子
i++;
j--;
}
while ((i < 15) && (j > 0)) {//看有没有超越界限
begindown = true;
enddown = true;
count = 0;
if ((j < 14) && (i > 1) && (-1 == currentBoardSituation[i - 1][j + 1])) {
begindown = false;//表示棋子没死
}
while ((i < 15) && (j > 0) && (currentBoardSituation[i][j] == flag)) {
i++;
j--;
count++;
}
if ((i < 15) && (j > 0) && (currentBoardSituation[i][j] == -1)) {
enddown = false;
}
if (count > 0) {
result += CheckValue(begindown, enddown, count,oldcount, kong);
if (!enddown) kong = 1;
oldcount = count;
} else {
if (!enddown) kong++;
}
i++;
j--;
}
}
}
//判断在向右斜的斜线上?20:30
for (var k = 1; k < 15; k++) {
var begindown;//表示前面是不是死棋
var enddown;//表示相连棋子后面是不是死棋
var count;
var kong = 0;
var i = 1;
var j = k;
while ((i < 15) && (j < 15) && (currentBoardSituation[i][j] != flag)) {//找到第一个黑子
i++;
j++;
}
while ((i < 15) && (j < 15)) {//看有没有超越界限
begindown = true;
enddown = true;
count = 0;
if ((j > 1) && (i > 1) && (-1 == currentBoardSituation[i - 1][j - 1])) {
begindown = false;//表示棋子没死
}
while ((i < 15) && (j < 15) && (currentBoardSituation[i][j] == flag)) {
i++;
j++;
count++;
}
if ((i < 15) && (j < 15) && (currentBoardSituation[i][j] == -1)) {
enddown = false;
}
if (count > 0) {
result += CheckValue(begindown, enddown, count,oldcount, kong);
if (!enddown) kong = 1;
oldcount = count;
} else {
if (!enddown) kong++;
}
i++;
j++;
}
kong = 0;
if (k != 1) {
i = k;
j = 1;
while ((i < 15) && (j < 15) && (currentBoardSituation[i][j] != flag)) {//找到第一个黑子
i++;
j++;
}
while ((i < 15) && (j < 15)) {//看有没有超越界限
begindown = true;
enddown = true;
count = 0;
if ((j > 1) && (i > 1) && (-1 == currentBoardSituation[i - 1][j - 1])) {
begindown = false;//表示棋子没死
}
while ((i < 15) && (j < 15) && (currentBoardSituation[i][j] == flag)) {
i++;
j++;
count++;
}
if ((i < 15) && (j < 15) && (currentBoardSituation[i][j] == -1)) {
enddown = false;
}
if (count > 0) {
result += CheckValue(begindown, enddown, count,oldcount, kong);
if (!enddown) kong = 1;
oldcount = count;
} else {
if (!enddown) kong++;
}
i++;
j++;
}
}
}
return result;
}
function CheckValue(begindown, enddown, count,oldcount,kong) {
var result = 0;
if ((!begindown) && (!enddown)) {//活
result = 10;
} else if ((begindown) && (!enddown)) {//眠
result = 1;
} else if ((!begindown) && (enddown)) {//眠
result = 1;
}
switch (count) {
case 0: result = 0; break;
case 1: break;//o
case 2: result *= 10; break;//oo
case 3: result *= 100; break;//ooo
case 4: result *= 1000; break;//oooo
case 5: result = 100000; break;//ooooo
default:
}
switch(oldcount){
case 0:break;
case 1:if(kong == 1){
switch(count){
case 1:result+=1;break;//o_o
case 2:result+=10;break;//o_oo
case 3:result+=100;break;//o_ooo
default:
}
}else if(kong == 2){
switch(count){
case 2:result+=1;break;//o__oo
case 3:result+=10;break;//o__ooo
default:
}
}
break;
case 2:if(kong == 1){
switch(count){
case 1:result+=10;break;//oo_o
case 2:result+=100;break;//oo_oo
case 3:result+=1000;break;//oo_ooo
default:
}
}else if(kong == 2){
switch(count){
case 2:result+=10;break;//oo__oo
case 3:result+=100;break;//oo__ooo
default:
}
}
break;
case 3:
if(kong == 1){
switch(count){
case 1:result+=100;break;//ooo_o
case 2:result+=1000;break;//ooo_oo
case 3:result+=1000;break;//ooo_ooo
default:
}
}else if(kong == 2){
switch(count){
case 2:result+=100;break;//ooo__oo
case 3:result+=1000;break;//ooo__ooo
default:
}
}
break;
case 4:
if(kong == 1){
switch(count){
case 1:result+=1000;break;//oooo_o
case 2:result+=1000;break;//oooo_oo
case 3:result+=1000;break;//oooo_ooo
default:
}
}else if(kong == 2){
switch(count){
case 2:result+=100;break;//oooo__oo
case 3:result+=1000;break;//oooo__ooo
default:
}
}
break;
case 5:break;
default:
}
return result;
}
function isGameOver(row, col, flag, data) {
var count = 1;
//上下
for (var i = row - 1; i >= 0; i--) {
if (data[i][col] == flag) {
count++;
} else {
break;
}
}
for (var i = row + 1; i < 15; i++) {
if (data[i][col] == flag) {
count++;
} else {
break;
}
}
if (count == 5) {
return true;
}
//左右
count = 1;
for (var j = col - 1; j >= 0; j--) {
if (data[row][j] == flag) {
count++;
} else {
break;
}
}
for (var j = col + 1; j < 15; j++) {
if (data[row][j] == flag) {
count++;
} else {
break;
}
}
if (count == 5) {
return true;
}
//从左上到右下
count = 1;
for (var i = row - 1, j = col - 1; i >= 0 && j >= 0; i-- , j--) {
if (data[i][j] == flag) {
count++;
} else {
break;
}
}
for (var i = row + 1, j = col + 1; i < 15 && j < 15; i++ , j++) {
if (data[i][j] == flag) {
count++;
} else {
break;
}
}
if (count == 5) {
return true;
}
//从右上到左下
count = 1;
for (var i = row - 1, j = col + 1; i >= 0 && j < 15; i-- , j++) {
if (data[i][j] == flag) {
count++;
} else {
break;
}
}
for (var i = row + 1, j = col - 1; i < 15 && j >= 0; i++ , j--) {
if (data[i][j] == flag) {
count++;
} else {
break;
}
}
if (count == 5) {
return true;
}
return false;
}
server.listen(3000);
console.log("服务器开启!");
最后
以上就是乐观小懒猪为你收集整理的NodeJS+html5+css3 带人机对战和人人对战的 五子棋的全部内容,希望文章能够帮你解决NodeJS+html5+css3 带人机对战和人人对战的 五子棋所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复