用java编写一个高低猜游戏,可以让你赌硬币

问题描述 投票:0回答:1

我正在用java编写一个高低猜游戏。在游戏中我一切正常。我设置了 4 个难度级别,简单(数字 1-25)、中等(数字 1-50)、困难(数字 1-100),并选择您自己的范围来猜测随机数字。我已经做到了,所以您从 100 个硬币开始并为游戏进行下注,如果猜测正确,那么如果玩家想再次玩游戏,我很难将所得的硬币余额添加到 100 个硬币的起始余额中。我有不同的类,do while 循环位于游戏控制器中,提示用户再次玩这会调用不同的对象,例如设置随机数的难度数组、提醒游戏准备就绪的游戏准备方法和起始硬币余额,以及提示用户的猜测方法下注和猜测这些都是从视图类中调用的,谁能帮我完成最后的步骤,我被困在返回硬币并将它们添加到 100 的起始硬币中?

我尝试在游戏控制器中的 do while 循环中调用硬币并启动硬币,以便在再次玩游戏的键盘输入之后、在 while 循环和结束条件之前添加,这些硬币甚至没有在程序中读取,但也没有编译错误。

我的Main方法中的代码:

package com.mycompany.sandoval_highlow;
import javax.sound.sampled.LineUnavailableException;

public class Sandoval_HighLow 
{

    public static void main(String[] args) throws LineUnavailableException 
    {
        GameController logic = new GameController();
        logic.GamesStart();
    }
}

My Admin Class:


package com.mycompany.sandoval_highlow;

import javax.sound.sampled.LineUnavailableException;

public class Admin {
    GameSounds s = new GameSounds();
    View c = new View();
    
    public void GameStartup() throws LineUnavailableException
        {
            

        
        s.GameStart();
            System.out.println("_________________________");
            System.out.println("_~-_-~HIGH LOW GAME~-_-~_");
            System.out.println("_________________________\n");
            System.out.println("The high low game is a guessing game. \n");
            System.out.println("Guess a number between 1-100 to win. \n");
            System.out.println("At anytime enter -1 if you give up... \n\n");
            
        
    }
    
    public void GoodbyeMessage() throws LineUnavailableException 
    {

        s.GameOverSound();
        System.out.println("Game Over");
        System.out.println("Your ended with " + c.coins + " coins!");
    }
    
}

My GameController class:

package com.mycompany.sandoval_highlow;

import java.util.Scanner;
import javax.sound.sampled.LineUnavailableException;

public class GameController {

    Scanner keyboard = new Scanner(System.in);
    char quit = 'N';

    public void GamesStart() throws LineUnavailableException {

        Admin intro = new Admin();
        View v = new View();

        intro.GameStartup();

        do {
            v.RetrieveMagicNum();
            v.gameReady();
            v.guesses();

            System.out.println("Would you like to play again? Enter Y or N");
            quit = keyboard.next().toUpperCase().charAt(0);
            if (quit == 'Y') {
                v.coins += v.startingCoins;
                
            }

        } while (quit == 'Y');
      

        intro.GoodbyeMessage();
    }
}

My View class:

package com.mycompany.sandoval_highlow;

import java.util.Random;
import java.util.Scanner;
import javax.sound.sampled.LineUnavailableException;

public class View {

    int numGuesses;
    int guess;
    int difficulty;
    int min;
    int max;
    int magicNumber;
    int coins;
    int startingCoins = 100;
    int wager;


    Scanner keyboard = new Scanner(System.in);

    public void RetrieveMagicNum() throws LineUnavailableException {
        Random r = new Random();

        System.out.println("Select you difficulty level :)");
        System.out.println("Enter 1 for easy numbers 1-25");
        System.out.println("enter 2 for medium numbers 1-50");
        System.out.println("enter 3 for hard numbers 1-100");
        System.out.println("Enter 4 to set your own range: ");
        difficulty = keyboard.nextInt();

        switch (difficulty) {
            case 1:
                difficulty = 1;
                min = 1;
                max = 25;
                magicNumber = r.nextInt(max - min) + min;
                break;

            case 2:
                difficulty = 2;
                min = 1;
                max = 50;
                magicNumber = r.nextInt(max - min) + min;
                break;

            case 3:
                difficulty = 3;
                min = 1;
                max = 100;
                magicNumber = r.nextInt(max - min) + min;
                break;

            case 4:
                System.out.println("Enter minimum number for your range: ");
                min = keyboard.nextInt();
                System.out.println("Enter maximum number for your range: ");
                max = keyboard.nextInt();
                magicNumber = r.nextInt(max - min) + min;
                break;
        }

    }

    public void gameReady() throws LineUnavailableException {
        GameSounds S = new GameSounds();
        GameController q = new GameController();
        coins = startingCoins;
        System.out.println("GAME READY");
        System.out.println("COIN BALANCE: " + coins + " COINS");
        S.GameStart();
        
        if(q.quit == 'Y') {
            coins += startingCoins;
        }
        
       
        

    }

    public void guesses() throws LineUnavailableException {
        GameSounds S = new GameSounds();
        
        System.out.println("Place your bet: ");
        wager = keyboard.nextInt();
        while (wager > coins) {
            System.out.println("Enter a valid bet you have " + coins + " coins: ");
            wager = keyboard.nextInt();
        }
        System.out.println("Enter your guess: ");
        guess = keyboard.nextInt();

        while (guess != -1 && coins > 0) {

            if (guess < magicNumber && coins >= 0) {
                numGuesses = numGuesses + 1;
                coins = coins - wager;
                S.TooLowSound();
                System.out.println("The number  is too low");
                System.out.println("Guess #: " + numGuesses);
                System.out.println("COIN BALANCE: " + coins);
                System.out.println("enter -1 to give up or place another bet: ");
                wager = keyboard.nextInt();
                System.out.println("Enter your guess: ");
                guess = keyboard.nextInt();

            } else if (guess > magicNumber && coins >= 0) {
                S.TooHighSound();

                numGuesses = numGuesses + 1;
                coins = coins - wager;
                System.out.println("The number is too high");
                System.out.println("Guess #: " + numGuesses);
                System.out.println("COIN BALANCE: " + coins);
                System.out.println("enter -1 to quit or place another bet: ");
                wager = keyboard.nextInt();
                System.out.println("Enter you guess: ");
                guess = keyboard.nextInt();
            } else {

                S.WinningSound();
                coins = coins + wager;
                numGuesses = numGuesses + 1;
                System.out.println("Winner! You guessed the correct number. ");
                System.out.println("You won " + wager + " coins!");
                System.out.println("You guessed " + numGuesses + " times.");
                System.out.println("COIN BALANCE: " + coins);
                guess = -1;
                

            }

        }


       
    }

}


My GameSounds class:

package com.mycompany.sandoval_highlow;

import java.util.Random;
import java.util.Scanner;
import javax.sound.sampled.LineUnavailableException;

public class View {

    int numGuesses;
    int guess;
    int difficulty;
    int min;
    int max;
    int magicNumber;
    int coins;
    int startingCoins = 100;
    int wager;


    Scanner keyboard = new Scanner(System.in);

    public void RetrieveMagicNum() throws LineUnavailableException {
        Random r = new Random();

        System.out.println("Select you difficulty level :)");
        System.out.println("Enter 1 for easy numbers 1-25");
        System.out.println("enter 2 for medium numbers 1-50");
        System.out.println("enter 3 for hard numbers 1-100");
        System.out.println("Enter 4 to set your own range: ");
        difficulty = keyboard.nextInt();

        switch (difficulty) {
            case 1:
                difficulty = 1;
                min = 1;
                max = 25;
                magicNumber = r.nextInt(max - min) + min;
                break;

            case 2:
                difficulty = 2;
                min = 1;
                max = 50;
                magicNumber = r.nextInt(max - min) + min;
                break;

            case 3:
                difficulty = 3;
                min = 1;
                max = 100;
                magicNumber = r.nextInt(max - min) + min;
                break;

            case 4:
                System.out.println("Enter minimum number for your range: ");
                min = keyboard.nextInt();
                System.out.println("Enter maximum number for your range: ");
                max = keyboard.nextInt();
                magicNumber = r.nextInt(max - min) + min;
                break;
        }

    }

    public void gameReady() throws LineUnavailableException {
        GameSounds S = new GameSounds();
        GameController q = new GameController();
        coins = startingCoins;
        System.out.println("GAME READY");
        System.out.println("COIN BALANCE: " + coins + " COINS");
        S.GameStart();
        
        if(q.quit == 'Y') {
            coins += startingCoins;
        }
        
       
        

    }

    public void guesses() throws LineUnavailableException {
        GameSounds S = new GameSounds();
        
        System.out.println("Place your bet: ");
        wager = keyboard.nextInt();
        while (wager > coins) {
            System.out.println("Enter a valid bet you have " + coins + " coins: ");
            wager = keyboard.nextInt();
        }
        System.out.println("Enter your guess: ");
        guess = keyboard.nextInt();

        while (guess != -1 && coins > 0) {

            if (guess < magicNumber && coins >= 0) {
                numGuesses = numGuesses + 1;
                coins = coins - wager;
                S.TooLowSound();
                System.out.println("The number  is too low");
                System.out.println("Guess #: " + numGuesses);
                System.out.println("COIN BALANCE: " + coins);
                System.out.println("enter -1 to give up or place another bet: ");
                wager = keyboard.nextInt();
                System.out.println("Enter your guess: ");
                guess = keyboard.nextInt();

            } else if (guess > magicNumber && coins >= 0) {
                S.TooHighSound();

                numGuesses = numGuesses + 1;
                coins = coins - wager;
                System.out.println("The number is too high");
                System.out.println("Guess #: " + numGuesses);
                System.out.println("COIN BALANCE: " + coins);
                System.out.println("enter -1 to quit or place another bet: ");
                wager = keyboard.nextInt();
                System.out.println("Enter you guess: ");
                guess = keyboard.nextInt();
            } else {

                S.WinningSound();
                coins = coins + wager;
                numGuesses = numGuesses + 1;
                System.out.println("Winner! You guessed the correct number. ");
                System.out.println("You won " + wager + " coins!");
                System.out.println("You guessed " + numGuesses + " times.");
                System.out.println("COIN BALANCE: " + coins);
                guess = -1;
                

            }

        }


       
    }

}
java netbeans
1个回答
0
投票

您应该只初始化

GameController
中的金币,并且不要重置
gameReady()
中的金币余额,而是在重新启动游戏时将当前金币从
GameController
传递到 View。

© www.soinside.com 2019 - 2024. All rights reserved.