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

Username:   Password: 
 RegisterRegister   
 Rock, Scissor, Paper Game
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:45 pm   Post subject: Rock, Scissor, Paper Game

code:

/*Tat Wing Ho
Save it as thRSPGameMain.java
KeyboardReader is attached which u must need and put it in the same folder as thRSPGameMain.java
 *Rock, Scissor, Paper Game */
class RSPPlayer
{
        String name, hand;
        int wins=0;
        KeyboardReader reader;
        int choice;
       
        RSPPlayer()
        {
                hand="Rock";
                name="Player";
                reader=new KeyboardReader();
        }
       
        void showInfo()
        {
                System.out.println(name + " - " + wins + "wins");
        }
       
        void setHandPlayer()
        {
                do
                {
                        choice=reader.readInt("\nPlease enter 1, 2 or 3\n1)Rock\n2)Sissor\n3)Paper\n");
                        if(choice==1)
                                hand="Rock";
                                else if(choice==2)
                                        hand="Scissor";
                                        else if(choice==3)
                                                hand="Paper";
                                                else
                                                        System.out.print("No acceptable hand");
                        }while(choice<1 || choice>3);
                        System.out.print(name + hand + "\n");
        }
       
        void setHandComp()
        {
                        choice=(int)Math.floor(Math.random()*3+1);
                        if(choice==1)
                                hand="Rock";
                                else if(choice==2)
                                        hand="Scissor";
                                        else if(choice==3)
                                                hand="Paper";
                        System.out.print(name + hand + "\n");
        }
       
        void wins()
        {
                System.out.println(name + " Wins!!!\n");
                wins++;
        }
}

class RSPGame
{
        RSPPlayer player1, player2;
        String hand1, hand2;
       
        RSPGame()
        {
                player1=new RSPPlayer();

                player2=new RSPPlayer();
               
        }
       
        void title()
        {
                System.out.print("Rock, Scissor, Paper Game");
        }
       
        void setNames()
        {
                player1.name="Player1";
                player2.name="Computer";
        }
       
        void play()
        {
                title();
                setNames();

                do
                {
                player1.setHandPlayer();
                player2.setHandComp();
                hand1=player1.hand;
                hand2=player2.hand;
                               
                if(hand1.equals("Rock") && hand2.equals("Scissor") || hand1.equals("Scissor") && hand2.equals("Paper") || hand1.equals("Paper") && hand2.equals("Rock"))
                        player1.wins();
                        else if(hand2.equals("Rock") && hand1.equals("Scissor") || hand2.equals("Scissor") && hand1.equals("Paper") || hand2.equals("Paper") && hand1.equals("Rock"))
                                player2.wins();
                                else if(hand1.equals("Rock") && hand2.equals("Rock") || hand1.equals("Scissor") && hand2.equals("Scissor") || hand1.equals("Paper") && hand2.equals("Paper"))
                                        System.out.println("Tie!!!");
                }while(player1.wins !=2 && player2.wins !=2);
                if(player1.wins>player2.wins){
                        player1.showInfo();
                        player2.showInfo();
                        System.out.print("\n"+player1.name + "has the highest score!");
                        }else if(player2.wins>player1.wins){
                                player1.showInfo();
                                player2.showInfo();
                                System.out.println("\n"+player2.name + " has the highest score!");
                                }
               
                               
                System.out.println("\nThank you for playing");
        }
}

class thRSPGameMain
{
        static RSPGame game=new RSPGame();
        public static void main(String args[])
        {
                game.play();
        }
}



KeyboardReader.java
 Description:

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

Sponsor
Sponsor
Sponsor
sponsor
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  [ 1 Posts ]
Jump to:   


Style:  
Search: