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:
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;
}
}