public class Craps extends JFrame implements ActionListener { final int WON = 0,LOST =1, CONTINUE = 2;



Similar documents
Homework/Program #5 Solutions

INTRODUCTION TO COMPUTER PROGRAMMING. Richard Pierse. Class 7: Object-Oriented Programming. Introduction

// Correntista. //Conta Corrente. package Banco; public class Correntista { String nome, sobrenome; int cpf;

5.17 GUI. Xiaoyi Jiang Informatik I Grundlagen der Programmierung

The class JOptionPane (javax.swing) allows you to display a dialog box containing info. showmessagedialog(), showinputdialog()

Programming with Java GUI components

CS 335 Lecture 06 Java Programming GUI and Swing

Graphical User Interfaces

Using A Frame for Output

How to Convert an Application into an Applet.

How To Write A Program For The Web In Java (Java)

The Basic Java Applet and JApplet

Advanced Network Programming Lab using Java. Angelos Stavrou

GUIs with Swing. Principles of Software Construction: Objects, Design, and Concurrency. Jonathan Aldrich and Charlie Garrod Fall 2012

Tema: Encriptación por Transposición

CSS 543 Program 3: Online Tic-Tac-Toe Game Professor: Munehiro Fukuda Due date: see the syllabus

11. Applets, normal window applications, packaging and sharing your work

method is never called because it is automatically called by the window manager. An example of overriding the paint() method in an Applet follows:

Probability: The Study of Randomness Randomness and Probability Models. IPS Chapters 4 Sections

Extending Desktop Applications to the Web

Expected Value and the Game of Craps

The Abstract Windowing Toolkit. Java Foundation Classes. Swing. In April 1997, JavaSoft announced the Java Foundation Classes (JFC).

Essentials of the Java(TM) Programming Language, Part 1

file://c:\dokumente und Einstellungen\Marco Favorito\Desktop\ScanCmds.html

Callbacks. Callbacks Copyright 2007 by Ken Slonneger 1

Assignment # 2: Design Patterns and GUIs

Assignment No.3. /*-- Program for addition of two numbers using C++ --*/

Tutorial: Time Of Day Part 2 GUI Design in NetBeans

How To Build A Swing Program In Java.Java.Netbeans.Netcode.Com (For Windows) (For Linux) (Java) (Javax) (Windows) (Powerpoint) (Netbeans) (Sun) (

Introduction to Java Applets (Deitel chapter 3)

JiST Graphical User Interface Event Viewer. Mark Fong

Lab 11. Simulations. The Concept

MA 1125 Lecture 14 - Expected Values. Friday, February 28, Objectives: Introduce expected values.

Essentials of the Java Programming Language

Java Appletek II. Applet GUI

We { can see that if U = 2, 3, 7, 11, or 12 then the round is decided on the first cast, U = V, and W if U = 7, 11 X = L if U = 2, 3, 12.

Lab 9. Spam, Spam, Spam. Handout 11 CSCI 134: Spring, To gain experience with Strings. Objective

Lösningsförslag till tentamen

Object Oriented Programming with Java. School of Computer Science University of KwaZulu-Natal

GUI Components: Part 2

LAYOUT MANAGERS. Layout Managers Page 1. java.lang.object. java.awt.component. java.awt.container. java.awt.window. java.awt.panel

Tutorial Reference Manual. Java WireFusion 4.1

The overall size of these chance errors is measured by their RMS HALF THE NUMBER OF TOSSES NUMBER OF HEADS MINUS NUMBER OF TOSSES

Swing. A Quick Tutorial on Programming Swing Applications

Unit 19: Probability Models


Java GUI Programming. Building the GUI for the Microsoft Windows Calculator Lecture 2. Des Traynor 2005

Elementary Statistics and Inference. Elementary Statistics and Inference. 16 The Law of Averages (cont.) 22S:025 or 7P:025.

Mouse Event Handling (cont.)

How Scala Improved Our Java

Betting systems: how not to lose your money gambling

JIDE Action Framework Developer Guide

Management Information Systems 260 Web Programming Fall 2006 (CRN: 42459)

The game of roulette is played by throwing a small ball onto a rotating wheel with thirty seven numbered sectors.

Chapter 16. Law of averages. Chance. Example 1: rolling two dice Sum of draws. Setting up a. Example 2: American roulette. Summary.

J a v a Quiz (Unit 3, Test 0 Practice)

Stat 20: Intro to Probability and Statistics

Iteration CHAPTER 6. Topic Summary

How To Program In Java (Ipt) With A Bean And An Animated Object In A Powerpoint (For A Powerbook)

13:69E-1.10 Blackjack table; card reader device; physical characteristics; inspections

16.1 DataFlavor DataFlavor Methods. Variables

COME OUT POINT The number (4, 5, 6, 8, 9 or 10) thrown by the shooter on the Come Out roll.

36 Odds, Expected Value, and Conditional Probability

Statistics and Random Variables. Math 425 Introduction to Probability Lecture 14. Finite valued Random Variables. Expectation defined

PLACE BETS (E) win each time a number is thrown and lose if the dice ODDS AND LAYS HARDWAYS (F) BUY & LAY BETS (G&H)

public class demo1swing extends JFrame implements ActionListener{

Elementary Statistics and Inference. Elementary Statistics and Inference. 17 Expected Value and Standard Error. 22S:025 or 7P:025.

Skills and Topics for TeenCoder: Java Programming

Chapter 16: law of averages

First Java Programs. V. Paúl Pauca. CSC 111D Fall, Department of Computer Science Wake Forest University. Introduction to Computer Science

SIXTEEN PLAYERS, TWO DICE, ONE SHOOTER

Hooray for the Hundreds Chart!!

Dev Articles 05/25/07 11:07:33

CS170 Lab 11 Abstract Data Types & Objects

Mathematical Expectation

Java is commonly used for deploying applications across a network. Compiled Java code

The number (4, 5, 6, 8, 9 or 10) thrown by the shooter on the Come Out roll.

Week 5: Expected value and Betting systems

Contemporary Mathematics- MAT 130. Probability. a) What is the probability of obtaining a number less than 4?

Analysis Of Source Lines Of Code(SLOC) Metric

Lab 1A. Create a simple Java application using JBuilder. Part 1: make the simplest Java application Hello World 1. Start Jbuilder. 2.

Illustration 1: An applet showing constructed responses in an intuitive mode. Note the misprint!

Fondamenti di Java. Introduzione alla costruzione di GUI (graphic user interface)

Syllabus for CS 134 Java Programming

Tutorial 8: Quick Form Button

Week 2: Conditional Probability and Bayes formula

Website Builder Overview

Basic Probability. Probability: The part of Mathematics devoted to quantify uncertainty

Chapter 4 Lecture Notes

In the situations that we will encounter, we may generally calculate the probability of an event

Section 7C: The Law of Large Numbers

Lecture 13. Understanding Probability and Long-Term Expectations

Solution. Solution. (a) Sum of probabilities = 1 (Verify) (b) (see graph) Chapter 4 (Sections ) Homework Solutions. Section 4.

OBJECT ORIENTED PROGRAMMING IN JAVA EXERCISES

Transcription:

Lecture 15 The Game of "Craps" In the game of "craps" a player throws a pair of dice. If the sum on the faces of the pair of dice after the first toss is 7 or 11 the player wins; if the sum on the first throw is 2, 3, or 12 he loses. If he tosses a 4,5,6,8,9,or 10 then this sum is caleed the his "point" and he continues rolling until he makes his point, and wins, or tosses a 7, and loses. The final product we shall develop, as Lab 7, is on the web. We will approach the development by building the parts, trying to combine the parts into one piece, finding difficulties in carrying out the combinations, and then resolving these problems. Simulating the Game We begin by constructing a class Craps for simulating the game. The "Bank" will be built later. The class will employ JLabels, JTextFields, a JButton, and will also implement the ActionListener. The skeleton of Craps is as follows: import java.awt.*; import javax.swing.*; import java.awt.event.*; public class Craps extends JFrame implements ActionListener final int WON = 0,LOST =1, CONTINUE = 2; boolean firstroll = true; int sumofdice = 0; int mypoint = 0; int gamestatus = CONTINUE; JLabel die1label, die2label, sumlabel, pointlabel, pointlabel, setbetlabel; JTextField firstdie, seconddie, sum, point, setbet, display; JButton roll; Gridlayout grid; public Craps() // end Craps() public void actionperformed(actionevent e) 1

play(); //end actionperformed(actionevent e) public void play() // end play() public int rolldice() return 1; // end rolldice() // end Craps This is the bare structure of the class. The two major parts to be written are the constructor Craps() and the method play(). Incidentally, the class as it now stands will compile; it will not compile without the return 1 in rolldice(). The constructor This segment consists of building the constructor Craps2(). (The names will go from Craps to Craps2, to Craps3, etc in order to have a record of the different stages of the development of the program) As you can see, in the red print below, building the constructor consists of creating JLabels and JTextFields an adding them to the container. import java.awt.*; import javax.swing.*; import java.awt.event.*; public class Craps2 extends JFrame implements ActionListener final int WON = 0,LOST =1, CONTINUE = 2; boolean firstroll = true; int sumofdice = 0; int mypoint = 0; int gamestatus = CONTINUE; JLabel die1label, die2label, sumlabel, pointlabel, setbetlabel; JTextField firstdie, seconddie, sum, point, setbet, display; JButton roll; GridLayout grid; 2

public Craps2() Container c = getcontentpane(); grid = new GridLayout(6,2,2,2); c.setlayout(grid); setbetlabel = new JLabel(" Set your bet"); c.add(setbetlabel); setbet = new JTextField(); setbet.addactionlistener(this); c.add(setbet); die1label = new JLabel(" Die1"); c.add(die1label); firstdie = new JTextField(); firstdie.seteditable(false); c.add(firstdie); die2label = new JLabel(" Die2"); c.add(die2label); seconddie = new JTextField(); seconddie.seteditable(false); c.add(seconddie); sumlabel = new JLabel(" c.add(sumlabel); sum = new JTextField(); sum.seteditable(false); c.add(sum); Sum is"); pointlabel = new JLabel(" c.add(pointlabel); point = new JTextField(); point.seteditable(false); c.add(point); Point is"); roll = new JButton(" Roll Dice"); roll.addactionlistener(this); c.add(roll); display = new JTextField(); c.add(display); setsize(200,300); setlocation(300,200); show(); // <---- Be sure to show your work // end Craps2() 3

public void actionperformed(actionevent e) play(); //2 the action //end actionperformed(actionevent e) public void play() //3 implementing the action // end play() public int rolldice() //4 the simplest part return 1; // end rolldice() // end Craps2 To run this class as an applet use the simple applet below: import java.awt.*; import java.awt.event.*; import javax.swing.*; public class TestCraps2 extends JApplet Craps2 game; public void init() game = new Craps2(); Of course you will need the following file,testcraps2.html, to run the applet. <html> <applet code="testcraps2.class" height = 50 width = 500> </applet> </html> When is TestCraps2 compiled and viewed by means of the appletviewer the following appears on the screen: 4

The background colors above are the ones that appear when no special efforts are made to produce particular colors. In general, labels and fields that cannot be edited will be gray; fields that can be edited will be white. So, all we have so far is the grid that will hold our results. Playing the game For the time being we shall ignore the question of the size of the bet and concentrate on playing the game. Consequently the actionperformed( ) method will have the simple form: public void actionperformed(actionevent e) play(); //end actionperformed(actionevent e) Of course, writing play() is the major problem at this point. We start with a simple method for simulating one toss of a pair of dice: public int rolldice() int die1, die2, worksum; die1 = 1 + (int)(6*math.random()); die2 = 1 + (int)(6*math.random()); worksum = die1 + die2; firstdie.settext(integer.tostring(die1)); seconddie.settext(integer.tostring(die2)); sum.settext(integer.tostring(worksum)); return worksum; // end rolldice() There is nothing new here. 5

The play() method does contain a new idea, mainly switch(sumofdice),which is a variation of the if-then construction. public void play() if(firstroll) sumofdice = rolldice(); switch(sumofdice) case 7: case 11: gamestatus = WON; point.settext(""); case 2: case 3: case 12: gamestatus = LOST; point.settext(""); default: gamestatus = CONTINUE; mypoint = sumofdice; point.settext(integer.tostring(mypoint)); firstroll = false; // end switch // end if(firstroll) else sumofdice = rolldice(); if(sumofdice == mypoint) gamestatus = WON; if(sumofdice == 7) gamestatus = LOST; //end else if(gamestatus == CONTINUE) display.settext("roll Again"); if(gamestatus == WON) display.settext(" firstroll = true; Win!. Roll\n to play again"); if(gamestatus == LOST) display.settext(" Player Loses\n Play again"); firstroll = true; // end play 6

In general switch() is an integer-valued method is a method whose parameter is also an integer. The fragment switch(sumofdice) case 7: case 11: gamestatus = WON; point.settext(""); means if sumofdice is 7 or 11 then gamestatus = WON. For the record, the complete program at this point, Craps3, is: import java.awt.*; import javax.swing.*; import java.awt.event.*; public class Craps3 extends JFrame implements ActionListener final int WON = 0,LOST =1, CONTINUE = 2; boolean firstroll = true; int sumofdice = 0; int mypoint = 0; int gamestatus = CONTINUE; JLabel die1label, die2label, sumlabel, pointlabel, setbetlabel; JTextField firstdie, seconddie, sum, point, setbet, display; JButton roll; GridLayout grid; public Craps3() Container c = getcontentpane(); grid = new GridLayout(6,2,2,2); // 1 c.setlayout(grid); setbetlabel = new JLabel(" Set your bet"); c.add(setbetlabel); setbet = new JTextField(); setbet.addactionlistener(this); // 2 c.add(setbet); die1label = new JLabel(" Die1"); c.add(die1label); firstdie = new JTextField(); firstdie.seteditable(false); c.add(firstdie); die2label = new JLabel(" Die2"); c.add(die2label); seconddie = new JTextField(); seconddie.seteditable(false); 7

c.add(seconddie); sumlabel = new JLabel(" Sum is"); c.add(sumlabel); sum = new JTextField(); sum.seteditable(false); c.add(sum); pointlabel = new JLabel(" Point is"); c.add(pointlabel); point = new JTextField(); point.seteditable(false); c.add(point); roll = new JButton(" Roll Dice"); roll.addactionlistener(this); c.add(roll); display = new JTextField(); c.add(display); setsize(200,300); setlocation(300,200); show(); // end Craps3() public void actionperformed(actionevent e) play(); //end actionperformed(actionevent e) public void play() if(firstroll) sumofdice = rolldice(); switch(sumofdice) case 7: case 11: gamestatus = WON; point.settext(""); case 2: case 3: case 12: gamestatus = LOST; point.settext(""); default: gamestatus = CONTINUE; mypoint = sumofdice; point.settext(integer.tostring(mypoint)); 8

firstroll = false; // end switch // end if(firstroll) else sumofdice = rolldice(); if(sumofdice == mypoint) gamestatus = WON; if(sumofdice == 7) gamestatus = LOST; //end else if(gamestatus == CONTINUE) display.settext("roll Again"); if(gamestatus == WON) display.settext(" firstroll = true; Win!. Roll\n to play again"); if(gamestatus == LOST) display.settext(" Player Loses\n Play again"); firstroll = true; // end play public int rolldice() int die1, die2, worksum; die1 = 1 + (int)(6*math.random()); die2 = 1 + (int)(6*math.random()); worksum = die1 + die2; firstdie.settext(integer.tostring(die1)); seconddie.settext(integer.tostring(die2)); sum.settext(integer.tostring(worksum)); return worksum; // end rolldice() // end Craps3 Two supplements are needed to test it. First, an applet: import java.awt.*; import java.awt.event.*; import javax.swing.*; public class TestCraps3 extends JApplet 9

Craps3 game; public void init() game = new Craps3(); Then a TesTCraps3.html file: <html> <applet code="testcraps3.class" height = 50 width = 500> </applet> </html> When the program is run a window like the one in the first panel below appears. The player then clicks on the Roll Dice button. If he makes a 7 on the first toss, he wins, as in the second window. If he tosses a 10 on the first roll, as in third panel, the game continues. The setting of bets and accompanying bookkeeping have yet to be implemented. 10

Setting Colors and Fonts for Labels, TextFields, and Buttons There are several ways to go about changing colors and fonts on components. First, you can go to the beginning of the constructor and add the line c.setbackground(color.white): public Craps3() Container c = getcontentpane(); grid = new GridLayout(6,2,2,2); c.setlayout(grid); c.setbackground(color.white);. When this is done most of the components will have a white background. Some, such as buttons and text fields that are not editable will have a blue-gray background. Second, you can work with the individual components. In the case below the background has been set to white, the foreground (the text) has been set to red, and the font has been set to SansSerif, bold,16 point. pointlabel = new JLabel(" Point is"); c.add(pointlabel); point = new JTextField(); point.setbackground(color.white); point.setforeground(color.red); point.setfont(new Font("SansSerif", Font.BOLD, 16)); point.seteditable(false); c.add(point); 11