始める前に、はい、これは宿題なので、明らかに答えを求めているわけではありませんが、誰かが正しい方向に私を助けたり、アドバイスをくれたりしてくれれば幸いです.
割り当てのために、私はJavaでブラックジャックのゲームを作っています。現在、Card クラス、Deck クラス、およびメイン (CardGameTester) があります。現在、私は 1 人のプレイヤー (ユーザー) としかゲームをしていないので、かなり基本的なものです。私のメインは非常に面倒で、3 番目のクラスを用意することもできますが、代わりにこの方法で行うことにしました。
いくつかの奇妙なエラーが発生します。これは、配列リストを台無しにしたり、オブジェクトに関係があると思われます。誰かが私を助けてくれたら、私は本当に感謝しています.
コンパイルしようとすると発生するエラー:
6 errors and 1 warning found:
--------------
*** Errors ***
--------------
File: E:\Documents\Java Files\Assignment 3\CardGameTester.java [line: 17]
Error: The method add(Card) in the type java.util.ArrayList<Card> is not applicable for the arguments (Deck)
File: E:\Documents\Java Files\Assignment 3\CardGameTester.java [line: 28]
Error: The method add(Card) in the type java.util.ArrayList<Card> is not applicable for the arguments (Deck)
File: E:\Documents\Java Files\Assignment 3\CardGameTester.java [line: 35]
Error: Type mismatch: cannot convert from Card to int
File: E:\Documents\Java Files\Assignment 3\Deck.java [line: 15]
Error: The constructor Card() is undefined
File: E:\Documents\Java Files\Assignment 3\Deck.java [line: 19]
Error: The method shuffle() is undefined for the type java.util.ArrayList<Card>
File: E:\Documents\Java Files\Assignment 3\Deck.java [line: 30]
Error: c cannot be resolved
-------------
** Warning **
-------------
File: E:\Documents\Java Files\Assignment 3\Card.java [line: 5]
Warning: The field Card.suits is never read locally
ここにCard.javaがあります
public class Card
{
private int rankValue, suitValue;
private String ranks[] = {"Hearts", "Clubs", "Spades", "Diamonds"};
private String suits[] = {"Ace", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"};
public Card(int rank, int suit)
{
rankValue = rank;
suitValue = suit;
}
public String convertToString(int rank){
return ranks[rank];
}
public void setRank(int rank){
rankValue = rank;
}
public void setSuit(int suit){
suitValue = suit;
}
public int getRank(){
return rankValue;
}
public int getSuit(){
return suitValue;
}
public String toString(){
return "Rank: " + rankValue + "Suit: " + suitValue;
}
}
Deck.java は次のとおりです。
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
public class Deck
{
private ArrayList<Card> cards = new ArrayList<Card>();
public Deck()
{
for(int a = 1; a <= 4; a++)
{
for(int b = 1; b <= 13; b++)
{
Card c = new Card();
cards.add(c);
}
}
cards.shuffle();
}
public void shuffle(){
Collections.shuffle(cards);
}
public String deal(){
int index = 0;
cards.get(index);
cards.remove(index);
return "You have been dealt a: " + c.toString();
index++;
}
}
ここに私のメインCardGametester.javaがあります:
public class CardGameTester
{
public static void main(String[] args)
{
ArrayList<Card> hand = new ArrayList<Card>();
Deck d = new Deck();
Scanner scan = new Scanner(System.in);
System.out.println("Welcome to the game of Black Jack!\nWould you like to Start?");
String input = scan.nextLine();
if(input == "yes")
{
d.deal();
hand.add(d);
}
String input2;
while (input2 == "yes")
{
Scanner keyboard = new Scanner(System.in);
System.out.println("Would you like to hit or stay?");
input2 = keyboard.nextLine();
d.deal();
hand.add(d);
}
int total;
for(int index = 0; index <= hand.size(); index++)
{
total = hand.get(index);
}
System.out.println("Your total value for you cards are: " + total);
if(total <= 21){
System.out.println("Congrats, you have won for not going over 21");
}
else
{
System.out.println("Sorry, you lose for going over 21");
}
}
}