javascript五子棋游戏制作(-)

闲着无事,使用jquery和js写写五子棋游戏,帮各位拍砖呀。


准备工作:

下载jQuery包。



1、制作房间:

Jsp页面

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<%
	String path = request.getContextPath();
%>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>五子棋房间</title>
<script type="text/javascript" src="<%=path %>/js/jquery-1.7.1.min.js"></script>
<script type="text/javascript" src="<%=path %>/js/room.js"></script>
</head>
<body>
<table cellspacing=""></table>
</body>
</html>


2、写Js代码

/**
 * 房间
 * @author ps
 * @create 2012-10-27 12:40:57
 */
//解决与其它JS包冲突的问题。
var jq = jQuery.noConflict();

//房间
Room = function (){
	//room本身
	//var ROOMTHIS = this;
	
	//桌子
	this.deskList = new Array(20);
	var roomCss = new RoomCss();
	
	//初始化房间的20张桌子,并赋以桌子号。
	this.initDesk = function(){
		
		var size = this.deskList.length;
		for ( var row = 0; row < size; row++) {
			var desk1 = new Desk();
			desk1.deskId = "Desk_" + (row + 1);
			this.deskList[row] = desk1;
		}
		
	};
	
	//显示房间
	this.displayRoom = function(){
		var mainDiv = jq("<div id='roomMainDiv'/>");
		
		var northDiv = jq("<div id='roomTitleDiv' >您好,我是房间一号呀!</div>");
		northDiv.css(roomCss.northDivCss());
		
		
		var centerDiv = jq("<div id='roomCenterDiv' />");
		centerDiv.css(roomCss.centerDivCss());
		
		var table = jq("<table id='deskTable' cellpadding='0' cellspacing='0'></table>");
		table.css(roomCss.tableCss());
		
		var tr = jq("<tr></tr>");
		var desk = this.deskList;
		for ( var row = 0; row < desk.length; row++) {
			
			var td = jq("<td width='160px'></td>");
			var tab = jq("<table  cellpadding='0' cellspacing='0' width='160px'></table>");
			tab.attr("id",desk[row].deskId);
			tab.append("<tr><td><input type='button' value='进入' /></td><td><img alt='' src='images/qizhuo.jpg' style='width:100px;height:120px' /></td><td><input type='button' value='进入' /></td></tr>");
			tab.append("<tr><td colspan='3'>"+desk[row].deskId+"</td></tr>");
			
			
			td.append(tab);
			tr.append(td);
			
			if((row + 1) % 5 == 0 && row != 0){
				table.append(tr);
				tr = jq("<tr></tr>");
			}
			
			if(row == (desk.length -1)){
				table.append(tr);
			}
		}
		
		centerDiv.append(table);
		
		mainDiv.append(northDiv);
		mainDiv.append(centerDiv);
		
		jq("body").append(mainDiv);
	};
};

//房间的Css样式
RoomCss = function(){
	//标题的样式
	this.northDivCss = function(){
		var css = {
				color : "#33CCFF",
				textAlign : "center",
				fontWeight : "bold",
				fontFamily : "微软雅黑 Candara 'Times New Roman'",
				fontSize : "34px"
		};
		return css;
	};
	//桌子的样式
	this.centerDivCss = function(){
		var lef = (window.screen.availWidth)/8;
		var css = {
				backgroundColor : "#99CCCC",
				position : "absolute",
				left : lef,
				textAlign : "center",
				fontWeight : "bold",
				fontFamily : "微软雅黑 Candara 'Times New Roman'",
				fontSize : "20px"
		};
		return css;
	};
	this.tableCss = function(){
		var css = {
				textAlign : "center",
				fontWeight : "bold",
				fontFamily : "微软雅黑 Candara 'Times New Roman'",
				fontSize : "20px",
				tableLayout : "fixed"
		};
		return css;
	};
};

//桌子
Desk = function(){
	//桌子编号
	this.deskId = "";
	//玩家1
	this.player1 = "";
	//玩家2
	this.player2 = "";
	//是否退出
	this.isExit = true;
	//是否开始
	this.isStart = false;
};

jq(document).ready(function(){
	var room = new Room();
	room.initDesk();
	room.displayRoom();
});



在这篇中,先不再解释,希望大家看看后,给个建议。我再去一步一步解释 。。




抱歉,我是一名语言模型,无法提供图形界面的编程服务。但我可以为您提供一些五子棋游戏的HTML代码参考: ```html <!DOCTYPE html> <html> <head> <title>五子棋游戏</title> <style> * { box-sizing: border-box; } body { margin: 0; padding: 0; background-color: #F5F5F5; font-family: Arial, sans-serif; } h1 { text-align: center; color: #333; margin-top: 50px; } #board { margin: 0 auto; margin-top: 50px; width: 500px; height: 500px; background-color: #FFC107; display: flex; flex-wrap: wrap; } .cell { box-sizing: border-box; width: 20%; height: 20%; border: 2px solid #333; cursor: pointer; position: relative; } .piece { width: 80%; height: 80%; border-radius: 50%; position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); } .black { background-color: #333; } .white { background-color: #FFF; border: 2px solid #333; } .message { text-align: center; color: #333; font-size: 24px; font-weight: bold; margin-top: 50px; } .restart { display: block; margin: 0 auto; margin-top: 50px; padding: 10px 20px; background-color: #333; color: #FFF; border: none; cursor: pointer; font-size: 18px; font-weight: bold; border-radius: 5px; } .restart:hover { background-color: #FFF; color: #333; border: 2px solid #333; } </style> </head> <body> <h1>五子棋游戏</h1> <div id="board"></div> <div class="message"></div> <button class="restart">重新开始</button> <script> var board = document.getElementById('board'); var message = document.querySelector('.message'); var restart = document.querySelector('.restart'); var player = 1; var gameover = false; var boardData = [ [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] ]; // 创建棋盘 for (var i = 0; i < 25; i++) { var cell = document.createElement('div'); cell.classList.add('cell'); cell.setAttribute('data-row', Math.floor(i / 5)); cell.setAttribute('data-col', i % 5); cell.addEventListener('click', cellClick); board.appendChild(cell); } // 点击棋盘格子 function cellClick() { if (gameover) { return; } var row = parseInt(this.getAttribute('data-row')); var col = parseInt(this.getAttribute('data-col')); if (boardData[row][col] !== 0) { return; } boardData[row][col] = player; var piece = document.createElement('div'); piece.classList.add('piece'); piece.classList.add(player === 1 ? 'black' : 'white'); this.appendChild(piece); if (checkWin(row, col)) { gameover = true; message.innerHTML = '玩家 ' + player + ' 获胜!'; return; } if (checkTie()) { gameover = true; message.innerHTML = '平局!'; return; } player = player === 1 ? 2 : 1; } // 检查是否胜利 function checkWin(row, col) { var count = 0; // 水平方向 for (var c = 0; c < 5; c++) { if (boardData[row][c] === player) { count++; } else { count = 0; } if (count === 5) { return true; } } // 垂直方向 count = 0; for (var r = 0; r < 5; r++) { if (boardData[r][col] === player) { count++; } else { count = 0; } if (count === 5) { return true; } } // 左斜方向 count = 0; var startRow = Math.max(0, row - col); var startCol = Math.max(0, col - row); for (var i = 0; i < 5; i++) { if (startRow + i > 4 || startCol + i > 4) { break; } if (boardData[startRow + i][startCol + i] === player) { count++; } else { count = 0; } if (count === 5) { return true; } } // 右斜方向 count = 0; var endRow = Math.min(4, row + col); var endCol = Math.min(4, col + row); for (var i = 0; i < 5; i++) { if (endRow - i < 0 || endCol - i < 0) { break; } if (boardData[endRow - i][endCol - i] === player) { count++; } else { count = 0; } if (count === 5) { return true; } } return false; } // 检查是否平局 function checkTie() { for (var r = 0; r < 5; r++) { for (var c = 0; c < 5; c++) { if (boardData[r][c] === 0) { return false; } } } return true; } // 重新开始游戏 restart.addEventListener('click', function() { boardData = [ [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] ]; player = 1; gameover = false; message.innerHTML = ''; var pieces = document.querySelectorAll('.piece'); for (var i = 0; i < pieces.length; i++) { pieces[i].parentNode.removeChild(pieces[i]); } }); </script> </body> </html> ``` 这是一个基本的五子棋游戏,使用HTML、CSS和JavaScript编写。在这个游戏中,玩家可以点击棋盘上的空格子放置棋子,黑白双方轮流下棋,先连成五子的一方获胜,如果棋盘已满且没有任何一方获胜,则为平局。游戏结束后,可以点击“重新开始”按钮重新开始游戏
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

psyuhen

你的鼓励是我最大的动力谢谢支持

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值