1. 程式人生 > >第13章 指導學習:人機猜拳

第13章 指導學習:人機猜拳

nbsp bre bsp game 一輪 person 遊戲 exti new

趙冬梅
2018-10-15public class Person {
    String name = "匿名";         // 名字
    int score = 0;                 // 積分
    /**
     * 出拳
     * 
     * @return 出拳結果:1.剪刀 2.石頭 3.布
     */
    public int showFist() {
        // 接收用戶的選擇
        Scanner input = new Scanner(System.in);
        System.out.print("\n請出拳:1.剪刀 2.石頭 3.布 (輸入相應數字) :
"); int show = input.nextInt(); // 輸出出拳結果,並返回 switch (show) { case 1: System.out.println("你出拳: 剪刀"); break; case 2: System.out.println("你出拳: 石頭"); break; case 3: System.out.println("你出拳: 布");
break; } return show; } }


public class Computer {
       String name = "電腦"; // 名字
       int score = 0;;       // 積分
       
       /**
        * 出拳
        * @return 出拳結果:1.剪刀 2.石頭 3.布
        */
       public int showFist(){
           // 產生隨機數
           int show = (int)(Math.random()*10
)%3 + 1; //產生隨機數,表示電腦出拳 // 輸出出拳結果並返回 switch(show){ case 1: System.out.println(name+"出拳: 剪刀"); break; case 2: System.out.println(name+"出拳: 石頭"); break; case 3: System.out.println(name+"出拳: 布"); break; } return show; } }
public class Game1 {
    Person person;       //甲方
    Computer computer;   //乙方
    int count;           //對戰次數
    
    /**
     * 初始化
     */
    public void initial(){
        person = new Person();
        computer = new Computer();
        count = 0;
    }
        
    /**
     * 開始遊戲
     */
    public void startGame() {
        initial();
        System.out.println("----------------歡 迎 進 入 遊 戲 世 界----------------\n");
        System.out.println("\n\t\t******************");
        System.out.println  ("\t\t**  猜拳, 開始    **");
        System.out.println  ("\t\t******************");
        
        System.out.println("\n\n出拳規則:1.剪刀 2.石頭 3.布");
        /*選擇對方角色*/
        System.out.print("請選擇對方角色(1:劉備 2:孫權 3:曹操): ");
        Scanner input = new Scanner(System.in);
        int role = input.nextInt();
        if(role == 1){
            computer.name = "劉備";
        }else if(role == 2){
            computer.name = "孫權";
        }else if(role == 3){
            computer.name = "曹操";
        }  
        
        System.out.print("你選擇了"+computer.name+"對戰");
    }
}
public class Game2 {
    Person person;       //甲方
    Computer computer;   //乙方
    int count;           //對戰次數
    
    /**
     * 初始化
     */
    public void initial(){
        person = new Person();
        computer = new Computer();
        count = 0;
    }
        
    /**
     * 開始遊戲
     */
    public void startGame() {
        initial();  // 初始化
        System.out.println("----------------歡 迎 進 入 遊 戲 世 界----------------\n");
        System.out.println("\n\t\t******************");
        System.out.println  ("\t\t**  猜拳, 開始    **");
        System.out.println  ("\t\t******************");
        
        System.out.println("\n\n出拳規則:1.剪刀 2.石頭 3.布");
        /*選擇對方角色*/
        System.out.print("請選擇對方角色(1:劉備 2:孫權 3:曹操): ");
        Scanner input = new Scanner(System.in);
        int role = input.nextInt();
        if(role == 1){
            computer.name = "劉備";
        }else if(role == 2){
            computer.name = "孫權";
        }else if(role == 3){
            computer.name = "曹操";
        }    
        System.out.println("你選擇了 "+computer.name+"對戰");
        
        /*開始遊戲*/
        System.out.print("\n要開始嗎?(y/n) ");
        String con = input.next();
        int perFist;   //用戶出的拳
        int compFist;  //計算機出的拳
        if(con.equals("y")){
            /*出拳*/
            perFist = person.showFist();
            compFist = computer.showFist();
            /*裁決*/
            if((perFist == 1 && compFist == 1) || (perFist == 2 && compFist == 2) || (perFist == 3 && compFist == 3)){
                System.out.println("結果:和局,真衰!\n");  //平局
            }else if((perFist == 1 && compFist == 3) || (perFist == 2  && compFist == 1) || (perFist == 3 && compFist == 2)){
                System.out.println("結果: 恭喜, 你贏了!");  //用戶贏                
            }else{
                System.out.println("結果說:^_^,你輸了,真笨!\n");  //計算機贏                
            }            
        }        
    }
}
public class Game3 {
    Person person;       //甲方
    Computer computer;   //乙方
    int count;           //對戰次數
    
    /**
     * 初始化
     */
    public void initial(){
        person = new Person();
        computer = new Computer();
        count = 0;
    }
        
    /**
     * 開始遊戲
     */
    public void startGame() {
        initial();  // 初始化
        System.out.println("----------------歡 迎 進 入 遊 戲 世 界----------------\n");
        System.out.println("\n\t\t******************");
        System.out.println  ("\t\t**  猜拳, 開始    **");
        System.out.println  ("\t\t******************");
        
        System.out.println("\n\n出拳規則:1.剪刀 2.石頭 3.布");
        /*選擇對方角色*/
        System.out.print("請選擇對方角色(1:劉備 2:孫權 3:曹操): ");
        Scanner input = new Scanner(System.in);
        int role = input.nextInt();
        if(role == 1){
            computer.name = "劉備";
        }else if(role == 2){
            computer.name = "孫權";
        }else if(role == 3){
            computer.name = "曹操";
        }    
        System.out.println("你選擇了 "+computer.name+"對戰");
        
        /*開始遊戲*/
        System.out.print("\n要開始嗎?(y/n) ");
        String con = input.next();
        int perFist;   //用戶出的拳
        int compFist;  //計算機出的拳
        while(con.equals("y")){
            /*出拳*/
            perFist = person.showFist();
            compFist = computer.showFist();
            /*裁決*/
            if((perFist == 1 && compFist == 1) || (perFist == 2 && compFist == 2) || (perFist == 3 && compFist == 3)){
                System.out.println("結果:和局,真衰!嘿嘿,等著瞧吧 !\n");  //平局
            }else if((perFist == 1 && compFist == 3) || (perFist == 2  && compFist == 1) || (perFist == 3 && compFist == 2)){
                System.out.println("結果: 恭喜, 你贏了!");  //用戶贏
                person.score++;
            }else{
                System.out.println("結果說:^_^,你輸了,真笨!\n");  //計算機贏
                computer.score++;
            }
            count++;
            System.out.print("\n是否開始下一輪(y/n):  ");
            con = input.next();
        }
    }
}
public class Game4 {
    Person person;       //甲方
    Computer computer;   //乙方
    int count;           //對戰次數
    
    /**
     * 初始化
     */
    public void initial(){
        person = new Person();
        computer = new Computer();
        count = 0;
    }
        
    /**
     * 開始遊戲
     */
    public void startGame() {
        initial();  // 初始化
        System.out.println("----------------歡 迎 進 入 遊 戲 世 界----------------\n");
        System.out.println("\n\t\t******************");
        System.out.println  ("\t\t**  猜拳, 開始    **");
        System.out.println  ("\t\t******************");
        
        System.out.println("\n\n出拳規則:1.剪刀 2.石頭 3.布");
        /*選擇對方角色*/
        System.out.print("請選擇對方角色(1:劉備 2:孫權 3:曹操): ");
        Scanner input = new Scanner(System.in);
        int role = input.nextInt();
        if(role == 1){
            computer.name = "劉備";
        }else if(role == 2){
            computer.name = "孫權";
        }else if(role == 3){
            computer.name = "曹操";
        }   
        System.out.println("你選擇了 "+computer.name+"對戰");
        
        System.out.print("\n要開始嗎?(y/n) ");
        String con = input.next();
        int perFist;   //用戶出的拳
        int compFist;  //計算機出的拳
        while(con.equals("y")){
            /*出拳*/
            perFist = person.showFist();
            compFist = computer.showFist();
            /*裁決*/
            if((perFist == 1 && compFist == 1) || (perFist == 2 && compFist == 2) || (perFist == 3 && compFist == 3)){
                System.out.println("結果:和局,真衰!嘿嘿,等著瞧吧 !\n");  //平局
            }else if((perFist == 1 && compFist == 3) || (perFist == 2  && compFist == 1) || (perFist == 3 && compFist == 2)){
                System.out.println("結果: 恭喜, 你贏了!");  //用戶贏
                person.score++;
            }else{
                System.out.println("結果說:^_^,你輸了,真笨!\n");  //計算機贏
                computer.score++;
            }
            count++;
            System.out.print("\n是否開始下一輪(y/n):  ");
            con = input.next();
        }
        /*顯示結果*/
        showResult();
    }
    
    /**
     * 顯示比賽結果
     */
    public void showResult(){
        /*顯示最後結果*/
        System.out.println("---------------------------------------------------");
        System.out.println(computer.name + "  VS  " + person.name);
        System.out.println("對戰次數:"+ count);
        
        int result = calcResult();
        if(result == 1){
            System.out.println("結果:打成平手,下次再和你一分高下!");
        }else if(result == 2){
            System.out.println("結果:恭喜恭喜!");   //用戶獲勝
        }else{ 
            System.out.println("結果:呵呵,笨笨,下次加油啊!");   //計算機獲勝
        }
        System.out.println("---------------------------------------------------");
    }
    
    /**
     * 計算比賽結果
     * @return 1:戰平;2:用戶贏;3:電腦贏
     */
    public int calcResult(){
        if(person.score == computer.score){
              return 1; // 戰平
        }else if(person.score > computer.score){
              return 2; // 用戶贏
        }else{
              return 3; // 電腦贏
        }
        
    }
}
public class Game {
    Person person;       //甲方
    Computer computer;   //乙方
    int count;           //對戰次數
    
    /**
     * 初始化
     */
    public void initial(){
        person = new Person();
        computer = new Computer();
        count = 0;
    }
        
    /**
     * 開始遊戲
     */
    public void startGame() {
        
        System.out.println("----------------歡 迎 進 入 遊 戲 世 界----------------");
        System.out.println("\n\t\t******************");
        System.out.println  ("\t\t**  猜拳, 開始    **");
        System.out.println  ("\t\t******************");
        
        System.out.println("\n出拳規則:1.剪刀 2.石頭 3.布");
        
        Scanner input = new Scanner(System.in);
        String exit = "n";  // 退出系統
        
        do{
            initial();  // 初始化
            /*選擇對方角色*/
            System.out.print("請選擇對方角色(1:劉備 2:孫權 3:曹操): ");
            
            int role = input.nextInt();
            if(role == 1){
                computer.name = "劉備";
            }else if(role == 2){
                computer.name = "孫權";
            }else if(role == 3){
                computer.name = "曹操";
            }   
            
            // 擴展功能1:輸入用戶姓名
            /*輸入用戶姓名*/
            System.out.print("請輸入你的姓名:");
            person.name = input.next();
            
            System.out.println(person.name+"  VS  "+computer.name+"  對戰\n");
            // 擴展功能1結束
            
            System.out.print("要開始嗎?(y/n) ");
            String start = input.next();  // 開始每一局遊戲
            
            
            int perFist;   //用戶出的拳
            int compFist;  //計算機出的拳
            
            while(start.equals("y")){
                /*出拳*/
                perFist = person.showFist();
                compFist = computer.showFist();
                /*裁決*/
                if((perFist == 1 && compFist == 1) || (perFist == 2 && compFist == 2) || (perFist == 3 && compFist == 3)){
                    System.out.println("結果:和局,真衰!嘿嘿,等著瞧吧 !\n");  //平局
                }else if((perFist == 1 && compFist == 3) || (perFist == 2  && compFist == 1) || (perFist == 3 && compFist == 2)){
                    System.out.println("結果: 恭喜, 你贏了!");  //用戶贏
                    person.score++;
                }else{
                    System.out.println("結果說:^_^,你輸了,真笨!\n");  //計算機贏
                    computer.score++;
                }
                count++;
                System.out.print("\n是否開始下一輪(y/n):  ");
                start = input.next();    
            }
            /*顯示結果*/
            showResult();            
            
            // 擴展功能3:循環遊戲,直到退出系統
            System.out.print("\n要開始下一局嗎?(y/n):");
            exit = input.next();            
            System.out.println();
            // 擴展功能3結束
        }while(!exit.equals("n"));    
        
        System.out.println("系統退出!");
    }
    
    /**
     * 顯示比賽結果
     */
    public void showResult(){
        /*顯示對戰次數*/
        System.out.println("---------------------------------------------------");
        System.out.println(computer.name + "  VS  " + person.name);
        System.out.println("對戰次數:"+ count);
        
        // 擴展功能2:顯示最終的得分
        System.out.println("\n姓名\t得分");        
        System.out.println(person.name+"\t"+person.score);
        System.out.println(computer.name+"\t"+computer.score+"\n");
        // 擴展功能2結束
        
        /*顯示對戰結果*/
        int result = calcResult();
        if(result == 1){
            System.out.println("結果:打成平手,下次再和你一分高下!");
        }else if(result == 2){
            System.out.println("結果:恭喜恭喜!");   //用戶獲勝
        }else{ 
            System.out.println("結果:呵呵,笨笨,下次加油啊!");   //計算機獲勝
        }
        System.out.println("---------------------------------------------------");
    }
    
    /**
     * 計算比賽結果
     * @return 1:戰平;2:用戶贏;3:電腦贏
     */
    public int calcResult(){
        if(person.score == computer.score){
              return 1; // 戰平
        }else if(person.score > computer.score){
              return 2; // 用戶贏
        }else{
              return 3; // 電腦贏
        }
        
    }
}

 

  
























 

第13章 指導學習:人機猜拳