2016-09-19 4 views
0

플레이어와 컴퓨터 롤 주사위가 하나 또는 둘 모두가 250에 도달 할 때까지 코드를 작성하려고합니다 (매듭이 가능함). 플레이어와 컴퓨터는 3 가지 중 하나를 선택할 수 있습니다. 1 - 24 양면 넥타이, 2 - 10 양면 다이 또는 3 - 6 양면 다이. 주사위가 모두 같으면 10, 6면 주사위에 보너스가 주어집니다. 플레이어가 도착하면 2 개의 "호수"가 있습니다. 플레이어가 호수 시작 직전의 낮은 숫자로 돌아 가야 할 때, 늪지에서 플레이어가 움직일 때마다 움직이는 진흙탕 늪이 있습니다. 절반. 10 스팟 (10, 20, 30, 40 ETC)마다 무작위로 카드를 뽑습니다. 11 개 개의 카드는 플레이어가 무작위로 얻을 수 있습니다두 명의 플레이어가있는 주사위 게임, 3 개의 주사위 선택과 카드

1-4 : 플레이어는 1-6로 앞서 임의의 금액을 이동

5 : 플레이어가 4-11 (무작위 8 + 4에서 앞서 임의의 금액을 이동)

6 : 다른 플레이어 (아래 참조)되는 경우에 플레이어가 이동

7 : 플레이어는 다시 랜덤 이동 : 플레이어는 처음으로 되돌아 위치 0 (이동)

8-9 이동 금액 : 1-6

10-11 : 플레이어가 4-11에서 임의의 금액을 뒤로 이동합니다.

몇 가지 문제점이 있습니다. 내 첫번째 문제는 주사위 굴림이 매 턴마다 바뀌지 않는다는 것입니다. 따라서 3 개의 다이를 선택하면 3 개의 난수가 생길 수 있습니다. 만약 내가 다시 그 다이를 선택하면 같은 3 개의 숫자를 얻을 것입니다.

플레이어를 올바르게 업데이트하지 못할 수도 있습니다. 플레이어가 총 18 점을 굴리고 다음 턴에 14를 굴리면 18에서 14로 바뀝니다.

내 세 번째 문제는 내가 호수, 진흙 패치에 대한 인쇄문을 작성한 것과 상관없는 것처럼 보입니다. 우승자 발표는 항상 인쇄됩니다. 나는 다른 것들을 시도했지만 아무것도 작동하지 않는 것 같습니다.

필자는 코드 작성시 새로운 내용으로 (제 4 번째 작성된 프로그램입니다) 무엇이 잘못되었는지를 알기 위해 광범위한 지식이 없습니다. 코드를 전문적으로 수행 할 필요는 없으며 제대로 작동하기를 바랍니다. 모든 도움이 크게 감사드립니다.

/*This program will create a "Board" game. Each player can choose 
    from several different types of die. The computer and user will take 
    turns "rolling" a dice. There are several obstacles that can send one 
    of the players back. The goal is to get above 250*/ 

    import java.util.*; 

    public class Project4 { 

public static void main(String[] args) { 
    Scanner in=new Scanner(System.in); 
    //assigning variables 
    int p1, p2; 
    p1=p2=0; 
    int spacesmoved = 0; 

    //Setting up the randomization of the 24 sided die 
    int minimum1 = 1; 
    int maximum1 = 24; 
    Random rn1 = new Random(); 
    int range1 = maximum1 - minimum1 + 1; 
    int die1 = rn1.nextInt(range1) + minimum1; 

    //Setting up the randomization of the 10 sided die 
    int minimum2 = 1; 
    int maximum2 = 10; 
    Random rn2 = new Random(); 
    int range2 = maximum2 - minimum2+ 1; 
    int die2 = rn2.nextInt(range2) + minimum2; 
    int die22 = rn2.nextInt(range2) + minimum2; 
    int die222 = rn2.nextInt(range2) + minimum2; 

    //Setting up the randomization of the 6 sided die 
    int minimum3 = 1; 
    int maximum3 = 10; 
    Random rn3 = new Random(); 
    int range3 = maximum3 - minimum3+ 1; 
    int die3 = rn3.nextInt(range3) + minimum3; 
    int die33 = rn3.nextInt(range3) + minimum3; 
    int die333 = rn3.nextInt(range3) + minimum3; 

    //Setting a loop for the players to take turns until one, or both, reach > 250 
    while (p1 <= 250 && p2 <= 250) { 
     {System.out.println(" Current positions. Player: " + p1 + " Computer: " + p2); 
      System.out.println("Which die would you like to roll? die1(1) = one 24-sided die, die2(2) = two 10-sided dice, die3(3) = three 6-sided dice: "); 
     String diechoice = in.nextLine().toLowerCase(); 

     //Getting the die roll if the player chooses the 24 sided die 
     if (diechoice.equals ("1")) { 
      spacesmoved = (die1); 
     System.out.println("Player rolled a " + die1); 
     System.out.println("Player moves forward " + die1 +" spaces"); 
     p1+=spacesmoved; 

     } 

     //Getting the die roll if the player chooses the two 10 sided die 
     if (diechoice.equals ("2")) { spacesmoved = (die2 + die22); 
     System.out.println("First die is " + die2);//TESTTTT 
     System.out.println("Second die is a " + die22);//TEST 
     System.out.println(die2 + die22);//TESTTTTtttt 
      if (die2 == die22); { 
      spacesmoved = (die2 + die22 + die222); 
      System.out.println("Player rolled doubles, player gets to roll a 3rd 10 sided die"); 
      System.out.println("Players 3rd dice roll is " + die222); 
      System.out.println("Player moves forward a total of " + spacesmoved + " spots"); 
      p1 += spacesmoved; 
     } 
    // player1spot = (currentspot + spacesmoved); 
     } 

     //Getting the die roll if the player chooses three 6 sided die 
     if (diechoice.equals("3")) { spacesmoved = (die3 + die33 + die333); 
     System.out.println("die 1 is " + die3); 
     System.out.println("die 2 is " + die33); 
     System.out.println("die 3 is " + die333); 
     System.out.println("Player 1 moves forward a total of " + spacesmoved + " spots"); 
     { if (die3 == die33) 
      if (die33 == die333) 
      spacesmoved = (spacesmoved * 2); 

     p1 += spacesmoved; 
     }} 
     /*Setting up the lakes and muddy patch. If the player lands in a lake he goes back 
     to the lower edge of the lake. If in the mud his moves are cut in half ONLY while in the mud */ 

     {if (spacesmoved >= (83) || spacesmoved <= (89)); spacesmoved = (82); 
     System.out.println("Player landed in a lake, player goes back to space " + spacesmoved); 
     if (spacesmoved >= (152) || spacesmoved <= (155)); spacesmoved = (151); 
     System.out.println("Player landed in a lake, player goes back to space " + spacesmoved); 
     if (spacesmoved >= (201) || spacesmoved <= (233)); spacesmoved = (spacesmoved/2); 
     System.out.println("Player landed in mud, players turns are cut in half until player gets out"); 
     } 
     //Setting up the random cards if the player lands on a 10 
     if (p1 % 10==0); 
     { int minimum4 = 0; 
     int maximum4 = 11; 
     Random rn4 = new Random(); 
     int range4 = maximum4 - minimum4 + 1; 
     int card = rn4.nextInt(range4) + minimum4; 

     //if player gets a card that moves them ahead a random number between 1-6 
     if (card >=4); 
      int minimum = 0; 
      int maximum = 6; 
      Random rn = new Random(); 
      int range = maximum - minimum + 1; 
      int cardmove = rn.nextInt(range) + minimum; 
      p1 = cardmove; 

     //if player gets a card that moves them ahead a random number between 4-11 
     if (card == 5); 
      int minimum5 = 4; 
      int maximum5 = 11; 
      Random rn5 = new Random(); 
      int range5 = maximum5 - minimum5 + 1; 
      int cardmove5 = rn5.nextInt(range5) + minimum5; 
      p1 = cardmove5; 
     //if player gets a card that moves them to the spot of the other player 
     if (card == 6); 
      p2 = p1; 


     //if player gets a card that moves them back to 0 (moves location to 0) 
     if (card ==7); 
      p1 = 0; 

     //if player gets a card that moves them back between 1-6 spaces 
     if (card == (8) || card == 9); 
      int minimum6 = 1; 
      int maximum6 = 6; 
      Random rn6 = new Random(); 
      int range6 = maximum6 - minimum6 + 1; 
      int cardmove6 = rn6.nextInt(range6) + minimum6; 


     //if player gets a card that moves them back between 4-11 spaces 
     if (card == (10) || card == 11); 
      int minimum7 = 4; 
      int maximum7 = 11; 
      Random rn7 = new Random(); 
      int range7 = maximum7 - minimum7 + 1; 
      int cardmove7 = rn7.nextInt(range7) + minimum7; 
     } 
      //Setting up the computers turn 

     System.out.println("Computers turn"); 
     { 
     int minimum = 0; 
     int maximum = 2; 
     Random rn = new Random(); 
     int range = maximum - minimum + 1; 
     int computersturn = rn.nextInt(range) + minimum; 

     //If computer randomly chooses a 24 sided die 
     spacesmoved = (die1); 
     System.out.println("Computer rolled a " + die1); 
     System.out.println("Computer moved " + die1 +" spaces"); 
     p2+=spacesmoved; 

     } 

     //If the computer randomly chooses the two 10 sided die 
     if (diechoice.equals ("die2")) { spacesmoved = (die2 + die22); 
     System.out.println("First die is " + die2);//TESTTTT 
     System.out.println("Second die is a " + die22);//TEST 
     System.out.println(die2 + die22);//TESTTTTtttt 
      if (die2 == die22); { 
      spacesmoved = (die2 + die22 + die222); 
      System.out.println("Computer rolled doubles, player gets to roll a 3rd 10 sided die"); 
      System.out.println("Computer 3rd dice roll is " + die222); 
      System.out.println("Computer moves a total of " + spacesmoved + " spots"); 
      p2 += spacesmoved; 
     } 

     } 

     //If the computer randomly chooses three 6 sided die 
     if (diechoice.equals("die3")) { spacesmoved = (die3 + die33 + die333); 
     System.out.println("die 1 is " + die3); 
     System.out.println("die 2 is " + die33); 
     System.out.println("die 3 is " + die333); 
     System.out.println("Computer 1 moves a total of " + spacesmoved + " spots"); 
     { if (die3 == die33) 
      if (die33 == die333) 
      spacesmoved = (spacesmoved * 2); 

     p2 += spacesmoved; 


     } 




     //Setting the lakes and mud for the computer 

     if (spacesmoved >= (83) || spacesmoved <= (89)); spacesmoved = (82); 
     System.out.println("Computer landed in a lake, player goes back to space " + spacesmoved); 
     if (spacesmoved >= (152) || spacesmoved <= (155)); spacesmoved = (151); 
     System.out.println("Computer landed in a lake, player goes back to space " + spacesmoved); 
     if (spacesmoved >= (201) || spacesmoved <= (233)); spacesmoved = (spacesmoved/2); 
     System.out.println("Computer landed in mud, players turns are cut in half until player gets out"); 

     //Setting up the cards for the computer 
     if (p1 % 10==0); 
     { int minimum4 = 0; 
     int maximum4 = 11; 
     Random rn4 = new Random(); 
     int range4 = maximum4 - minimum4 + 1; 
     int card = rn4.nextInt(range4) + minimum4; 

     //if computer gets a card that moves them ahead a random number between 1-6 
     if (card >=4); 
      int minimum = 0; 
      int maximum = 6; 
      Random rn = new Random(); 
      int range = maximum - minimum + 1; 
      int cardmove = rn.nextInt(range) + minimum; 

     //if computer gets a card that moves them ahead a random number between 4-11 
     if (card == 5); 
      int minimum5 = 4; 
      int maximum5 = 11; 
      Random rn5 = new Random(); 
      int range5 = maximum5 - minimum5 + 1; 
      int cardmove5 = rn5.nextInt(range5) + minimum5; 

     //if computer gets a card that moves them to the spot of the other player 
     if (card == 6); 
      p1 = p2; 


     //if computer gets a card that moves them back to 0 (moves location to 0) 
     if (card ==7); 
      p1 = 0; 

     //if computer gets a card that moves them back between 1-6 spaces 
     if (card == (8) || card == 9); 
      int minimum6 = 1; 
      int maximum6 = 6; 
      Random rn6 = new Random(); 
      int range6 = maximum6 - minimum6 + 1; 
      int cardmove6 = rn6.nextInt(range6) + minimum6; 


     //if computer gets a card that moves them back between 4-11 spaces 
     if (card == (10) || card == 11); 
      int minimum7 = 4; 
      int maximum7 = 11; 
      Random rn7 = new Random(); 
      int range7 = maximum7 - minimum7 + 1; 
      int cardmove7 = rn7.nextInt(range7) + minimum7; 
     } 
     }  
     //Writing a final statment showing the winner, or if both tied. 
     {  if (p1 > p2); 
     System.out.println("Player 1 wins! Good job!"); 

     if (p2 >p1); 
      System.out.println("Computer wins! Better luck next time!"); 

     if (p2 == p1); 
      System.out.println("The game ends in a tie!");  
     } 
    } 

    } 




} 

} 여기

+1

한 번에 하나 질문하기 –

답변

2

난 당신이 언급 한 세 가지 문제와 관련하여 발견 것들입니다 :

문제 번호 1 :

당신은에 주사위의 값을 설정하는 코드 실행의 시작. 그 시점부터, 당신은 전혀 변하지 않습니다. 그것이 항상 매번 같은 숫자를 굴리는 문제의 원인입니다. die1 또는 다른 다이 변수를 사용할 때마다 파일 상단의 코드를 다시 실행하지만 생각하지 않을 수도 있습니다.

파일 상단의 코드는 한 번만 실행되고 그 변수에 저장된 값은 나머지 프로그램 실행에 사용됩니다. 당신이 그것을 바꿀 때까지.그래서 좀 더 다음과 같은 것을 원할 것입니다 :

//Getting the die roll if the player chooses the 24 sided die 
if (diechoice.equals ("1")) { 
    die1 = rn1.nextInt(range1) + minimum1; 
    System.out.println("Player rolled a " + die1); 
    System.out.println("Player moves forward " + die1 +" spaces"); 
    p1+=die1; 
} 

또한 주사위를 굴리는 다른 경우에도 변경해야합니다.

이렇게하는 또 다른 이점은 실제로 하나의 난수 생성기 만 있으면된다는 것입니다. 각 죽을 때마다 실제로는 필요하지 않습니다. 모든 다이 롤에 대해 동일한 것을 사용할 수 있습니다.

번호 2 문제 : 정말 잘못이가는 무언가가있는 경우, 다이 롤 잘못되어 가고,하지만 난 당신이 원하는 볼 수있는 곳 몇 군데를 알아 차렸다 정확히 모르겠어요

P1과 P2를 수행하는 내용을 변경 :

  • 플레이어가 그들보다 앞서 이동 카드를 얻을 때, 당신은 대신 =+=를 사용하는 것이 좋습니다. 즉 p1 += cardmove5 대신 p1 = cardmove5
  • 플레이어가 카드를 다시 가져 오면 p1 -= cardmove 문을 추가하는 것을 잊은 것처럼 보입니다.
  • 또한 p1과 p2가 올바른 위치에 있는지 확인하십시오. 예를 들어, 컴퓨터의 차례에서 카드를 가져와 다른 플레이어의 자리로 옮길 생각이라면 p2 = p1을 쓰는 것이지만 p1 = p2이됩니다. 컴퓨터가 0으로 되돌아가는 것과 같습니다. p1 = 0이 있지만, p2 = 0을 원하는 것처럼 보입니다. 그러니 조심하십시오. 그것은 당신이 || 운영자 곳을 사용하고 있다는 사실에 의한 것처럼

    이 문제는 같습니다

문제 번호 3 (. 또한 복사 붙여 넣기에주의 나는 그 일이 왜 추측하고있어) &&을 사용해야합니다. ||을 사용하면 효과적으로 "또는"이라고 말합니다. 그래서 첫 번째 문

if (spacesmoved >= (83) || spacesmoved <= (89)) 

는 "spacesmoved보다 크거나 같은 83 또는 미만 또는 89와 동일한 경우"... 초 동안 그것에 대해 생각으로 읽습니다. 83보다 크지 않거나 89보다 작은 숫자가 있습니까? 내 대답은 아니오 야. 모든 숫자가이 조건을 충족시킵니다. 당신은 의미 "와"하는 &&을 사용할 것 같은 :

if (spacesmoved >= (83) && spacesmoved <= (89)) 

단지 사이의 숫자 일 것이다, "보다 작거나 89와 같은 83 보다 크거나 같은 spacesmoved 경우" 83부터 89까지.

해당 블록과 다른 유사한 블록에있는 "if"문 뒤에 세미콜론을 제거 할 수도 있습니다. 그렇지 않으면 해당 조건의 코드가 실행되지 않습니다. 그것은 실제로 일어날 때 찾아내는 진짜로 거친 벌레이다.

"if"조건에서 여러 항목을 실행하려면 중괄호 {}으로 묶어야합니다. 그렇지 않으면 첫 번째 줄만 조건에 포함되고 그 이후의 모든 내용은 조건에 포함됩니다 행은 무조건 실행됩니다. 이것은 세 번째 문제를 일으키는 또 다른 사실입니다.

마지막으로 "else if"및 "else"문을 사용해야합니다. 코드 흐름을 이해하는 데 도움이됩니다. 나는 당신을 위해 모든 일을하지 않을거야,하지만이 코드 블록은 아마 다음과 같아야합니다

if (p1 >= (83) && p1 <= (89)) 
{ 
    p1 = (82); 
    System.out.println("Player landed in a lake, player goes back to space " + p1); 
} 
else if (p1 >= (152) && p1 <= (155)) 
{ 
    p1 = (151); 
    System.out.println("Player landed in a lake, player goes back to space " + p1); 
} 
else if (p1 >= (201) && p1 <= (233)) 
{ 
    spacesmoved = (spacesmoved/2); 
    p1 -= spacesmoved; 
    System.out.println("Player landed in mud, players turns are cut in half until player gets out"); 
} 

당신은 잘 배우고, 당신이 보인다

보너스 팁 코드 흐름을 꽤 잘 생각하고 있습니다. 계속해서 일하고 배우면 그걸 얻을 수 있습니다.

괄호 사용법을 살펴보십시오. 그 (것)들을 사용하여 아무것도 아프지 않으나, 당신이 필요로하는 것보다 더 많이 사용하고 있습니다.

행운을 빈다. 그리고 계속 배우십시오!