Using Solidity variables

Now that we have an idea of what variables in Solidity are, let's put them to practical use in a contract. Let's take a look at the contract present in our game:

pragma solidity 0.5.0;

contract Gaming {
/*Our Online Gaming Contract*/
address owner;
bool online;
}

Right after the contract is declared, there are two variables declared. The first one is an owner , which is an address data type, and this represents the Ethereum address of the person who deployed this contract to the Ethereum network. Then we have a bool data type called online, and we'll use this to determine whether your game is online or offline; it allows us to take the game offline if we need to.

Well, there's a special function in Ethereum contracts called a constructor, which executes once and only once when the contract is deployed. Inside it, we're going to do two things: we'll set the owner variable we declared in the beginning of our contract to the Ethereum address that deployed this contract to the network:

    constructor() public payable {
owner = msg.sender;
online = true;
}

This means that if I deployed the contract, I own the game and if you deploy the contract, you own the game. This is a way for you to write contracts and deliver them to your clients, and when they deploy them, they own all the assets and the currency that the contract accumulates. We'll also set our online variable equal to true; we'll use this later to effectively take our game offline if we need to.

We also have this function called winOrLose(), which we'll call from our React application and supply the required parameters to determine if our player wins or loses this round. There's another function called isWinner() that returns the value as true, if the player has won, and false if they lost. So let's examine how that works using the following code snippet:

function winOrLose(uint display, bool guess, uint wager)
external payable returns (bool) {
if (isWinner == true ) {
/*Player won*/
msg.sender.transfer(wager*2);
return true;
} else if (isWinner == false) {
/*Player lost*/
return false;
}
}

Here, we've got an if statement, and the condition we're evaluating is contained within it. Next, we have our opening and closing brackets containing the code to execute, if this statement evaluates as true. Inside the parentheses, we have our isWinner() variable, which is a Boolean data type, and we use the == sign to evaluate whether this variable evaluates to a Boolean true.  If it does, it will execute the code contained within the block.

This code uses a special message sender variable, which contains the address of the account calling the function. In this case that's our player. The player has won, so we'll use the .transfer() member to transfer double the amount wagered to the player; the reason that we're doing double, is that the player had to include the amount they wanted to wager with this transaction, so we need to return that to them, plus the amount that they won from that wager.

If that statement doesn't evaluate as true though, that block of code never executes, so the code execution continues down to the else if block. It operates in the same way as the if block. It will evaluate the statement inside the parentheses, and if isWinner() is false, the block will return false to our React client.

Inside our React code, we'll check for this value, and update the UI accordingly to let the player know that they lost this round.