2015-11-13 53 views
-2

好了,所以我需要創建創建一個剪刀石頭布的遊戲類型用一個例子輸出看起來像這樣需要的代碼:試圖編寫一個剪刀石頭布的遊戲

Rock­Paper­Scissors ­ pick your weapon[R,P,S]:: R 
player had rock 
computer had paper 
!Computer wins <<Paper Covers Rock>>! 
Do you want to play again? y 

Rock­Paper­Scissors ­ pick your weapon[R,P,S]:: R 
player had rock 
computer had scissors 
!Player wins <<Rock Breaks Scissors>>! 
Do you want to play again? y 

Rock­Paper­Scissors ­ pick your weapon[R,P,S]:: R 
player had rock 
computer had rock 
!Draw Game! 
Do you want to play again? n 

所以我基本上完全難倒,我不知道該從哪裏做什麼,代碼需要能夠讓用戶選擇R,P或S,並讓計算機確定這樣的隨機實例。我不要求你們做整個事情對我來說(但隨時),但只是一些方向/幫助將不勝感激,所以這裏是我到目前爲止有:

RockPaperScissors類:

import java.util.Scanner; 
import static java.lang.System.*; 
import java.util.Random; 

public class RockPaperScissors 
{ 
    private String playChoice; 
    private String compChoice; 

    public RockPaperScissors 
    { 
     playChoice=null; //same as playChoice=""; 
     compChoice=null; 
    } 
    //loaded constructor 
    public RockPaperScissors(String player) 
    { 
     //add code here to call the setPlayers method 
    } 
    //set playChoice based on player choice and compChoice based on random number 
    public void setPlayers (String player) 
    { 
     //switch case 
     //R-player="rock" 
     //P-player="paper" 
     //S-player="scissors" 

     //int num=random number 0 1 or 2 
     //switch case 
     //0-computer="rock" 
     //1-computer="paper" 
     //2-computer="scissors" 
    } 
    //using logic and possible player and computer choices, determine a winner and return the string that informs the player who won 
    public String determineWinner() 
    { 
     String winner=""; 
     //if playChoice is the same as compChoice no winner (draw) 
     //else-if-else if statements to compare playChoice and compChoice to find the winner 
     return winner; 
    } 
    public String toString() 
    { 
     String output=""; 
     output=output + "player had " + playChoice+"\n; 
     output+="computer had "+ compChoice; 
     return output; 
    } 
}  

和轉輪類:

import java.util.Scanner; 
import static java.lang.System.*; 

public class RPSRunner 
{ 
    public static void main(String args[]) 
    { 
     weapon[R,P,S]:: "); 
     RockPaperScissors(keyboard.next()); 
     Scanner keyboard = new Scanner(System.in); 
     char response; 
     do{ 
      System.out.print("Rock­Paper­Scissors ­ pick your 
      RockPaperScissors test = new 
      System.out.println(test); //uses toString method 
      System.out.println(test.determineWinner()+"\n"); 
      System.out.print("Do you want to play again? "); 
      response = keyboard.next().charAt(0); 
     }while(response=='Y'||response=='y'); 
    } 
} 

感謝任何及所有的幫助。

如這裏要求的是整個分配:

LAB描述:程序搖滾,剪子,布的一個簡單的遊戲。生成隨機選擇

爲電腦播放器。接下來,該玩家將作出選擇。最後,你選擇兩個選項

並查看哪個選手是贏家。你將需要使用隨機數字。

幫助隨機數,可以在這裏(https://goo.gl/YgH1ld

+0

爲什麼不使用字符,並在跑步者中運行整個遊戲?你可以使用'keyboard.next()。charAt(0)'作爲R,P或S,你也可以爲你的Y/N做'keyboard.next()。toLowerCase()。charAt(0)'; –

+0

這篇文章可能有些幫助http://stackoverflow.com/questions/9553058/scalable-solution-for-rock-paper-scissor?rq = 1 – Raf

+0

嗯,這是班級的任務,所以它必須按照我發佈的內容完成,但是我知道你來自哪裏。 – jbinx

回答

0

找到你需要能夠:

  1. 打印一個提示,要求選擇武器(DONE)
  2. 以一個輸入武器
  3. 挑選電腦玩家隨機武器
  4. 確定誰贏得
  5. P RINT一個提示,要求再次播放(DONE)
  6. 以輸入再次播放(DONE)
  7. 循環回到步驟1(DONE)

所以,你需要實現2-4。

主要問題是跑步者的破損。我張貼修正版本低於它實現2(但不驗證它的R,P,或S):

import java.util.Scanner; 
import static java.lang.System.*; 

public class RPSRunner 
{ 
    public static void main(String args[]) 
    { 
     RockPaperScissors(keyboard.next()); 
     Scanner keyboard = new Scanner(System.in); 
     char response; 
     do{ 
      System.out.print("Rock­Paper­Scissors ­ pick your weapon[R,P,S]:: "); 
      String player = keyboard.next(); 
      RockPaperScissors test = new RockPaperScissors(player); 
      System.out.println(test); //uses toString method 
      System.out.println(test.determineWinner()+"\n"); 
      System.out.print("Do you want to play again? "); 
      response = keyboard.next().charAt(0); 
     }while(response=='Y'||response=='y'); 
    } 
} 

與實驗室片開始。你認爲這裏需要什麼?

public RockPaperScissors(String player) 
    { 
     //add code here to call the setPlayers method 
    }