//程式設計師 : 劉冠麟(花花)(PC016)  

//程式目的 : 龜兔賽跑

//繳交日期 : 2007/12/25

import java.util.Random; //叫亂數

public class TRGame
{
        public static void main (String args[])
      {
                Random r = new Random();
               
                int a = 1;//宣告一個整數變數,紀錄烏龜

                int b = 1;//宣告一個整數變數,紀錄兔子

                int c = 1;//宣告一個整數型,紀錄回合數

                boolean z = false;  //宣告一個布林變數,紀錄比賽結果
                               
                while( z != true )

                {

                   System.out.printf( "第 %-2d 回合\n" , c);

                   if ( a == b )

                        System.out.println( "目前狀況:平手\n");

                   else

                   {

                          if ( a

                              System.out.println( "目前狀況:兔子先生領先!\n"); 

                          if ( a >= b )

                              System.out.println( "目前狀況:烏龜先生領先!\n");    

                   }  

                      int a1 = r.nextInt(10);//宣告一個整數型別的變數a1, 用來操作烏龜的動作

                      switch( a1 )

                            {
                             case 0:

                             case 1:                               
                                   System.out.println( "烏龜滑一跤!向後退6步!");
                                   a -= 6;    //20%的機率烏龜滑倒, 退後 6個單位
                                   break;

                             case 2:

                             case 3:

                             case 4:
                                   System.out.println( "烏龜慢速移動前進1步!");
                                   a += 1;   //30%的機率烏龜前進一小步, 前進 1個單位
                                   break;

                            default:
                                   System.out.println( "烏龜快速移動前進3步!");
                                   a += 3;    //50%的機率烏龜前進一大步, 前進 3個單位                                                  
                            }
                     
                      int b1 = r.nextInt(10);//宣告一個整數型別的變數b1, 用來操作兔子的動作

                      switch( b1 )

                            {

                             case 0:
                                   System.out.println( "兔子大滑一跤,後退12步!");
                                   b -= 12;  //10%的機率兔子滑倒, 後退12個單位
                                   break;

                             case 1:                   
           
                             case 2:     
                                   System.out.println( "兔子睡著了,原地停留1回合!"); //20%的機率兔子原地停留一回合
                                   break;                                                       
                             case 3:

                             case 4:
                                   System.out.println( "兔子小滑一跤,後退2步!");
                                   b -= 2;   //20%的機率兔子滑倒, 後退 2單位
                                   break;

                             case 5:

                             case 6:

                             case 7:
                                   System.out.println( "兔子小跳躍,前進1步!");
                                   b += 1;   //30%的機率兔子前進 1單位
                                   break;    
 
                            default:
                                   System.out.println( "兔子大跳躍,前進9步!");
                                   b += 9;   //20%的機率兔子前進 9單位         
                                                                           
                            }

                      if( a                           a = 1;

                      if( b                           b = 1;

                      if( a > 70)
                          a = 70;

                      if( b > 70)
                          b = 70;             
   
                      System.out.printf( "\n目前烏龜所在位置: %-2d\n目前兔子所在位置: %-2d\n", a, b);

                      for(int i = 1; i

                          {
                              System.out.print( a == i ? "T" : "*"); //以圖顯示位置
                          }

                      System.out.println();

                      for(int i = 1; i

                          {
                              System.out.print( b == i ? "R" : "*"); //以圖顯示位置
                          }

                      System.out.println();
                     
                      if ( a >= 70)

                        {
                            System.out.println( "\n\n抵達終點!烏龜獲勝!");
                            z = true;   //當烏龜 >= 70,烏龜獲勝 變數c為真
                        }

                      if ( b >= 70)

                        {
                            System.out.println( "\n\n抵達終點!兔子獲勝!");
                            z = true;  //當兔子 >= 70,兔子獲勝 變數c為真
                        }

                      c++;   //避免無限迴圈    
                              
                }
     &nbsp

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 a25561970 的頭像
    a25561970

    萬花筒

    a25561970 發表在 痞客邦 留言(0) 人氣()