Selaa lähdekoodia

Working_Game

master
rvaken 3 vuotta sitten
vanhempi
commit
8196a550b2
7 muutettua tiedostoa jossa 349 lisäystä ja 344 poistoa
  1. 80
    121
      board.php
  2. 11
    12
      difficulty.php
  3. 24
    0
      functies.php
  4. 79
    46
      game.php
  5. 0
    51
      position.php
  6. 155
    24
      ship.php
  7. 0
    90
      shotform.php

+ 80
- 121
board.php Näytä tiedosto

@@ -1,149 +1,108 @@
<?php
session_start();
require_once 'position.php';
require_once 'ship.php';

class Board
{

private $gameBoard;
// protected $gameBoard;

private $ships;
// protected $ships;

private $currentShips;
protected $currentShips;

// We constructen de array die het spelbord zal vullen
// We constructen de array die het spelbord zal vullen

function __construct()
{
$this->gameBoard = array();
$this->initArray();
$this->initShips();
//$this->displayBoard();
}

//

private function initArray(){


for ($x = 0; $x < 52; $x ++){
function __construct($difficulty)
{

$this->gameBoard[$x]= new Position($x);
$this->initShips($difficulty);
}
}





private function initShips() {
if ($_SESSION['difficulty'] == 'Easy'){
$currentShips[] = $this->initVliegdekschip();
$currentShips[] = $this->initSlagschip();

} else if ($_SESSION['difficulty'] == 'Normal'){
$currentShips[] = $this->initSlagschip();
$currentShips[] = $this->initKruiser();

} else if ($_SESSION['difficulty'] == 'Hard'){
$currentShips[] = $this->initTorpedojager();
$currentShips[] = $this->initDuikboot();
protected function initShips($difficulty)
{

if ($difficulty == 'Easy') {
$this->currentShips[] = new torpedojager();
$this->currentShips[] = new duikboot();
} else if ($difficulty == 'Normal') {
$this->currentShips[] = new slagSchip();
$this->currentShips[] = new kruiser();
} else if ($difficulty == 'Hard') {
$this->currentShips[] = new vliegdekSchip();
$this->currentShips[] = new slagSchip();
}
}

}

public function returnShips(){
return $this->currentShips;
}

private function initVliegdekschip() {
$this->ships[] = new Ship("Vliegdekschip", 5);
$rand = rand(0, 46);
$this->gameBoard[$rand] = "<";
$this->gameBoard[$rand+1] = "=";
$this->gameBoard[$rand+2] = "=";
$this->gameBoard[$rand+3] = "=";
$this->gameBoard[$rand+4] = ">";
}

private function initSlagschip() {
$this->ships[] = new Ship("Slagschip", 4);
$rand = rand(0, 47);
$this->gameBoard[$rand] = "<";
$this->gameBoard[$rand+1] = "=";
$this->gameBoard[$rand+2] = "=";
$this->gameBoard[$rand+3] = ">";
}
//Overlapping fixen
protected function legalShipPosition()
{
// $this->currentShip[0]->returnPosBegin() >
// in_array($needle, $haystack)
// while (bovenstaande true)
// herbereken initship;
}

private function initKruiser() {
$this->ships[] = new Ship("Kruiser", 3);
$rand = rand(0, 48);
$this->gameBoard[$rand] = "<";
$this->gameBoard[$rand+1] = "=";
$this->gameBoard[$rand+2] = ">";
}
public function returnCurrentShips()
{
return $this->currentShips;
}

private function initTorpedojager() {
$this->ships[] = new Ship("Torpedojager", 2);
$rand = rand(0, 49);
$this->gameBoard[$rand] = "<";
$this->gameBoard[$rand+1] = ">";
}

private function initDuikboot() {
$this->ships[] = new Ship("Duikboot", 1);
$rand = rand(0, 50);
$this->gameBoard[$rand] = "=";
}
public function printBoard($shots)
{
// Hier uw code maken voor uw bord te printen:
$html = "<pre>";
for ($y = 0; $y < 3; $y++) {
for ($x = 0; $x < 52; $x++) {
if ($y !== 1) {
if ($x == 0 || $x == 51) {
$html .= "+";
} else {
$html .= "-";
}
} elseif ($x == 0 || $x == 51) {
$html .= "|";
} else {

public function getBoard() {
return $this->gameBoard;
}

public function getShips() {
return $this->ships;
}
if (!empty($shots)) {
if (
in_array($x, $shots)
// || in_array($x, $_SESSION['currentShips']->returnCurrentShips()[1]->posArr())
) {
if (
in_array($x, $_SESSION['currentShips']->returnCurrentShips()[0]->posArr()) ||
in_array($x, $_SESSION['currentShips']->returnCurrentShips()[1]->posArr())
) {


// if (
// $x == $_SESSION['currentShips']->returnCurrentShips()[0]->returnPosEind() ||
// $x == $_SESSION['currentShips']->returnCurrentShips()[1]->returnPosEind()
// ) {
// $html .= ">";
// } else {
$html .= "x";
// }
} else {
$html .= "o";
}
} else {
$html .= " ";
}
} else {
$html .= " ";
}
}
}

public function getShipByName($name) {
foreach($this->ships as $ship):
if ($ship->getName() === $name)
return $ship;
endforeach;
$html .= "\n";
}

return false;
}

public function displayBoard() {
$html .= "</pre>";

for ($y = 0; $y < 3; $y++) {
for ($x = 0; $x < 53; $x++) {
if ($y == 0 || $y == 2) {
if ($x == 0) {
echo "+";
} else if ($x == 52) {
echo "+" . "<br>";
} else {
echo "-";
}
} else {
if ($x == 0) {
echo "|";
} else if ($x == 52){
echo "|" . "<br>";
} else {
echo " ";
}
}
}
return $html;
}
}

}

?>

+ 11
- 12
difficulty.php Näytä tiedosto

@@ -1,47 +1,46 @@


<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Difficulty</title>
</head>

<body>
<form method="POST" action="#">
<form method="POST" action="#">
<div class="input-group">
<p>Select your difficulty</p>
<label>Difficulty</label>
<br>
<input type="radio" name="difficulty" value="Easy">
<input type="radio" name="difficulty" id="Easy" value="Easy" checked>
<label for="Easy">Easy</label>
<br>
<input type="radio" name="difficulty" value="Normal">
<input type="radio" name="difficulty" id="Normal" value="Normal">
<label for="Normal">Normal</label>
<br>
<input type="radio" name="difficulty" value="Hard">
<input type="radio" name="difficulty" id="Hard" value="Hard">
<label for="Hard">Hard</label>
<br>
<input type="submit" name="submit" id="submit" value="Submit">
</div>
</form>
</body>

</html>

<?php
<?php

require_once 'board.php';
require_once 'ship.php';
require_once 'position.php';

session_start();

if (isset($_POST['submit'])){
if (isset($_POST['submit'])) {
$_SESSION['gaming'] = true;
$_SESSION['difficulty'] = $_POST['difficulty'];


header("Location: game.php");
}


+ 24
- 0
functies.php Näytä tiedosto

@@ -0,0 +1,24 @@
<?php
require_once 'board.php';
session_start();

function assignShips($difficulty)
{
$_SESSION['currentShips'] = new Board($difficulty);
for ($x = 0; $x <= count($_SESSION['currentShips']->returnCurrentShips()) - 1; $x++) {
$_SESSION['shipsPositions'][$x] = $_SESSION['currentShips']->returnCurrentShips()[$x]->posArr();
}
}

function hitOrMis($shot)
{

for ($x = 0; $x <= count($_SESSION['currentShips']->returnCurrentShips()) - 1; $x++) {

if ($_SESSION['currentShips']->returnCurrentShips()[$x]->hitOrMis($shot)) {
$plaats = array_search($shot, $_SESSION['shipsPositions'][$x]);
unset($_SESSION['shipsPositions'][$x][$plaats]);
return $_SESSION['currentShips']->returnCurrentShips()[$x]->returnName() . ' geraakt!<br>';
}
}
}

+ 79
- 46
game.php Näytä tiedosto

@@ -1,25 +1,31 @@
<?php

require_once 'board.php';
include 'functies.php';

session_start();

require_once 'board.php';
require_once 'ship.php';
require_once 'position.php';
$form_state = 0;
var_dump($_SESSION['difficulty']);

if (isset($_POST['btnLogOut'])) {
session_destroy();
header('Location: index.php');
}

if (isset($_POST['btnShot'])) {
$shot = $_POST['shot'];
$_SESSION['shots'][] = $shot;


// ---HTML---

echo hitOrMis($shot);
}
?>



<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
@@ -27,57 +33,84 @@ var_dump($_SESSION['difficulty']);
<title>Game</title>

</head>

<body>
<?php
if ($_POST['shot']){
$form_state = 1;
$_SESSION['shot'] = $_POST['shot'];
}
?>
<?php
if (isset($_POST['shot'])) {
$form_state = 1;
$_SESSION['shot'] = $_POST['shot'];
}
?>

<?php if ($form_state !== 1) { ?>

<?php if ($form_state !== 1) { ?>
<form action="game.php" method="POST">
<h2>Vul uw schot in</h2>
<div class="input-shot">
<label>Shot</label>
<input type="number" name="shot">
<input type="submit" id="shot" value="Shoot!">
</div>
<br>
<input type="submit" value="Uitloggen" name="btnLogOut">
</form>
<form action="game.php" method="POST">
<h2>Vul uw schot in</h2>
<div class="input-shot">
<label>Shot</label>
<input type="number" name="shot" required autofocus>
<input type="submit" id="shot" value="Shoot!" name="btnShot">
</div>
<br>

</form>
<?php } ?>
<?php if ($form_state == 1) { ?>

<body>

<h2><?php echo "Uw schot was: " . $_POST['shot']; ?></h2>
<h2><?php echo "Uw schot was: " . $_POST['shot']; ?></h2>

<form action="game.php" method="POST">
<input type="submit" value="Next shot">
</form>
<form action="game.php" method="POST">
<?php

if (empty($_SESSION['shipsPositions'][0]) && empty($_SESSION['shipsPositions'][1])) { ?>
<input type="submit" name="btnPlayAgain" value="Play again">
<?php } else { ?>
<input type="submit" name="btnNextShot" value="Next shot">

<?php } ?>
</form>


</body>

</body>
<?php } ?>
<pre><?php
//if ($_SESSION['gaming']){
//$_SESSION['gaming']->displayBoard();
$test = new Board();
$_SESSION['newBoard'] = $test;
$_SESSION['gaming'] = false;
//}

$_SESSION['newBoard']->displayBoard();
var_dump($_SESSION['newBoard']->returnShips());

<?php

if (isset($_POST['btnPlayAgain'])) {
assignShips($_SESSION['difficulty']);
unset($_SESSION['shots']);
}
// Als currentships niet meer bestaat
if (!isset($_SESSION['currentShips'])) {
assignShips($_SESSION['difficulty']);
}


if (empty($_SESSION['shipsPositions'][0])) {
echo $_SESSION['currentShips']->returnCurrentShips()[0]->returnName() . " vernietigd!<br>";
}
if (empty($_SESSION['shipsPositions'][1])) {
echo $_SESSION['currentShips']->returnCurrentShips()[1]->returnName() . " vernietigd!<br>";
}




// var_dump($_SESSION['currentShips']);
var_dump($_SESSION['shipsPositions']);


// Als de functie gemaakt is, in board...
echo $_SESSION['currentShips']->printBoard($_SESSION['shots']);

?>
</pre>

</body>
</html>

</html>

+ 0
- 51
position.php Näytä tiedosto

@@ -1,51 +0,0 @@
<?php
class Position
{
// boolean die nakijkt of er een schip staat op een positie
private $shipPos;

// welk schip op een positie staat
private $shipName;

// boolean die bijhoudt of een positie is beschoten of niet
private $shotStatus;

public function __construct($arrPos)
{
$this->shipPos = false;
$this->shotStatus = false;
}

public function displayPos() {
if($this->shipPos && $this->shotStatus) {
return 'x';
}
else if ($this->shotStatus) {
return '+';
} else {
return' ';
}
}
public function getShipPos() {
return $this->shipPos;
}

public function getShipName() {
return
$this->shipName;
}

public function setShipPos() {
$this->shipPos = true;
}

public function setShotStatus() {
$this->shotStatus = true;
}

public function setShipName($name) {
$this->shipName = $name;
}
}
?>

+ 155
- 24
ship.php Näytä tiedosto

@@ -1,43 +1,174 @@
<?php
Class Ship

class Ships
{
protected $length;
protected $posBegin;
protected $posEind;
protected $posMidden;
protected $amountShots;
protected $arrPos;
protected $ammo;


function __construct()
{
$this->createShip();
}

protected function createShip()
{
$this->calcposMidden();
$this->calcposBegin();
$this->calcposEind();
$this->initPosArr();
}


protected function calcposBegin()
{
$this->posBegin = $this->posMidden - floor($this->length / 2);
}


protected function calcposEind()
{
$this->posEind = $this->posBegin + $this->length - 2;
}

public function calcposMidden()
{

private $size;
private $name;
private $remainingHits;
private $isSunk;
$this->posMidden = rand(4, 47);
}

// ship constuctor
public function __construct($name, $size)
public function returnPosBegin()
{
$this->name = $name;
$this->size = $size;
$this->remainingHits = $size;
$this->isSunk = false;
return $this->posBegin;
}
public function returnPosEind()
{
return $this->posEind;
}

public function getName() {
return $this->name;
protected function initPosArr()
{
for ($i = $this->posBegin; $i <= $this->posEind; $i++) {
$this->arrPos[] = $i;
}
}

public function getSize() {
return $this->size;
//aanpassen!!
public function ammo()
{
return $this->ammo = $this->length;
}

public function getRemainingHits() {
return $this->remainingHits;

public function posArr()
{
return $this->arrPos;
}

public function getIsSunk() {
return $this->isSunk;
public function hitOrMis($shot)
{
if ($shot >= $this->posBegin && $shot <= $this->posEind) {
return true;
} else {
return false;
}
}
}

public function receiveHit() {
$this->remainingHits--;
class vliegdekSchip extends Ships
{
protected $shipName;
protected function createShip()
{
$this->length = 8;
$this->calcposMidden();
$this->calcposBegin();
$this->calcposEind();
$this->initPosArr();
//$this->ammo = $this-length + 4;
$this->shipName = "Vliegdekship";
}

public function sink() {
$this->isSunk = true;
public function returnName()
{
return $this->shipName;
}
}
class slagSchip extends Ships
{
protected $shipName;
protected function createShip()
{
$this->length = 7;
$this->calcposMidden();
$this->calcposBegin();
$this->calcposEind();
$this->initPosArr();
$this->shipName = "Slagship";
}

public function returnName()
{
return $this->shipName;
}
}
class kruiser extends Ships
{
protected $shipName;
protected function createShip()
{
$this->length = 6;
$this->calcposMidden();
$this->calcposBegin();
$this->calcposEind();
$this->initPosArr();
$this->shipName = "Kruiser";
}

public function returnName()
{
return $this->shipName;
}
}

class torpedojager extends Ships
{
protected $shipName;
protected function createShip()
{
$this->length = 5;
$this->calcposMidden();
$this->calcposBegin();
$this->calcposEind();
$this->initPosArr();
$this->shipName = "Torpedojager";
}

public function returnName()
{
return $this->shipName;
}
}
class duikboot extends Ships
{
protected $shipName;
protected function createShip()
{
$this->length = 4;
$this->calcposMidden();
$this->calcposBegin();
$this->calcposEind();
$this->initPosArr();
$this->shipName = "Duikboot";
}

public function returnName()
{
return $this->shipName;
}
}
?>

+ 0
- 90
shotform.php Näytä tiedosto

@@ -1,90 +0,0 @@
<?php
session_start();

unset($_SESSION['errormsg']);


function showShotForm($msg){
echo $msg;
};

if (isset($_POST ['submit_shot'])){
$db = new SQLite3('db.sqlite');
$stmt = $db->prepare("insert into shots(users_id , shot VALUES (:user_id, :shot)");
$stmt->bindValue(":user_id", $_SESSION['id'], SQLITE3_NUM );
$stmt->bindValue(":shot", $_SESSION['id'], $_POST['shot'], SQLITE3_NUM);
$res = $stmt->execute();
$error = $db->lastErrorMsg();
echo $error;
echo "<br/>";
?>
<?php
showShotForm($_SESSION['login']);
}
?>
<?php
class DB

{

protected $dbase;

public function __construct(){
$this->dbase = new SQLite3("db.sqlite");
}

public function checkLogin($username, $password){

$stmt = $this->dbase->prepare("SELECT * FROM users WHERE login = :usrn AND password = :passw");
$stmt->bindValue(":usrn", $username, SQLITE3_TEXT);
$stmt->bindValue(":pass", $password, SQLITE3_TEXT);
$res = $stmt->execute();
if ($row = $res->fetchArray(SQLITE3_ASSOC)) {
return ['id' => $row['id'], 'login' => $row['login']];
} else {
return False;
}
}
}

function test_input($data)
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}

function validateShot($shot) {

$shot = test_input($shot);

if (is_numeric($shot)) {

return $shot;

} else {
$_SESSION['errormsg'] = "Invalid input, fill in a valid number.";
}
}


?>


<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Shotform</title>
</head>

</html>

Loading…
Peruuta
Tallenna