Designing solutions through programming - June 1 Lesson Notes: Difference between revisions

From Computer Science Wiki
(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...")
 
 
(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,$destinatio
function monster($current_player_location, $current_monster_location,$destination) {
     echo "player is: $current_player_location Monster is: $current_monster_locat
     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_pla
monster($current_player_location,$current_monster_location,($current_player_location-1));
        if($current_player_location==50){ $grid[50]=1; }
if($current_player_location==50){ $grid[50]=1; }
        $grid[$current_player_location] = $_SESSION['terrain_in_destination'];
$grid[$current_player_location] = $_SESSION['terrain_in_destination'];
        $terrain_in_destination = $grid[($current_player_location-1)];
$terrain_in_destination = $grid[($current_player_location-1)];
        $_SESSION['terrain_in_destination'] = $terrain_in_destination;
$_SESSION['terrain_in_destination'] = $terrain_in_destination;
        $grid[($current_player_location-1)] = 4;
$grid[($current_player_location-1)] = 4;
     } elseif($move == 'E' || $move == 'e'){
     } elseif($move == 'E' || $move == 'e'){
        if($current_player_location==50){ $grid[50]=1; }
      monster($current_player_location,$current_monster_location,($current_player_location+1));
        $grid[$current_player_location] = $_SESSION['terrain_in_destination'];
if($current_player_location==50){ $grid[50]=1; }
        $terrain_in_destination = $grid[($current_player_location+1)];
$grid[$current_player_location] = $_SESSION['terrain_in_destination'];
        $_SESSION['terrain_in_destination'] = $terrain_in_destination;
$terrain_in_destination = $grid[($current_player_location+1)];
        $grid[($current_player_location+1)] = 4;
$_SESSION['terrain_in_destination'] = $terrain_in_destination;
$grid[($current_player_location+1)] = 4;
     } elseif($move == 'N' || $move == 'n'){
     } elseif($move == 'N' || $move == 'n'){
        if($current_player_location==50){ $grid[50]=1; }
      monster($current_player_location,$current_monster_location,($current_player_location-20));
        $grid[$current_player_location] = $_SESSION['terrain_in_destination'];
if($current_player_location==50){ $grid[50]=1; }
        $terrain_in_destination = $grid[($current_player_location-20)];
$grid[$current_player_location] = $_SESSION['terrain_in_destination'];
        $_SESSION['terrain_in_destination'] = $terrain_in_destination;
$terrain_in_destination = $grid[($current_player_location-20)];
        $grid[($current_player_location-20)] = 4;
$_SESSION['terrain_in_destination'] = $terrain_in_destination;
$grid[($current_player_location-20)] = 4;
     } elseif($move == 'S' || $move == 's'){
     } elseif($move == 'S' || $move == 's'){
        if($current_player_location==50){ $grid[50]=1; }
      monster($current_player_location,$current_monster_location,($current_player_location+20));
        $grid[$current_player_location] = $_SESSION['terrain_in_destination'];
if($current_player_location==50){ $grid[50]=1; }
        $terrain_in_destination = $grid[($current_player_location+20)];
$grid[$current_player_location] = $_SESSION['terrain_in_destination'];
        $_SESSION['terrain_in_destination'] = $terrain_in_destination;
$terrain_in_destination = $grid[($current_player_location+20)];
        $grid[($current_player_location+20)] = 4;
$_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

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'){
      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 />


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]