如何使用预定义的牌组制作二十一点游戏 class
How to make a Black Jack game out of a predefinded deck class
所以在 class 中我们不得不做 ArrayBag class,我相信你们都知道。如果不是它在整个互联网上。不管怎样,我们的项目是把它变成黑杰克游戏。但是我不知道该怎么做。我想做一张牌 class 一副牌 class 然后是黑杰克 class。然而,我又一次不知道如何将它实现到 Array 包中 class。有人帮忙!我将添加我目前所拥有的代码。
数组包
package bag;
//I will will keep T because its the norm, but i really don't want to.
public class ArrayBag <T> implements BagInterface<T>{
private final T [] bag; // our generic array
private static final int DefualtItemCapacity = 52; // our default capacity of items...Make DEFAULT all caps because all the i's made it look ugly
private int numberOfItems ;
public ArrayBag (int itemCapacity){
numberOfItems = 0 ; // set numberOfItems to empty 0
@SuppressWarnings("unchecked") // java i know what im doing, #Suppressed !
T[] tempBag = (T[]) new Object [itemCapacity];
bag= tempBag;
}// end constructor
public ArrayBag() {
this(DefualtItemCapacity);
} // if nothing, call DefualtItemCapacity, dont have to check for null if we overload the method
// thank you eclipse, for creating the methods from the interface :)
@Override
public int getCurrentSize() {
// TODO Auto-generated method stub
return numberOfItems;
}// end current size
@Override
public boolean isFull() {
// TODO Auto-generated method stub
return numberOfItems==bag.length; // return numberOfItems as full
}// end full
@Override
public boolean isEmpty() {
// TODO Auto-generated method stub
return numberOfItems==0;// return numberOfItems as empty
}// end empty
@Override
public boolean add(T newItem) {
boolean result = true ;
if(isFull()){ // bag is full , return false
result = false ;
} else { // bag is not full add new item
bag[numberOfItems]= newItem ;
numberOfItems++;
}
return result;
}// end bool add
@Override
public T remove() {
T temp = bag[numberOfItems-1]; // bag numberOfItems is now one less
bag[numberOfItems-1]=null ; // then we have to set that spot to null
numberOfItems--; // then make sure numberOfItems is one less aswell
return temp;
}// end T remove
@Override
public boolean remove(T anItem) {
boolean result = true ;
for (int i = 0; i < numberOfItems; i++){
if(bag[numberOfItems]== anItem ) {
bag[numberOfItems] = null ;
/* items[numItems-1] = null;*/ // this might shift it left
numberOfItems--;
result = true;
}else {
result = false ; // item not found
}
}
return result;
}// end bool remove
@Override
public void clear() {
while(!isEmpty()){ // while the bag is not empty we can remove stuff.
remove();
}
}// end clear
@Override
public T grab() {
int i;
while (numberOfItems != 0){ // while numberOfItems is not empty
i = (int)(Math.random( ) * numberOfItems) + 1; // i is a random value
return bag[i]; // return random item from bag
}
return null ; // is empty , return null
} // end grab
@Override
public int getFrequencyOf(T anItem) { // count how many items are the same
int count=0 ; // count how many are the same
for (int i = 0; i < numberOfItems; i++){
if(bag[i]==anItem){ // if current item = an item add to the count value
count++;
}
}
return count;
}// end Frequency
@Override
public boolean contains(T anItem) {
while(anItem != null){
for (int i = 0; i < numberOfItems; i++){
if (anItem.equals(bag[i])){ //bag contains the specified item, use .equals because java is dumb and == gives an error.
return true;
}
}
}return false; // bag doesnt contain item
}// end contains
@Override
public T[] toArray() { // creates and returns an array containing all of the items
T[] result = (T[]) new Object [numberOfItems];
for(int i =0; i < numberOfItems; i++){
result[i]=bag[i];
}
return result;
} // end toArray
} // end class
卡片Class
package bag;
public class Card {
private String cardSuit ;
private int card ;
Card(String suit, int card ){
this.cardSuit = cardSuit ;
this.card = card ;
}
public String toString(){ // Mr.Englend, i overloaded the toString :'(
String CardNumber = null;
switch (this.card){
case 1:
CardNumber = "Ace" ;
break;
case 2:
CardNumber = "Two" ;
break;
case 3:
CardNumber = "Three" ;
break;
case 4:
CardNumber = "Four" ;
break;
case 5:
CardNumber = "Five" ;
break;
case 6:
CardNumber = "Six" ;
break;
case 7:
CardNumber = "Seven" ;
break;
case 8:
CardNumber = "Eight" ;
break;
case 9:
CardNumber = "Nine" ;
break;
case 10 :
CardNumber = "Ten" ;
break ;
case 11:
CardNumber = "Jack" ;
case 12:
CardNumber = "Queen" ;
case 13:
CardNumber = "King" ;
break;
case 14:
CardNumber = "Ace" ;
break;
default:
CardNumber = "ERROR" ;
break;
}
return CardNumber + "Of " +cardSuit.toString() ;
}
}
二十一点Class
package bag;
import java.util.Scanner;
public class BlackJack {
private static ArrayBag Deck = new ArrayBag();
private Card[] mycard ;
//private static Card Card = new Card();
public static void main(String[] args) {
boolean play = true ;
boolean winner = false ; // bool win
int money = 100 ; // amount of money
int bet = 0 ; // amount user bets
Scanner input = new Scanner(System.in);
Deck.add(3);
System.out.println(Deck.isEmpty());
System.out.println(Deck.contains(3));
System.out.println(Deck.toArray());
}// end main
void shuffle(){
for(int i = 1; i <= 52 ; i++){
System.out.println(Deck.add(i));
}
}
void drawCard(){
while(!Deck.isEmpty()){
} // end while not empty
} // draw card
}// end class
一个简单的 google 搜索可以成为您最好的朋友:
http://math.hws.edu/eck/cs124/javanotes4/c5/ex-5-5-answer.html
所以在 class 中我们不得不做 ArrayBag class,我相信你们都知道。如果不是它在整个互联网上。不管怎样,我们的项目是把它变成黑杰克游戏。但是我不知道该怎么做。我想做一张牌 class 一副牌 class 然后是黑杰克 class。然而,我又一次不知道如何将它实现到 Array 包中 class。有人帮忙!我将添加我目前所拥有的代码。
数组包
package bag;
//I will will keep T because its the norm, but i really don't want to.
public class ArrayBag <T> implements BagInterface<T>{
private final T [] bag; // our generic array
private static final int DefualtItemCapacity = 52; // our default capacity of items...Make DEFAULT all caps because all the i's made it look ugly
private int numberOfItems ;
public ArrayBag (int itemCapacity){
numberOfItems = 0 ; // set numberOfItems to empty 0
@SuppressWarnings("unchecked") // java i know what im doing, #Suppressed !
T[] tempBag = (T[]) new Object [itemCapacity];
bag= tempBag;
}// end constructor
public ArrayBag() {
this(DefualtItemCapacity);
} // if nothing, call DefualtItemCapacity, dont have to check for null if we overload the method
// thank you eclipse, for creating the methods from the interface :)
@Override
public int getCurrentSize() {
// TODO Auto-generated method stub
return numberOfItems;
}// end current size
@Override
public boolean isFull() {
// TODO Auto-generated method stub
return numberOfItems==bag.length; // return numberOfItems as full
}// end full
@Override
public boolean isEmpty() {
// TODO Auto-generated method stub
return numberOfItems==0;// return numberOfItems as empty
}// end empty
@Override
public boolean add(T newItem) {
boolean result = true ;
if(isFull()){ // bag is full , return false
result = false ;
} else { // bag is not full add new item
bag[numberOfItems]= newItem ;
numberOfItems++;
}
return result;
}// end bool add
@Override
public T remove() {
T temp = bag[numberOfItems-1]; // bag numberOfItems is now one less
bag[numberOfItems-1]=null ; // then we have to set that spot to null
numberOfItems--; // then make sure numberOfItems is one less aswell
return temp;
}// end T remove
@Override
public boolean remove(T anItem) {
boolean result = true ;
for (int i = 0; i < numberOfItems; i++){
if(bag[numberOfItems]== anItem ) {
bag[numberOfItems] = null ;
/* items[numItems-1] = null;*/ // this might shift it left
numberOfItems--;
result = true;
}else {
result = false ; // item not found
}
}
return result;
}// end bool remove
@Override
public void clear() {
while(!isEmpty()){ // while the bag is not empty we can remove stuff.
remove();
}
}// end clear
@Override
public T grab() {
int i;
while (numberOfItems != 0){ // while numberOfItems is not empty
i = (int)(Math.random( ) * numberOfItems) + 1; // i is a random value
return bag[i]; // return random item from bag
}
return null ; // is empty , return null
} // end grab
@Override
public int getFrequencyOf(T anItem) { // count how many items are the same
int count=0 ; // count how many are the same
for (int i = 0; i < numberOfItems; i++){
if(bag[i]==anItem){ // if current item = an item add to the count value
count++;
}
}
return count;
}// end Frequency
@Override
public boolean contains(T anItem) {
while(anItem != null){
for (int i = 0; i < numberOfItems; i++){
if (anItem.equals(bag[i])){ //bag contains the specified item, use .equals because java is dumb and == gives an error.
return true;
}
}
}return false; // bag doesnt contain item
}// end contains
@Override
public T[] toArray() { // creates and returns an array containing all of the items
T[] result = (T[]) new Object [numberOfItems];
for(int i =0; i < numberOfItems; i++){
result[i]=bag[i];
}
return result;
} // end toArray
} // end class
卡片Class
package bag;
public class Card {
private String cardSuit ;
private int card ;
Card(String suit, int card ){
this.cardSuit = cardSuit ;
this.card = card ;
}
public String toString(){ // Mr.Englend, i overloaded the toString :'(
String CardNumber = null;
switch (this.card){
case 1:
CardNumber = "Ace" ;
break;
case 2:
CardNumber = "Two" ;
break;
case 3:
CardNumber = "Three" ;
break;
case 4:
CardNumber = "Four" ;
break;
case 5:
CardNumber = "Five" ;
break;
case 6:
CardNumber = "Six" ;
break;
case 7:
CardNumber = "Seven" ;
break;
case 8:
CardNumber = "Eight" ;
break;
case 9:
CardNumber = "Nine" ;
break;
case 10 :
CardNumber = "Ten" ;
break ;
case 11:
CardNumber = "Jack" ;
case 12:
CardNumber = "Queen" ;
case 13:
CardNumber = "King" ;
break;
case 14:
CardNumber = "Ace" ;
break;
default:
CardNumber = "ERROR" ;
break;
}
return CardNumber + "Of " +cardSuit.toString() ;
}
}
二十一点Class
package bag;
import java.util.Scanner;
public class BlackJack {
private static ArrayBag Deck = new ArrayBag();
private Card[] mycard ;
//private static Card Card = new Card();
public static void main(String[] args) {
boolean play = true ;
boolean winner = false ; // bool win
int money = 100 ; // amount of money
int bet = 0 ; // amount user bets
Scanner input = new Scanner(System.in);
Deck.add(3);
System.out.println(Deck.isEmpty());
System.out.println(Deck.contains(3));
System.out.println(Deck.toArray());
}// end main
void shuffle(){
for(int i = 1; i <= 52 ; i++){
System.out.println(Deck.add(i));
}
}
void drawCard(){
while(!Deck.isEmpty()){
} // end while not empty
} // draw card
}// end class
一个简单的 google 搜索可以成为您最好的朋友: http://math.hws.edu/eck/cs124/javanotes4/c5/ex-5-5-answer.html