这就像纸牌游戏。如何将所有字符串转移到指定的数组,如何将所有上部字符串转换为星号代表纸牌背面。
public static void main(String... args) {
//Strings that stands for cards.
String[] shuffled;
shuffled= new String[52];
String[] cards =
{ "A@", "A#", "A$", "A&", "2@", "3@", "4@", "5@", "6@", "7@", "8@", "9@",
"10@", "K@", "Q@", "J@", "2#", "3#", "4#", "5#", "6#", "7#", "8#",
"9#", "10#", "K#", "J#", "2$", "3$", "4$", "5$", "6$", "7$", "8$",
"9$", "10$", "K$", "Q$", "J$", "2&", "3&", "4&", "5&", "6&", "7&",
"8&", "9&", "10&", "K&", "Q&", "J&", "Q#" };
List<String> list = Arrays.asList(cards);
//Shuffle.
Collections.shuffle(list);
final int columns = 7;
final int rows = 7;
int card = 0;
// loop over rows.In this stage i want to transfer all the codes in an array.
for (int i=0; i<rows; i++) {
// Fill empty columns in this row
for (int j=0; j<i; j++) {
System.out.print("\t");
}
// Add #columns - row# cards to this row
for (int j=i; j<columns; j++) {
System.out.print(list.get(card++) + "\t");
}
// advance to next row
System.out.println();
}
}
答案 0 :(得分:0)
<强> EDITED 强>
不是将卡片存储为字符串,然后以某种方式跟踪它们是面朝上还是朝下,而是考虑将卡片的价值和适合度封装在一个类中,以及有关它是否被暴露的信息。
public class Card {
String value;
String suit;
boolean exposed;
public Card(String id) {
int size = id.length();
this.value = id.substring(0, size-1);
this.suit = id.substring(size-1, size);
}
public Card show() {
exposed = true;
return this;
}
public Card hide() {
exposed = false;
return this;
}
@Override
public String toString() {
if(exposed) {
return(value + suit);
} else {
return "*";
}
}
}
默认情况下,通过
等调用构建卡片,面朝下Card c = new Card("A#");
因此您可以通过将卡片阵列的元素传递给构造函数来构建卡片。
拨打show
和hide
来更改卡是否公开,而toString
方法意味着您可以打印出每张卡而且调用代码不需要知道卡片是面朝上还是朝下 - 这是Card
类,用于确定是否打印了值*
。
然后定义一个包含52张卡片的包,以实现改组和处理:
public class Pack {
List<Card> pack = new ArrayList();
Iterator<Card> iterator;
public Pack() {
String[] cards =
{ "A@", "A#", "A$", "A&", "2@", "3@", "4@", "5@", "6@", "7@", "8@", "9@",
"10@", "K@", "Q@", "J@", "2#", "3#", "4#", "5#", "6#", "7#", "8#",
"9#", "10#", "K#", "J#", "2$", "3$", "4$", "5$", "6$", "7$", "8$",
"9$", "10$", "K$", "Q$", "J$", "2&", "3&", "4&", "5&", "6&", "7&",
"8&", "9&", "10&", "K&", "Q&", "J&", "Q#" };
for(String card : cards) {
pack.add(new Card(card));
}
}
void shuffle() {
Collections.shuffle(pack);
}
void startDealing() {
iterator = pack.iterator();
}
public Card nextCard() {
return(iterator.next());
}
}
注意如何通过简单的构造函数,shuffle
方法操作包,并处理startDealing
和一系列nextCard
调用。
最后,你的游戏课程可以专注于布局和操纵卡片的机制。我假设列的数量是固定的,但行数可能会有所不同,所以我将该板实现为Lists
的数组,其中每列都是List
。
public class Game {
final int numColumns = 7;
ArrayList<Card>[] board = new ArrayList[numColumns];
Game() {
for(int column = 0; column < numColumns; column++) {
board[column] = new ArrayList<Card>();
}
Pack pack = new Pack();
pack.shuffle();
pack.startDealing();
int numRows = 7;
// loop over rows.In this stage i want to transfer all the codes in an array.
for (int row=0; row<numRows; row++) {
// Add #columns - row# cards to this row
for (int column=row; column<numColumns; column++) {
Card card = pack.nextCard();
if(column == row) card.show();
board[column].add(row, card);
}
}
}
public int numRows() {
int numRows = 0;
for (List column : board) {
numRows = Math.max(numRows, column.size());
}
return numRows;
}
public int columnSize(int column) {
return board[column].size();
}
public void printBoard () {
for(int row = 0; row < numRows(); row++) {
for(int column = 0; column < numColumns; column++) {
if(board[column].size() > row) {
Card card = board[column].get(row);
System.out.print(card);
}
System.out.print("\t");
}
System.out.println();
}
}
public static void main(String[] args) {
Game g = new Game();
g.printBoard();
}
}
其余的卡片,我认为是您的银行,仍在pack
,您可以与pack.nextCard()
交易。您可以将这些已发卡添加到新List
,以便您可以跟踪它们。要再次查看卡片,请再次从Pack
和List
创建新的startDealing()
。