2017-03-12 22 views
0

我正在做一個項目,讓用戶登錄,然後要求用戶選擇要玩哪個遊戲,然後編寫遊戲代碼。除了能夠在兩場比賽之間進行選擇之外,我已經做了一切。如何做到這一點的任何提示將大大幫助!在一個代碼中選擇2個遊戲

import java.io.*; 

import java.util.ArrayList; 
import java.util.List; 
import java.util.Random; 
import java.util.Scanner; 
import java.io.InputStreamReader; 


public class SkillsDemo31 { 
private static boolean again = true; 
private static int action; 


public static void main(String[] args) throws IOException { 

    //*************************** 
    //Login 
    //*************************** 

    class User { 
     User (String username, String password) 
     { 
      this.username = username; 
      this.password = password; 
     } 

     String GetUsername() {return username;} 
     String GetPassword() {return password;} 

     private String username; 
     private String password; 
    } 


    String greeting = "Hello"; 
    String username; 
    String password; 

    // Used to hold the instance of a user who successfully logged in 
    User loggedInUser = null; 

    // Create an empty list to hold users 
    List<User> listOfUsers = new ArrayList<>(); 

    // Add 3 users to the list 
    listOfUsers.add(new User("Gerry","spintown")); 
    listOfUsers.add(new User("Evelyn","poker")); 
    listOfUsers.add(new User("Joan","bonus")); 


    BufferedReader br = new BufferedReader(
      new InputStreamReader(System.in)); 


    System.out.println("*** Welcome to the program ***\n"); 
    System.out.println(greeting); 

    System.out.println("Please type your username :"); 
    username = br.readLine(); 
    System.out.println("Please type your password :"); 
    password = br.readLine(); 

    for (User user : listOfUsers) 
    { 
     if (user.GetUsername().equals(username)) 
     { 
      if (user.GetPassword().equals(password)) 
      { 
       loggedInUser = user; 
       // when a user is found, "break" stops iterating through the list 
       break; 
      } 
     } 
    } 

    // if loggedInUser was changed from null, it was successful 
    if (loggedInUser != null) 
    { 
     System.out.println("User successfully logged in: "+loggedInUser.GetUsername()); 
    } 
    else 
    { 
     System.out.println("Invalid username/password combination"); 
    } 

    //********************************** 
    //Choice of Games 
    //********************************** 
    again = true; 
    action = 0; 

    while (again) 
    { 
     System.out.println("Please type 1 for Rock, Paper, Scissors or 2 for Play pick up sticks:"); 
     action = Integer.parseInt(br.readLine()); 
     if (action == 1) 
     { 
      System.out.println("\nYou have chosen to play Rock, Paper, Scissors"); 
     } 
     else if (action == 2) 
     { 
      System.out.println("\nYou have chosen to Play pick up sticks"); 
      again = false; 
     } 


     //****************************** 
     //Rock,Paper,Scissors 
     //******************************** 




      Random rnd = new Random(); 
      int input; 
      int score = 0; 
      int B = 1; 


      System.out.println("Pick 1,2, or 3 for:"); 
      System.out.println("Rock (1), Paper(2), or Scissors (3)"); 
      while (B != 0) { 
       // 1 = rock 
       // 2 = paper 
       // 3 = scissors 
       // N= Integer.parseInt(br.readLine()) 
       int Rock = 1, Paper = 2, Scissor = 3; 

       input = Integer.parseInt(br.readLine()); 
       int randomNumber = rnd.nextInt(3 - 1 + 1) + 1; 
       if (randomNumber == Rock) { 
        if (input == Rock) { 
         System.out.println("Rock Vs. Rock: Tie"); 
        } else if (input == Paper) { 
         System.out.println("Paper Vs. Rock: You Win!"); 
         System.out.println("Congratulations!"); 
         score++; 
        } else if (input == Scissor) { 
         System.out.println("Scissors Vs. Rock: You Lose"); 
        } 
       } else if (randomNumber == Paper) { 
        if (input == Rock) { 
         System.out.println("Rock Vs. Paper: You Loose"); 
        } else if (input == Paper) { 
         System.out.println("Paper Vs. Paper: Tie"); 
        } else if (input == Scissor) { 
         System.out.println("Scissors Vs. Paper: You Win"); 
         System.out.println("Congratulations!"); 
         score++; 
        } 

       } else if (randomNumber == Scissor) { 
        if (input == Rock) { 
         System.out.println("Rock Vs. Scissors: You Win"); 
         System.out.println("Congratulations!"); 
         score++; 
        } else if (input == Paper) { 
         System.out.println("Paper Vs. Scissors: You Loose"); 
        } else if (input == Scissor) { 
         System.out.println("Scissors Vs. Scissors: Tie"); 
        } 

       } 

     int Y=5, N=10; 
     System.out.println("Your score is "+ score); 
      System.out.println("Do you want to play again? Press(5) For Yes/(10) For No"); 
     input = Integer.parseInt(br.readLine()); 
     if(input==Y){ 
      B=1; 
      System.out.println("Rock, Paper,Scissors"); 
     } 
     else if(input==N) 
     {System.exit(0); 
      System.out.println("Have A Good Day!"); 
      } 

     //*********************** 
     //Pick Up Sticks 
     //*********************** 



     Scanner scanner = new Scanner(System.in); 

     while (true) { 

      int numSticks = 21; 
      System.out.println("Would You Like to go first? (Yes/No)"); 
      Scanner input1 = new Scanner(System.in); 
      String goFirst = input1.nextLine(); 
      Scanner take = new Scanner (System.in); 
      int numToTake = 0; 
      int score2 = 0; 




      while (numSticks > 0) { 


       if (goFirst.equals("Yes") || goFirst.equals("yes")) { 
       System.out.println("There are " + numSticks + " sticks "); 
       System.out.println("How many sticks do you want to take (1 or 2)"); 
       numToTake = take.nextInt(); 

       if (numToTake > 2) { 
        numToTake = 2; 

       } 

       else if (numToTake < 1) { 
        numToTake = 1; 
       } 
       numSticks = numSticks - numToTake; 

       if (numSticks <= 0) { 
        System.out.println("You lose"); 
        System.out.println("Your score is " + score); 
       } 
       else { 

        if((numSticks - 2) % 3 == 0 || numSticks - 2 == 0) { 
        numToTake = 1; 
        } 
        else { 
        numToTake = 2; 
        } 
        System.out.println("Computer takes " + numToTake + " sticks "); 
        numSticks = numSticks - numToTake; 

        if (numSticks <= 0) { 
        System.out.println(" You win "); 
        score++; 
        System.out.println("Your score is " + score); 
        } 
       } 

       } 
       else { 
        if((numSticks - 2) % 3 == 0 || numSticks - 2 == 0) { 
        numToTake = 1; 
        } 
        else { 
        numToTake = 2; 
        } 
        System.out.println("Computer takes " + numToTake + " sticks "); 
        numSticks = numSticks - numToTake; 

        if (numSticks <= 0) { 
        System.out.println("You win"); 
        score++; 
        System.out.println("Your score is " + score); 
        } 
        else { 
         System.out.println("There are " + numSticks + " sticks "); 
         System.out.println("How many sticks do you want to take (1 or 2)"); 
         numToTake = take.nextInt(); 

         if (numToTake > 2) { 
          numToTake = 2; 
         } 
         else if (numToTake < 1){ 
          numToTake = 1; 
         } 
         numSticks = numSticks - numToTake; 

         if(numSticks <0){ 
          System.out.println("You win"); 
          score++; 


         } 




         } 

        } 



     } 


      System.out.println("Do you want to play again, type (5) for yes or (10) for no"); 

       if (scanner.nextLine().equals("10")) { 
        break; 
        } 
     } 
    } 
    } 
} 

回答

0

您應該爲每一場比賽的一類,並創建一個啓動方法有開始根據您的用戶輸入,例如遊戲:

  System.out.println("Please type 1 for game 1 or 2 for game2:"); 
      action = Integer.parseInt(br.readLine()); 
      if (action == 1) 
      { 
       System.out.println("\nYou have chosen to game 1"); 
       Game1 game1 = new Game1(); 
       game1.start(); 
      } 
      else if (action == 2) 
      { 
       System.out.println("\nYou have chosen game 2"); 
       Game2 game2 = new Game2(); 
       game2.start(); 
      } 

這個代碼是基於你的代碼。

+0

你們這兩個人的評論基本上是一樣的,所以我不知道如何感謝解決方案 –

0

鮑勃,我只是檢查你的代碼。

根據我所看到的,我對你的問題的理解是,「用戶登錄並選擇遊戲類型後,兩個遊戲都會隨後運行,而你只想運行遊戲用戶選擇」。

如果是這樣的話,那麼解決辦法很簡單:

  1. 以遊戲的部分代碼了從

    而(再次){// 在此塊中,只有讓你的用戶選擇代碼。 } block。該塊只是爲了獲取用戶的選擇。

  2. 在選擇塊之後,您將從變量「action」中獲得用戶的選擇。現在你只需要添加一個if塊如下:

    if(action == 1){ //將你的Rock,Paper,Scissors遊戲代碼放在這裏,如用戶選擇1 // ***** ************************* //搖滾,紙,剪刀 // ************** ****************** ... } else if(action == 2){//將您的Pick Up Sticks遊戲代碼放在這裏,就像用戶選擇2一樣。 // *********************** //拾取棒 // *************** ******** ... }

由於您只想一次運行一個遊戲,因此您需要一個「IF」條件來告訴代碼要運行哪個遊戲。 所以,只要「如果」就足夠了,但從我的觀點來看,將「選擇」部分和「遊戲」部分分開將使代碼更簡單,更易於理解。

+0

你們兩個人的評論基本上是一樣的,所以我不會如何感謝解決方案 - –