Escolar Documentos
Profissional Documentos
Cultura Documentos
last
O X
last
O X
last
O X O
last
O X O X
last
O X O X
last
O X O X
X X O
last
O X O
X wins!
X X X O
last
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
last
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
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
last 11
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
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
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
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
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
last 16
Processing 2D arrays
last 17
2D output
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
last 19
tic-tac-toe
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] = ' '; }
last 22
11
tic-tac-toe
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
More on strings
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
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); }
: : : : :
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
14
More on strings
so then
for (int i = 0; i < NUMNAMES; i++) cout << names[i] << endl;
last 29
More on strings
so then
for (int i = 0; i < NUMNAMES; i++) cout << names[i] << endl;
last 30
15