所以我正在构建 Go Fish 的一个版本。我已完成所有编码,但它不会运行,因为在 Player 类中,我无法在方法中使用 Card.rank 参数。我明白这一点,但我仍然需要能够以某种方式输入排名。
看看我的 Card 构造函数以及我尝试使用需要排名输入的 Player 方法的任何实例(Player.subSets 和 Player.searchHand)。 (为了简洁起见,我也只显示了一名玩家的回合代码。)
程序(主)类:
import java.util.*;
public class Program
{
public static void main(String args[])
{
String[] rank = {"two", "three", "four", "five", "six", "seven", "eight",
"nine", "ten", "jack", "queen", "king", "ace"};
String[] suit = {"hearts", "diamonds", "spades", "clubs"};
Scanner scan = new Scanner(System.in);
String something = "yes", something2 = "yes", success = "yes"; //Use with while loops
String turn = "one";
String temp; //Use with setting names
String temp2, temp3; //For player being searched
Card temp4 = new Card(); //For player being searched
Card[] deck = new Card[52]; //Deck array
int playercount = 0;
Player one = new Player("temp");
Player two = new Player("temp");
Player three = new Player("temp");
Player four = new Player("temp");
//Start game
while (something.equalsIgnoreCase("yes"))
{
//Prepare game
Card.makeDeck(deck, rank, suit);
deck = Card.getDeck();
Card.shuffle(deck);
while (something2.equalsIgnoreCase("yes") || playercount < 2) //Add players to game
{
System.out.println("Would a(nother) player like to join?");
something2 = scan.nextLine();
System.out.println();
if (something2.equalsIgnoreCase("yes"))
{
if (playercount <= 4)
{
if (playercount == 0)
{
System.out.println("What is your name: ");
Player one1 = new Player(scan.nextLine());
one = one1;
playercount++;
System.out.println();
}
else if (playercount == 1)
{
System.out.println("What is your name: ");
Player two2 = new Player(scan.nextLine());
two = two2;
playercount++;
System.out.println();
}
else if (playercount == 2)
{
System.out.println("What is your name: ");
Player three3 = new Player(scan.nextLine());
three = three3;
playercount++;
System.out.println();
}
else if (playercount == 3)
{
System.out.println("What is your name: ");
Player four4 = new Player(scan.nextLine());
four = four4;
playercount++;
System.out.println();
}
else {System.out.println("Only four players are allowed.");
something2 = "no";}
}
}
else if (playercount < 2)
{
System.out.println("You need at least two players...");
System.out.println();
}
}
//Deal cards
if (playercount == 2)
{
for (int i = 1; i < 8; i++)
{
one.addCard(Card.draw(deck));
deck = Card.getDeck();
two.addCard(Card.draw(deck));
deck = Card.getDeck();
}
}
else if (playercount == 3)
{
for (int i = 1; i < 8; i++)
{
one.addCard(Card.draw(deck));
deck = Card.getDeck();
two.addCard(Card.draw(deck));
deck = Card.getDeck();
three.addCard(Card.draw(deck));
deck = Card.getDeck();
}
}
else
{
for (int i = 1; i < 6; i++)
{
one.addCard(Card.draw(deck));
deck = Card.getDeck();
two.addCard(Card.draw(deck));
deck = Card.getDeck();
three.addCard(Card.draw(deck));
deck = Card.getDeck();
four.addCard(Card.draw(deck));
deck = Card.getDeck();
}
}
//Take turns
while (something.equalsIgnoreCase("yes"));
{
success = "yes";
if (turn.equalsIgnoreCase("one") && something.equalsIgnoreCase("yes"))
{
System.out.println("Player one: " + one.getName() + "'s turn!");
System.out.println();
//Output hand
System.out.println("You have: " + Card.toString(one.getHand()));
System.out.println();
//Ask what who to search
System.out.println("Whose hand would you like to check?");
System.out.println("(Enter a player's name.)");
temp2 = scan.nextLine(); //Set player name to temp2
System.out.println();
//Ask what to search
while (success.equalsIgnoreCase("yes"))
{
System.out.println("Remember, you have: " + Card.toString(one.getHand()));
System.out.println();
System.out.println("Would you like to search for a two, three, four, five, six, seven, eight, nine, ten, jack, queen, king, or ace?");
temp3 = scan.nextLine(); //Set desired rank to temp3
//Check player two
if (temp2.equalsIgnoreCase(two.getName()))
{
if (two.searchHand(two.getHand(), temp3) != null)
{
while (two.searchHand(two.getHand(), temp3) != null)
{
one.addCard(two.searchHand(two.getHand(), temp3));
two.subCard(two.searchHand(two.getHand(), temp3));
one.subSets(one.getHand(), temp3);
}
}
else if (two.searchHand(two.getHand(), temp3) == null)
{
System.out.println();
System.out.println("Go fish!");
System.out.println(one.getName() + " drew a card.");
System.out.println();
one.addCard(Card.draw(deck));
one.subSets(one.getHand(), temp3);
deck = Card.getDeck();
turn = "two";
success = "no";
something = one.checkWin();
if (something.equalsIgnoreCase("no"))
System.out.println(one.getName() + " won!");
}
}
//Check player three
if (temp2.equalsIgnoreCase(three.getName()))
{
if (three.searchHand(three.getHand(), temp3) != null)
{
while (three.searchHand(three.getHand(), temp3) != null)
{
one.addCard(three.searchHand(three.getHand(), temp3));
three.subCard(three.searchHand(three.getHand(), temp3));
one.subSets(one.getHand(), temp3);
}
}
else if (three.searchHand(three.getHand(), temp3) == null)
{
System.out.println();
System.out.println("Go fish!");
System.out.println(one.getName() + " drew a card.");
System.out.println();
one.addCard(Card.draw(deck));
one.subSets(one.getHand(), temp3);
deck = Card.getDeck();
turn = "two";
success = "no";
something = one.checkWin();
if (something.equalsIgnoreCase("no"))
System.out.println(one.getName() + " won!");
}
}
//Check player four
if (temp2.equalsIgnoreCase(four.getName()))
{
if (four.searchHand(four.getHand(), temp3) != null)
{
while (four.searchHand(four.getHand(), temp3) != null)
{
one.addCard(four.searchHand(four.getHand(), temp3));
four.subCard(four.searchHand(four.getHand(), temp3));
one.subSets(one.getHand(), temp3);
}
}
else if (four.searchHand(four.getHand(), temp3) == null)
{
System.out.println();
System.out.println("Go fish!");
System.out.println(one.getName() + " drew a card.");
System.out.println();
one.addCard(Card.draw(deck));
one.subSets(one.getHand(), temp3);
deck = Card.getDeck();
turn = "two";
success = "no";
something = one.checkWin();
if (something.equalsIgnoreCase("no"))
System.out.println(one.getName() + " won!");
}
}
}
}
//ALL THE CODE FOR OTHER TURNS HERE
}
//Replay
System.out.println("Would you like to play again?");
something = scan.nextLine();
}
}
}
玩家等级:
import java.util.*;
public class Player
{
private static String name;
private static Card[] hand = new Card[52];
private static Card[] sets = new Card[52];
private static int handsize = 0, nullcount = 0;
private static String win = "no";
private static int temp = 1; //For subCard
private static int temp2 = 0, temp3 = 0; //For subSets
//Constructor
public Player(String n)
{
name = n;
}
//Mutators
//Add new card to hand
public static void addCard(Card c)
{
hand[handsize] = c;
handsize++;
}
//Lose card that was searched for
public static void subCard(Card c)
{
for (int i = 0; i < hand.length; i++)
{
if (c == hand[i])
{
temp = i;
if (temp < 51)
temp++;
hand[i] = hand[temp];
}
}
hand[(hand.length - 1)] = null;
handsize--;
}
//Lose sets of cards
public static void subSets(Card[] h, Card.rank r)
{
//See if there are four of a kind
for (int i = 0; i < h.length; i++)
{
if (h[i].rank == r)
{
temp2++;
}
}
//Drop set of four
if (temp2 == 4)
{
for (int i = 0; i < h.length; i++)
{
if (h[i].rank == r)
h[i] = null;
for (int ii = (i + 1); ii < (h.length - 1); ii++)
{
temp3 = i;
h[temp3] = h[ii];
temp3++;
}
h[(h.length - 1)] = null;
}
}
hand = h;
}
//Accessors
public static String getName()
{
return name;
}
public Card[] getHand()
{
return hand;
}
public Card searchHand(Card[] h, Card.rank r)
{
for (int i = 0; i < h.length; i++)
{
if (h[i].rank == r)
return h[i];
else if (i == h.length - 1)
return null;
}
}
public String checkWin()
{
for (int i = 0; i < hand.length; i++)
{
if (hand[i] == null)
nullcount++;
}
if (nullcount == (hand.length - 1))
return "no"; //Set equal to something in Program
else
{
nullcount = 0;
return "yes"; //Set equal to something in Program
}
}
}
卡片类别:
import java.util.*;
public class Card
{
public String suit;
public String rank;
private static int temp = 0, temp2 = 0; //Use for reseting rank and suit
private static Card temp3; //Use for draw method
private static int temp4 = 1; //Use for draw and shuffle, always back to 1
private static Card[] deck = new Card[52];
private static Random random = new Random();
//Constructors
public Card()
{
this.rank = "two";
this.suit = "hearts";
}
public Card(String r, String s)
{
this.rank = r;
this.suit = s;
}
//Mutators
//Make deck
public static void makeDeck(Card[] c, String[] r, String[] s)
{
for (int i = 0; i < c.length; i++)
{
c[i] = new Card(r[temp], s[temp2]);
temp++; temp2++;
//Reset rank and suit
if (temp > 12)
temp = 0;
if (temp2 > 3)
temp2 = 0;
}
deck = c;
}
//Accessors
//Return deck
public static Card[] getDeck()
{
return deck;
}
//Shuffle
public static Card[] shuffle(Card[] c)
{
for (int i = 0; i < c.length; i++)
{
int rand = (int)(random.nextInt(52)*(i + 1));
//Don't let anything be in a slot that doesn't exist
while (rand > c.length)
{
temp4 = random.nextInt(52);
rand -= temp4;
}
if (rand < 0)
{
rand += temp4;
Card temp = c[i];
c[i] = c[rand];
c[rand] = temp;
}
}
deck = c;
temp4 = 1;
return deck;
}
//Draw
public static Card draw(Card[] c)
{
temp3 = c[0];
for (int i = 0; i < c.length - 1; i++)
{
c[i] = c[temp4];
if (temp4 < 51)
temp4++;
}
c[(c.length - 1)] = null;
temp4 = 1;
deck = c;
return temp3;
}
//Return string
public static String toString(Card[] h)
{
String temp5 = "yes";
while (temp5.equals("yes"))
{
for (int i; i < h.length; i++)
{
if (h[i] != null)
return h[i].rank + " of " + h[i].suit +", ";
else temp5 = "no";
}
}
}
}
最佳答案
Card.rank 是一个字符串
public class Card
{
public String rank;
所以方法声明需要
public static void subSets(Card[] h, String r)
至于
public Card searchHand(Card[] h, Card.rank r)
{
for (int i = 0; i < h.length; i++)
{
if (h[i].rank == r)
return h[i];
else if (i == h.length - 1)
return null;
}
}
如果 h.length 为零,您将返回什么?
关于java - 一个 'cannot find value' 挡在我和我的第一款游戏之间。 [显示代码],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20672276/