Home Web Front-end JS Tutorial Ideas for implementing the Landlord Game with JavaScript_javascript skills

Ideas for implementing the Landlord Game with JavaScript_javascript skills

May 16, 2016 pm 03:13 PM

The knowledge in this article will share with you the ideas of using js to write Landlords. Please forgive me if the code is not well written.

Here we will talk about the main functions of Doudizhu: shuffling, dealing, players playing cards, computer playing cards, verification of playing rules, there is no judgment about winning or losing, it just implements these main functions, the following are in order Let’s talk about the implementation of several functions:

1. Shuffle:

var pukes=this.manage.pukes;//存放扑克牌的数组
//洗牌
for(var i=;i<pukes.length;i++){
var tmp=pukes[i];
var index=util.random(i,pukes.length);//随机交换两张牌
pukes[i]=pukes[index];
pukes[index]=tmp;
} 
Copy after login

 2. Dealing the cards (The default version of the simplified version is that the player is the landlord and the computer is the farmer). Since the order of the cards has been disrupted during the previous shuffling, dealing the cards is just a simple loop of pukes Elements in are added to the pukes field in each player instance.

//发牌
var start=;
for(var i=;i<this.manage.pukes.length-;i++) {
if(start==this.manage.players.length){
start=;
}
this.manage.pukes[i].status=true;
this.manage.players[start].pukesLen++;
this.manage.players[start++].pukes.push(this.manage.pukes[i]);
}
for(var i=this.manage.pukes.length-;i<this.manage.pukes.length;i++){
//地主多三张
this.manage.pukes[i].status=true;
this.manage.players[this.manage.curPlayerIndex].pukesLen++;
this.manage.players[this.manage.curPlayerIndex].pukes.push(this.manage.pukes[i]);
}
Copy after login

 3. The player plays the card . The player plays the card in two steps: the computer and the player themselves. The computer plays the card is a very stupid way of playing the card (play as soon as there is a card):

//出牌
if(this.options.playerIndex==this.manage.curPlayerIndex) {
var spks = [],gz=false;
if (this.manage.curMaxPlayerIndex == this.options.playerIndex) {
this.manage.deskPukes = [];
}
if (this.isCompute) {
//电脑自动出牌
var start = ;
var len=this.manage.deskPukes.length||;
while (start < this.pukes.length) {
spks = [];
for (var i = ,j=start; i <len&&j<this.pukes.length; i++) {
//随便选一张 可以出就行
if(this.pukes[j].status) {
spks.push(this.pukes[j++]);
}
}
if(spks.length) {
if (rules.valids(spks, this.manage.deskPukes)) {
gz = true;
break;
}
}
start++;
}
}
else {
//玩家选择出牌
for (var i = ; i < this.pukes.length; i++) {
if (this.pukes[i].selected && this.pukes[i].status) {
spks.push(this.pukes[i]);
}
}
if (rules.valids(spks, this.manage.deskPukes)) {
gz=true;
}
else{
alert("出牌不符合规则!");
}
}
if(gz){
this.manage.curMaxPlayerIndex=this.options.playerIndex;
this.manage.deskPukes = [];
for (var i = ; i < spks.length; i++) {
this.pukesLen--;
this.manage.deskPukes.push(spks[i]);
spks[i].status = false;
}
}
this.manage.renderPukes();
this.manage.renderCurDiscard();
if(this.isCompute||gz) {
this.manage.nextPlayer();
}
}
else{
alert("没轮到你出牌!");
}
Copy after login

 4. Verification of the rules of playing cards, is a combination of many functions, and then called in a loop. If true is returned, the playing of cards complies with the rules:

//以下为出牌规则
var rules={
_rules:[
new danzRule(),
new duiRule(),
new sandRule(),
new zandRule(),
new shunzRule(),
new liandRule()
],
valids:function(_pukes,_curPukes){
for(var i=;i<this._rules.length;i++){
if(this._rules[i].valid(_pukes,_curPukes)){
return true;
}
}
return false;
}
};
function danzRule(){
//单张规则
}
danzRule.prototype.valid=function(_pukes,_curPukes){
//校验
var pukes=_pukes;//玩家的牌
var curPukes=_curPukes;//左面的牌
if(pukes&&pukes.length==){
//比较牌面值
if(!curPukes||!curPukes.length){
return true;
}
if(curPukes[].dians==&&pukes[].dians<){
//特殊处理
return false;
}
if(pukes[].dians==&&curPukes[].dians<){
//特殊处理
return true;
}
return pukes[].dians>curPukes[].dians;
}
return false;
}
function duiRule(_pukes,_curPukes){
//两张规则
}
duiRule.prototype.valid=function(_pukes,_curPukes){
//校验
var pukes=_pukes;//玩家的牌
var curPukes=_curPukes;//左面的牌
if(pukes&&pukes.length==){
//比较牌面值
if(pukes[].dians>&&pukes[].dians>){
return true;
}
if(pukes[].dians!=pukes[].dians){
return false;
}
if(!curPukes||!curPukes.length){
return true;
}else {
if(curPukes.length!=){
return false;
}
if (curPukes[].dians > && curPukes[].dians > ) {
return false;
}
if (curPukes[].dians != curPukes[].dians) {
return false;
}
if (curPukes[].dians == ) {
return false;
}
}
if(pukes[].dians==){
return true;
}
return pukes[].dians>curPukes[].dians;
}
return false;
}
function sandRule(){
//三带
}
sandRule.prototype.valid=function(_pukes,_curPukes){
//校验
var pukes=_pukes;//玩家的牌
var curPukes=_curPukes;//左面的牌
if(pukes&&(pukes.length>=)){
//比较牌面值
var books=getBooks(pukes);
if(!valid(books))return false;
if(!curPukes||!curPukes.length)return true;
if(curPukes.length!=books.length)return false;
var books=getBooks(curPukes);
if(!valid(books))return false;
return getSum(books)>getSum(books);
}
return false;
function getSum(books){
var sum=;
for(var i=;i<books.length;i++) {
if(books[i]==){
if(i==)return ;
sum+=i;
}
}
return sum;
}
function valid(books){
//验证三带是否有效
var counts= ,countsd= ,d=true,start=false,startIndex=-;
for(var i=;i<books.length;i++) {
if(start&&books[i]==&&startIndex!=(i-)){
return false;
}else{
startIndex=i;
}
if(books[i]==){
if(!start) {
start = true;
startIndex = i;
}
counts++;
}
if(books[i]==){
d=false;
}
}
for(var i=;i<books.length;i++) {
if(d&&books[i]==){
countsd++;
}
else if(!d&&books[i]==){
countsd++;
}
}
return counts>&&counts==countsd;
}
function getBooks(pukes){
//返回三带的每个点数的个数
var books=[];
for(var i=;i<pukes.length;i++){
if(!books[pukes[i].dians]){
books[pukes[i].dians]=;
}else{
books[pukes[i].dians]++;
}
}
return books;
}
}
function zandRule(){
//炸弹
}
zandRule.prototype.valid=function(_pukes,_curPukes){
var pukes=_pukes;//玩家的牌
var curPukes=_curPukes;//左面的牌
if(pukes&&pukes.length==) {
if(!allEqual(pukes)){
return false;
}
if(!curPukes||(curPukes.length>&&curPukes.length!=)||!allEqual(curPukes)){
return true;
}
else{
if(pukes[].dians==){
return true;
}
if(curPukes[].dians==){
return false;
}
return pukes[].dians>curPukes[].dians;
}
}
return false;
function allEqual(pukes){
if(!pukes||!pukes.length)return false;
var base=pukes[].dians;
for(var i=;i<pukes.length;i++){
if(base!=pukes[i].dians){
return false;
}
}
return true;
}
}
function liandRule(){
//连对
}
liandRule.prototype.valid=function(_pukes,_curPukes) {
var pukes=_pukes;//玩家的牌
var curPukes=_curPukes;//左面的牌
if(pukes&&pukes.length>=) {
if(!verificationCoherence(pukes)){
return false;
}
if(!curPukes||curPukes.length<=){
return true;
}
if(!verificationCoherence(curPukes)){
return false;
}
if(pukes.length!=curPukes.length){
return false;
}
return getSumDians(pukes)>getSumDians(curPukes);
}
return false;
function getSumDians(pukes){
var sum=;
for(var i=;i<pukes.length;i++) {
sum+=pukes[i].dians;
}
return sum;
}
function verificationCoherence(pukes){
//验证连贯性
if(!pukes||!pukes.length)return false;
var books=[];
for(var i=;i<pukes.length;i++){
if(pukes[i].dians==||pukes[i].dians>){
return false;
}
if(!books[pukes[i].dians]){
books[pukes[i].dians]=;
}else{
books[pukes[i].dians]++;
}
if(books[pukes[i].dians]>){
return false;
}
}
var start=false;
for(var i=;i<books.length;i++) {
if(books[i]&&books[i]!=){
return false;
}
if(books[i]==&&!start){
start=true;
}
if(start&&books[i]!=){
return false;
}
}
return true;
}
}
function shunzRule(){
//顺子
}
shunzRule.prototype.valid=function(_pukes,_curPukes){
var pukes=_pukes;//玩家的牌
var curPukes=_curPukes;//左面的牌
if(pukes&&pukes.length>=) {
if(!verificationCoherence(pukes)){
return false;
}
if(!curPukes||curPukes.length<=){
return true;
}
if(!verificationCoherence(curPukes)){
return false;
}
if(pukes.length!=curPukes.length){
return false;
}
return getSumDians(pukes)>getSumDians(curPukes);
}
return false;
function getSumDians(pukes){
var sum=;
for(var i=;i<pukes.length;i++) {
sum+=pukes[i].dians;
}
return sum;
}
function verificationCoherence(pukes){
//验证连贯性
if(!pukes||!pukes.length)return false;
var books=[];
for(var i=;i<pukes.length;i++){
if(pukes[i].dians==||pukes[i].dians>){
return false;
}
if(!books[pukes[i].dians]){
books[pukes[i].dians]=;
}else{
return false;
}
}
var start=false;
for(var i=;i<books.length;i++) {
if(books[i]==&&!start){
start=true;
}
if(start&&!books[i]){
return false;
}
}
return true;
}
}
Copy after login

The above 4 steps are what I think are the main 4 functions. Other functions, such as initialization, event registration, etc., are already commented in the source code. If they are not well written, please do not comment.

That’s all the ideas for implementing the Landlord game with JavaScript. I hope it will be helpful to you!

Statement of this Website
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

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Replace String Characters in JavaScript Replace String Characters in JavaScript Mar 11, 2025 am 12:07 AM

Detailed explanation of JavaScript string replacement method and FAQ This article will explore two ways to replace string characters in JavaScript: internal JavaScript code and internal HTML for web pages. Replace string inside JavaScript code The most direct way is to use the replace() method: str = str.replace("find","replace"); This method replaces only the first match. To replace all matches, use a regular expression and add the global flag g: str = str.replace(/fi

How do I create and publish my own JavaScript libraries? How do I create and publish my own JavaScript libraries? Mar 18, 2025 pm 03:12 PM

Article discusses creating, publishing, and maintaining JavaScript libraries, focusing on planning, development, testing, documentation, and promotion strategies.

How do I optimize JavaScript code for performance in the browser? How do I optimize JavaScript code for performance in the browser? Mar 18, 2025 pm 03:14 PM

The article discusses strategies for optimizing JavaScript performance in browsers, focusing on reducing execution time and minimizing impact on page load speed.

How do I debug JavaScript code effectively using browser developer tools? How do I debug JavaScript code effectively using browser developer tools? Mar 18, 2025 pm 03:16 PM

The article discusses effective JavaScript debugging using browser developer tools, focusing on setting breakpoints, using the console, and analyzing performance.

jQuery Matrix Effects jQuery Matrix Effects Mar 10, 2025 am 12:52 AM

Bring matrix movie effects to your page! This is a cool jQuery plugin based on the famous movie "The Matrix". The plugin simulates the classic green character effects in the movie, and just select a picture and the plugin will convert it into a matrix-style picture filled with numeric characters. Come and try it, it's very interesting! How it works The plugin loads the image onto the canvas and reads the pixel and color values: data = ctx.getImageData(x, y, settings.grainSize, settings.grainSize).data The plugin cleverly reads the rectangular area of ​​the picture and uses jQuery to calculate the average color of each area. Then, use

How to Build a Simple jQuery Slider How to Build a Simple jQuery Slider Mar 11, 2025 am 12:19 AM

This article will guide you to create a simple picture carousel using the jQuery library. We will use the bxSlider library, which is built on jQuery and provides many configuration options to set up the carousel. Nowadays, picture carousel has become a must-have feature on the website - one picture is better than a thousand words! After deciding to use the picture carousel, the next question is how to create it. First, you need to collect high-quality, high-resolution pictures. Next, you need to create a picture carousel using HTML and some JavaScript code. There are many libraries on the web that can help you create carousels in different ways. We will use the open source bxSlider library. The bxSlider library supports responsive design, so the carousel built with this library can be adapted to any

Enhancing Structural Markup with JavaScript Enhancing Structural Markup with JavaScript Mar 10, 2025 am 12:18 AM

Key Points Enhanced structured tagging with JavaScript can significantly improve the accessibility and maintainability of web page content while reducing file size. JavaScript can be effectively used to dynamically add functionality to HTML elements, such as using the cite attribute to automatically insert reference links into block references. Integrating JavaScript with structured tags allows you to create dynamic user interfaces, such as tab panels that do not require page refresh. It is crucial to ensure that JavaScript enhancements do not hinder the basic functionality of web pages; even if JavaScript is disabled, the page should remain functional. Advanced JavaScript technology can be used (

How to Upload and Download CSV Files With Angular How to Upload and Download CSV Files With Angular Mar 10, 2025 am 01:01 AM

Data sets are extremely essential in building API models and various business processes. This is why importing and exporting CSV is an often-needed functionality.In this tutorial, you will learn how to download and import a CSV file within an Angular

See all articles