Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Text Based BlackJack(modified version of regular ones)
Index -> Programming, Java -> Java Submissions
View previous topic Printable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Tat




PostPosted: Wed Feb 04, 2004 9:34 pm   Post subject: Text Based BlackJack(modified version of regular ones)

code:
/*Tat Ho
YOU NEED TO GET THE ATTACHMENT KEYBOARDREADER TO WORK
Most stuff are based on my teacher's expectations including maximum cards = 3, betting twice and maybe more.  This was the hardest project i had to do since most ideas were from my brain(other projects we use teacher's outline including all the properties and methods in a class and classes and so on)
Sry if this game is non realistic but it meets all the requirements for my teacher's expectations
Opps almost forgot u need to save it as thBJMain.java
 *Black Jack Game*/
 
class thBJMain
{
        static BJGame game=new BJGame();
       
        public static void main(String args[])
        {
                game.play();
        }
}

class BJGame
{
        BJPlayer p1=new BJPlayer();
        BJPlayer p2=new BJPlayer();
        String playagain;
        String choice1, choice2;
        KeyboardReader reader=new KeyboardReader();
       
        void title()
        {
                System.out.println("Black Jack Game");
                reader.pause();
        }
       
        void play()
        {

                title();
                enterNames();
                while(p2.name.equals(p1.name))
                {
                        p2.name=reader.readLine("Name was taken, please enter another one");
                }              
                do{
                p1.countbet=0;
                p2.countbet=0;
                p1.count=0;
                p2.count=0;
                p1.score=0;
                p2.score=0;
                p1.bet=0;
                p2.bet=0;
                p1.displayInfo();
                p2.displayInfo();
                enterBets();
                p1.getCard();
                System.out.println(p1.name + " card" + p1.count + "" + " is " + p1.hand);
                p1.getCard();
                System.out.println(p1.name + " card" + p1.count + "" + " is " + p1.hand);
                System.out.println(p1.name +"'s score:" + p1.score + "\n");
                p2.getCard();
                System.out.println(p2.name + " card" + p2.count + "" + " is " + p2.hand);
                p2.getCard();
                System.out.println(p2.name + " card" + p2.count + "" + " is " + p2.hand);
                System.out.println(p2.name +"'s score:" + p2.score + "\n");
               
               
                choice1=reader.readLine("Enter Hit or Stand " + p1.name+ " ");
                  choice1=choice1.toUpperCase();
                  while(!choice1.equals("HIT") && !choice1.equals("STAND"))
                  {
                        choice1=reader.readLine("Invalid approach please enter Hit or Stand " + p1.name);
                        choice1=choice1.toUpperCase();
                  }
                  if(choice1.equals("HIT")){
                        p1.enterBet();
                        p1.getCard();
                        System.out.println(p1.name + " card" + p1.count + "" + " is " + p1.hand);
                        System.out.println(p1.name +"'s score:" + p1.score);
                        }else if(choice1.equals("STAND"))
                              p1.enterBet();
               
                
                choice2=reader.readLine("Enter hit or stand " + p2.name + " ");
                  choice2=choice2.toUpperCase();
                  while(!choice2.equals("HIT") && !choice2.equals("STAND"))
                  {
                        choice2=reader.readLine("Invalid approach please enter Hit or Stand " + p2.name);
                        choice2=choice1.toUpperCase();
                  }
                  if(choice2.equals("HIT")){
                        p2.enterBet();
                        p2.getCard();
                        System.out.println(p2.name + " card" + p2.count + "" + " is " + p2.hand);
                        System.out.println(p2.name +"'s score:" + p2.score);
                        }else if(choice2.equals("STAND"))
                              p2.enterBet();
                             
               
               
                if(p1.score>21)
                        p1.score=0;
                        else if(p2.score>21)
                                p2.score=0;
                               
                if(p1.score>p2.score){
                        System.out.println(p1.name + " wins with a score of " + p1.score + "\n");
                        p1.money+=2*p1.countbet;
                        p1.displayInfo();
                        p2.displayInfo();
                        }else if(p1.score<p2.score){
                                System.out.println(p2.name + " wins with a score of " + p2.score + "\n");
                                p2.money+=2*p2.countbet;
                                p2.displayInfo();
                                p1.displayInfo();
                                }else if(p1.score == p2.score){
                                System.out.println("Tie!");
                                p1.money+=p1.bet;
                                p2.money+=p2.bet;
                                p1.displayInfo();
                                p2.displayInfo();
                                }
                if(playersBroke()==true){
                        playagain="";
                        System.out.println("One of the players is broke");
                }
                else if(playersBroke()==false){
                        playagain=reader.readLine("Play again, enter YES if so or else you will exit the game");
                        playagain=playagain.toUpperCase();
                        }

                        }while(playagain.equals("YES") || playagain.equals("Y"));
               
               
               
        }
       
        void enterNames()
        {
                p1.enterName();
                p2.enterName();
        }
       
        void enterBets()
        {
                p1.enterBet();
                p2.enterBet();
        }
        boolean playersBroke()
        {
                if(p1.money<=0 || p2.money<=0)
                        return true;
                                else
                                  return false;
        }

}

class BJPlayer
{
        KeyboardReader reader=new KeyboardReader();
        Card card=new Card();
        String name, hand="unknown";
        int bet=0, money=1000, score=0;
        int count=0;
        int countbet;
       
        void enterName()
        {
                name=reader.readLine("Please enter your name ");
        }
       
        void enterBet()
        {
                do
                {
                        bet=reader.readInt(name + " please enter a bet that is less than " + money + "\n");
                               
                }while(money<bet);
                if(bet<=money){
                                money-=bet;
                                countbet+=bet;
                        }              
        }
       
        void displayInfo()
        {
                System.out.println("Name: " + name);
                System.out.println("Money: " + money);
                System.out.println("Total Bet: " + countbet + "\n");
        }
        void getCard()
        {
                card.randomCard();
                if(card.cardvalue==1){
                  if(score>=11)
                    score+=1;
                  else if(score<11)
                        score+=11;
                  hand="Ace of Spade";
                
                 }else if(card.cardvalue==2){
                        score+=2;
                        hand="2 of Spade";
                 }else if(card.cardvalue==3){
                        score+=3;
                        hand="3 of Spade";
                 }else if(card.cardvalue==4){
                        score+=4;
                        hand="4 of Spade";
                 }else if(card.cardvalue==5){
                        score+=5;
                        hand="5 of Spade";
                 }else if(card.cardvalue==6){
                        score+=6;
                        hand="6 of Spade";
                 }else if(card.cardvalue==7){
                        score+=7;
                        hand="7 of Spade";
                 }else if(card.cardvalue==8){
                        score+=8;
                        hand="8 of Spade";
                 }else if(card.cardvalue==9){
                        score+=9;
                        hand="9 of Spade";
                 }else if(card.cardvalue==10){
                        score+=10;
                        hand="10 of Spade";
                 }else if(card.cardvalue==11){
                        score+=10;
                        hand="Jack of Spade";
                 }else if(card.cardvalue==12){
                        score+=10;
                        hand="Queen of Spade";
                 }else if(card.cardvalue==13){
                        score+=10;
                        hand="King of Spade";
                 }
          if(card.cardvalue==14){
                  if(score>=11)
                    score+=1;
                  else if(score<11)
                        score+=11;
                  hand="Ace of Heart";
                
                 }else if(card.cardvalue==15){
                        score+=2;
                        hand="2 of Heart";
                 }else if(card.cardvalue==16){
                        score+=3;
                        hand="3 of Heart";
                 }else if(card.cardvalue==17){
                        score+=4;
                        hand="4 of Heart";
                 }else if(card.cardvalue==18){
                        score+=5;
                        hand="5 of Heart";
                 }else if(card.cardvalue==19){
                        score+=6;
                        hand="6 of Heart";
                 }else if(card.cardvalue==20){
                        score+=7;
                        hand="7 of Heart";
                 }else if(card.cardvalue==21){
                        score+=8;
                        hand="8 of Heart";
                 }else if(card.cardvalue==22){
                        score+=9;
                        hand="9 of Heart";
                 }else if(card.cardvalue==23){
                        score+=10;
                        hand="10 of Heart";
                 }else if(card.cardvalue==24){
                        score+=10;
                        hand="Jack of Heart";
                 }else if(card.cardvalue==25){
                        score+=10;
                        hand="Queen of Heart";
                 }else if(card.cardvalue==26){
                        score+=10;
                        hand="King of Heart";
                 }
                
         if(card.cardvalue==27){
                  if(score>=11)
                    score+=1;
                  else if(score<11)
                        score+=11;
                  hand="Ace of Club";
                
                 }else if(card.cardvalue==28){
                        score+=2;
                        hand="2 of Club";
                 }else if(card.cardvalue==29){
                        score+=3;
                        hand="3 of Club";
                 }else if(card.cardvalue==30){
                        score+=4;
                        hand="4 of Club";
                 }else if(card.cardvalue==31){
                        score+=5;
                        hand="5 of Club";
                 }else if(card.cardvalue==32){
                        score+=6;
                        hand="6 of Club";
                 }else if(card.cardvalue==33){
                        score+=7;
                        hand="7 of Club";
                 }else if(card.cardvalue==34){
                        score+=8;
                        hand="8 of Club";
                 }else if(card.cardvalue==35){
                        score+=9;
                        hand="9 of Club";
                 }else if(card.cardvalue==36){
                        score+=10;
                        hand="10 of Club";
                 }else if(card.cardvalue==37){
                        score+=10;
                        hand="Jack of Club";
                 }else if(card.cardvalue==38){
                        score+=10;
                        hand="Queen of Club";
                 }else if(card.cardvalue==39){
                        score+=10;
                        hand="King of Club";
                 }
                
          if(card.cardvalue==40){
                  if(score>=11)
                    score+=1;
                  else if(score<11)
                        score+=11;
                  hand="Ace of Diamond";
                
                 }else if(card.cardvalue==41){
                        score+=2;
                        hand="2 of Diamond";
                 }else if(card.cardvalue==42){
                        score+=3;
                        hand="3 of Diamond";
                 }else if(card.cardvalue==43){
                        score+=4;
                        hand="4 of Diamond";
                 }else if(card.cardvalue==44){
                        score+=5;
                        hand="5 of Diamond";
                 }else if(card.cardvalue==45){
                        score+=6;
                        hand="6 of Diamond";
                 }else if(card.cardvalue==46){
                        score+=7;
                        hand="7 of Diamond";
                 }else if(card.cardvalue==47){
                        score+=8;
                        hand="8 of Diamond";
                 }else if(card.cardvalue==48){
                        score+=9;
                        hand="9 of Diamond";
                 }else if(card.cardvalue==49){
                        score+=10;
                        hand="10 of Diamond";
                 }else if(card.cardvalue==50){
                        score+=10;
                        hand="Jack of Diamond";
                 }else if(card.cardvalue==51){
                        score+=10;
                        hand="Queen of Diamond";
                 }else if(card.cardvalue==52){
                        score+=10;
                        hand="King of Diamond";
                 }
                
                 count++;      
        }
       
}

class Card
{
        int cardvalue;
        void randomCard()
        {
                cardvalue=(int)(Math.floor(Math.random()*52+1));
        }
}



KeyboardReader.java
 Description:
Save it as KeyboardReader.java
if u dont want to download it, u can look up the source code on my other posts, copy that and save it

Download
 Filename:  KeyboardReader.java
 Filesize:  4.47 KB
 Downloaded:  574 Time(s)

Sponsor
Sponsor
Sponsor
sponsor
Chrisd534




PostPosted: Wed May 30, 2007 5:24 pm   Post subject: RE:Text Based BlackJack(modified version of regular ones)

good job but i thing u shud make 1 with GUI
stinger4life




PostPosted: Thu May 31, 2007 4:55 pm   Post subject: Re: Text Based BlackJack(modified version of regular ones)

Stop spamming and trying to get someone to make your own program.

And upping a 3 year old thread should be a bannable offense.
Display posts from previous:   
   Index -> Programming, Java -> Java Submissions
View previous topic Tell A FriendPrintable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 3 Posts ]
Jump to:   


Style:  
Search: