用JAVA写一个扑克牌游戏——金钩钓鱼
时间: 2023-07-06 19:13:46 浏览: 232
金钩钓鱼是一种常见的扑克牌游戏,使用Java语言编写此游戏可以让你更好地学习和理解Java编程语言。下面是一个简单的实现,供你参考:
首先,我们需要定义一个Card类来表示扑克牌:
```java
public class Card {
private String suit; // 花色
private String rank; // 点数
public Card(String suit, String rank) {
this.suit = suit;
this.rank = rank;
}
public String getSuit() {
return suit;
}
public String getRank() {
return rank;
}
public String toString() {
return suit + rank;
}
}
```
然后,我们需要定义一个Deck类来表示一副扑克牌:
```java
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class Deck {
private List<Card> cards;
public Deck() {
String[] suits = {"♠", "♥", "♦", "♣"};
String[] ranks = {"A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"};
cards = new ArrayList<Card>();
for (String suit : suits) {
for (String rank : ranks) {
Card card = new Card(suit, rank);
cards.add(card);
}
}
}
public void shuffle() {
Collections.shuffle(cards);
}
public Card draw() {
if (cards.isEmpty()) {
return null;
}
return cards.remove(0);
}
}
```
接下来,我们需要定义一个Player类来表示玩家:
```java
import java.util.ArrayList;
import java.util.List;
public class Player {
private String name;
private List<Card> hand;
public Player(String name) {
this.name = name;
hand = new ArrayList<Card>();
}
public String getName() {
return name;
}
public List<Card> getHand() {
return hand;
}
public boolean hasCard(Card card) {
return hand.contains(card);
}
public void addCard(Card card) {
hand.add(card);
}
public void removeCard(Card card) {
hand.remove(card);
}
public void printHand() {
System.out.print(name + "的手牌:");
for (Card card : hand) {
System.out.print(card.toString() + " ");
}
System.out.println();
}
}
```
最后,我们需要定义一个Game类来实现游戏逻辑:
```java
import java.util.Scanner;
public class Game {
private Deck deck;
private Player[] players;
private int currentPlayerIndex;
private boolean isEnd;
public Game() {
deck = new Deck();
players = new Player[4];
for (int i = 0; i < 4; i++) {
players[i] = new Player("玩家" + (i + 1));
}
currentPlayerIndex = 0;
isEnd = false;
}
public void start() {
deck.shuffle();
dealCards();
while (!isEnd) {
Player currentPlayer = players[currentPlayerIndex];
currentPlayer.printHand();
Card cardToAsk = askCard();
Player playerToAsk = choosePlayer(currentPlayer);
if (playerToAsk.hasCard(cardToAsk)) {
System.out.println(playerToAsk.getName() + "有" + cardToAsk.toString() + "," + currentPlayer.getName() + "获得此牌!");
playerToAsk.removeCard(cardToAsk);
currentPlayer.addCard(cardToAsk);
if (playerToAsk.getHand().isEmpty()) {
System.out.println(playerToAsk.getName() + "的手牌已经出完,游戏结束!");
isEnd = true;
}
} else {
System.out.println(playerToAsk.getName() + "没有" + cardToAsk.toString() + "," + currentPlayer.getName() + "摸一张牌!");
Card newCard = deck.draw();
if (newCard != null) {
currentPlayer.addCard(newCard);
} else {
System.out.println("牌已经发完了,游戏结束!");
isEnd = true;
}
}
nextPlayer();
}
}
private void dealCards() {
for (int i = 0; i < 13; i++) {
for (int j = 0; j < 4; j++) {
Card card = deck.draw();
players[j].addCard(card);
}
}
}
private Card askCard() {
Scanner scanner = new Scanner(System.in);
System.out.print(players[currentPlayerIndex].getName() + "请问要要哪张牌?");
String cardString = scanner.nextLine();
String suit = cardString.substring(0, 1);
String rank = cardString.substring(1);
Card card = new Card(suit, rank);
return card;
}
private Player choosePlayer(Player currentPlayer) {
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.print(currentPlayer.getName() + "请问要问哪个玩家?");
String playerName = scanner.nextLine();
for (Player player : players) {
if (player.getName().equals(playerName)) {
if (!player.equals(currentPlayer)) {
return player;
} else {
System.out.println("不能问自己,请重新选择!");
break;
}
}
}
System.out.println("没有找到该玩家,请重新选择!");
}
}
private void nextPlayer() {
currentPlayerIndex++;
if (currentPlayerIndex == 4) {
currentPlayerIndex = 0;
}
}
public static void main(String[] args) {
Game game = new Game();
game.start();
}
}
```
以上就是一个简单的用Java编写的金钩钓鱼游戏。注意,这只是一个简单的实现,你可以根据自己的需要对代码进行修改和拓展。
阅读全文