Week 9 - Monday CS 121.

Slides:



Advertisements
Similar presentations
CHESS FOR KIDS Lesson 1.
Advertisements

L.O. Today you will learn how to play chess. How to Play Chess.
Chapter 7 Multidimensional Arrays. Defining a two dimensional array elementType[][] arrayName; // Java pro elementType arrayName[][]; // C++ alternate.
Week 9: Methods 1.  We have written lots of code so far  It has all been inside of the main() method  What about a big program?  The main() method.
Week 6: Arrays 1.  Loops are great  But, without a way to talk about a group of values, we can’t get the full potential out of a loop  Enter: the array.
CS1061 C Programmuing Lecture 12 Arrays A. O’Riordan, 2004.
How to play Chess.
Panther Chess Club Sponsors: –Mr. Sergio Treviño (English, Room 408) –Mr. Felix Esquivel (Criminal Justice, Room 50) Meetings every Monday and Wednesday.
Apostles Chess Club Session Three. Chess Piece Symbols The symbols shown above are the ones most used when showing chess pieces in print or on the internet.
HISTORY The problem was originally proposed in 1848 by the chess player Max Bezzel, and over the years, many mathematicians, including Gauss have worked.
How to Play Chess By: John. Dedication I dedicate this project to my family because we all love chess.
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}};
M180: Data Structures & Algorithms in Java Arrays in Java Arab Open University 1.
Chess By Kezia Farley.
Lesson 1 History of Chess Why We Teach Chess Goal of Chess.
A game based off of the esteemed classic By: Tadziu Kosiara.
ENEE150 – 0102 ANDREW GOFFIN Functional Decomposition.
Every chess master was once a beginner. Irving Chernev
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:
CHESS Basics for Beginners. BOARD SET-UP The letters go across the board in front of you. “White on right!” Each player has a white square in their right.
Week 9 - Monday.  What did we talk about last time?  Method practice  Lab 8.
A Levoy PowerPoint Presentation
Week 9 - Wednesday.  What did we talk about last time?  2D arrays  Queen attacking pawn example  Started Game of Life.
Arrays.
Week 10 - Wednesday.  What did we talk about last time?  Method example  Roulette simulation  Types in Java.
Two Dimensional Arrays. Students will be able to: code algorithms to solve two- dimensional array problems. use 2-D arrays in programs. pass two-use 2-D.
Week 6 - Friday.  What did we talk about last time?  Loop examples.
Chapter 9 Introduction to Arrays Fundamentals of Java.
CS 115 Lecture 17 2-D Lists Taken from notes by Dr. Neil Moore.
LESSON 8: INTRODUCTION TO ARRAYS. Lesson 8: Introduction To Arrays Objectives: Write programs that handle collections of similar items. Declare array.
Steps and Vocabulary Martha Rice. You can use the same problem solving methods to solve just about any problem, from word problems to logic problems to.
COP 3275 – Finishing Loops and Beginning Arrays Instructor: Diego Rivera-Gutierrez.
Comp1004: Loops and Arrays I Whiles, For and Arrays[]
Some counting questions with the Rules of Chess. Chessboard and Chess pieces The game of chess is played on an 8-by-8 grid of alternately colored squares.
Week 2 - Wednesday CS 121.
EGR 2261 Unit 10 Two-dimensional Arrays
Two-Dimensional Arrays
Arrays in 60 seconds err.. minutes
A basic tutorial for fundamental concepts
Topic Dimensional Arrays
Week 8 - Friday CS 121.
How To Play Chess (And other Information)
CHESS.
2-D Lists Taken from notes by Dr. Neil Moore
Two-Dimensional Arrays
עקרנות תכנות מונחה עצמים
Introduction to Programming
Lesson 2: Building Blocks of Programming
Topic 26 Two Dimensional Arrays
Two-Dimensional Arrays
Multidimensional Arrays
Two-Dimensional Arrays
Multidimensional array
Computer Architecture and Assembly Language
Presented By: David Miller
Loops and Arrays in JavaScript
February , 2009 CSE 113 B.
Suggested self-checks: Section 7.11 #1-11
Continue with Life, Magic and Catch -up
Chapter 26 Comparing Counts.
Rules to play chess. Chess is a played with 16 pieces: 8 pawns, 2 towers, 2 knights, 2 bishops, 1 queen and 1 king. Movements: Pawns: They only can move.
Computer Architecture and Assembly Language
AP Java Learning Objectives
2-D Lists Taken from notes by Dr. Neil Moore
Week 6 - Monday CS221.
Week 7 - Friday CS222.
Week 7 - Monday CS 121.
Week 7 - Friday CS 113.
Presentation transcript:

Week 9 - Monday CS 121

Last time What did we talk about last time? Method practice Lab 8

Questions?

Project 3

2D Arrays

2D arrays Just as it is possible to make a one dimensional list out of a single data type, it is also possible to make a table out of one data type We can extend the arrays you know to have two dimensions with very similar syntax

Declaration To declare a two dimensional array, we just use two sets of square brackets ([][]): Doing so creates a variable that can hold a 2D array of ints As before, we still need to instantiate the array to have a specific size: int [][] table; table = new int[5][10];

Visualization of 2D arrays Like matrices, we usually visualize the first dimension as the rows and the second dimension as the columns Second Dimension 0 1 2 3 4 5 6 7 8 9 1 2 3 4 First Dimension

Visualization of 2D arrays Let’s write a little code to put data into the table int [][] table = new int[5][10]; int label = 1; for( int i = 0; i < 5; i++ ) for( int j = 0; j < 10; j++ ) { table[i][j] = label; label++; }

Visualization of 2D arrays The result of that code is: Second Dimension 0 1 2 3 4 5 6 7 8 9 1 2 3 4 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 First Dimension

Chessboard We could represent a chessboard as an 8 x 8 array of chars Use the following encoding: 'P' = pawn 'N' = knight 'B' = bishop 'R' = rook 'Q' = queen 'K' = king Use upper case characters for black pieces and lower case characters for white ones

Checking a pawn for danger Imagine there is a pawn randomly set on the board and a queen of the opposite color on the board Write a program to see if the queen can capture the pawn in the next move Q p

Algorithm for chess problem Find the row and column location of both the queen and the pawn The pawn is in danger if: The queen and the pawn have the same row The queen and the pawn have the same column If the absolute value of the differences between their rows and the absolute value of the differences between their columns are the same

Conway’s Game of Life A cell is represented by a block in a grid Each cell has 8 neighbors Simple rules for a cell “coming to life” or “dying”: A live cell with fewer than 2 live neighbors dies from loneliness A live cell with more than 3 live neighbors dies from overcrowding A live cell with exactly 2 or 3 neighbors keeps living A dead cell with exactly 3 neighbors comes to life

Implementing Conway's Game of Life We can represent the grid of cells with a 2D array of boolean values true means alive false means dead Each iteration, we draw the grid onto the screen with StdDraw Black means alive White means dead Then, we update the grid to contain the new values The grid stores the state of the game We still have to use StdDraw to draw that state

3D and Higher Dimension Arrays

4th dimensional rocketships going up It doesn’t have to stop at 2 dimensions! You can have 3 or more Here’s an example with 3 dimensions: int[][][] rubiksCube = new int[3][3][3]; int count = 1; for( int i = 0; i < 3; i++ ) for( int j = 0; j < 3; j++ ) for( int k = 0; k < 3; k++ ) { rubiksCube[i][j][k] = count; count++; }

What does a 3D array look like? It looks like whatever you want it to You can visualize it in 3D if you want There are other techniques It’s just a way to store data It doesn’t actually look like anything inside the computer

Why use a high dimensional array? Sometimes you have data categorized in several different ways For example, E-town might keep some statistics according to Year, Gender, and Race 0 – Freshman 1 – Sophomore 2 – Junior 3 – Senior Perfect candidate for a 3D array 0 – Male 1 – Female 0 – African American 1 – Asian 2 – Caucasian 3 – Other

Why not to use a high dimensional array Too many brackets Too much stuff Total size used is the product of the length of all the dimensions 100 x 100 x 100 = 1,000,000 Hard to visualize, hard to imagine Up as high as 4 is sometimes useful Don’t go beyond 2 on a regular basis

Upcoming

Next time… Finish Game of Life Overloading methods More method practice

Reminders Keep reading Chapter 8 of the textbook Keep working on Project 3 Due this Friday