gpt4 book ai didi

java - 将抽取的卡片与 ArrayList 中的 gif 进行匹配?

转载 作者:太空宇宙 更新时间:2023-11-04 13:53:37 25 4
gpt4 key购买 nike

我正在创建一个纸牌游戏,但无法显示纸牌 gif。从 52 张卡片组数组列表中抽取一张卡片,然后我需要将该抽取的卡片与卡片 gif 数组列表中名称类似的 gif 进行匹配,以在卡片 GUI 上显示该 gif。我将我的游戏、卡片、牌组和图形类放在下面,让您了解我的程序是如何工作的:

卡片-

    package uk.ac.aber.dcs.cs12320.cards;

public class Card {
public String number;
public String suit;

public Card(String n, String s) {
number = n;
suit = s;
}

@Override
public String toString() {
return number + suit;
}

}

游戏-

     import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;

import uk.ac.aber.dcs.cs12320.cards.Card;
import uk.ac.aber.dcs.cs12320.cards.gui.TheFrame;

public class Game {

private Scanner scan;
private Deck deck;
private TheFrame frame;
private ArrayList<Card> onTable = new ArrayList<Card>();

public Game() {
frame = new TheFrame();
deck = new Deck();
try {
deck.buildDeck();
} catch (IOException e) {
System.err.println("Error reading in deck...");
System.exit(-1);
}

}

private void runMenu() throws IOException {

String response;
do {
printMenu();
System.out.println("What would you like to do:");
scan = new Scanner(System.in);
response = scan.nextLine().toUpperCase();
switch (response) {
case "1":
PrintDeck();
break;
case "2":
ShuffleCards();
break;
case "3":
DealCard();
break;
case "4":
MoveToPrevious();
break;
case "5":
Move2PilesBack();
break;
case "6":
AmalgamateInMiddle();
break;
case "7":
PlayforMe();
break;
case "8":
ShowLowScores();
case "Q":
break;
default:
System.out.println("Try again");

}
drawCards();

} while (!(response.equals("Q")));
}

private void ShowLowScores() {
// TODO Auto-generated method stub

}

private void PlayforMe() {
// TODO Auto-generated method stub

}

private void AmalgamateInMiddle() {
// TODO Auto-generated method stub

}

private void Move2PilesBack() {
// TODO Auto-generated method stub

}

private void MoveToPrevious() {
// TODO Auto-generated method stub

}

private void DealCard() {
Card c = deck.removeTopCard();

System.out.println(c);
}

private void ShuffleCards() {

deck.shuffle();

}

private void drawCards() {
ArrayList<String> visibleCards = new ArrayList<String>();

for (Card card : onTable) {
visibleCards.add(card.number + card.suit + ".gif");

}

frame.cardDisplay(visibleCards);

}

private void PrintDeck() throws IOException {
for (Card card : deck.getDeck()) {
System.out.println(card);
}

}

private void printMenu() {

System.out.println("1 - Print the pack ");
System.out.println("2 - Shuffle");
System.out.println("3 - Deal a card");
System.out.println("4 - Move last pile onto previous one");
System.out.println("5 - Move last pile back over two piles");
System.out.println("6 - Amalgamate piles in the middle");
System.out.println("7 - Play for me");
System.out.println("8 - Show low scores");
System.out.println("q - Quit");

}

public static void main(String args[]) throws IOException {
System.out.println("****Welcome to patience is virtue****");
Game cardsgame = new Game();
cardsgame.runMenu();
System.out.println("****Thanks for playing****");
}
}

甲板-

  

import java.util.Collections;
import java.util.List;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;

import uk.ac.aber.dcs.cs12320.cards.Card;

public class Deck {
private ArrayList<Card> cards;

public Deck() {
cards = new ArrayList<Card>();
}

public void buildDeck() throws IOException {

List<String> cardLines = Files.readAllLines(Paths.get("cards.txt"));
for (int i = 0; i < cardLines.size(); i += 2) {
// System.out.println()
cards.add(new Card(cardLines.get(i), cardLines.get(i + 1)));

}
}

public Card removeTopCard() {
return cards.remove(0);
}

public List<Card> getDeck() {
return cards;
}

public void shuffle() {
Collections.shuffle(cards);
System.out.println(cards);
}

}

图形(框架)类-

     package uk.ac.aber.dcs.cs12320.cards.gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import javax.swing.JFrame;
import javax.swing.JPanel;

import uk.ac.aber.dcs.cs12320.cards.Card;

public class TheFrame extends JFrame {

public static boolean paintComponent;
private ThePanel canvas;

/**
* The constructor creates a Frame ready to display the cards
*/
public TheFrame() {

// Calls the constructor in the JFrame superclass passing up the name to
// display in the title
super("Becky's Patience");

// When you click on the close window button the window will be closed
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

// This has North, East, South, West and Center positions for components
setLayout(new BorderLayout());

// This is what we will draw on (see the inner class below)
canvas = new ThePanel(null);
setSize(700, 300);
this.add(canvas, BorderLayout.CENTER);

setVisible(true); // Display the window
}

/**
* Displays all cards
*
* @param cards
* an arraylist of strings of the form 3h.gif for 3 of hearts
*/
/*public void cardDisplay(ArrayList<String> cards) {
canvas.cardDisplay(cards);
}*/

/**
* Call before cardDisplay at end of game (takes away the unused pile)
*/
public void allDone() {
canvas.allDone();
}

// /////////////////////////////////////////////////

/*
* This is an example of an inner class (like Russian dolls)
* It private so can only be seen by the outer class. It's part
* of the implementation of TheFrame. Because it extends JPanel we
* can draw on it
*/

Map<Card, Image> loadCards(ArrayList<Card> cards)
{
Map<Card, Image> cardImages = new HashMap<>();
for (Card card : cards)
{
String file = "cards/" + card.number + card.suit + ".gif";
Image image = Toolkit.getDefaultToolkit().getImage(file);
cardImages.put(card, image);
}
return cardImages;
}

private class ThePanel extends JPanel {
private Map<Card, Image> cardImages;
private ArrayList<Card> currentCardDisplayed;
private boolean done;

private ThePanel(ArrayList<Card> cards) {
setBackground(Color.cyan);
done = false;
cardImages = loadCards(cards);
}

private void cardDisplay(ArrayList<String> c) {
cards = c;
repaint();
}

private void allDone() {
done = true;
}

/**
* This is called automatically by Java when it want to draw this panel.
* So we have to put our drawing command in here.
* @param g Is the graphics object on which we draw.
*/
@Override
public void paintComponent(Graphics g) {
// Always do this. It's giving the JPanel superclass a change to
// paint its parts before we paint ours. E.g. we don't draw the
// edge of the window, one of the super-classes does that.
super.paintComponent(g);
int x = 20;
int y = 50;
// Loop through all the cards get each image in turn
for (Card card: currentCardDisplayed) {
g.drawImage(cardImages.get(card), x, y, 70, 100, this);
x += 72; // The x position is moved on in order to position the next card
// This could be improved by having a horizontal scroll bar
}
if (!done) {
// Draws the face-down top card of our pack of cards
String file = "cards/b.gif";
image = Toolkit.getDefaultToolkit().getImage(file);
g.drawImage(image, 100, 152, 70, 100, this);
}
}
} // ThePanel inner class

这是 gif 在其文件夹中的样子,该文件夹是我的图形类中的数组列表: Here is what the gifs look like in their folder, which is an arraylist in my graphical class :

下面是文本卡数组列表洗牌后抽出的卡的样子: Here is what the drawn card looks like after the textual card arraylist has been shuffled:

卡片 gif 的 arrayList(包含卡片的所有 52 个 gif)位于图形类中,而我的 arrayList 包含文本形式的卡片组(我从文本文件导入)位于卡片组中。 gif arraylist 中的 gif 名称与我的文本卡片 arraylist 中的卡片名称相同(例如,3h.gif 是我的卡片 arraylist 中的 3h),因此我相信我可以使用 .equals 语句来匹配已绘制到 gif 的卡片。

任何关于我如何做到这一点的帮助都会很棒,我已经加粗了有助于表达我的意思的重要区域,感谢您的帮助:)!

更新:我在 Game 和 TheFrame 类中遇到一些错误。 TheFrame 中的卡片显示下方显示“卡片”,表示该卡片不存在。它还表示“图像”无法解析为 Paint 组件方法中的变量。在游戏中,drawCards 方法中的 CardDisplay 是“未定义类型 TheFrame”。如果有人可以帮助解决这些错误,那就太棒了:)!

最佳答案

您可能想要的不是保留 ArrayList <String> cards在你的ThePanel 。相反,您希望在构建面板时加载图像对象,并将它们保存在 map 中。此方法将为您加载 map :

   void loadCards(ArrayList<Card> cards)
{
Map<Card, Image> cardImages = new HashMap<>();
for (Card card : cards)
{
String file = "cards/" + card.number + card.suit + ".gif";
image = Toolkit.getDefaultToolkit().getImage(file);
cardImages.put(card, image);
}
return cardImages;
}

将此方法的结果存储在您的 ThePanel 中:

private class ThePanel extends JPanel {
private Map<Card, Image> cardImages;
private ArrayList<Card> currentCardDisplayed;
private boolean done;

private ThePanel(ArrayList<Card> cards) {
setBackground(Color.cyan);
done = false;
cardImages = loadCards(cards);
}

然后在重画中,您可以执行以下操作:

  for (Card card: currentCardDisplayed) {
g.drawImage(cardImages.get(card), x, y, 70, 100, this);
....
}

最后,既然您将使用映射,那么实现 Card.equals 和 Card.hashCode 可能是个好主意!

关于java - 将抽取的卡片与 ArrayList 中的 gif 进行匹配?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30052721/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com