Classwork 7: Craps. N. Duong & R. Rodriguez, Java Crash Course January 6, 2015

Similar documents
Lesson 2.4 Arraylist

CPSC 427: Object-Oriented Programming

Review Chapter 6 in Bravaco. Short Answers 1. This type of method does not return a value. a. null b. void c. empty d. anonymous

C Functions. 5.2 Program Modules in C

Due Date: Two Program Demonstrations (Testing and Debugging): End of Lab

Problem Solving through Programming In C Prof. Anupam Basu Department of Computer Science & Engineering Indian Institute of Technology, Kharagpur

Java Outline (Upto Exam 2)

COMP 110 Programming Exercise: Simulation of the Game of Craps

Intro. Scheme Basics. scm> 5 5. scm>

Tic Tac Toe Game! Day 8

Tutorial 12 Craps Game Application: Introducing Random Number Generation and Enumerations

STUDENT LESSON A14 Boolean Algebra and Loop Boundaries

Program #7: Let s Play Craps!

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #29 Arrays in C

Intro to Programming. Unit 7. What is Programming? What is Programming? Intro to Programming

A PROGRAM IS A SEQUENCE of instructions that a computer can execute to

Singly linked lists in C.

The for Loop. Lesson 11

Lecture 05 I/O statements Printf, Scanf Simple statements, Compound statements

Mat 2170 Week 9. Spring Mat 2170 Week 9. Objects and Classes. Week 9. Review. Random. Overloading. Craps. Clients. Packages. Randomness.

Static Methods. Why use methods?

BEGINNER PHP Table of Contents

CS 106 Introduction to Computer Science I

Student Responsibilities. Mat 2170 Week 9. Notes About Using Methods. Recall: Writing Methods. Chapter Six: Objects and Classes

CSCI 1100L: Topics in Computing Lab Lab 11: Programming with Scratch

Arrays. myints = new int[15];

Basics of Programming with Python

Programming Data Structures and Algorithms Prof. Shankar Balachandran Department of Computer Science Indian Institute of Technology, Madras

Fundamentals. Fundamentals. Fundamentals. We build up instructions from three types of materials

Chapter 1 Operations With Numbers

CPSC 427: Object-Oriented Programming

C Pointers 2013 Author Riko H i

COMP 110/L Lecture 4. Kyle Dewey

do fifty two: Language Reference Manual

These are notes for the third lecture; if statements and loops.

6.S189 Homework 1. What to turn in. Exercise 1.1 Installing Python. Exercise 1.2 Hello, world!

introduction to Programming in C Department of Computer Science and Engineering Lecture No. #40 Recursion Linear Recursion

Assignment Checklist. Prelab Activities. Lab Exercises. Labs Provided by Instructor. Postlab Activities. Section:

Functions. Computer System and programming in C Prentice Hall, Inc. All rights reserved.

Civil Engineering Computation

CS103 Handout 29 Winter 2018 February 9, 2018 Inductive Proofwriting Checklist

LOOPS. Repetition using the while statement

Last Time: Rolling a Weighted Die

Foundations, Reasoning About Algorithms, and Design By Contract CMPSC 122

Text Input and Conditionals

Function Call Stack and Activation Records

Mobile Computing Professor Pushpendra Singh Indraprastha Institute of Information Technology Delhi Java Basics Lecture 02

Chapter 5 Methods. public class FirstMethod { public static void main(string[] args) { double x= -2.0, y; for (int i = 1; i <= 5; i++ ) { y = f( x );

Com S 227 Spring 2018 Assignment points Due Date: Thursday, September 27, 11:59 pm (midnight) "Late" deadline: Friday, September 28, 11:59 pm

Ex: If you use a program to record sales, you will want to remember data:

SPRITES Moving Two At the Same Using Game State

CONTENTS: Arrays Strings. COMP-202 Unit 5: Loops in Practice

Programming assignment A

Stepwise Refinement. Lecture 12 COP 3014 Spring February 2, 2017

Excel Basics Rice Digital Media Commons Guide Written for Microsoft Excel 2010 Windows Edition by Eric Miller

Note. The above image and many others are courtesy of - this is a wonderful resource for designing circuits.

CONTENTS: What Is Programming? How a Computer Works Programming Languages Java Basics. COMP-202 Unit 1: Introduction

static String usersname; public static int numberofplayers; private static double velocity, time;

do fifty-two: An Introductory Programming Language

CMPSCI 187 / Spring 2015 Hangman

Introduction to Java Programs for Packet #4: Classes and Objects

Lab 1 Implementing a Simon Says Game

Introduction to Computer Science Unit 3. Programs

MA 1128: Lecture 02 1/22/2018

Python allows variables to hold string values, just like any other type (Boolean, int, float). So, the following assignment statements are valid:

Lab 1 Implementing a Simon Says Game

int j = 0, sum = 0; for (j = 3; j <= 79; j++) { sum = sum + j; System.out.println(sum); //Show the progress as we iterate thru the loop.

Part II Composition of Functions

Get JAVA. I will just tell you what I did (on January 10, 2017). I went to:

How To Think Like A Computer Scientist, chapter 3; chapter 6, sections

Out for Shopping-Understanding Linear Data Structures English

COMP-202 More Complex OOP

Dr M Kasim A Jalil. Faculty of Mechanical Engineering UTM (source: Deitel Associates & Pearson)

Functions in C++ Problem-Solving Procedure With Modular Design C ++ Function Definition: a single

Arrays in C++ Instructor: Andy Abreu

ACORN.COM CS 1110 SPRING 2012: ASSIGNMENT A1

Definition: A data structure is a way of organizing data in a computer so that it can be used efficiently.

Programming Lab 1 (JS Hwk 3) Due Thursday, April 28

Announcements. Lab Friday, 1-2:30 and 3-4:30 in Boot your laptop and start Forte, if you brought your laptop

COMP 250 Winter generic types, doubly linked lists Jan. 28, 2016

Lecture 5: Methods CS2301

Design and Analysis of Algorithms Prof. Madhavan Mukund Chennai Mathematical Institute. Module 02 Lecture - 45 Memoization

PROFESSOR: Well, now that we've given you some power to make independent local state and to model objects,

Java Assignment 3: Loop Practice Ver 3.0 Last Updated: 12/1/2015 8:57 AM

LAB: WHILE LOOPS IN C++

Control, Quick Overview. Selection. Selection 7/6/2017. Chapter 2. Control

Game keystrokes or Calculates how fast and moves a cartoon Joystick movements how far to move a cartoon figure on screen figure on screen

Haskell Refresher Informatics 2D

Introduction. C provides two styles of flow control:

CS211 Computers and Programming Matthew Harris and Alexa Sharp July 9, Boggle

Getting Started. Excerpted from Hello World! Computer Programming for Kids and Other Beginners

Core Competency DOO (Design Object-Oriented)

How to approach a computational problem

Lecture 9. Assignment. Logical Operations. Logical Operations - Motivation 2/8/18

Chapter 3. More Flow of Control. Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

COMP-202: Foundations of Programming. Lecture 6: Conditionals Jackie Cheung, Winter 2016

Initial Coding Guidelines

Functions and Recursion

Linked Lists. What is a Linked List?

EAS230: Programming for Engineers Lab 1 Fall 2004

Transcription:

Classwork 7: Craps N. Duong & R. Rodriguez, Java Crash Course January 6, 2015 For this classwork, you will be writing code for the game Craps. For those of you who do not know, Craps is a dice-rolling game. How this game is played is by having a single player roll two dice. If the sum of the numbers on the dice is either 7 or 11 and this is the player s first roll of the game, the players automatically wins. If the sum of the numbers on the dice is either 2, 3, or 12 and this is the player s first roll of the game, the player loses. Otherwise, the sum of the numbers on the dice is the new winning number and the player continues to roll the dice until he wins the game. If the player is not on his first roll of the game and he rolls a 7, the player loses. However, if the player is not on his first roll of the game and he rolls the new winning number (the sum of the numbers on the dice on the first roll of the game), he wins. Otherwise, he will continue to roll until he either wins or loses the game. When the player wins or loses the game, his new winning number is no longer applicable meaning he will not win if he rolls on the "new winning number" (this is put in quotes because it s technically no longer a new winning number). To download the game files, please CLICK ON HERE! 1 The Program Explained You may feel a bit overwhelmed by the amount of files we need in order to create and run this simple dice-rolling game, and that s fine! However you should also realize that this is the amount of work that is required to be done in order to have a simple and properly functioning game. And with more complex games, there will be more files! This is just to give you a little bit of taste into actually programming something bigger than what we have done previously in this class. For this classwork, the only file you will be editing in will be CrapsGame.java. While you are welcome to look into the other files, please do not make any changes in the other files because it may break the program. In addition to this, it is OK if you do not understand a lot of the code in the other files. A lot of the ideas, and concepts written in the other files are well beyond this simple crash course. The biggest idea you need to understand is that CrapsGame.java does not require a main() method! Instead, we will be trying to write code for the method inside of CrapsGame.java abstractly. And in order for you to be able to write a method abstractly, you must know how Classwork 7 Page 1

a method is constructed well. Below this will be a short explanation on understanding what a method is suppose to do. 1 public static int iseven(int num) { 2... 3 } 4 Listing 1: Understanding Methods 5 / ** Everything below this is considered a comment -- which will not 6 * be read by the program. 7 * 8 * You may notice that the method inside of CrapsGame.java may not 9 * have static -- this is fine, static has to do with the way objects 10 * work. It is OK if static does not exist in the method declaration. 11 * 12 * What does it return? "int" -- how do we know this? Because after 13 * public and before the method s name is the type "int." Now we know 14 * what TYPE to return. 15 * 16 * Now what kind of argument(s) -- if any does it take in? You 17 * figure this out by looking between the parenthesis (...). If 18 * there are nothing inside of it, the program has no arguments, 19 * meaning it does not take in anything. However, in this specific 20 * case, we see that it takes in "int num". What this means, the 21 * program will take in "a variable called num with the type int". 22 * Now we know what arguments this method takes in. 23 * / At the moment, all you really need to know is that somewhere in the other files, there is a line of code that uses the method processroll(...), which you will be required to write the logic for. And since the current processroll(...) does not have actual logic to it, it does not return the correct value. Therefore, currently, our program is not working as it is supposed to. Now in order to fix this, we will need to write the logic out for the method processroll(...). 2 Understanding processroll(...) First thing you need to understand is that methods use the idea of abstraction. Meaning processroll(...) uses the idea of abstraction too! Therefore do not think of the game; instead, think of what processroll(...) is supposed to do. So here s the big question, what is processroll(...) suppose to do? Let s look at its declaration to figure it out. Listing 2: processroll() 1 public int processroll(int sumofroll) { Classwork 7 Page 2

2 // FIXME -- write the appropriate algorithm 3 return 0; // dummy return -- need to return an actual value later 4 } At the moment, we see that nothing is written inside the class besides a comment telling us to fix it with an appropriate algorithm. There is also a dummy return because the method demands something to be returned. So this gives us the first hint. This method needs to return something, but what? Using what we learned above, the type that comes after public but before the methodname is the type that gets returned. In this situation, it is an int. Now continue reading through the declaration, we see that something exists between the parenthesis. As mentioned above, this means that the method has an arguments (or in other words, the method takes something in). Now how do we figure out what type does it take in? By reading the variable created, which in this case is "int sumofroll". Translating this into English, it means "a variable sumofroll with the type int." Putting it all together: this method we are supposed to write will return an int. This method takes in an int called sumofroll for us to use in any manner we wish for the method to work properly. 3 What processroll(...) Does Now that you know the little details of the what processroll(...) expects from its declaration, we will move onto what is the "appropriate algorithm" for processroll(...) is. And what I mean by this is what the method processroll(...) is suppose to do. We know what it returns and takes in, but what does it do? processroll(...) is suppose to take its given argument, sumofroll, to return -1, 0, or 1. In this situation, -1, 0, 1 is lose, continue-playing, and win respectively. You will have to write an algorithm that will appropriate return either -1, 0, or 1 depending on whether or not the sumofroll is a winning number, losing number, or otherwise a number that does not affect the game and will let the player keep rolling the dice. You will be expected to figure out this algorithm by the description of the game given above and/or using the pseudocode we will be providing you in the next section. For those of you who do not know what pseudocode is, it is a guide/reference for an algorithm based on code to guide you on which paths/ideas/directions you should be taking in order to code the specific algorithm. Classwork 7 Page 3

4 processroll(...) Pseudocode The following is only a guide or reference on paths/ideas/directions you can take in order to create a functioning algorithm for the method processroll(...). You will need to translate this into actual Java code/syntax. While the following code may look like actual Java code, it is using the idea of abstraction such that some of the statements/commands we call inside the pseudocode may not actually exist inside of Java but when a human person read the pseudocode, they get understand what the pseudocode is trying to do and then the human person will translate it to the Java language. Again, do not copy this into your file. It will not work! Listing 3: processroll(...) pseudocode 1 processroll(int sumofroll) { 2 if (firstround) { 3 if (sumofroll is 7 or 11) { 4 WIN(); 5 } else if (sumofroll is 2 or 3 or 12) { 6 LOSE(); 7 } else { 8 SAVE_CURRENT_ROLL_SUM(); 9 CONTINUE_GAME(); 10 } 11 } else { // if it s not firstround, it s any other rounds else! 12 if (sumofroll is 7) { 13 RESET_WINNING_NUMBER(); 14 LOSE(); 15 } else if (sumofroll is winningnumber) { 16 RESET_WINNING_NUMBER(); 17 WIN(); 18 } else { 19 CONTINUE_GAME(); 20 } 21 } 22 } To see the pseudocode s commands defined, look onto the next page. Classwork 7 Page 4

firstround sumofroll winningnumber WIN() LOSE() CONTINUE_GAME() SAVE_CURRENT_ROLL_SUM() RESET_WINNING_NUMBER() The boolean representing whether or not this is the Player s first round in rolling the dice The variable representing the sum of the numbers on the dice after rolling. The variable representing the sum of the numbers on the dice that will be considered as the "winning number" after the first roll. Because this variable is to be remembered even after processroll(...) is called again, its declaration or creation should be outside of the processroll(...) scope the Player has won the game the Player has lost the game the Player has lost the game This will record/save/store the current sumofroll into winningnumber This will method will change the variable winningnumber to 0 so that a winning number will not exist anymore since the game will restart after the Player loses or wins 5 Disclaimer This program was originally created by Michael Ferraro for his APCS class. Some revisions were made by R. Rodriguez and N. Duong. We have his permission to use his code for this class assignment. Classwork 7 Page 5