Designing solutions through programming - June 1 Lesson Notes

From Computer Science Wiki

Class plan.png 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'){
	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'){
	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'){
	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 />


Homework.png What is our homework?[edit]

  1. None

Target.png How am I being assessed today?[edit]

  1. You get a formative assessment today :-)

Ourstandards.png Standards we are covering today[edit]

Computer1.png As a computer scientist, you have:[edit]

  • Confidence in dealing with complexity
  • Persistence in working with difficult problems
  • Tolerance for ambiguity
  • The ability to deal with open-ended problems
  • The ability to communicate and work with others to achieve a common goal or solution

Credit.png Credits[edit]