2015-10-26 21 views
0

您好我一直在到處找,確定在指定的內容被認爲是不確定的:https://open.kattis.com/problems/rockpaperscissors剪刀石頭布kattis:測試用例

我通過第一次測試,但未能保持第二。我原本以爲我沒有通過第二次測試,因爲我只允許兩名球員參加比賽,我們必須允許多名球員加入。

我修正了這個問題,但仍未通過第二次測試。

我把我認爲是未定義的但我完全卡住了。

所以問:有人可以給我一個測試用例,讓我朝正確的方向發展嗎?

我不是要求代碼,而是測試用例,如果您注意到我在代碼中輸入了錯誤信息,請告知這是否是導致我的問題的原因。

import java.io.*; 
import java.util.*; 

public class RockPaperScissor { 
/**Conditions****** 
    * 
*/ 
public static void main(String[] args) { 
    /************User Input************/ 
    Scanner sc = new Scanner(System.in); 
    /*************Variables***************/ 

    String Player1; 
    String Player2; 
    String game=""; 
    String playerGame=sc.nextLine(); 
    //System.out.println("PlayerA= " +numOfGames); 


//System.out.println(game); 
/**run through games until game=0;**/ 
do{ 


    String[] playerGameSplit = playerGame.split("\\s+"); 
    int numOfPlayers=Integer.parseInt(playerGameSplit[0]); 
    double numOfGames=(Integer.parseInt(playerGameSplit[1])*numOfPlayers*(numOfPlayers-1))/2; 
// double player1Win=0, player2Win=0, player1Loss=0, player2Loss=0; 
    Integer[] player = new Integer[numOfPlayers]; 
    Integer[] playerLoss = new Integer[numOfPlayers]; 
    for(int x=0; x<player.length;x++){ 
     player[x]=0; 
     playerLoss[x]=0; 
    } 
    /*****Runs Rock Paper Scissors Game*****/ 
    for (int x = 0; x<numOfGames; x++) { 
     /**Starts first game**/ 
     game=sc.nextLine(); 
     String[] splitter = game.split("\\s+"); 
     if(compare(splitter[1],splitter[3])==1){ 
       player[Integer.parseInt(splitter[0])-1]=player[Integer.parseInt(splitter[0])-1]+1; 
      playerLoss[Integer.parseInt(splitter[2])-1]=playerLoss[Integer.parseInt(splitter[2])-1]+1; 
     }else if(compare(splitter[1],splitter[3])==-1){ 
      player[Integer.parseInt(splitter[2])-1]=player[Integer.parseInt(splitter[2])-1]+1; 
      playerLoss[Integer.parseInt(splitter[0])-1]=playerLoss[Integer.parseInt(splitter[0])-1]+1; 
     } 
    } 

       /***Prints Required OutPut***/ 
    for(int x=0; x<player.length;x++){ 


     //if(player[x]==0&&playerLoss[x]==0){ 
      ////double ratio= player[x]/(player[x]+playerLoss[x]); 
      //System.out.println("-"); 
     //}else 
     if(Double.isInfinite((double)player[x]/(double)(player[x]+playerLoss[x]))){ 
      //double ratio= player[x]/(player[x]+playerLoss[x]); 
      System.out.println("-"); 
     }else if(Double.isNaN((double)player[x]/(double)(player[x]+playerLoss[x]))){ 
      System.out.println("-"); 
     }else{ 
      double ratio= (double)player[x]/(double)(player[x]+playerLoss[x]); 
     System.out.printf("%.3f", ratio); 
     System.out.println(); 
     } 
    } 

    System.out.println(); 
    /**moves Game to next one**/ 
    playerGame=sc.nextLine(); 


    }while(playerGame.matches("[\\d][\\s][\\d]")); 
    //System.out.println("do we make here");  
    } 

       /*********METHODS*******/ 
    public static Integer compare(String Player1,String Player2){ 
    //System.out.println(Player1+ " "+ Player2); 
    if(Player1.matches("rock")&&Player2.matches("scissors")){ 
    return 1; 
    }else 
if(Player2.matches("rock")&&Player1.matches("scissors")){ 
    return -1; 
}else 
if(Player1.matches("paper")&&Player2.matches("rock")){ 
    return 1; 
}else 
if(Player2.matches("paper")&&Player1.matches("rock")){ 
    return -1; 
}else 
if(Player1.matches("scissors")&&Player2.matches("paper")){ 
    return 1; 
}else 
if(Player2.matches("scissors")&&Player1.matches("paper")){ 
    return -1; 
    }else 
     return 0; 

    } 
} 

    /***********Sample Input*************/ 

    /** 
    2 4 
1 rock 2 paper 
1 scissors 2 paper 
1 rock 2 rock 
2 rock 1 scissors 
2 1 
1 rock 2 paper 
3 1 
1 rock 2 paper 
3 rock 2 paper 
1 rock 3 paper 
0 
**/ 

/***********Sample Output*************/ 
/** 
0.333 
0.667 

0.000 
1.000 
**/ 
+0

到目前爲止,您是否有過任何測試用例?另外,爲了使測試更容易,請嘗試使用更多功能。例如,你可以有一個函數解析playerGameSplit並設置2個變量「numOfPlayers」和「numOfGames」。然後,您可以輕鬆地測試該功能,並在轉到下一部分之前確定該代碼段是否正常工作。 – PunDefeated

+0

這些在線代碼提交從未使用過對象嗎?我還沒有看到很多(超出我自己)那些模擬玩家或回合或錦標賽的模型。如果你做了這些事情,玩家的平均數將會是微不足道的。 –

+0

@EdwinBuck 好的我做了你所說的但仍然短小,所以如果你可能看看我的新代碼,並讓我知道你的想法。 – Paragon

回答

1

既然您有玩家,您可以單元測試玩家。

import org.junit.Test; 
import org.junit.Assert; 

public class PlayerTest { 

    @Test 
    public void newPlayerRatio() { 
    try { 
     Player player = new Player(); 
     Assert.assertEquals(0.0d, player.getWinRatio(), 0.001d); 
    } catch (Exception e) { 
     Assert.fail(String.format("Unexpected Exception thrown of type %s", e.getClass())); 
    } 
} 

只有在隔離部分代碼時才能完成代碼的部分測試。這就是爲什麼我要求這些類,他們封裝了一些東西。封裝意味着它們包含一些行爲。一旦程序的一部分包含(完全負責)某些行爲,就很容易測試。

由於我的經驗,我可以說你會在上面的測試中找到意想不到的結果;因爲,在你做之前,你不檢查是否要除以零。

寫幾個額外的單元測試(測試與勝利比例的球員有一勝,一勝和一損失等)。你有六打之後,轉到代碼的下一部分。

最終你會有一些排序或讀者需要一行輸入和更新錦標賽。一旦你到達那個部分,你可以嘗試給它一行代碼,看看它是否在比賽中用「正確」值調用「正確」的方法。可能的情況是有一些角落案件正在扼殺你的程序。一旦你完成了一系列測試,任何破壞代碼單元做假設的變化都會使測試立即失敗。爲此,我建議你看看Apache的Maven(或者類似的東西)來自動化你的編譯運行測試(或者至少看看你的IDE,看看如何在每次代碼更改時運行測試)。

+0

嗯,我從來沒有使用過Junit,這看起來非常有益,謝謝你提供你的見解。我正在研究這個,看看我找到了什麼。 – Paragon