[go: up one dir, main page]

0% found this document useful (0 votes)
14 views26 pages

Lesson 2 Two Dimensional Arrays

The document provides an overview of two-dimensional arrays, including their structure, creation in Python, and applications in games. It details how to create and manipulate these arrays, as well as techniques for printing them in a grid format. Additionally, it outlines the requirements for creating a Tic-Tac-Toe game using arrays and includes various tasks and exam questions related to arrays.

Uploaded by

20jurijczukd
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views26 pages

Lesson 2 Two Dimensional Arrays

The document provides an overview of two-dimensional arrays, including their structure, creation in Python, and applications in games. It details how to create and manipulate these arrays, as well as techniques for printing them in a grid format. Additionally, it outlines the requirements for creating a Tic-Tac-Toe game using arrays and includes various tasks and exam questions related to arrays.

Uploaded by

20jurijczukd
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 26

Part 2:

Two-dimensional arrays
Two dimensional arrays
• A one-dimensional array can be seen as data elements
organised in a row. A two-dimensional array can be
visualised as a grid (or table) with rows and columns.
• For example, a nine-by-nine grid could be referenced with
numbers for each row and letters for each column. A
nine-by-nine, two-dimensional array could be declared
with a statement such as:
game [9][9]
• Many games use two dimensional arrays to plot the visual
environment of a game. Positions in a two dimensional
array are referenced like a map using horizontal and
vertical reference numbers (known as matrices).
• We can also make three-dimensional arrays as well. Many
games (such as Minecraft) use three-dimensional arrays
to model an environment.
Creating a two-dimensional array
• To create a two-dimensional nine-by-nine array in Python, we would use code such
as:
game= [[0 for x in range (9)] for y in range(9)]
• This statement contains two FOR loops and each element is initialised in turn.
• We can now use statements like the following to add data to positions in the array:
game[0][0] = '3'
game[0][1] = '3'
game[0][2] = '3'
game[1][0] = '2'
game[1][1] = '2'
Our array would now look like this:
Task 1 – Complete the array
The following array is created using python;

Complete the table below based on the following input;


Index [0] [1] [2] [3]
This is how the code
[0] will be output in
[1] python;
[2]

[3]
Writing 2D Arrays/Lists in Python
• To begin, a list is declared in the same way as a normal list. Next, you need to create another
array. This is done by adding a comer and creating another list of data. Note how both levels of
the array are wrapped in additional square brackets;
my_list = [[1, 3, 5, 7, 9, 11, 13, 15],[2, 4, 6, 8, 10, 12, 14,
16]]
• This array essentially can be considered to look like this;
index 0 1 2 3 4 5 6 7

0 1 3 5 7 9 11 13 15

1 2 4 6 8 10 12 14 16

To locate a value, first identify the row index followed by the column index, e.g.
[Row index][Column index]
The value of [0][0] is 1
The value of [0][5] is 11
The value of [1][4] is 10
Task 2: Creating 2 dimensional arrays in python
1. Write out the following code. 2. In the table below explain what
the following commands did:
Command Explanation

print (a)

print (a[0])

print (a[1][1])

names =
[[names1],names2]]

print (names [1])

print (names [1][2])


So far, all the grids have printed information on the same
Array programming techniques: line. Python does have a function which allows you to
Print out a 2D list to look like a grid print string on a new line ‘\n’ This can be combined
with the .join() command which concatenates (joins)
1. Printing out a hand written list: a sequence of string, sequentially.

Note how you can


amend different
index within the
2. Printing out a list the fast way! list.

Adjust these numbers to


10,10 to create this grid!
Array programming techniques:
Print out a 2D list to look like a grid
3. Using .append to create a grid

Adjust these
numbers to
change the size of
your grid.
Task 3: Array programming techniques - Create grids
Using any of the three techniques shown to create a grid, create the following;
• 6 x 6 grid
• 10 x 10 grid
Evidence your programming and output grids below;

Coding evidence: Output evidence:


6 x 6 grid 6 x 6 grid

10 x 10 grid 10 x 10 grid
Task 4: Noughts and Crosses (Tic-Tac-Toe)

Here you will create a 2 player game of noughts and crosses on


python.
To begin, we need to consider the main requirements;
• The game is played on a 3 x 3 grid
• One player is designated ‘X’s, the other ‘O’s.
• Each player takes it in turn to place an X or O on the grid
• Once a place on the grid has been marked with an X or O it
cannot be used again
• The first player to get 3 symbols in a row (horizontally,
vertically or diagonally) wins
• If all grid places are filled and there is no winner, the game is
declared a draw.
Algorithmic design
Using a flowchart or pseudocode, write an algorithm which considers all the requirements identified
on the previous slide. If you are creating a flowchart, use www.draw.io, if you are writing
pseudocode, use notepad/++. Print screen your completed algorithm below.
Part 1 – Create the board and global
variables
The board can be created using a standard array which includes 9 placements to create a
3 x 3 grid. Variables are required to check for player turn and the current state of the
game;
Part 2 – Create a function to output
the board
This function draws the board on the screen using the board list. It also uses %c placeholders as we
used previously in the dice task. Within this programme they represent the list locations.
Part 3 – Create a check function
This function checks if a list position is empty or not. The Boolean result
will be used later when running the game.
Part 4 – Check if a player has won, drawn or should carry on

This function checks every possibility of winning through Boolean checks. If the win checks return
False, the function checks for a draw. If the draw check returns False, the game carries on running.
Part 5 – Run the game
This code runs the game which requires the output produced from the functions we
made earlier. There is one piece of code in particular which is vital to the success of the
game;
if(player % 2 != 0):
This code uses the % symbol. In python this symbol is used to divide two integers to
check for a remainder number.
Remember: integers cannot be converted to floats, e.g. 3 /1 cannot become 1.5, the
returned value would be remainder 1
The purpose of this code is to check whose turn it is. It does this by checking if by
dividing the value of player by 2, will there be a remainder. If the remainder is not equal
to 0 (i.e. there is a remainder), the number must be odd which therefore means its
player 1’s turn, else it must be an even number which means it is player 2’s turn.
It is also used later to check who has won.
The other important code is allowing the user to input their go. Note their input has
been converted to an integer.
Part 5 – Run the game
Part 6 – Check who’s won!
The last section of code only runs if a draw or win has been returned True
from the CheckWin function. Again the % character is used to check for an
odd or even number, representing the winning player.
Evidence – Your game code below
Evidence – Print screen your game running below
Array Exam Questions
Array Exam Questions
1. What is a data structure? 2. What is an array?

a) A list of variables
a) A list of variables
b) A list of strings
b) A list of variables and strings
c) A series of memory locations, each with
c) A facility that holds more than one value
the same name, that hold related data

3. Why are arrays used? 4. In an array, what is an element?

a) Arrays store multiple data values under one a) The smallest number an array can hold
name, reducing the complexity of our program
b) The biggest number an array can hold
b) Arrays store more data than if we were using
c) An element is one value in an array
variables
c) Arrays store more data than if we were using
strings
Array Exam Questions
5. How many elements would the array 6. In the array 'numbers(6, 10, 7, 12, 9, 2)', what is the
'student_marks(10)' hold? value of element numbers(4)?

a) Nine elements a) Nine


b) Ten elements b) Twelve
c) Eleven elements c) Two

7. When creating the array 'scores(9)', what does the 8. What is the difference between an array and a list?
'9' in brackets represent?
a) An array holds numbers, whereas a list holds
a) The number of elements the array can hold strings
b) An array can only hold data of the same
b) The largest number of digits an element can
data type, whereas lists can hold values of
hold
different data types
c) The largest number of characters an element c) An array holds both numbers and strings,
can hold whereas a list only holds numbers
Answers
Answers
1. What is a data structure? 2. What is an array?

a) A list of variables
a) A list of variables
b) A list of strings
b) A list of variables and strings
c) A series of memory locations, each with
c) A facility that holds more than one value
the same name, that hold related data

3. Why are arrays used? 4. In an array, what is an element?

a) Arrays store multiple data values under one a) The smallest number an array can hold
name, reducing the complexity of our program
b) The biggest number an array can hold
b) Arrays store more data than if we were using
c) An element is one value in an array
variables
c) Arrays store more data than if we were using
strings
Remember: Computers start
Answers counting from 0!

5. How many elements would the array 'scores(9)' 6. In the array 'numbers(6, 10, 7, 12, 9, 2)', what is the
hold? value of element numbers(4)?

a) Nine elements a) Nine


b) Ten elements b) Twelve
c) Eleven elements c) Two

7. When creating the array 'scores(9)', what does the 8. What is the difference between an array and a list?
'9' in brackets represent?
a) An array holds numbers, whereas a list holds
a) The number of elements the array can hold strings
b) An array can only hold data of the same
b) The largest number of digits an element can
data type, whereas lists can hold values of
hold
different data types
c) The largest number of characters an element c) An array holds both numbers and strings,
can hold whereas a list only holds numbers

You might also like