用JS写一个斗地主游戏
时间: 2023-12-22 09:06:06 浏览: 129
斗地主是一款经典的扑克牌游戏,本着学以致用的原则,以下是一个用JavaScript实现的简单斗地主游戏,供参考:
HTML:
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>斗地主</title>
<style>
* {
margin: 0;
padding: 0;
}
.container {
margin: 20px auto;
width: 900px;
height: 500px;
background-color: #F0E68C;
border: 1px solid #000;
position: relative;
}
.player {
position: absolute;
bottom: 0;
width: 300px;
height: 100px;
background-color: #FFF;
border: 1px solid #000;
}
.player .cards {
position: absolute;
bottom: 10px;
left: 10px;
}
.player .button {
position: absolute;
bottom: 10px;
right: 10px;
}
.card {
width: 50px;
height: 75px;
background-color: #FFF;
border: 1px solid #000;
position: relative;
margin-right: -25px;
float: left;
cursor: pointer;
}
.card img {
position: absolute;
top: 5px;
left: 5px;
width: 40px;
height: 60px;
}
.card.selected {
border-color: #F00;
}
.button {
display: inline-block;
margin-left: 10px;
padding: 5px 10px;
background-color: #FFF;
border: 1px solid #000;
cursor: pointer;
}
</style>
</head>
<body>
<div class="container">
<div class="player" id="player">
<div class="cards"></div>
<div class="button">
<span id="play-btn" class="button">出牌</span>
<span id="pass-btn" class="button">不出</span>
</div>
</div>
</div>
<script src="doudizhu.js"></script>
</body>
</html>
```
JavaScript:
```javascript
// 扑克牌花色
var suits = ["spades", "hearts", "diamonds", "clubs"];
// 扑克牌点数
var ranks = [
{name: "3", value: 3},
{name: "4", value: 4},
{name: "5", value: 5},
{name: "6", value: 6},
{name: "7", value: 7},
{name: "8", value: 8},
{name: "9", value: 9},
{name: "10", value: 10},
{name: "J", value: 11},
{name: "Q", value: 12},
{name: "K", value: 13},
{name: "A", value: 14},
{name: "2", value: 15},
{name: "joker", value: 16},
{name: "JOKER", value: 17}
];
// 扑克牌
var deck = [];
// 玩家手牌
var playerCards = [];
// 选中的牌
var selectedCards = [];
// 出牌区域
var playArea = [];
// 当前出牌玩家
var currentPlayer = 0;
// 初始化扑克牌
function initDeck() {
for (var i = 0; i < suits.length; i++) {
for (var j = 0; j < ranks.length; j++) {
deck.push({
suit: suits[i],
rank: ranks[j],
selected: false
});
}
}
deck.push({
rank: ranks[13],
selected: false
});
deck.push({
rank: ranks[14],
selected: false
});
}
// 洗牌
function shuffleDeck() {
for (var i = 0; i < deck.length; i++) {
var j = Math.floor(Math.random() * deck.length);
var temp = deck[i];
deck[i] = deck[j];
deck[j] = temp;
}
}
// 发牌
function dealCards() {
playerCards = [];
for (var i = 0; i < 3; i++) {
playerCards.push([deck[i], deck[i + 3], deck[i + 6], deck[i + 9], deck[i + 12], deck[i + 15], deck[i + 18], deck[i + 21], deck[i + 24]]);
}
}
// 排序
function sortCards(cards) {
cards.sort(function (a, b) {
return a.rank.value - b.rank.value;
});
}
// 显示玩家手牌
function showPlayerCards() {
var cardsDiv = document.querySelector("#player .cards");
cardsDiv.innerHTML = "";
for (var i = 0; i < playerCards[currentPlayer].length; i++) {
var card = playerCards[currentPlayer][i];
var cardDiv = document.createElement("div");
cardDiv.setAttribute("class", "card");
cardDiv.setAttribute("data-value", card.rank.value);
if (card.selected) {
cardDiv.setAttribute("class", "card selected");
}
var cardImg = document.createElement("img");
cardImg.setAttribute("src", "images/" + card.suit + "_" + card.rank.name + ".png");
cardDiv.appendChild(cardImg);
cardsDiv.appendChild(cardDiv);
cardDiv.addEventListener("click", function (event) {
var cardDiv = event.target.parentNode;
var value = cardDiv.getAttribute("data-value");
var selected = cardDiv.getAttribute("class").indexOf("selected") >= 0;
if (selected) {
cardDiv.setAttribute("class", "card");
for (var j = 0; j < selectedCards.length; j++) {
if (selectedCards[j].rank.value == value) {
selectedCards.splice(j, 1);
break;
}
}
} else {
cardDiv.setAttribute("class", "card selected");
for (var j = 0; j < playerCards[currentPlayer].length; j++) {
if (playerCards[currentPlayer][j].rank.value == value) {
selectedCards.push(playerCards[currentPlayer][j]);
break;
}
}
}
});
}
}
// 清空选中的牌
function clearSelectedCards() {
selectedCards = [];
var cardsDiv = document.querySelectorAll("#player .cards .card");
for (var i = 0; i < cardsDiv.length; i++) {
cardsDiv[i].setAttribute("class", "card");
}
}
// 出牌
function playCards() {
if (selectedCards.length == 0) {
alert("请选择要出的牌!");
return;
}
if (!isValid() && playArea.length > 0) {
alert("出牌不符合规则!");
return;
}
for (var i = 0; i < selectedCards.length; i++) {
for (var j = 0; j < playerCards[currentPlayer].length; j++) {
if (playerCards[currentPlayer][j].rank.value == selectedCards[i].rank.value) {
playerCards[currentPlayer].splice(j, 1);
break;
}
}
}
selectedCards = [];
playArea = playArea.concat(selectedCards);
if (playerCards[currentPlayer].length == 0) {
alert("恭喜你获得胜利!");
return;
}
currentPlayer = (currentPlayer + 1) % playerCards.length;
showPlayerCards();
clearSelectedCards();
}
// 不出
function pass() {
currentPlayer = (currentPlayer + 1) % playerCards.length;
showPlayerCards();
clearSelectedCards();
}
// 判断出牌是否符合规则
function isValid() {
return true;
}
// 初始化游戏
function initGame() {
initDeck();
shuffleDeck();
dealCards();
for (var i = 0; i < playerCards.length; i++) {
sortCards(playerCards[i]);
}
showPlayerCards();
}
// 开始游戏
function startGame() {
initGame();
var playBtn = document.querySelector("#play-btn");
playBtn.addEventListener("click", function () {
playCards();
});
var passBtn = document.querySelector("#pass-btn");
passBtn.addEventListener("click", function () {
pass();
});
}
startGame();
```
以上代码实现了斗地主游戏的基本功能,包括洗牌、发牌、排序、出牌、不出等操作,但是还有很多细节需要完善,比如如何判断出牌是否符合规则、如何实现机器人出牌等等,读者可以在此基础上进行扩展。
阅读全文