jeudi 12 avril 2018

A way of using a variable to record a game state in PHP?

I won't post the full code as it would be long winded and unnecessary. I am writing a blackjack game within the laravel framework and I need a way to record whether the game has begun or not. Currently the player could just continually ask for new cards until they receive a favourable hand. When I set an if statement at the start of the blackjack function with a $GameOn variable I get an error saying of course that it is undefined, where should I define this variable?

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Auth;
use View;

class GameController extends Controller
{
private $hit_count;
private $GameOn; 

public function blackjack()
{
//check if user is logged in
if ($user = Auth::user()) {
  if ($GameOn == false) {

Here I build the deck shuffle and deal the cards and work out the scores at any point in the game. I also save necessary variables to the session.

return view('blackjack', $data);
  }
//send user to login page if they aren't logged in
} else if ($user = Auth::guest()){
    header('Location: /login');
}

}  public function playblackjack(Request $request){
    if ($user = Auth::user()) {
        $GameOn = true;

Here I need the game state to be on so I set the variable $GameOn to true. I pull the necessary session variables and control what happens when the user selects whether they want to hit or stand.

switch ($turn_played) {
            case 'Hit':
            //Give the player another card.
            break;  

            case 'Stand':
            //work out who won the game set $GameOn to false ready for new game.
            $GameOn = false;
            break;

            case 'NewGame':
            $hit_count = 0;
            header('Location: /blackjack');
            break;
        }

If you are responding to my question I thank-you in advance and could you also note that I don't intend to use Javascript as part of my site!



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire