search
HomeWeb Front-endH5 TutorialSample code sharing for the HTML5 web version of the black and white backgammon game

I have nothing to do, so I used H5 to play a few small games. Of course, I am just a novice, so I just want to play with them. Don’t blame me if you are a master.
1. HTML5 web version of the black and white backgammon game code, please download the source code appendix!

Part of the front-end code:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>五子棋</title>
<style type="text/css">
*{
margin:0;
padding:0;
}
.gobang{
margin:10px auto;
width:642px;
height: 642px;
/*border:1px solid;*/
background: url(picture/bak.jpg);
overflow: hidden;
}
.text{
margin:0 auto;
width:100px;
height:40px;
text-align: center;
color:#f00;
border:1px solid red;
line-height: 40px;
display: block;
}
#can{
margin:0px auto;
border:1px solid green;
display: block;
}
</style>
</head>
<body>
<canvas>PK</canvas>
<div>

<canvas id="can" width="640" height="640">
您的浏览器不支持canvas
</canvas>
</div>
<script>
var text = document.getElementsByClassName(&#39;text&#39;);



//定义二维数组作为棋盘
var maps = new Array(16);
var len = maps.length;
// alert(len)
for(var i=0;i<len;i++){
maps[i] = new Array();
for(var j = 0;j<len;j++){
maps[i][j] = 0;
// console.log(maps[i][j]);
}
}

//初始化棋子
var black = new Image();
var white = new Image();
var clientWidth = document.documentElement.clientWidth;
black.src = "picture/black.png";
white.src = "picture/white.png";
//棋盘初始化
var can = document.getElementById(&#39;can&#39;);
var ctx = can.getContext("2d");  //获取该canvas的2D绘图环境对象
ctx.strokeStyle = "#333";
for(var m=0;m<len-1;m++){
for(var n=0;n<len-1;n++){
ctx.strokeRect(m*40+20,n*40+20,40,40);  //绘制40的小正方形
}
}
//绘制文字
var can1 = document.getElementsByClassName(&#39;text&#39;);
var ctx1 = can1[0].getContext("2d");

ctx1.beginPath();
ctx1.font=("100px Georgia");
ctx1.fillStyle="#F70707";
// ctx1.fillText("Hello",40,100);

var isBlack = true;
//下子
can.onclick=function play(e){
// alert(e.clientX);
//获取棋盘偏移量
var l = this.offsetLeft+20;
var t = this.offsetTop+20;
//获取点击相对棋盘坐标
var x =e.clientX - l;
var y = e.clientY -t;
// alert(x);
var row,col,index = 0;

if(x%40 < 20){
col = parseInt(x/40);
}else{
col = parseInt(x/40)+1;
}
row = y%40<20 ? parseInt(y/40) : parseInt(y/40)+1;
// alert(row+"行"+col+"行");  //第几列行第几列

if(maps[row][col]===0){
if(isBlack){
ctx.drawImage(black,col*40,row*40);   //下黑子
isBlack = false;
maps[row][col] = 2; //黑子为2
iswin(2,row,col);
}else{
ctx.drawImage(white,col*40,row*40);
isBlack = true;
maps[row][col] = 1; //白子为1
iswin(1,row,col);
}
}

function iswin(t,row,col){
var orgrow,orgcol,total;
reset();
// alert(total);

//判断每行是否有五个
while(col>0 &&maps[row][col-1]==t){  //当前子左边还有
total++;
col--;

};
row = orgrow;
col = orgcol;
while(col+1<16 &&maps[row][col+1]==t){  //当前子右边还有
col++;
total++;
};
// alert(total);
celebrate();

//判断每列是否有五个
reset();

while(row>0&&maps[row-1][col]==t){   //当前子上面还有
total++;
row--;
}
row = orgrow;
col = orgcol;
while(row+1<16&&maps[row+1][col]==t){  //下面
total++;
row++;
}
celebrate();

//左上 右下有没有五个
reset();
while(row>0&&col>0&&maps[row-1][col-1]==t){ //左上1
row--;
col--;
total++;
}
row = orgrow;
col = orgcol;
while(row+1<16&&col+1<16&&maps[row+1][col+1]==t){  //右下1
row++;
col++;
total++;
}
// alert(total)
celebrate();

//左下 右上有没有五个
reset();
// alert(total);
while(row>0&&col+1<16&&maps[row-1][col+1]==t){  //右上
row--;
col++;
total++;
}
row = orgrow;
col = orgcol;
while(row+1<16&&col>0&&maps[row+1][col-1]==t){   //左下
row++;
col--;
total++;
}
// alert(total);
celebrate();

function celebrate(){       //显示哪边赢
if(total>=5){
if(t==1){
// alert("白子赢");
// text[0].innerHTML="白子赢";
// cxt1.clearRect(0,0,can1.width,can1.height);
ctx1.clearRect(0,0,can1[0].width,can1[0].height);
ctx1.fillText("白子赢",0,100);
}else{
// alert("黑子赢");
// text[0].innerHTML="黑子赢";
// cxt1.clearRect(0,0,can1.width,can1.height);
ctx1.clearRect(0,0,can1[0].width,can1[0].height);
ctx1.fillText("黑子赢",0,100);
}
}
}
function reset(){
orgrow = row;
orgcol = col;
total = 1;
}
}

}
</script>

</body>
</html>

The above is the detailed content of Sample code sharing for the HTML5 web version of the black and white backgammon game. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Mastering Microdata: A Step-by-Step Guide for HTML5Mastering Microdata: A Step-by-Step Guide for HTML5May 14, 2025 am 12:07 AM

MicrodatainHTML5enhancesSEOanduserexperiencebyprovidingstructureddatatosearchengines.1)Useitemscope,itemtype,anditempropattributestomarkupcontentlikeproductsorevents.2)TestmicrodatawithtoolslikeGoogle'sStructuredDataTestingTool.3)ConsiderusingJSON-LD

What's New in HTML5 Forms? Exploring the New Input TypesWhat's New in HTML5 Forms? Exploring the New Input TypesMay 13, 2025 pm 03:45 PM

HTML5introducesnewinputtypesthatenhanceuserexperience,simplifydevelopment,andimproveaccessibility.1)automaticallyvalidatesemailformat.2)optimizesformobilewithanumerickeypad.3)andsimplifydateandtimeinputs,reducingtheneedforcustomsolutions.

Understanding H5: The Meaning and SignificanceUnderstanding H5: The Meaning and SignificanceMay 11, 2025 am 12:19 AM

H5 is HTML5, the fifth version of HTML. HTML5 improves the expressiveness and interactivity of web pages, introduces new features such as semantic tags, multimedia support, offline storage and Canvas drawing, and promotes the development of Web technology.

H5: Accessibility and Web Standards ComplianceH5: Accessibility and Web Standards ComplianceMay 10, 2025 am 12:21 AM

Accessibility and compliance with network standards are essential to the website. 1) Accessibility ensures that all users have equal access to the website, 2) Network standards follow to improve accessibility and consistency of the website, 3) Accessibility requires the use of semantic HTML, keyboard navigation, color contrast and alternative text, 4) Following these principles is not only a moral and legal requirement, but also amplifying user base.

What is the H5 tag in HTML?What is the H5 tag in HTML?May 09, 2025 am 12:11 AM

The H5 tag in HTML is a fifth-level title that is used to tag smaller titles or sub-titles. 1) The H5 tag helps refine content hierarchy and improve readability and SEO. 2) Combined with CSS, you can customize the style to enhance the visual effect. 3) Use H5 tags reasonably to avoid abuse and ensure the logical content structure.

H5 Code: A Beginner's Guide to Web StructureH5 Code: A Beginner's Guide to Web StructureMay 08, 2025 am 12:15 AM

The methods of building a website in HTML5 include: 1. Use semantic tags to define the web page structure, such as, , etc.; 2. Embed multimedia content, use and tags; 3. Apply advanced functions such as form verification and local storage. Through these steps, you can create a modern web page with clear structure and rich features.

H5 Code Structure: Organizing Content for ReadabilityH5 Code Structure: Organizing Content for ReadabilityMay 07, 2025 am 12:06 AM

A reasonable H5 code structure allows the page to stand out among a lot of content. 1) Use semantic labels such as, etc. to organize content to make the structure clear. 2) Control the rendering effect of pages on different devices through CSS layout such as Flexbox or Grid. 3) Implement responsive design to ensure that the page adapts to different screen sizes.

H5 vs. Older HTML Versions: A ComparisonH5 vs. Older HTML Versions: A ComparisonMay 06, 2025 am 12:09 AM

The main differences between HTML5 (H5) and older versions of HTML include: 1) H5 introduces semantic tags, 2) supports multimedia content, and 3) provides offline storage functions. H5 enhances the functionality and expressiveness of web pages through new tags and APIs, such as and tags, improving user experience and SEO effects, but need to pay attention to compatibility issues.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software