import java.util.Scanner; public class Main{ //global variables static Scanner kbin = new Scanner(System.in); public static void main(String[] args){ play_game(); } public static int draw_card(){ return (int)(Math.random()*9)+1; //return number in range 1-10 } public static int player_turn(String pName){ int card; //declare variable to store drawn card String choice; //a variable to store the user's choice to continue int total = 0; //initialise total to 0 System.out.println("\n\n"+pName+"'s turn!"); //draw card loop while(true){ card = draw_card(); System.out.println(pName+" drew a "+card); total = total + card; //update total; if(total<=21){ System.out.println("Current total: "+total); }else{ System.out.println("Your total is "+total+"! BUSTED!"); break; //break out of the loop! } System.out.println("\nDraw another card? y/n"); choice = kbin.nextLine(); if(!choice.equals("y")){ break; //player doesn't want to draw another card } } return total; } public static void determineWinner(String p1, int s1, String p2, int s2){ System.out.println(p1+"'s total: "+s1); System.out.println(p2+"'s total: "+s2); if(s1 <= 21 && s1>s2){ System.out.println(p1+" wins!"); }else if(s2 <= 21 && s2 > s1){ System.out.println(p2+" wins!"); }else System.out.println("It's a tie!"); } public static String getPlayerName(int pNum){ String name; System.out.println("Enter player "+pNum+"'s name: "); name = kbin.nextLine(); return name; } public static void play_game(){ String player1, player2; int score1, score2; System.out.println("Welcome to Busted!\n\n"); player1 = getPlayerName(1); player2 = getPlayerName(2); //player1 turn score1 = player_turn(player1); //player2 turn score2 = player_turn(player2); determineWinner(player1, score1, player2, score2); } }