设计一个麻将游戏 java 代码
时间: 2023-10-30 19:07:47 浏览: 259
Mahjong.rar_游戏_Java_
这是一个较为复杂的项目,需要考虑到游戏规则、图形界面、玩家互动等多方面。以下是一个简单的麻将游戏Java代码示例:
```java
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
public class MahjongGame {
private List<String> tiles; // 所有麻将牌
private List<String> player1; // 玩家1的手牌
private List<String> player2; // 玩家2的手牌
private List<String> player3; // 玩家3的手牌
private List<String> player4; // 玩家4的手牌
private int round; // 第几局
public MahjongGame() {
initTiles();
dealTiles();
}
// 初始化麻将牌
private void initTiles() {
tiles = new ArrayList<>();
// 添加万、条、筒
for (int i = 0; i < 3; i++) {
for (int j = 1; j <= 9; j++) {
for (int k = 0; k < 4; k++) {
tiles.add((i + 1) + "" + (j) + "");
}
}
}
// 添加东、南、西、北、中、发、白
for (int i = 1; i <= 7; i++) {
for (int j = 0; j < 4; j++) {
tiles.add((i + 3) + "");
}
}
// 打乱麻将牌
Collections.shuffle(tiles);
}
// 发牌
private void dealTiles() {
player1 = new ArrayList<>();
player2 = new ArrayList<>();
player3 = new ArrayList<>();
player4 = new ArrayList<>();
for (int i = 0; i < 13; i++) {
player1.add(tiles.remove(0));
player2.add(tiles.remove(0));
player3.add(tiles.remove(0));
player4.add(tiles.remove(0));
}
}
// 开始游戏
public void start() {
round = 1;
while (round <= 4) {
System.out.println("第" + round + "局开始!");
playRound();
round++;
}
System.out.println("游戏结束!");
}
// 进行一局游戏
private void playRound() {
int dealer = (round - 1) % 4 + 1; // 庄家
int winner = -1; // 胜者
int drawCount = 0; // 和牌次数
boolean end = false; // 结束标志
List<String> tilesInWall = new ArrayList<>(tiles); // 剩余麻将牌
List<String> discardPile = new ArrayList<>(); // 打出的牌
System.out.println("庄家是玩家" + dealer + "。");
// 玩家1开始摸牌
int currentPlayer = dealer % 4 + 1; // 当前玩家
while (!end) {
String tile = tilesInWall.remove(0); // 摸牌
switch (currentPlayer) {
case 1:
player1.add(tile);
System.out.println("玩家1摸了一张牌:" + tile);
break;
case 2:
player2.add(tile);
System.out.println("玩家2摸了一张牌:" + tile);
break;
case 3:
player3.add(tile);
System.out.println("玩家3摸了一张牌:" + tile);
break;
case 4:
player4.add(tile);
System.out.println("玩家4摸了一张牌:" + tile);
break;
}
// 判断是否和牌或者打牌
if (isWin(currentPlayer, tile)) {
winner = currentPlayer;
System.out.println("玩家" + winner + "和牌!");
end = true;
} else {
String discard = discard(currentPlayer); // 打牌
discardPile.add(discard);
System.out.println("玩家" + currentPlayer + "打出了一张牌:" + discard);
currentPlayer = currentPlayer % 4 + 1;
// 判断是否流局
if (tilesInWall.size() == 0) {
drawCount++;
if (drawCount >= 4) {
end = true;
}
}
}
}
// 结算分数
if (winner != -1) {
System.out.println("本局胜者是玩家" + winner + "。");
} else {
System.out.println("本局流局。");
}
}
// 判断是否和牌
private boolean isWin(int player, String tile) {
List<String> tiles = new ArrayList<>();
switch (player) {
case 1:
tiles.addAll(player1);
break;
case 2:
tiles.addAll(player2);
break;
case 3:
tiles.addAll(player3);
break;
case 4:
tiles.addAll(player4);
break;
}
tiles.add(tile);
// TODO: 判断是否和牌
return false;
}
// 打牌
private String discard(int player) {
List<String> tiles = new ArrayList<>();
switch (player) {
case 1:
tiles.addAll(player1);
break;
case 2:
tiles.addAll(player2);
break;
case 3:
tiles.addAll(player3);
break;
case 4:
tiles.addAll(player4);
break;
}
Scanner scanner = new Scanner(System.in);
System.out.println("请玩家" + player + "选择要打出的一张牌:");
// 打印玩家的手牌
for (int i = 0; i < tiles.size(); i++) {
System.out.print((i + 1) + ":" + tiles.get(i) + "\t");
}
System.out.println();
int index = scanner.nextInt() - 1;
while (index < 0 || index >= tiles.size()) {
System.out.println("输入有误,请重新输入:");
index = scanner.nextInt() - 1;
}
String discard = tiles.remove(index); // 打出选中的牌
switch (player) {
case 1:
player1 = tiles;
break;
case 2:
player2 = tiles;
break;
case 3:
player3 = tiles;
break;
case 4:
player4 = tiles;
break;
}
return discard;
}
}
```
这只是一个简单的麻将游戏Java代码示例,实际上还需要考虑很多其他因素,比如:玩家之间的交互、图形界面的设计等。
阅读全文