Results 1 to 4 of 4
  1. #1
    shazakala is offline Member
    Join Date
    Mar 2011
    Posts
    19
    Rep Power
    0

    Default Need help with AI in roll dice game

    now I'm sorry I have to show the whole program but I feel that it might be necessary in how to do the finishing touch's to this assignment (I have done most the work my self just need you guys to help me out with the last part that I have attempted to no avail). Basically between

    "//--------------------------------------------------------------
    //--------------------------------------------------------------
    // DO NOT CHANGE THE CODE ABOVE THIS LINE
    //--------------------------------------------------------------
    //--------------------------------------------------------------"

    and

    "//---------------------------------------------------------------------------
    //---------------------------------------------------------------------------
    // ADD ALL THE METHODS FROM STAGE 4 BELOW
    // (This includes methods from stage 1 and stage 2 and stage 3)
    //---------------------------------------------------------------------------
    //---------------------------------------------------------------------------"

    is this block of code in which I cant figure out how to do, from what I understand I have to write a piece of code for the computer that takes into consideration whether it should stay or roll again and risk going over 21 while also looking at what number the user is at and if its a safe bet to roll against it. you can see my attempt but its very rough and needs a complete over haul but I don't want you guys to write all of that part of the code, just give me how it starts, how it ends and maybe one or two variables inside that gives the computer options on what to do so I understand how all the options should be written out, please ignore fixing the code outside of it as it runs and I'll smooth it out once the whole program runs. thank you very much if you spend the time helping me :) now here is the program:

    Java Code:
    		
    		String name = getName(); 
    		displayIntro(name);	
    		
    		while (selection !=  QUIT) {			
    			displayMainMenu();
    			selection = getMenuSelection();
    			
    			if (selection == PLAY_A_NEW_GAME) {
    				numberOfGames++;
    				gameResult = playAGame();
    				
    				if (gameResult == USER_NUMBER) {
    					wonByUser++;
    				} else if (gameResult == COMPUTER_NUMBER) {
    					wonByComputer++;
    				}
    			} else if (selection == SEE_STATS) {
    				displayStatistics(name, "Computer", numberOfGames, wonByUser, wonByComputer);
    			}
    		}
    		
    		displayStatistics(name, "Computer", numberOfGames, wonByUser, wonByComputer);
    		displayThankYou(name);
    	}	
    	
    	private int playAGame() {
    			int gameWinner = 0;
    			int selection;
    			boolean stayU = false;
    			boolean stayC = false;
    
    			boolean gameIsOver = false;
    			int currentScoreU = getStartingScore();
    			int currentScoreC = getStartingScore();
    
    			int currentPlayer = getRandomStartingPlayer();
    			displayCurrentState(currentScoreU, currentScoreC, stayU, stayC);
    
    			while (! gameIsOver(currentScoreU, currentScoreC, stayU, stayC)) {
    
    				if (currentPlayer == COMPUTER_NUMBER) {	
    					if (!stayC) {
    						displayCurrentPlayer(currentPlayer);
    						System.out.println();
    						selection = getComputerSelection(currentScoreU, currentScoreC, stayU);	
    
    						if (selection == ROLL) {
    							currentScoreC = haveARoll(currentScoreC);
    							if (currentScoreC == VENTUNO) {
    								stayC = true;
    							}
    						} else {
    							stayC = true;
    						}
    						displayCurrentState(currentScoreU, currentScoreC, stayU, stayC);					
    					}			
    				} else {
    					if (!stayU) {
    						displayCurrentPlayer(currentPlayer);
    						displayPlayMenu();
    						selection = getMenuSelection();
    
    						if (selection == ROLL) {
    							currentScoreU = haveARoll(currentScoreU);
    							if (currentScoreU == VENTUNO || (stayC && currentScoreU > currentScoreC)) {
    								stayU = true;
    							}
    						} else {
    							stayU = true;
    						}	
    						displayCurrentState(currentScoreU, currentScoreC, stayU, stayC);				
    					}
    				}			
    
    
    				currentPlayer = getOtherPlayer(currentPlayer);
    			}
    
    			gameWinner = getWinnerNumber(currentScoreU, currentScoreC);
    			displayGameResult(currentScoreU, currentScoreC, gameWinner);		
    			return gameWinner;
    	}
    //--------------------------------------------------------------
    //--------------------------------------------------------------
    // DO NOT CHANGE THE CODE ABOVE THIS LINE
    //--------------------------------------------------------------
    //--------------------------------------------------------------
    	private int getComputerSelection(int currentScoreU, int currentScoreC, boolean staysU) {
    		boolean computerChoice = true;
    		if(currentScoreC < 16){
    			computerChoice = true;
    		}else if(currentScoreU > VENTUNO){
    			computerChoice = false;
    		}else if(currentScoreC = VENTUNO){
    			computerChoice = false;
    		}else if((currentScoreU = VENTUNO) && (CurrentScoreC < currentScoreC)){
    			computerChoice = true;
    		}else if(StaysU > currentScoreC){
    			computerChoice = true;
    		}else{
    			computerChoice = true;
    		}
    		
    		return computerChoice; //this line will need to be changed
    	}
    //---------------------------------------------------------------------------
    //---------------------------------------------------------------------------
    // ADD ALL THE METHODS FROM STAGE 4 BELOW 
    //   (This includes methods from stage 1 and stage 2 and stage 3)
    //---------------------------------------------------------------------------
    //---------------------------------------------------------------------------
    	private void displayStatistics(String name1, String name2, int totalNumberOfGames, int gamesWonByU, int gamesWonByC)	{
    		displayDashes();
    		displayDashes();
    		System.out.println("- Number of games played: " + totalNumberOfGames);
    		System.out.println("- Games won by Jing: " + gamesWonByU);
    		System.out.println("- Games won by Computer: " + gamesWonByC);
    		System.out.println("- games resulting in a draw: " + (totalNumberOfGames - gamesWonByU - gamesWonByC));
    		if(gamesWonByU > gamesWonByC){
    			System.out.println("- Jing is winning by " + (gamesWonByU - gamesWonByC));
    		}else if(gamesWonByC > gamesWonByU){
    				System.out.println("- Computer is winning by " + (gamesWonByC - gamesWonByU));
    			}else{
    				System.out.println("- Jing is drawing with the Computer");
    			}
    		displayDashes();
    		displayDashes();
    	}
    
    	private void displayGameResult(int currentScoreU, int currentScoreC, int winnerNumber)	{
    		displayStars();
    		displayStars();
    		System.out.println("*       User score: " + currentScoreU);
    		System.out.println("*       Computer score: " + currentScoreC);
    			if(winnerNumber == COMPUTER_NUMBER){
    				System.out.println("*       Computer has won.");
    			}else if(winnerNumber == USER_NUMBER){
    				System.out.println("*       User has won. Well done!");
    			}else if(winnerNumber == RESULT_IS_A_DRAW){
    				System.out.println("*       Result is a draw.");
    			}
    		displayStars();
    		displayStars();
    	}	
    
    	private int getWinnerNumber(int currentScoreU, int currentScoreC) {		
    		int whoWon = 0;
    		if(currentScoreU > 21){
    			whoWon = COMPUTER_NUMBER;
    		}else if(currentScoreC > 21){
    				whoWon = USER_NUMBER;
    		}else if(currentScoreU > currentScoreC){
    			whoWon = USER_NUMBER;
    		}else if(currentScoreC > currentScoreU){
    			whoWon = COMPUTER_NUMBER;
    		}else{
    			whoWon = RESULT_IS_A_DRAW;
    		}
    	return whoWon;
    	}
    	
    	private boolean gameIsOver(int currentScoreU, int currentScoreC, boolean staysU, boolean staysC) {		
    		boolean gameOver = staysU && staysC;
    		if (currentScoreU > VENTUNO){
    			gameOver = true;
    		}else if (currentScoreC > VENTUNO){
    			gameOver = true;
    		}
    	return gameOver;
    	}			
    
    	private void displayCurrentState(int currentScoreU, int currentScoreC, boolean stayU, boolean stayC) {		
    		displayDashes();
    		displayDashes();
    		System.out.print("- User score:" + currentScoreU);
    
    		if(stayU == true){
    			System.out.println(".  User has stayed.");
    		}else if(stayU == false){
    			System.out.println();
    		}
    		System.out.print("- Computer score:" + currentScoreC);	
    		if(stayC == true){
    			System.out.println(".  Computer has stayed.");
    		}else if(stayC == false){
    			System.out.println();
    		}
    		displayDashes();
    		displayDashes();
    	}	
    	
    	private void displayCurrentPlayer(int currentPlayer) {
    		int whichPlayer = currentPlayer;
    		if (whichPlayer == 2){
    			System.out.println("Computer's turn to play.");
    		}
    		if (whichPlayer == 1){
    			System.out.println("User's turn to play.");
    		}
    	}
    	private int getOtherPlayer(int currentPlayer) {
    		int getPlayer = currentPlayer;
    		if (getPlayer == 2){
    			return getPlayer - 1;
    		}
    		if (getPlayer == 1){
    			return getPlayer +1;
    		}
    		return getPlayer;
    	}
    
    	private int getRandomStartingPlayer() {		
    		int randomNumber = (int)(Math.random() * (COMPUTER_NUMBER - USER_NUMBER + 1)) + USER_NUMBER;
    		return randomNumber;
    	}	
    
    	private int haveARoll(int currentScore) {
    		int diceRoll = getRandomDiceThrow();
    		System.out.println("Dice throw is: " + diceRoll);
    		int addedResults = currentScore + diceRoll;
    		return addedResults;
    	}
    
    	private int getRandomDiceThrow() {
    		final int MIN = 1;
    		final int MAX = 6;
    		int randomNumber = (int)(Math.random() * (MAX - MIN + 1)) + MIN;
    		return randomNumber;
    	}
    
    	private int getStartingScore() {
    		final int MIN = 12;
    		final int MAX = 16;
    		int randomNumber = (int)(Math.random() * (MAX - MIN +1)) + MIN;
    		return randomNumber;
    	}	
    	
    	private void displayThankYou(String name) {	
    		System.out.println();	
    		System.out.println("Thank you " + name + " for playing Ventuno. ");
    		System.out.println();
    	}
    
    
    	private void displayStars() {	
    		System.out.println("*********************************************");
    	}
    
    	private void displayDashes() {	
    		System.out.println("----------------------------------------------");
    	}	
    
    	private void displayPlayMenu() {
    		System.out.println();
    		System.out.println("   " + OPTION_ROLL);
    		System.out.println("   " + OPTION_STAY);
    	}
    
    	private void displayMainMenu() {
    		System.out.println();
    		System.out.println("   " + OPTION_PLAY_A_NEW_GAME);
    		System.out.println("   " + OPTION_SEE_STATS);
    		System.out.println("   " + OPTION_QUIT);
    	}	
    		
    	private void displayIntro(String name) {
    		System.out.println();
    		System.out.println();
    		System.out.println("Welcome " + name + " to the game of Ventuno");
    		System.out.println("Program written by scal051 ");
    		System.out.println();
    		System.out.println();
    	}
    
    	private String getName() {
    		System.out.print("Enter name: ");
    		String nameInput = Keyboard.readInput();
    		return nameInput;
    	}
    
    	private int getMenuSelection() {
    		int randomNumber = (int)(Math.random()* 7 + 1);
    		System.out.println("    Enter selection: " + randomNumber);
    		System.out.println();
    		return randomNumber -1;
    	}
    
    }

  2. #2
    mr_guy is offline Member
    Join Date
    Feb 2011
    Posts
    18
    Rep Power
    0

    Default

    The computer should only take into account two variables: its current score, and the players score. if the players score is higher than the computers score, than the computer should roll regardless of its chances. from there you should evaluate the chances of the computer and the player of going over 21 on their next roll. there are pretty much four options:

    1.the player has a high chance and the computer has a high chance of going over 21 next roll
    2.the player has a high chance and the computer has a low chance
    3.the player has a low chance and the computer has a high chance
    4.the player has a low chance and the computer has a low chance

    if 2 than the computer should definitely roll again
    if 4 than the computer should probably roll again
    if 3 than the computer should not roll again
    if 1 than the computer should consider rolling again (up to you, using a random number might be a good idea to choose here)

    hope this helps

  3. #3
    shazakala is offline Member
    Join Date
    Mar 2011
    Posts
    19
    Rep Power
    0

    Default

    I'm sorry that is usefull but could I have a code based example? much thanks for the reply though mr guy (cool name btw)

  4. #4
    shazakala is offline Member
    Join Date
    Mar 2011
    Posts
    19
    Rep Power
    0

    Default

    fixed, although when running the program it didn't go so well....

Similar Threads

  1. HELP WITH DICE ROLL PROBLEM (netbeans)
    By IdrinkJava in forum New To Java
    Replies: 3
    Last Post: 03-14-2011, 04:55 PM
  2. Roll The dice
    By Subhanrukh in forum New To Java
    Replies: 8
    Last Post: 04-19-2010, 09:39 AM
  3. Need help with java rapid roll game
    By blunderblitz in forum New To Java
    Replies: 1
    Last Post: 03-02-2010, 01:36 PM
  4. Roll dice class with three dices
    By nube07 in forum New To Java
    Replies: 4
    Last Post: 07-14-2008, 02:37 AM
  5. Roll 2-Dice "Pig" Game Help
    By King8654 in forum AWT / Swing
    Replies: 7
    Last Post: 04-07-2008, 07:58 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •