-
Notifications
You must be signed in to change notification settings - Fork 1
Arrays
$TestArray[m,n] (Multidimensional) $TestArray[m_n] (Multidimensional)
$userNames[0] = "Heather"; $userNames[1] = "Nikki"; $userNames[2] = "Mich"; First, we need to create a new script:
exec("./arrays.cs");
// Set up all of the arrays // with default values function initArrays() { // Initialize single dimensional array // containing a list of names $names[0] = "Heather"; $names[1] = "Nikki"; $names[2] = "Mich";
exec("./arrays.cs"); Add the following: initArrays();
// Print out all the values // in the $names array function printNames() { // Print each name using // hard coded values (0,1,2) echo("0:" @ $names[0]); echo("1:" @ $names[1]); echo("2:" @ $names[2]); }
printNames();
0: Heather 1: Nikki 2: Mich
function printNames() {
// Change the value of an array item // %id = index to change // %name = the new value function setNames(%id, %name) { // Our array only contains three elements: // [0] [1] [2] // If anything other than 0, 1, or 2 is // passed in, inform the user of an error if(%id > 2 || %id < 0) { error("Index " @ %id @ " out of range"); error("Please use 0 - 2 as the %id"); } else $names[%id] = %name; }
setNames(0, "Brad");
// Print out the the values // in the $board array function printBoardValues() { // %i loops through rows for(%i = 0; %i < 3; %i++) { // %j loops through columns for(%j = 0; %j < 3; %j++) { // Print the value of the [%i,%j] echo("[" @ %i @ "," @ %j @ "]: " @ $board[%i, %j]); } } }
[0,0]: _ [0,1]: _ [0,2]: _ [1,0]: _ [1,1]: _ [1,2]: _ [2,0]: _ [2,1]: _ [2,2]: _
// Print tic-tac-toe board // in a relative format function printBoard() { // Print out an entre row in 1 echo echo($board[0,0] @" "@ $board[0,1] @" "@ $board[0,2]); echo($board[1,0] @" "@ $board[1,1] @" "@ $board[1,2]); echo($board[2,0] @" "@ $board[2,1] @" "@ $board[2,2]); }
_ _ _
// Set a specific value in the array // to an X or O function setBoardValue(%row, %column, %value) { // Make sure "X" or "O" was passed in if(%value !$= "X" && %value !$= "O") { echo("Invalid entry:\nPlease use \'X\' or \'O\'"); return; } }
// Set a specific value in the array // to an X or O function setBoardValue(%row, %column, %value) { // Make sure "X" or "O" was passed in if(%value !$= "X" && %value !$= "O") { echo("Invalid entry:\nPlease use \'X\' or \'O\'"); return; }
printBoard(); setBoardValue(0,0,"X"); setBoardValue(0,1,"O"); printBoard();
_ _ _
// Set all values of $board // array back to "nothing" // In this case, nothing is _ function resetBoard() { // %i loops through rows for(%i = 0; %i < 3; %i++) { // %j loops through columns for(%j = 0; %j < 3; %j++) { // Set value to _ $board[%i, %j] = "_"; } } }
// Compare the values of each array // item in a row // If row contains the same values // Return true for a victory // Return false if values are different function checkForWin() { // Make sure at least the first symbol is X or O // Then compare the three values of a row
%string1 = "Hello"; %string2 = "Hello"; %string3 = "World";
function checkForWin() { // Make sure at least the first symbol is X or O // Then compare the three values of a row //if($board[0,0] !$= "" && $board[0,0] $= $board[0,1] && $board[0,1] $= $board[0,2]) //return true; // //if($board[1,0] !$= "" && $board[1,0] $= $board[1,1] && $board[1,1] $= $board[1,2]) //return true; // //if($board[2,0] !$= "_" && $board[2,0] $= $board[2,1] && $board[2,1] $= $board[2,2]) //return true;
if($board[0,0] !$= "" &&)
!strcmp($board[0,0], $board[0,1])
&& !strcmp($board[0,1], $board[0,2])
function setBoardValue(%row, %column, %value) { // Make sure "X" or "O" was passed in if(%value !$= "X" && %value !$= "O") { echo("Invalid entry:\nPlease use 'X' or 'O'"); return; }
This guide covered the concept of arrays, both single and multi-dimensional. Lessons from past guides were also used: string comparisons, logical operators, function declaration and calling, loop structures, etc.
|