Programming for Art: Arrays – 2D ART 315 Dr. J. R. Parker Art/Digital Media Lab Lec 16 Fall 2010.

Slides:



Advertisements
Similar presentations
Variables Conditionals Boolean Expressions Conditional Statements How a program produces different results based on varying circumstances if, else if,
Advertisements

Introduction to Programming
Game with US Beginner Tutorial. Welcome!! Who I am What is Processing? Basic Coding Input Methods Images Classes Arrays.
RAPTOR Syntax and Semantics By Lt Col Schorsch
Variables and Operators
 Variables  What are they?  Declaring and initializing variables  Common uses for variables  Variables you get “for free” in Processing ▪ Aka: Built-in.
Lesson Four: More of the Same
Representing a Game Board In a game, we represent the action taking place using an array – In a very simple game, we use individual variables to represent.
IAT 800 Lab 1: Loops, Animation, and Simple User Interaction.
ICM Week 2. Structure - statements and blocks of code Any single statement ends with semicolon ; When we want to bunch a few statements together we use.
R-1 University of Washington Computer Programming I Lecture 17: Multidimensional Arrays © 2000 UW CSE.
Lecture 3 IAT 800. Sept 15, Fall 2006IAT 8002 Suggestions on learning to program  Spend a lot of time fiddling around with code –Programming is something.
© The McGraw-Hill Companies, 2006 Chapter 16 Two-dimensional arrays.
PROCESSING Animation. Objectives Be able to create Processing animations Be able to create interactive Processing programs.
Review Blocks of code {.. A bunch of ‘statements’; } Structured programming Learning Processing: Slides by Don Smith 1.
Programming for Artists ART 315 Dr. J. R. Parker Art/Digital Media Lab Lec 10 Fall 2010.
Lawrence Snyder University of Washington, Seattle © Lawrence Snyder 2004.
Introducing Arrays We will often need to store collections of information –a list of names to sort –a list of values to compute averages, standard deviation,
NestedLoops-part11 Nested Loops – part 1 Barb Ericson Georgia Institute of Technology Nov 2009.
Field Trip #26 Create a Find a Word Puzzle in Java By Keith Lynn.
CSC1401 Viewing a picture as a 2D image - 1. Review from the last week We used a getPixels() to get all of the pixels of a picture But this has been somewhat.
Computer Programming for Engineers. Outline Tic-Tac-Toe (O-X Game) Drawing 3x3 grid Receiving the inputs Checking for a winner Taking turns between.
How to Create a Videogame By: Connor McCann. Java Java is one of many programming languages Java is used to run web browsers and most PC video games I.
When constructing a two-dimensional array, specify how many rows and columns are needed: final int ROWS = 3; final int COLUMNS = 3; String[][] board =
Review of ArT3 Programming Course David Meredith Aalborg University
Programming for Artists ART 315 Dr. J. R. Parker Art/Digital Media Lab Lec 11 Fall 2010.
VB Games: Preparing for Memory Brainstorm controls & events Parallel structures (again), Visibility, LoadPicture, User-defined procedures, Do While/Loop,busy.
Two Dimensional Arrays
______________________________________________________________________________________ SCHOOL OF INTERACTIVE ARTS + TECHNOLOGY [SIAT] |
Review Recursion Call Stack. Two-dimensional Arrays Visualized as a grid int[][] grays = {{0, 20, 40}, {60, 80, 100}, {120, 140, 160}, {180, 200, 220}};
By Melissa Dalis Professor Susan Rodger Duke University June 2011 Multiplication Table.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington 2D arrays COMP 102 # T1.
Programming for Artists ART 315 Dr. J. R. Parker Art/Digital Media Lab Lec 10 Fall 2010.
Two Dimensional Arrays. Two-dimensional Arrays Declaration: int matrix[4][11]; 4 x 11 rows columns
Introduction to Image processing using processing.
1 Georgia Tech, IIC, GVU, 2006 MAGIC Lab Rossignac Lecture 02b: Tutorial for Programming in Processing Jarek Rossignac.
Two-Dimensional Arrays That’s 2-D Arrays Girls & Boys! One-Dimensional Arrays on Steroids!
CIS 3.5 Lecture 2.2 More programming with "Processing"
Lesson Two: Everything You Need to Know
Variables Art &Technology, 3rd Semester Aalborg University Programming David Meredith
Mouse Inputs in Processing. Interacting with the Mouse mouseX and mouseY: pg mouseXmouseY –The position of the mouse in the canvas pmouseX and.
Lesson Two: Everything You Need to Know
Lesson 3: Arrays and Loops. Arrays Arrays are like collections of variables Picture mailboxes all lined up in a row, or storage holes in a shelf – You.
______________________________________________________________________________________ SCHOOL OF INTERACTIVE ARTS + TECHNOLOGY [SIAT] |
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington 2D arrays COMP 102 # T1.
Introducing Arrays We will often need to store collections of information –a list of names to sort –a list of values to compute averages, standard deviation,
G RAPHICS & I NTERACTIVE P ROGRAMMING Lecture 2 More Programming with Processing.
1 Arrays of Arrays An array can represent a collection of any type of object - including other arrays! The world is filled with examples Monthly magazine:
CS Class 15 Today  More practice with arrays  Introduction to Multi-dimensional arrays Announcements  Programming project #4 due 10/23 by midnight.
Test Review. General Info. All tests will be comprehensive. You will be tested more on your understanding of code as opposed to your ability to write.
1 SIC / CoC / Georgia Tech MAGIC Lab Rossignac Processing  Install Processing  Learn how to edit, run, save, export,
Battleships. You need to know How to draw two 11 x 11 grids How to read off names of squares.
When constructing a two-dimensional array, specify how many rows and columns are needed: final int ROWS = 3; final int COLUMNS = 3; String[][] board =
Arrays. 2 Why do we care (about arrays)? What if you have a whole bunch of cars (or aliens or balls or ???) bouncing around the screen? How do we keep.
Variables. Something to mention… void setup(){ size(200, 200); background(255); smooth(); } void draw() { stroke(0); strokeWeight(abs(mouseX-pmouseX));
A 2-D Array is a structure that storage space both vertically and horizontally. Thus, the array has both rows and columns. 2-D Arrays are used to create.
Task 1 and Task 2. Variables in Java Programs Variables start with lower case letter Variables are descriptive of what they store Variables are one word.
BATTLESHIP! BATTLESHIP!
Computation as an Expressive Medium
Programming for Art: Algorithms
Two-Dimensional Arrays and Nested Loops – part 1
Lesson Plan Title: Sea Battle (Paper-pencil Game) Level: Middle
Exam1 Review CSE113 B.Ramamurthy 11/29/2018 B.Ramamurthy.
Two-Dimensional Arrays and Nested Loops – part 1
Programming for Artists
Battleship
More programming with "Processing"
Programming for Art: Images
LCC 6310 Computation as an Expressive Medium
Chapter 4, Variables Brief Notes
Presentation transcript:

Programming for Art: Arrays – 2D ART 315 Dr. J. R. Parker Art/Digital Media Lab Lec 16 Fall 2010

Arrays We’ve looked at arrays, but all so far have contained integers. Of course, and array can hold any type that can be declared. Can have arrays of float, short, byte, boolean, etc etc. What about char? Sure, that too.

Arrays of arrays An interesting idea is ‘what about an array of arrays? It could look like this: Each element of The array is – an array! A

Arrays of arrays Each element of A is itself an array. I.E. A[i] is an array A

Arrays of arrays For integers: int [] []A = new int[8][3]; A

Arrays of arrays We can think of this as 8 columns of 3 rows each. A Row 0 Row 1 Row 2 Column

Arrays of arrays Can access elements as A[i][j] for i=0..7 and j = A Row 0 Row 1 Row 2 Column

Arrays of arrays Here’s a simpler way to see it. A Row 0 Row 1 Row 2 Column

Arrays of arrays A[1][2] = 12 In math this could be called a matrix 12 A Row 0 Row 1 Row 2 Column

Arrays of arrays This is a 2D array. How is it done? Memory, after all, is one dimensional. We do it by mapping 2D indices onto 1D ones. 12 A Row 0 Row 1 Row 2 Column

Arrays of arrays Advanced material: Array element A[i][j] is accessed as A + (i*Nrows)+j 12 A Row 0 Row 1 Row 2 Column

Advanced Material A + (i*Nrows)+j 12 A Row 0 Row 1 Row Col 0 Col 1 Col 2 A[1][2] = A+(1*Nrows)+2 = A+3+2 = A+5 A

What Use are 2D Arrays? An obvious item is that they can represent the screen. Each element could be a pixel

Battleship (Game) aircraft carrieraircraft carrier 5 battleshipbattleship 4 cruisercruiser 3 submarinesubmarine 3 destroyerdestroyer 2

15 Battleship Guess a square – B2 - if a ship is there, it is called a ‘hit’ and the space is marked.. Otherwise it is a ‘miss’. Normally there would be two players, but this is just an example…

16 First stage:draw the board; clicks check the square // Battleships // J Parker 2010 int row=0, col=0; int k=0; int [][]board = new int[10][10]; void setup () { size (300, 300, P2D); background(90,90,0); rectMode (CENTER); } void draw () { int i,j; // Draw the Board for (i=0; i<width; i+=30) line (i, 0, i, width); for (j=0; j<height; j+=30) line (0, j, height, j); } void mousePressed() { row = mouseY/30; col = mouseX/30; rect(col*30+15, row*30+15, 30, 30); }

17 Convert from screen coordinates to board indices: The board is 10x10, the screen is 300x300 Each squares 30x30 The first square is 0-29 in x and 0-9 in y The second square in any row is from 30-59: screen x/30 =1 Third square is from 60-89, or x/30 = 2 … and so on.

18 What Use are 2D Arrays? We need to set-up the board and record the moves. When a square is clicked on, check it. Is there a ship there? Connect the mouse clicks to a 2D array that stores the ships.

19 Random setup is difficult We need to: 1.Select a row and column coordinate to start. 2. Select a direction (horizontal or vertical) 3. Check the array elements that would be occupied by the ship (N elements, x or y) to ensure they are empty. If not, goto 1 4. Place the number of the ship in each occupied array location. Empty locations are 0, ship #1 is carrier, etc.

20 Random Setup

21 Random Setup void setup () { size (300, 300, P2D); background(90,90,0); rectMode (CENTER); for (int i=0; i<10; i++) for (int j=0; j<10; j++) board[i][j] = 0; setupN (5, 5); setupN (4, 4); setupN (3, 3); setupN (3, 2); setupN (2, 1); } The nested FOR loops set all elements Of the array that represents the board to 0. setupN (a, b) finds a place for a ship on the board; the ship has a elements and is numbered b. So, setupN(5,5) sets up the carrier. We only do this at the beginning of the Game, so it is in the initialization Routine setup().

22 setupN void setupN (int N, int label) { int is, js, f=0; boolean again=true; while(again) { if (random(100) < 60) { is = (int)random(10); js = (int)random(10-N); f = 1; for (int i=0; i<N; i++) if(board[is][js+i] != 0) { f = 0; break; } if (f==0) continue; again = false; for (int i=0; i<N; i++) board[is][js+i] = label; } else { is = (int)random(10-N); js = (int)random(10); f = 1; for (int i=0; i<N; i++) if(board[is+i][js] != 0) { f = 0; break; } if (f==0) continue; again=false; for (int i=0; i<N; i++) board[is+i][js] = label; }

Game Play The player clicks on a square and it acts like a button. If there is a ship there, it turns red If no ship, it turns white. When all squares of a ship are hit, ship sinks When all ships are sunk, game over.

Game Play // 17 hits in the entire game int k=0, total=17; // The 10x10 board int [][]board = new int[10][10]; // Each ship, # possible hits int []ships = {0, 2, 3, 3, 4, 5}; We will subtract 1 from the possible hits when a ship it hit, When it reaches 0, ship is sunk. We also subtract 1 from total. For a hit. When total=0, game is over.

Game Play void mousePressed() { int k = 0; fill (90,90,0); // Background color text ("Sunk", 100, 10); // Erase row = mouseY/30; col = mouseX/30; // Selected cell fill (255,255,255); // Set to white if (board[row][col] == 0) // … if empty rect(col*30+15, row*30+15, 30, 30); else // A ship is here! { fill (255, 0, 0); // Set to red rect(col*30+15, row*30+15, 30, 30);

Game Play k = board[row][col]; // Which ship #?? if (k < 0) return; // Empty ships[k] -= 1; // A hit takes away 1 total -= 1; // From the total too board[row][col] = -1; // -1 is not a legal color, so will stay. // No more hits left = sunk if (ships[k] == 0) text ("Sunk!", 100, 10); // All ships sunk = game over if (total <= 0) println ("Game over."); }

Here’s a trial

More Use of 2D Arrays? X tic-tac-toe...

What Use are 2D Arrays? O X tic-tac-toe

Minesweeper