private static final char[] Alphabet = "abcdefghijklmnopqrstuvwxyz".tochararray();

Similar documents
-Alfred North Whitehead. Copyright Pearson Education, 2010 Based on slides by Marty Stepp and Stuart Reges from

Exam 2. Programming I (CPCS 202) Instructor: M. G. Abbas Malik. Total Marks: 40 Obtained Marks:

Building Java Programs

CSC 1051 Algorithms and Data Structures I. Final Examination May 12, Name

Question 1 [20 points]

Building Java Programs

Introduction to Computer Science Unit 2. Exercises

CSC 1051 Algorithms and Data Structures I. Final Examination May 12, Name: KEY. Question Value Score

Java Classes: Random, Character A C S L E C T U R E 6

Introduction to Computer Programming

CSC 1051 Algorithms and Data Structures I. Midterm Examination October 11, Name: KEY

Name: Checked: Preparation: Response time experiment accessing the system clock

! definite loop: A loop that executes a known number of times. " The for loops we have seen so far are definite loops. ! We often use language like

RANDOM NUMBER GAME PROJECT

COMP 110 Programming Exercise: Simulation of the Game of Craps

Conditional Programming

Mr. Monroe s Guide to Mastering Java Syntax

The for Loop, Accumulator Variables, Seninel Values, and The Random Class. CS0007: Introduction to Computer Programming

Name: Checked: Preparation: Response time experiment accessing the system clock

CMSC 150 INTRODUCTION TO COMPUTING LAB WEEK 3 STANDARD IO FORMATTING OUTPUT SCANNER REDIRECTING

Lab 6. Name: Checked:

CCHS Math Recursion Worksheets M Heinen CS-A 12/5/2013. Recursion Worksheets Plus Page 1 of 6

AP Computer Science A Unit 2. Exercises

Java Foundations: Introduction to Program Design & Data Structures, 4e John Lewis, Peter DePasquale, Joseph Chase Test Bank: Chapter 2

Midterm Examination (MTA)

Fundamentals of Programming Data Types & Methods

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

Flow Control. Key Notion. Statement Categories. 28-Oct-10

2.2 - Making Decisions

H212 Introduction to Software Systems Honors

CS 200 Using Objects. Jim Williams, PhD

Interpreted vs Compiled. Java Compile. Classes, Objects, and Methods. Hello World 10/6/2016. Python Interpreted. Java Compiled

Manipulating One-dimensional Arrays

AP COMPUTER SCIENCE A

CIS 1068 Program Design and Abstraction Spring2016 Midterm Exam 1. Name SOLUTION

Flow Control. Boaz Kantor Introduction to Computer Science, Fall semester IDC Herzliya

Name: Checked: Access the Java API at the link above. Why is it abbreviated to Java SE (what does the SE stand for)?

Lesson 7 Part 2 Flags

Introduction to Computer Science Unit 5. Programs: Strings

Oct Decision Structures cont d

AP Computer Science Unit 1. Programs

1 Short Answer (10 Points Each)

(A) 99 ** (B) 100 (C) 101 (D) 100 initial integers plus any additional integers required during program execution

It is a constructor and is called using the new statement, for example, MyStuff m = new MyStuff();

C212 Early Evaluation Exam Mon Feb Name: Please provide brief (common sense) justifications with your answers below.

New York University Introduction to Computer Science Exam Sample Problems 2013 Andrew I. Case. Instructions:

1. An operation in which an overall value is computed incrementally, often using a loop.

Wentworth Institute of Technology. Engineering & Technology WIT COMP1000. Java Basics

Computational Expression

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

Unit 4: Classes and Objects Notes

Building Java Programs

Controls Structure for Repetition

SECONDARY SCHOOL, L-IMRIEĦEL HALF YEARLY EXAMINATIONS 2016/2017

CSC 1051 Villanova University. CSC 1051 Data Structures and Algorithms I. Course website:

CS 302: Introduction to Programming

CSE 1223: Introduction to Computer Programming in Java Chapter 7 File I/O

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

Programming with Java

Type boolean. Building Java Programs. Recap: Type boolean. "Short-circuit" evaluation. De Morgan's Law. Boolean practice questions.

Unit 10: Sorting/Searching/Recursion

Final Exam Practice. Partial credit will be awarded.

Give one example where you might wish to use a three dimensional array

New York University Introduction to Computer Science Exam Sample Problems 2013 Andrew I. Case. Instructions:

Multiple Choice Using Object Methods

Introduction to Computer Science Unit 4B. Programs: Classes and Objects

Question: Total Points: Score:

Building Java Programs

Repetition, Looping. While Loop

CSC 1051 Algorithms and Data Structures I. Midterm Examination February 24, Name: KEY 1

Section 003 Fall CS 170 Exam 1. Name (print): Instructions:

1. [3 pts] What is your section number, the period your discussion meets, and the name of your discussion leader?

CS 101 Fall 2006 Midterm 1 Name: ID:

CSC 1051 Data Structures and Algorithms I

Ryerson University Vers HAL6891A-05 School of Computer Science CPS109 Midterm Test Fall 05 page 1 of 6

CSC 1051 Algorithms and Data Structures I. Midterm Examination February 25, Name: KEY A

Practice Midterm 1 Answer Key

CONTENTS: Compilation Data and Expressions COMP 202. More on Chapter 2

FOR INTERNAL SCRUTINY (date of this version: 17/2/2016) UNIVERSITY OF EDINBURGH COLLEGE OF SCIENCE AND ENGINEERING SCHOOL OF INFORMATICS

Sequence structure. The computer executes java statements one after the other in the order in which they are written. Total = total +grade;

CMPSCI 187 / Spring 2015 Hangman

Object Oriented Programming with JAVA

(A) 99 (B) 100 (C) 101 (D) 100 initial integers plus any additional integers required during program execution

Utility Classes Are Killing Us

Object Oriented Programming. Java-Lecture 1

Warm up Exercise. What are the types and values of the following expressions: * (3 + 1) 3 / / 2.0 (int)1.0 / 2

CSIS 10A Assignment 4 SOLUTIONS

Introduction to Arrays

There are several files including the start of a unit test and the method stubs in MindNumber.java. Here is a preview of what you will do:

CHAPTER 5 VARIABLES AND OTHER BASIC ELEMENTS IN JAVA PROGRAMS

Chapter 9 Lab Text Processing and Wrapper Classes

6 COMPUTER PROGRAMMING

Mid Term Exam 1. Programming I (CPCS 202) Instructor: M. G. Abbas Malik Date: Sunday November 3, 2013 Total Marks: 50 Obtained Marks:

Section 2.2 Your First Program in Java: Printing a Line of Text

M105: Introduction to Programming with Java Midterm Examination (MTA) Makeup Spring 2013 / 2014

Faculty of Science Midterm. COMP-202B - Introduction to Computing I (Winter 2008)

Menu Driven Systems. While loops, menus and the switch statement. Mairead Meagher Dr. Siobhán Drohan. Produced by:

For that purpose, java provides control structures that serve to specify what has to be done by our program, when and under which circumstances.

Building Java Programs

Tutorial 12. Exercise 1: Exercise 2: CSC111 Computer Programming I

Transcription:

//Shelley Latreille Hang Man Game /* This program plays the game Hang Man with the user. Hang Man is a game that requires the user to guess the letters of a word before running out of incorrect guesses. Each incorrect guess will build another part of the Man who is hanging from the gallows. When the game starts, the user is presented with an unoccupied gallows, all the letters in the alphabet, and a set of blanks representing the letters in the word. The user guesses a letter. The user will be allowed 7 incorrect guesses. The program determines if that letter is in the word and if it is, then it uncovers all the letters in the word. In addition to finding the letters in the word, it also removes the letter from the alphabet so the user knows that the letter should not be chosen again. If the user selects a letter that is not in the word, then a body part will be added to the man. Incorrect letter guesses will also be removed from the alphabet. If the user continues to guess incorrectly, the man will continue to be built until the total man appears on the gallows and the user loses the game. The user wins if all the letters in the word are guessed correctly before the man is completely built. */ package hang.man.shelley.latreille; import java.util.random; import java.util.scanner; public class HangManShelleyLatreille //the user's guesses private static char[] wordtoguess; //check the length of the random words to be sent to wordtoguess[] private static int wordlength; //stores the random word temporarily. the user will guess this word private static String guessword; private static char[] dash; private static final char[] Alphabet = "abcdefghijklmnopqrstuvwxyz".tochararray(); private static char usersguess; private static boolean gameover = false; //words provided. one will be randomly selected for the user to guess private static final String[] Words =

"macabre", "demise", "gallows", "surreptitious", "lackadaisical" ; private static String testword; private static int count = 0; public static void main(string[] args) Scanner input = new Scanner(System.in); Random rand = new Random(); //random nextint(5). returns a number 0 through 4 int num = rand.nextint(5); //guessword is assigned to a random String guessword = Words[num]; wordlength = guessword.length(); dash = new char[wordlength]; dash = guessword.tochararray(); dasharray(guessword); //informs user what the program does System.out.println("Hang Man\n" + "This program will play the " + "game of Hang Man with you.\n" + "You will be asked to guess the letters of a word " + "before running out of incorrect guesses. \n" + "Each incorrect guess will build another part of the man who " + "is hanging from the gallows. \n" + "When the game begins, you will see an unoccupied gallows, " + "all the letters in the alphabet, \n" + "and a set of blanks representing the letters in the word. \n" + "You will guess a letter. You will be allowed 7 " + "incorrect guesses. \n" + "If the letter you guess is in the word, " + "then all the letters in the word that match your guess " + "will be uncovered. \n" + "The letter you guess will also be removed from the alphabet " + "that is displayed. \n" + "If you select a letter that is not in the word, " + "a body part will be added to the man. \n" + "If you continue to guess incorrectly, " + "the man will continue to be built. \n" + "You will win the game if all the letters in the word are " + "guessed correctly before the man is completely built. \n"); while ((7 - count)!= 0 && gameover!= true)

int numberofguesses = 0; printgallows(); System.out.println(" "); printalphabet(dash); wordtoguess = explode(guessword); System.out.println("\nPlease input one letter of your choice: "); usersguess = input.nextline().charat(0); updatealphabet(usersguess); wordtoguess = guessword.tochararray(); for (int i = 0; i < wordtoguess.length; i++) if (wordtoguess[i] == usersguess) dash[i] = usersguess; testword = new String(dash); for (int i = 0; i < guessword.length(); i++) if (usersguess!= wordtoguess[i]) numberofguesses++; if (numberofguesses == wordlength) count++; System.out.println("You have " + (7 - count) + " guesses left!"); endgame(); System.out.println((gameOver!= true)? "\n\ni am sorry, you lost " + "the game!" : "\n\ncongratulations! You won the " + "game! " + guessword + " is the correct word!"); if (count == 7) System.out.println("\n-------"); System.out.println(" \t\t");

System.out.println(" / \\"); System.out.println("\n\nYour word was: " + guessword); //grabs a String. uses the method tochararray(); to convert to a char[] public static char[] explode(string s) wordtoguess = s.tochararray(); return wordtoguess; //grabs a character array. prints each character with a space public static void printalphabet(char[] c) for (int i = 0; i < c.length; i++) System.out.print(c[i] + " "); System.out.println(" "); public static char[] dasharray(string s1) dash = new char[wordlength]; for (int i = 0; i < dash.length; i++) dash[i] = '_'; return dash; // letters in the alphabet will be removed and replaced with a dash public static char[] updatealphabet(char guess) for (int i = 0; i < Alphabet.length; i++) if (Alphabet[i] == (guess)) Alphabet[i] = '_';

System.out.print("" + Alphabet[i] + " "); System.out.println(""); return Alphabet; // prints the gallows // if the user's input is incorrect it will display the man public static void printgallows() System.out.println("\n-------"); System.out.println(" \t\t"); if (count == 1) System.out.println(" O"); if (count == 2) if (count == 3) System.out.println(" / "); if (count == 4) if (count == 5) if (count == 6) System.out.println(" /");

System.out.println(" "); System.out.println(" "); System.out.println(" "); printalphabet(alphabet); public static boolean endgame() if (guessword.equals(testword)) gameover = true; return gameover; return gameover;