Designing solutions through programming - June 1 Lesson Notes: Difference between revisions
Mr. MacKenty (talk | contribs) (Created page with " __NOTOC__ <table cellspacing="10" style="width:100%;"> <tr> <td style="width:50%; margin:0; margin-top:10px; margin-right:10px; border:1px solid #dfdfdf; padding:0 1em 1em 1...") |
Mr. MacKenty (talk | contribs) |
||
(One intermediate revision by the same user not shown) | |||
Line 9: | Line 9: | ||
session_start(); | session_start(); | ||
function monster($current_player_location, $current_monster_location,$ | function monster($current_player_location, $current_monster_location,$destination) { | ||
echo "player is: $current_player_location Monster is: $ | echo "player is: $current_player_location Monster is: $current_monster_location <br />"; | ||
if($destination == $current_monster_location){ | if($destination == $current_monster_location){ | ||
echo "from function: MONSTER!!! <br />"; | echo "from function: MONSTER!!! <br />"; | ||
} | } | ||
Line 20: | Line 20: | ||
if ($_SERVER['REQUEST_METHOD'] === 'POST') { | if ($_SERVER['REQUEST_METHOD'] === 'POST') { | ||
$current_player_location = $_SESSION['current_player_location']; | $current_player_location = $_SESSION['current_player_location']; | ||
$current_monster_location = $_SESSION['current_monster_location']; | $current_monster_location = $_SESSION['current_monster_location']; | ||
$grid = $_SESSION['grid']; | $grid = $_SESSION['grid']; | ||
$move = $_POST['move']; | $move = $_POST['move']; | ||
if($move == 'W' || $move == 'w'){ | if($move == 'W' || $move == 'w'){ | ||
monster($current_player_location,$current_monster_location,($current_player_location-1)); | |||
if($current_player_location==50){ $grid[50]=1; } | |||
$grid[$current_player_location] = $_SESSION['terrain_in_destination']; | |||
$terrain_in_destination = $grid[($current_player_location-1)]; | |||
$_SESSION['terrain_in_destination'] = $terrain_in_destination; | |||
$grid[($current_player_location-1)] = 4; | |||
} elseif($move == 'E' || $move == 'e'){ | } elseif($move == 'E' || $move == 'e'){ | ||
monster($current_player_location,$current_monster_location,($current_player_location+1)); | |||
if($current_player_location==50){ $grid[50]=1; } | |||
$grid[$current_player_location] = $_SESSION['terrain_in_destination']; | |||
$terrain_in_destination = $grid[($current_player_location+1)]; | |||
$_SESSION['terrain_in_destination'] = $terrain_in_destination; | |||
$grid[($current_player_location+1)] = 4; | |||
} elseif($move == 'N' || $move == 'n'){ | } elseif($move == 'N' || $move == 'n'){ | ||
monster($current_player_location,$current_monster_location,($current_player_location-20)); | |||
if($current_player_location==50){ $grid[50]=1; } | |||
$grid[$current_player_location] = $_SESSION['terrain_in_destination']; | |||
$terrain_in_destination = $grid[($current_player_location-20)]; | |||
$_SESSION['terrain_in_destination'] = $terrain_in_destination; | |||
$grid[($current_player_location-20)] = 4; | |||
} elseif($move == 'S' || $move == 's'){ | } elseif($move == 'S' || $move == 's'){ | ||
monster($current_player_location,$current_monster_location,($current_player_location+20)); | |||
if($current_player_location==50){ $grid[50]=1; } | |||
$grid[$current_player_location] = $_SESSION['terrain_in_destination']; | |||
$terrain_in_destination = $grid[($current_player_location+20)]; | |||
$_SESSION['terrain_in_destination'] = $terrain_in_destination; | |||
$grid[($current_player_location+20)] = 4; | |||
} | } | ||
} else { | } else { | ||
Line 77: | Line 80: | ||
$grid[123]=5; | $grid[123]=5; | ||
} // this ends the ISPOST | } // this ends the ISPOST | ||
// print the array so it looks like a grid | // print the array so it looks like a grid | ||
Line 118: | Line 121: | ||
echo "<br /><hr /><br />"; | echo "<br /><hr /><br />"; | ||
echo "The player is currently at position: $current_player_location <br />"; | echo "The player is currently at position: $current_player_location <br />"; | ||
echo "The monster is currently at position: $current_monster_location <br /> | echo "The monster is currently at position: $current_monster_location <br />"; | ||
echo "<br /><hr /><br />"; | echo "<br /><hr /><br />"; | ||
// print_r($count_terrain_features); | // print_r($count_terrain_features); |
Latest revision as of 08:25, 1 June 2017
What are we going to learn today?[edit]<?php
session_start();
function monster($current_player_location, $current_monster_location,$destination) {
echo "player is: $current_player_location Monster is: $current_monster_location <br />";
if($destination == $current_monster_location){
echo "from function: MONSTER!!! <br />";
}
}
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$current_player_location = $_SESSION['current_player_location'];
$current_monster_location = $_SESSION['current_monster_location'];
$grid = $_SESSION['grid'];
$move = $_POST['move'];
if($move == 'W' || $move == 'w'){
monster($current_player_location,$current_monster_location,($current_player_location-1));
if($current_player_location==50){ $grid[50]=1; }
$grid[$current_player_location] = $_SESSION['terrain_in_destination'];
$terrain_in_destination = $grid[($current_player_location-1)];
$_SESSION['terrain_in_destination'] = $terrain_in_destination;
$grid[($current_player_location-1)] = 4;
} elseif($move == 'E' || $move == 'e'){
monster($current_player_location,$current_monster_location,($current_player_location+1));
if($current_player_location==50){ $grid[50]=1; }
$grid[$current_player_location] = $_SESSION['terrain_in_destination'];
$terrain_in_destination = $grid[($current_player_location+1)];
$_SESSION['terrain_in_destination'] = $terrain_in_destination;
$grid[($current_player_location+1)] = 4;
} elseif($move == 'N' || $move == 'n'){
monster($current_player_location,$current_monster_location,($current_player_location-20));
if($current_player_location==50){ $grid[50]=1; }
$grid[$current_player_location] = $_SESSION['terrain_in_destination'];
$terrain_in_destination = $grid[($current_player_location-20)];
$_SESSION['terrain_in_destination'] = $terrain_in_destination;
$grid[($current_player_location-20)] = 4;
} elseif($move == 'S' || $move == 's'){
monster($current_player_location,$current_monster_location,($current_player_location+20));
if($current_player_location==50){ $grid[50]=1; }
$grid[$current_player_location] = $_SESSION['terrain_in_destination'];
$terrain_in_destination = $grid[($current_player_location+20)];
$_SESSION['terrain_in_destination'] = $terrain_in_destination;
$grid[($current_player_location+20)] = 4;
}
} else {
// This progam creates and prints a simple grid
// initialize the array
$grid = [];
// populate the array:
for($i=0;$i<300;$i++){
$new_number = rand(1,3);
if($new_number == 2 || $new_number ==3){
$new_number = rand(1,3);
}
$grid[] = $new_number;
}
// set initial player location:
$grid[50]=4;
// set initial monster location:
$grid[123]=5;
} // this ends the ISPOST
// print the array so it looks like a grid
echo "<!doctype html>";
echo "<html>";
echo "<meta charset=\"utf-8\"/>";
echo "<style>
body { font-family: \"courier\"; }</style>";
$count = 0;
$row_length = 20;
$player_location = 50;
foreach($grid as $i) {
$count = $count + 1;
if($i==1){
echo " . ";
} elseif($i==2) {
echo "<font color=\"green\"> T </font>";
} elseif($i==3) {
echo "<font color=\"red\"> M </font>";
} elseif($i == 4) {
echo "*";
} elseif($i == 5) {
echo " X ";
}
if($count == $row_length) {
echo "<br />";
$count = 0;
}
}
$current_player_location = array_search(4,$grid);
$current_monster_location = array_search(5,$grid);
$count_terrain_features = array_count_values($grid);
echo "<br /><hr /><br />";
echo "The player is currently at position: $current_player_location <br />";
echo "The monster is currently at position: $current_monster_location <br />";
echo "<br /><hr /><br />";
// print_r($count_terrain_features);
$_SESSION['grid'] = $grid;
$_SESSION['current_player_location'] = $current_player_location;
$_SESSION['current_monster_location'] = $current_monster_location;
?>
<form action="#" method="POST">
<p>Enter your move (N,S,W,E)<input type="text" name="move" autofocus /></p>
<input type="submit" value="click here to move" />
</form>
<br />
<br />
|
What is our homework?[edit]
|
|
How am I being assessed today?[edit]
|
||
Standards we are covering today[edit] |
As a computer scientist, you have:[edit]
|
|
Credits[edit] |