java - How to apply suits to the cards in matching game? -
basically, project description asks user create matching game, consisting of:
- a blueprint creation of playing card attributes are: suit, face value, faceup status, , optional unicode value suit icon
- a 2 dimensional playing table (4x4), 16 pairs of matching cards, , running count of number of face-down cards. include method retrieve specific card table @ input x,y position.
- a gameboard, , loop continues play until cards remain face up. loop includes interface user pick 2 cards (inputting x,y table positions), checking if 2 cards equal, decrementing count of facedown cards, , setting faceup boolean of cards. essnetially, program should run until cards face up, , game won. separated program in 4 separate classes below.
1.
public class card { private final int cardvalue; private boolean faceup; public card(int value) { cardvalue = value; faceup = false; } public int getvalue() { return cardvalue; } public boolean isfaceup() { return faceup; } public void setfaceup(boolean input) { faceup = input; } public static void printboard(card[][] cards) { system.out.println("\t\t1\t2\t3\t4"); system.out.println("\t____________"); for(int = 0; < cards.length; i++) { system.out.println((i + 1) + "\t|\t"); for(int j = 0; j < cards[0].length; j++) if(cards[i][j].isfaceup()) { system.out.print(cards[i][j].getvalue() + "\t"); } else system.out.println("*\t"); } system.out.println(); } }
2.
public class createboard { public static card[][] createboard() { card[][] board = new card[4][4]; for(int = 1; <= 8; i++) { for(int j = 1; j <= 2; j++) { boolean boardlocation = false; while(!boardlocation) { int row = (int)(math.random() * 4); int column = (int)(math.random() * 4); if(board[row] == null && board[column] == null) { boardlocation = true; board[row][column] = new card(i); } } } } return board; } }
3.
public class game { public static boolean wongame(card[][] board) { for(int = 0; < board.length; i++) { for(int j = 0; j < board[0].length; j++) { if(!board[i][j].isfaceup()) return false; } } return true; } }
and finally, main class:
public class gamedriver { public static void main(string[] args) { card[][] board = createboard.createboard(); scanner keyboard = new scanner(system.in); system.out.println("starting game..."); while(!game.wongame(board)) { card.printboard(board); system.out.println("enter x-coordinate #1 (1-4): "); int column1 = keyboard.nextint(); system.out.println("enter y-coordinate #1 (1-4): "); int row1 = keyboard.nextint(); system.out.println("enter x-coordinate #2 (1-4): "); int column2 = keyboard.nextint(); system.out.println("enter y-coordinate #2 (1-4): "); int row2 = keyboard.nextint(); card card1 = board[row1][column1]; card card2 = board[row2][column2]; if(card1.getvalue() == card2.getvalue() && !(row1 == row2 && column1 == column2)) { card1.setfaceup(true); card2.setfaceup(true); } else if(row1 == row2 && column1 == column2) { system.out.println("points selected same, try again"); } else { system.out.println(card1.getvalue() + " , " + card2.getvalue() + " not match"); } } card.printboard(board); } }
code runs perfectly, clueless on how add suits card. need 2 suits applied every card on board (hearts , diamonds example, if code produced 2 4s, 1 hearts , 1 diamonds) know use enumeration, don't understand how work. appreciated!
here's guide enums recommend read. it's quick reading, plenty of examples. java tutorials pretty useful.
here's code can use.
public enum suit { hearts ,spades ,diamonds ,clubs } public class card { private final int cardvalue; private final suit suit; private boolean faceup; public card(int value, suit suit) { this.cardvalue = value; this.faceup = false; this.suit = suit; } // ...snip... public suit getsuit() { return this.suit; } }
and you'd construct card call like:
card mycard = new card(3, suit.hearts);
i recommend not take advice of using set of char values. why? bunch of reasons. 1 thing, if use enum
type, know can't accidentally pass invalid value; typo can't screw entire program. another, don't need pass around list of acceptable chars everywhere, or- worse- have switch-case or if-statement checking every possible value every time , throwing error if uses invalid value.
then there's fact that, if use 1 char per enum value, enum values can't have meaningful names (how supposed figure out 'c'
, 's'
mean?), whereas there's little ambiguity in suit.clubs
or suit.spades
(and, remember, code written people, not computers. suspicious of inexperienced enough argue otherwise).
Comments
Post a Comment