Vai ai contenuti
package slots; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Component; import java.awt.FlowLayout; import java.awt.Graphics; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.border.TitledBorder; public class SlotMachineProgram extends JFrame { private int total = 2000; //this is the max bet you can have private int bet = 100; //default bet amount private int maxWin; //this variable stores the max wins private String[] symbols = { "Cherry", "Bear", "Diamond", "Penguin", "Star", "Shoe", "Banana", "Orange", "Apple" }; //symbols that are available for the slot machine //amount winning int[] gotTwo = { 30, 16, 15, 12, 11, 10, 9, 7, 5 }; //Prize moneys for two matches int[] gotThree = { 60, 32, 30, 24, 22, 20, 18, 14, 10 }; //prize money for three matches public JPanel slotPanel = new JPanel(new GridLayout(1, 3)); //creates a grid that holds the three slot options public JLabel[] slot = new JLabel[3]; //create the three slots //LABELS private JLabel amount = new JLabel("Total: $" + total); //this is the label for the total amount private JLabel betLabel = new JLabel("Bet: $" + bet); //this is the label for the bet amount //BUTTONS private JButton add50Button = new JButton("+50"); //creates a button called add50Button that adds 50 dollars to the bet private JButton spin = new JButton("Spin"); //creates a button that spins the slot machine private JButton maxBetButton = new JButton("Max"); //creates a button that makes the max amount the bet private JButton subtract50Button = new JButton("-50"); //creates a button that subtracts 50 from bet private JButton getBet = new JButton("Enter Bet"); //a button that pops up a new window that allows the user to type in a bet public SlotMachineProgram() { //constructor maxWin = total; //set win to total //Coloring Buttons spin.setBackground(Color.GREEN); //sets color of button to be green spin.setOpaque(true); //Sets Button Opaque so it can be seen maxBetButton.setBackground(Color.CYAN); //sets color of button to be green maxBetButton.setOpaque(true); //Sets Button Opaque so it can be seen add50Button.setBackground(Color.YELLOW); //sets color of button to be green add50Button.setOpaque(true); //Sets Button Opaque so it can be seen subtract50Button.setBackground(Color.MAGENTA); //sets color of button to be green subtract50Button.setOpaque(true); //Sets Button Opaque so it can be seen getBet.setBackground(Color.ORANGE); //sets color of button to be green getBet.setOpaque(true); //Sets Button Opaque so it can be seen //money panel JPanel moneyPanel = new JPanel(new GridLayout(2, 1)); //creates a grid called money panel that is 2x1 that stores total and bet moneyPanel.setBorder(new TitledBorder("Money")); //creates the title of the panel moneyPanel.add(amount); //adds the amount to the panel moneyPanel.add(betLabel); //adds the BetLabel to the panel //button panel JPanel buttonsPanel = new JPanel(new GridLayout(2, 1)); //creates a new panel called buttonsPanel that has all of the buttons JPanel buttonTopPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 5, 2)); //this orients the buttons to the middle in a row buttonsPanel.setBorder(new TitledBorder(" Buttons")); //sets the title of the buttons panel to buttons buttonTopPanel.add(add50Button); //add the add50button to the button panel buttonTopPanel.add(spin); //adds a spin button buttonTopPanel.add(maxBetButton); //adds a max bet button JPanel BottomPanel = new JPanel(); //creates a panel for the bottom of the screen BottomPanel.add(subtract50Button); //adds the subtract 50 button BottomPanel.add(getBet); //adds the get bet button buttonsPanel.add(buttonTopPanel, BorderLayout.NORTH); //adds a border on the north edge buttonsPanel.add(BottomPanel, BorderLayout.SOUTH); //adds a border on the south edge // TODO //set slots to their default image slot[0] = new JLabel(new ImageIcon("/images/left.png")); slot[1] = new JLabel(new ImageIcon("/images/middle.png")); slot[2] = new JLabel(new ImageIcon("/images/right.png")); //add Action listeners to bet buttons add50Button.addActionListener(new ActionListener() { //action listener for the add 50 button @Override public void actionPerformed(ActionEvent e) { //makes an action performed method bet += 50; //increase bet by 50 if the button add50 is clicked if (bet > total) bet = total; //else skip betLabel.setText("Bet: $" + bet); //show the bet next to the bet label } }); maxBetButton.addActionListener(new ActionListener() { //creates an action listener for the maxbetbutton @Override public void actionPerformed(ActionEvent e) { //creates a method for the maxbetbutton's action listener bet = total; //make bet the maxBet betLabel.setText("Bet: $" + bet); //show bet next to the bet label } }); subtract50Button.addActionListener(new ActionListener() { //creates an actionlistener for the subtract 50 button @Override public void actionPerformed(ActionEvent e) { //creates an actionperformed method bet -= 50; //subtracts the value of the bet by 50 if (bet < 0) bet = 0; //is the user tries to have a negative bet, set bet to 0 betLabel.setText("Bet: $" + bet); //display the value of the bet next to a bet label } }); getBet.addActionListener(new ActionListener() { //create an actionlistener for the getbet button that allows the user to type in a custom amount @Override public void actionPerformed(ActionEvent e) { // create an actionperformed method String input; //creates a variables called input of type string. This is what is displayed on the screen int betInput; //creates a variables called betInput of type int //"How much do you want to bet" is the text that appears in the bet amount window. //"Enter your bet amount" is the header on the bet window input = JOptionPane.showInputDialog(null, "How Much Do You Want To Bet? ", "Enter Your Bet Amount", JOptionPane.QUESTION_MESSAGE); betInput = Integer.parseInt(input); //set the user input to the betInput if (betInput > total) { //if the inputed value is more than the total amount of money they user has left bet = total; //set the bet to equal the amount of money the user has } else { //if the inputed bet amount is less than the total bet = betInput; //set the bet input the value of the bet } betLabel.setText("Bet: $" + bet); //updates the bet label to show the bet value next to its label } }); spin.addActionListener(new ActionListener() { //creates an actionlistener for the spin button @Override public void actionPerformed(ActionEvent e) { //creates a method called actionperformed String[] rand = new String[3]; //creates an array of 3 strings int addTotal; //creates a variable called addTotal //generate 3 new slots for (int i = 0; i < 3; i++) { //loops through the three panes int choice; //creates a varaibe called choice double random = Math.random(); //creates a random variable and assigns it a random value random *= 8; //times the value by 8 choice = (int) random; //assigns the value of choice to be random with a cast of int rand[i] = symbols[choice]; //assigns the value rand at index i to be symbols at index choice slot[i].setIcon(new ImageIcon("/images/" + symbols[choice] + ".png")); //shows the image for the slot in the panel // TODO } //check for matches //three matches if (rand[0] == rand[1] && rand[1] == rand[2]) { //if there were three matches int index = 0; //creates a variable index of type int and assigns it to the value 0 and for (int i = 0; i < symbols.length; i++) //a loop that runs the length of symbols if (rand[0] == symbols[i]) { //if rand[0] is equal to symbols[i] index = i; //assign index to equal i } addTotal = bet * gotThree[index]; //increase addTotal by bet times gotThree[index], which stores the bonuses for triple pairs total += addTotal; //increment total by addTotal //pop-up menu saying the bet amount, the amount won and the updated total JOptionPane.showMessageDialog(null, "Your Bet: $" + bet + "\nYou Win: $" + addTotal + "\nMoney Left: $" + total, "Three Matches", JOptionPane.INFORMATION_MESSAGE); amount.setText("Total: $" + total); //set the label Total: to equal total if (total > maxWin) { //if the new total is greater than maxWin maxWin = total; //maxWin is now assigned to total } } else if (rand[0] == rand[1] || rand[1] == rand[2] || rand[2] == rand[0]) { //checks if there is a match between 2 tiles int match, index = 0; //creates integer variables match and index, assigning it to 0 if (rand[0] == rand[1] || rand[0] == rand[2]) { //if the first box has a match match = 0; //match is assigned the value of 0 } else { //if the second of third box has the match match = 1; //assign match to the value of 1 } //find the index for (int i = 0; i < symbols.length; i++) { //A loop that iterates the amount of times of the length of symbols if (rand[0] == symbols[i]) { //if rand at index 0 is equal to symbols at index i index = i; //set index to equal i } } addTotal = bet * gotTwo[index]; //increment addTotal by bet*gotTwo[index] //Creates a pop-up window that displays the bet amount and the winnings JOptionPane.showMessageDialog(null, "Your Bet: $" + bet + "\nYou Win: $" + addTotal, "Two Matches", JOptionPane.INFORMATION_MESSAGE); total += addTotal; //increments total by addTotal amount.setText("Total: $" + total); //updates the text next to total if (total > maxWin) { //if the total is greater than maxWin maxWin = total; //set maxWin to total } } else { //run this is there are no matches total -= bet; //decrement total by bet //Creates a pop-up window that shows the lost amount (bet) and the money left JOptionPane.showMessageDialog(null, "You Lost: $" + bet + "\nMoney Left: $" + total, "No Matches", JOptionPane.INFORMATION_MESSAGE); amount.setText("Total: $" + total); //updates the total amount that will be seen on the screen if (total > maxWin) { //if the total is greater than maxWin maxWin = total; //sets maxWin to equal total } if (bet > total) { //if bet is greater than the amount of money left (total) than bet = total; //set the bet to total betLabel.setText("Bet: $" + bet); //create the betLabel and update the value of it } if (total == 0) { //if the total is the bet = 0; //sets the bet value to equal 0 betLabel.setText("Bet: $" + bet); //updates the betLabel to the new bet value //Creates a pop-up message saying "You Lost :( and it also displays the maximum amount that the user won JOptionPane.showMessageDialog(null, "You Lost :( \nThe Maximum That You Won Was $" + maxWin, "\nGame Over", JOptionPane.INFORMATION_MESSAGE); System.exit(0); //exit the program } } } }); JPanel panelOnBottom = new JPanel(new BorderLayout()); //creates a panel on the bottom panelOnBottom.add(moneyPanel, BorderLayout.WEST); //adds the money panel to the west/left border of the window panelOnBottom.add(buttonsPanel, BorderLayout.EAST); //adds the buttons panel to the east/right border of the window // TODO JPanel all = new JPanel(new GridLayout(2, 1)); //creates a panel called all with a grid layout of 2x1 slotPanel.setVisible(true); slotPanel.setOpaque(true); all.add(slotPanel); //adds slotPanel to the 'all' panel all.add(panelOnBottom); //adds the panelOnBottom to the 'all' panel add(all); //add all to the window } public static void main(String[] args) { //the main class JFrame frame = new SlotMachineProgram(); //creates a new frame of type SlotMachineProgram frame.setTitle("Gambling Time!"); //creates the title of the frame to be Gambling Time! frame.setLocationRelativeTo(null); //sets the location of the window to null, meaning it opens in the middle frame.setSize(400, 300); //sets the size of the window to be 400 by 300 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //sets the default close method to exit frame.setResizable(false); //makes the window not resizable frame.setVisible(true); //makes the window visible } } //set slots to their default image slot[0] = new JLabel(new ImageIcon("/images/left.png")); slot[1] = new JLabel(new ImageIcon("/images/middle.png")); slot[2] = new JLabel(new ImageIcon("/images/right.png")); slotPanel.add(slot[0]); slotPanel.add(slot[1]); slotPanel.add(slot[2]); Quindi, sei sicuro che tutte le immagini esistano in una directory /images come dichiarato al primo posto nel codice. private String[] symbols = { "Cherry", "Bear", "Diamond", "Penguin", "Star", "Shoe", "Banana", "Orange", "Apple" }; // symbols that are available for the slot machine
Slot machine '10', 'Doppia barra| Barra doppia| Doppia barra' => '20', 'Tripla barra| Tripla barra| Tripla barra' => '30', 'Ciliegia| Ciliegia| Cherry' => '50', 'Sette| Sette| Sette' => '70', 'Diamante| Diamante| Diamante' => '100', ); /*La chiave nel nostro caso sono i giri vincenti della slot machine. Sotto forma di "Bar| Bar| Bar'. Il '|' rappresenta la separazione delle ruote. Il valore è quanti soldi lo vincono, girano una qualsiasi delle chiavi, ad esempio "Bar| Bar| Bar'. La chiave punta sempre al valore in questo modo: 'Bar| Bar| Barra' => '10'. Quindi le possibili combinazioni sono: "Bar| Bar| Bar' paga '10', 'Double Bar| Barra doppia| Double Bar' paga '20', 'Tripla barra| Tripla barra| Triple Bar' paga '30', 'Cherry| Ciliegia| Cherry' paga '50', 'Sette| Sette| Sette' paga '70', 'Diamante| Diamante| Diamond' paga '100' */ Todo New Line: imposta una variabile denominata $wheel 1 che memorizza una matrice vuota. */ $wheel 1 = matrice(); Todo New Line: crea un ciclo foreach che scorre in ciclo la matrice $faces soluzione. Ogni volta che il ciclo assegna un $face a $wheel 1[]. */ foreach($faces come $element) { $wheel 1[] = $element; } /* Todo New Line: utilizzo di una funzione di matrice integrata per invertire la matrice memorizzata in $wheel 1 e assegnare il risultato a $wheel 2. */ $wheel 2 = array_reverse($wheel 1); Todo New Line: imposta una variabile denominata $wheel 3 e assegnala $wheel 1. Su una slot machine la prima e la terza ruota sono le stesse. */ $wheel 3 = $wheel 1; /* Nessuna modifica al codice riportato di seguito */ Queste righe rilevano se l'utente ha fatto clic sul pulsante di rotazione. Imposta i valori iniziali se non hanno fatto clic sul pulsante di rotazione. Se hanno allora utilizza l'ultima posizione della ruota come partenza per il prossimo giro. */ if (isset($_POST['submit'])) { $start 1 = $stop 1; $start 2 = $stop 2; $start 3 = $stop 3; $total = $_POST['total']; $bust = $_POST['bust']; } else { $start 1 = 0; $start 2 = 0; $start 3 = 0; $total = 100; $bust = false; } Queste righe rilevano se l'utente ha fatto clic sul pulsante di reimpostazione. Imposta i valori iniziali se hanno fatto clic sul pulsante di ripristino. */ if (isset($_POST['reset'])) { $start 1 = 0; $start 2 = 0; $start 3 = 0; $total = 100; $bust = false; } /* Ognuna delle righe seguenti imposta un valore casuale. O nel nostro caso in cui ogni ruota si fermerà */ $stop 1 = rand(count($wheel 1) + $start 1, 10 * count($wheel 1)) % count($wheel 1); $stop 2 = rand(count($wheel 1) + $start 2, 10 * count($wheel 1)) % count($wheel 1); $stop 3 = rand(count($wheel 1) + $start 3, 10 * count($wheel 1)) % count($wheel 1); /* Usando il valore casuale estraiamo da ogni matrice la faccia corretta, cioè la barra. */ $result 1 = $wheel 1[$stop 1]; $result 2 = $wheel 2[$stop 2]; $result 3 = $wheel 3[$stop 3]; /* La riga sottostante concatena i risultati di ogni ruota insieme all'operatore punto (.) Finiamo con qualcosa del tipo: "Doppia barra| Tripla barra| Cherry'. Questo sarà casuale ogni volta che "giriamo". */ $slot_result = $result 1 . '|' . $result 2 . '|' . $result 3; /*******************************/ Todo New Line: utilizzo di un test dell'istruzione if se l'utente ha esaurito i fondi. La variabile che contiene la quantità di denaro che l'utente ha viene chiamata $total. Quindi, per verificare se $total è 0. Se total è 0, assegnare alla $bust variabile il valore true. */ if ($total == 0) {$bust = true;} altro {$bust = false;} Todo New Line: utilizzo di un test dell'istruzione if se $bust è true. Se $bust è vero, stampa la frase "Non hai soldi!". Altrimenti stampare i risultati dello spin. Suggerimento: i risultati sono memorizzati in 3 variabili chiamate $result 1, $result 2, $result 3. */ if ($bust == true){ echo "

Non si dispone di denaro!"; } altro {echo $slot_result; if (isset($payouts[$slot_result]))) { $total = $total + $payouts[$slot_result]; echo "

Hai vinto: $" . $payouts[$slot_result]; } else { $total = $total - 10; echo "

Hai perso: $10"; } } /*Nel blocco else aggiungere un altro se tale test se hanno vinto denaro. Per eseguire questa procedura, verificare la condizione seguente: isset($payouts[$slot_result]). Questo verifica se i risultati generati casualmente di ogni ruota corrispondono a una chiave vincente: 'Bar| Bar| Bar'. Aggiungere queste due riga al metodo if: $total = $total + $payouts[$slot_result]; eco "Hai vinto: $" . $payouts[$slot_result]; Aggiungere queste due righe all'altra: $total = $total - 10; eco "Hai perso: $10"; /* Nessuna modifica al codice riportato di seguito */ echo "

Totale contanti: $".$total; /*******************************/ La commissione per la ricerca, lo sviluppo e lo sviluppo
Slot Machine in Javascript

Vinci

Author: test
Tested in Firefox, Chrome and IE8
Slots
0 x
Play
Slots
0 x
Play
Audio not supported
Slots
0 x

Loading..

Play
Audio not supported
Slots
0 x

Loading..

Play
Audio not supported
Detecting online status

Display On Hover

Torna ai contenuti