Picture It Very Basic Game Picture Pepper. Original Game import java.util.Scanner; public class Game { public static void main() { Scanner scan=new Scanner(System.in);

Slides:



Advertisements
Similar presentations
IS Programming Fundamentals 1 (Lec) Date: September 14, Array.
Advertisements

Continuation of chapter 6…. Nested while loop A while loop used within another while loop is called nested while loop. Q. An illustration to generate.
Introduction to Programming
1 March 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems
Introduction to Programming
Input review If review Common Errors in Selection Statement Logical Operators switch Statements Generating Random Numbers practice.
1 Todays Objectives Announcements Homework #1 is due next week Return Quiz 1 – answers are posted on the Yahoo discussion page site Basic Java Programming.
For(int i = 1; i
EXAMPLES (Arrays). Example Many engineering and scientific applications represent data as a 2-dimensional grid of values; say brightness of pixels in.
Introduction to Computer Science Robert Sedgewick and Kevin Wayne Recursive Factorial Demo pubic class Factorial {
Programming Methodology (1). Implementing Methods main.
1 Arrays An array is a special kind of object that is used to store a collection of data. The data stored in an array must all be of the same type, whether.
STRING AN EXAMPLE OF REFERENCE DATA TYPE. 2 Primitive Data Types  The eight Java primitive data types are:  byte  short  int  long  float  double.
Review BP Dari slide pak cahyo pertemuan 7 dan pertemuan 2 dengan sedikit modifikasi.
Loops –Do while Do While Reading for this Lecture, L&L, 5.7.
Computer Programming Lab(7).
© 2011 Pearson Education, publishing as Addison-Wesley Chapter 3: Program Statements 3.6 – Iterators – p
Craps. /* * file : Craps.java * file : Craps.java * author: george j. grevera, ph.d. * author: george j. grevera, ph.d. * desc. : program to simulate.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.1 Chapter 3 Selections.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. Multithreading Example from Liang textbook.
Building Java Programs
Computer Programming Lab 8.
CSCI S-1 Section 5. Deadlines for Problem Set 3 Part A – Friday, July 10, 17:00 EST Parts B – Tuesday, July 14, 17:00 EST Getting the code examples from.
Overloading methods review When is the return statement required? What do the following method headers tell us? public static int max (int a, int b)
Random (1) Random class contains a method to generate random numbers of integer and double type Note: before using Random class, you should add following.
1 BUILDING JAVA PROGRAMS CHAPTER 3 THE SCANNER CLASS AND USER INPUT.
Can we talk?. In Hello World we already saw how to do Standard Output. You simply use the command line System.out.println(“text”); There are different.
CSci 111 – computer Science I Fall 2014 Cynthia Zickos WRITING A SIMPLE PROGRAM IN JAVA.
Lecture 9 Using Objects. Remember: 3 Different Kinds of Classes 1.Application Class – what we've been doing – Has public static void main ( String []
Lecture 8 Using casts, Strings and WordUtil. Agenda Generating random numbers Casts – Casting a double into an int – Casting an int into a char – Casting.
Picture It Game with GUI Control Class Pepper. Class Diagram Game (controller & maybe model) – holds the state of the game – has a static main method.
Category 1 Category 5 Category 4 Category 3 Category
Java - Classes JPatterson. What is a class? public class _Alpha { public static void main(String [] args) { } You have been using classes all year – you.
CSC1401 Strings (text). Learning Goals Working with Strings as a data type (a class) Input and output of Strings String operations.
Crop and Straighten. If you scan a picture it may be tilted on the scanner. Then in a image editing program like photoshop you can straighten the picture.
Introduction to array: why use arrays ?. Motivational example Problem: Write a program that reads in and stores away 5 double numbers After reading in.
1 Chap. 3 Creating Objects The String class Java Class Library (Packages) Math.random() Reading for this Lecture: L&L, 3.1 – 3.6.
Files Review For output to a file: –FileOutputStream variable initialized to filename (String) and append/not append (boolean) –PrintWriter variable initialized.
1 Advanced Programming Examples Output. Show the exact output produced by the following code segment. char[,] pic = new char[6,6]; for (int i = 0; i
CS12230 Introduction to Programming Extra example– More on responsibilities 1.
Java Scanner Class Keyboard Class. User Interaction So far when we created a program there was no human interaction Our programs just simply showed one.
CSCI S-1 Section 8. Coming Soon Problem Set Four (72 + 5/15 points) – Tuesday, July 21, 17:00 EST.
import java.util.Scanner; class myCode { public static void main(String[] args) { Scanner input= new Scanner(System.in); int num1; System.out.println(“Enter.
Jeopardy $100 VariablesErrorsLoops Classes and Objects Program Structure $200 $300 $400 $500 $400 $300 $200 $100 $500 $400 $300 $200 $100 $500 $400 $300.
Programming – Lecture 15 Going Beyond the ACM Library.
CSC111 Quick Revision.
Introduction to programming in java
CSC1401 Input and Output (and we’ll do a bit more on class creation)
Exercise Java programming
using System; namespace Demo01 { class Program
Compiling and Running a Java Program
Building Java Programs
Maha AlSaif Maryam AlQattan
CS305J Introduction to Computing
Computer Programming Methodology Input and While Loop
Introduction to Methods in java
Something about Java Introduction to Problem Solving and Programming 1.
Simple Java I/O part I Using scanner 8-Nov-18.
Computing Adjusted Quiz Total Score
March 29th Odds & Ends CS 239.
TO COMPLETE THE FOLLOWING:
Introduction to Classes and Methods
Java Language Basics.
AP Java Warm-up Boolean Array.
Java Lesson 36 Mr. Kalmes.
Recursive GCD Demo public class Euclid {
class PrintOnetoTen { public static void main(String args[]) {
Module 4 Loops and Repetition 4/7/2019 CSE 1321 Module 4.
Module 3 Selection Structures 4/5/2019 CSE 1321 Module 3.
Lecture 9 Using Objects.
Presentation transcript:

Picture It Very Basic Game Picture Pepper

Original Game import java.util.Scanner; public class Game { public static void main() { Scanner scan=new Scanner(System.in); String name= "Pepper"; // hard code name for example int place=1; //place is the position on the game board for(int turn=1; turn<=10; turn++) { int sum=diceThrow(name); // returns value of 2 dice place=movePlayer(name, place, sum); // returns new location of player } System.out.println("You are now on square "+place + " GAME IS DONE."); } public static int diceThrow(String name) { return 12; // your diceThrow code will send back something better and print it. } public static int movePlayer(String name, int startPt, int numberToMove) { System.out.println("You are now on square " + (( numberToMove + startPt ) %100)); return ( numberToMove + startPt ) %100; } }

Board Creation - new class Create a class that has a method to draw your game. It needs to know the location of your piece. import javalib.worldimages.*; import java.awt.Color; public class MyWorld { public static WorldImage drawGame (int loc) { WorldImage board = AImage.makeRectangle(500,500, Color.yellow, Mode.solid); return board; } }

See the Board - change game Add a canvas: import javalib.worldcanvas.*; import java.util.Scanner; public class Game { public static void main() { WorldCanvas gameSpace = new WorldCanvas(500,500,"game place") ; Scanner scan=new Scanner(System.in); String name= "Pepper"; // hard code name for example int place=1; //place is the position on the game board gameSpace.show(); gameSpace.drawImage(MyWorld.drawGame(place)); for(int turn=1; turn<=10; turn++) { int sum=diceThrow(name); // returns value of 2 dice place=movePlayer(name, place, sum); // returns new location of player } System.out.println("You are now on square "+place + " GAME IS DONE."); }

Start Board Drawing import javalib.worldimages.*; import java.awt.Color; public class MyWorld { public static WorldImage drawGame (int loc) { WorldImage board = AImage.makeRectangle(500,500, Color.yellow, Mode.solid); // Create a vertical and horizontal line WorldImage horizLine = AImage.makeLine(new Posn(0,0),new Posn(500,0)); WorldImage verticalLine = AImage.makeLine(new Posn(0,0),new Posn(0,500)); // See how it looks with the center of horizontal at 250,50 and vertical at 50/250 board = board.place(horizLine,250,50); board = board.place(verticalLine,50,250); return board; } }

Finish Board Picture Finish Board Picture import javalib.worldimages.*; import java.awt.Color; public class MyWorld { public static WorldImage drawGame (int loc) { WorldImage board = AImage.makeRectangle(500,500, Color.yellow, Mode.solid); WorldImage horizLine = AImage.makeLine(new Posn(0,0),new Posn(500,0)); WorldImage verticalLine = AImage.makeLine(new Posn(0,0),new Posn(0,500)); for (int count = 50; count <= 500; count= count + 50){ board = board.place(horizLine,250,count); board = board.place(verticalLine,count,250); } return board; } }

Add Player - Place anywhere import javalib.worldimages.*; import java.awt.Color; public class MyWorld { public static WorldImage drawGame (int loc) { WorldImage board = AImage.makeRectangle(500,500, Color.yellow, Mode.solid); WorldImage horizLine = AImage.makeLine(new Posn(0,0),new Posn(500,0)); WorldImage verticalLine = AImage.makeLine(new Posn(0,0),new Posn(0,500)); for (int count = 50; count <= 500; count= count + 50){ board = board.place(horizLine,250,count); board = board.place(verticalLine,count,250); } WorldImage hisPiece = AImage.makeCircle (10, Color.blue, Mode.solid); WorldImage game = board.place(hisPiece, loc, loc) ; return game; } }

Put Player on correct square import javalib.worldimages.*; import java.awt.Color; public class MyWorld { public static WorldImage drawGame (int loc) { WorldImage board = AImage.makeRectangle(500,500, Color.yellow, Mode.solid); WorldImage horizLine = AImage.makeLine(new Posn(0,0),new Posn(500,0)); WorldImage verticalLine = AImage.makeLine(new Posn(0,0),new Posn(0,500)); for (int count = 50; count <= 500; count= count + 50){ board = board.place(horizLine,250,count); board = board.place(verticalLine,count,250); } WorldImage hisPiece = AImage.makeCircle (10, Color.blue, Mode.solid); WorldImage game = board.place(hisPiece, (loc%10)*50-25, loc/10*50+25) ; return game; } }

Game redraw and slow down Add a canvas: import javalib.worldcanvas.*; import java.util.Scanner; public class Game { public static void main() { WorldCanvas gameSpace = new WorldCanvas(500,500,"game place") ; Scanner scan=new Scanner(System.in); String name= "Pepper"; // hard code name for example int place=1; //place is the position on the game board gameSpace.show(); gameSpace.drawImage(MyWorld.drawGame(place)); for(int turn=1; turn<=10; turn++) { int sum=diceThrow(name); // returns value of 2 dice place=movePlayer(name, place, sum); // returns new location of player gameSpace.drawImage(MyWorld.drawGame(place)); System.out.println( " Press Enter to continue."); scan.nextLine(); } System.out.println("You are now on square "+place + " GAME IS DONE."); }

Your Turn - Change your game Goal: Just want to move a piece around an empty board. Create a new class to draw called MyWorld import javalib.worldimages.*; import java.awt.Color; public class MyWorld { public static WorldImage drawGame (int loc) { WorldImage board = AImage.makeRectangle(500,500, Color.yellow, Mode.solid); return board; } }

Change your game to draw Use your homework code and fill in the red and blue parts, or use my code to start : import javalib.worldcanvas.*; import java.util.Scanner; public class Game { public static void main() { WorldCanvas gameSpace = new WorldCanvas(500,500,"game place") ; Scanner scan=new Scanner(System.in); String name= "Pepper"; // hard code name for example int place=1; //place is the position on the game board gameSpace.show(); gameSpace.drawImage(MyWorld.drawGame(place)); for(int turn=1; turn<=10; turn++) { int sum=diceThrow(name); // returns value of 2 dice place=movePlayer(name, place, sum); // returns new location of player gameSpace.drawImage(MyWorld.drawGame(place)); System.out.println( " Press Enter to continue."); scan.nextLine(); } System.out.println("You are now on square "+place + " GAME IS DONE."); } public static int diceThrow(String name) { return 12; // your diceThrow code will send back something better and print it. } public static int movePlayer(String name, int startPt, int numberToMove) { System.out.println("You are now on square " + (( numberToMove + startPt ) %100)); return ( numberToMove + startPt ) %100; } }

Code MyWorld drawGame method Add any shaped piece to your board - ex: WorldImage hisPiece = AImage.makeCircle (10, Color.blue, Mode.solid); Place the piece onto the board: WorldImage game = board.place(hisPiece, loc, loc) ; Return game instead now return game; Change the piece to move as you please by adjusting the piece's x and y

Summary Know how to use WorldCanvas object Separated some drawing knowledge out of your game class ◦ Called a method in another class (like tester) Used scanner to slow down the game so you can see what happens Visualized how you can translate a location to a spot on a game board