# Blockchain Summer Research
*Subhash Prasad and Junhyung Yoon*
# NFT Matching Game
## Project Idea
Our idea for this project was to be able to create an NFT matching game. It was a simple framework, yet utilizing the nfts, connecting to the blockchain, and executing transactions would be a challenge.
# Number Guessing Game
## Project Idea
The game is based around a simple concept: guessing a random number from 1-100. The player starts their "turn" by entering a number into the program. The program displays a message based on the accuracy of their guesses.
### Possible Display Messages
- "You got it!" (If the player has guessed the correct number)
- "Too Low!" (If the player's guess is lower than the actual number')
- "Too High!" (If the player's guess is higher than the actual number')
This message concludes the player's turn, and they will have the opportunity to guess again. The objective of the game is to guess the number in the least number of guesses possible. At the end of the game (when the player guesses correct) the program will display the number of guesses it took.
This project is designed to be implemented on the blockchain and transactions are sent to the player when they make a move.
## Game Logic Code
### GuessingGame.sol
```solidity=
// works without tries:
// SPDX-License-Identifier: MIT
pragma solidity >=0.5.0 <0.9.0;
contract GuessingGame {
uint private randomNum;
uint private tries = 0;
constructor() public {
randomNum = uint(blockhash(block.number-1)) % 100;
}
function createRandom() public {
randomNum = uint(blockhash(block.number-1)) % 100;
}
function message() public {
tries++;
}
function guess(uint num) public view returns (string memory) {
int diff = int(num) - int(randomNum);
if (diff == 0) {
return "Correct! You got it in x tries!";
} else if (diff > 0) {
return "Wrong! Guess lower!";
} else {
return "Wrong! Guess higher!";
}
}
}
```
This code works perfectly on Remix. It is run using MetaMask on the Ropsten Test Network.
## Current Web3 Platform and Base Code
We decided to use the web3 platform called Moralis in order to run out dAPP. We encountered trouble with connecting our web3 dAPP on MetaMask however. We were able to obtain some help from Moralis developers and we are still working with them to get the dAPP running. Some other web3 platforms we considered
https://moralis.io/?network=o&device=c&msclkid=67132aab14101ecde15049a4e4bc0c8a&utm_source=bing&utm_medium=cpc&utm_campaign=search-brand-high-cpa-markets-msads&utm_term=moralis&utm_content=%2F
### main.js
```javascript=
const serverUrl = "https://xhxcg575gt1v.usemoralis.com:2053/server";
const appId = "wRtrpiPsOqgfSerU8hQJEExwmJnGMvttM2RD3Aus";
Moralis.start({ serverUrl, appId });
async function login() {
let user = Moralis.User.current();
if (!user) {
try {
user = await Moralis.authenticate({ signingMessage: "Authenticate" })
console.log(user)
console.log(user.get('ethAddress'))
} catch (error) {
console.log(error)
}
}
}
async function logOut() {
await Moralis.User.logOut();
console.log("logged out");
}
document.getElementById("btn-login").onclick = login;
document.getElementById("btn-logout").onclick = logOut;
async function donate() {
let options = {
contractAddress: "0x356d2E7a0d592bAd95E86d19479c37cfdBb68Ab9",
functionName: "newDonation",
abi: [
{
inputs: [
{ internalType: "string", name: "note", type: "string" },
],
name: "newDonation",
outputs: [],
stateMutability: "payable",
type: "function",
},
],
Params: {
Note: "Thanks for your work",
},
msgValue: Moralis.Units.ETH(0.1),
};
await Moralis.User.logOut();
console.log("logged out");
}
```
### index.html
```javascript=
<html>
<head>
<title>Vanilla Boilerplate</title>
<script src=”https://cdn.jsdelivr.net/npm/web3@latest/dist/web3.min.js”></script>
<script src=”https://unpkg.com/moralis-v1@1.11.0/dist/moralis.js”></script>
</head>
<body>
<button id=”btn-login”>Moralis Metamask Login</button>
<button id=”btn-logout”>Logout</button>
<script type=”text/javascript” src=”./main.js”></script>
</body>
</html>
```