Sei sulla pagina 1di 15

Two dimensional arrays

Imagine the game tic-tac-toe or noughts and crosses

first previous next

last

Two dimensional arrays

Imagine the game tic-tac-toe or noughts and crosses

O X

first previous next

last

Two dimensional arrays

Imagine the game tic-tac-toe or noughts and crosses

O X

first previous next

last

Two dimensional arrays

Imagine the game tic-tac-toe or noughts and crosses

O X O

first previous next

last

Two dimensional arrays

Imagine the game tic-tac-toe or noughts and crosses

O X O X

first previous next

last

Two dimensional arrays

Imagine the game tic-tac-toe or noughts and crosses

O X O X

first previous next

last

Two dimensional arrays

Imagine the game tic-tac-toe or noughts and crosses

O X O X

X X O

first previous next

last

Two dimensional arrays

Imagine the game tic-tac-toe or noughts and crosses

O X O
X wins!

X X X O

first previous next

last

Two dimensional arrays


How could we represent the board in a C++ program? Given what we know so far about arrays O
top row 0 middle row 0 bottom row 0 1 2
first previous next

X X X X O O

char toprow[3]; char middlerow[3]; char bottomrow[3];

last

Two dimensional arrays


But it would be much better if we could indicate that all 9 boxes are part of the same entity each of the three rows is exactly the same: an array of 3 chars
row1 0 row2 0 row3 0 1 2
first previous next last 10

Two dimensional arrays


arrays are used to group items of the same type could we use an array to group together the rows? YES
row 0 row 1 row 2 0 1 2 0 board 1 2 0 1 2

first previous next

last 11

Two dimensional arrays


so now board[0] will describe the entire first row board[1] will describe the entire second row board[3] will describe the entire third row
0 board 1 2

board[0] contains an array of char board[1] contains an array of char board[2] contains an array of char

first previous next

last 12

Declaring 2D arrays

an array of arrays like the board is called a twodimensional (or 2D) array we can declare such a structure in C++
const int MAXROW = 3 const int MAXCOL = 3 char board[MAXROW][MAXCOL]

0 board 1 2 0 1 2

and assign values into any of the locations by specifying 0 the row and column
board[2][1] = X;

board

1 2 0 X 1 2
last 13

first previous next

Declaring 2D arrays

an array of arrays like the board is called a twodimensional (or 2D) array we can declare such a structure in C++
const int MAXROW = 3 const int MAXCOL = 3 char board[MAXROW][MAXCOL]

0 board 1 2 0 1 2

and assign values into any of the locations by specifying 0 the row and column
board[2][1] = X; board[1][1] = O;

board

1 2 0

O X 1 2
last 14

first previous next

Declaring 2D arrays

an array of arrays like the board is called a twodimensional (or 2D) array we can declare such a structure in C++
const int MAXROW = 3 const int MAXCOL = 3 char board[MAXROW][MAXCOL]

0 board 1 2 0 1 2

and assign values into any of the locations by specifying X 0 the row and column
board[2][1] = X; board[1][1] = O; board[0][2] = X;

board

1 2 0

O X 1 2
last 15

first previous next

Processing 2D arrays

what if we want to display the state of the board during the course of the game?

output the values in each column of row 0 output the values in each column of row 1 output the values in each column of row2

we use nested for loops


for (int row = 0; row < MAXROW; row++) for (int column = 0; column < MAXCOL; column++) cout << [ << row << ][ << column << ] << board[row][column] << endl;

first previous next

last 16

Processing 2D arrays

so for the 3 moves we saw earlier we would see


[0][0] [0][1] [0][2] X [1][0] [1][1] O [1][2] [2][0] [2][1] X [2][2]

first previous next

last 17

2D output

but we really want it to look like the board


for arrays of more than 1 dimension we have to specify the number of elements in the other dimensions

void display_board(char any_board[][MAXCOL], int rowsize, int colsize) { for (int row = 0; row < rowsize; row++) { cout << row; for (int col = 0; col < colsize; col++) cout << "\t" << any_board[row][col]; cout << endl; } for (int col = 0; col < colsize; col++) cout << "\t" << col; cout << endl; }
first previous next last 18

2D output

sample output 0 1 2 X X 0 O O 1

X 2

first previous next

last 19

tic-tac-toe

high level algorithm

int main() { // declare variables set first player initialise the board (provide board and dimensions) display the board (provide board and dimensions) do { get player row and column until an empty position is chosen update the board display the board swap player } while the game is not over }
first previous next last 20

10

tic-tac-toe
int main() { char board[MAXROW][MAXCOL]; int row, column; char player; player = X; initialise_board(board, MAXROW, MAXCOL); display_board(board, MAXROW, MAXCOL); do { do { // get row and column } while (board[row][column] != ); board[row][column] = player; display_board(board, MAXROW, MAXCOL); if (player == X) player = O; else player = X; } while (!game_over(board, MAXROW, MAXCOL)); } first previous next last 21

tic-tac-toe
void initialise_board(char any_board[][MAXCOL], int rowsize, int colsize) { for (int row = 0; row < rowsize; row++) for (int col = 0; col < colsize; col++) any_board[row][col] = ' '; }

first previous next

last 22

11

tic-tac-toe

how to determine if the game has finished?


any row contains all X any row contains all O any column contains all X any column contains all O all positions are full (there are no empty squares) we will ignore diagonals for now

have a go at writing a function

bool game_over(char any_board[][MAXCOL], int rowsize, int colsize);

full tic-tac-toe on the web site


first previous next last 23

More on strings

what if we wanted to store the names of students in the class?


const int SIZE = 30; char char char .. .. char name1[SIZE]; name2[SIZE]; name3[SIZE]; name155[SIZE];

so why not group the names together into an array?


first previous next last 24

12

More on strings

so lets have an array of strings ie an array, where each element is an array of char ie a 2D array of char
const int STRINGSIZE = 10; const int NUMNAMES = 5; char names[NUMNAMES][STRINGSIZE];
first previous next last 25

More on strings

so we have
0 1 2 3 4 0 1 2 3 4 5 6 7 8 9
last 26

first previous next

13

More on strings

so then

const int STRINGSIZE = 10; const int NUMNAMES = 5; char names[NUMNAMES][STRINGSIZE]; for (int i = 0; i < NUMNAMES; i++) { cout << Enter name : ; cin.getline(names[i], STRINGSIZE); }

Enter Enter Enter Enter Enter

name name name name name

: : : : :

S Jones Jo Blow Bill Roy Ken Smith Cathy Ng

first previous next

last 27

More on strings

so then
0 1 2 3 4 S J B K C 0 o i e a 1 J l n t 2 o B l h 3 n l e s \0 ? ? ? ? ? o w \0 R o i

y \0 t

S m y 4 5

h \0 ? 9
last 28

N g \0 6 7 8

first previous next

14

More on strings

so then
for (int i = 0; i < NUMNAMES; i++) cout << names[i] << endl;

first previous next

last 29

More on strings

so then
for (int i = 0; i < NUMNAMES; i++) cout << names[i] << endl;

first previous next

last 30

15

Potrebbero piacerti anche