Skip to content

Commit

Permalink
Update lilypad
Browse files Browse the repository at this point in the history
  • Loading branch information
Dougy Lee committed May 4, 2024
1 parent 19f1c95 commit 54b2736
Show file tree
Hide file tree
Showing 8 changed files with 335 additions and 66 deletions.
5 changes: 5 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 23 additions & 3 deletions client/src/Home.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export default function Home() {

const contractAddress = "0x4B9E83Fa8f5C5C4402F49F2AbBBD61DDBA75c9b7";

const nftData = [
const defaultNftData = [
{
tokenId: 1,
name: "Botson",
Expand All @@ -23,7 +23,7 @@ export default function Home() {
brain: "https://www.google.com",
description: "A bot that can do anything it sets it mind to",
price: 100,
training: 100,
training: 0,
},
{
tokenId: 2,
Expand All @@ -47,6 +47,7 @@ export default function Home() {
},
];

const [nftData, setNftData] = useState(defaultNftData);
const miner = null;

const mintRobot = async () => {
Expand Down Expand Up @@ -98,6 +99,21 @@ export default function Home() {
}
}

function onTrain() {
setNftData([
{
tokenId: 1,
name: "Botson",
imageUrl:
"https://ipfs.io/ipfs/QmaG3Bqcy8mu1QwPmnp8gMEoN2N9fN99e2UGFKPgg7JLPv",
brain: "https://www.google.com",
description: "A bot that can do anything it sets it mind to",
price: 100,
training: 100,
},
]);
}

return (
<Scaffold>
{!connectedAccount && (
Expand All @@ -118,7 +134,11 @@ export default function Home() {
>
Mint a new Robot
</button>
<NftList nftData={nftData} />
<NftList
nftData={nftData}
onTrain={onTrain}
mineLink={nftData[0].training === 0 ? "/mine1" : "/mine2"}
/>
</Scaffold>
);
}
47 changes: 3 additions & 44 deletions client/src/NftList.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,42 +3,7 @@ import {
ArrowTrendingUpIcon,
} from "@heroicons/react/20/solid";

const nftData = [
{
tokenId: 1,
name: "Botson",
imageUrl:
"https://ipfs.io/ipfs/QmaG3Bqcy8mu1QwPmnp8gMEoN2N9fN99e2UGFKPgg7JLPv",
brain: "https://www.google.com",
description: "A bot that can do anything it sets it mind to",
price: 100,
training: 100,
},
{
tokenId: 2,
name: "Machina",
imageUrl:
"https://ipfs.io/ipfs/QmVCYBq5Sasjp99MD6YMKp7xJwSxpxXjTCf85ekKaReFng",
brain: "https://www.google.com",
description: "An upgraded bot",
price: 100,
training: 500,
},
{
tokenId: 3,
name: "Beast",
imageUrl:
"https://ipfs.io/ipfs/QmSjfLd4h7PzJQ9RARUpr4z5918qZ4ziK9RFUnNGLefsR1",
brain: "https://www.google.com",
description: "A battle hardened bot",
price: 100,
training: 1000,
},
];

const miner = null;

export default function NftList({ nftData }) {
export default function NftList({ nftData, onTrain, mineLink }) {
return (
<ul
role="list"
Expand Down Expand Up @@ -75,9 +40,9 @@ export default function NftList({ nftData }) {
<div className="-mt-px flex divide-x divide-gray-200">
<div className="flex w-0 flex-1">
<a
href={mineLink}
className="relative -mr-px inline-flex w-0 flex-1 items-center justify-center gap-x-3 rounded-bl-lg border border-transparent py-4 text-sm font-semibold text-gray-900"
onClick={(e) => {
e.preventDefault();
console.log(
"Mine Clicked",
nft.tokenId
Expand All @@ -94,13 +59,7 @@ export default function NftList({ nftData }) {
<div className="-ml-px flex w-0 flex-1">
<a
className="relative inline-flex w-0 flex-1 items-center justify-center gap-x-3 rounded-br-lg border border-transparent py-4 text-sm font-semibold text-gray-900"
onClick={(e) => {
e.preventDefault();
console.log(
"Train Clicked",
nft.tokenId
);
}}
onClick={onTrain}
>
<ArrowTrendingUpIcon
className="h-5 w-5 text-gray-400"
Expand Down
149 changes: 149 additions & 0 deletions client/src/RunGameScreen1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
import React, { useState, useEffect } from "react";
import Mine from "./Mine";

export default function RunGameScreen1() {
const [count, setCount] = useState(0);
const [row, setRow] = useState(0);
const [col, setCol] = useState(0);

const Action = {
UP: 0,
DOWN: 1,
LEFT: 2,
RIGHT: 3,
};

const [qTable, setQTable] = useState(createQTable(5, 5, 4));

console.log(qTable);
function createQTable(rows, cols, numActions) {
// Initialize the Q-table as a 3D array
const qTable = new Array(rows)
.fill(null)
.map(() =>
new Array(cols)
.fill(null)
.map(() => new Array(numActions).fill(0))
);

// Set higher values for the DOWN action
for (let row = 0; row < rows; row++) {
for (let col = 0; col < cols; col++) {
qTable[row][col][Action.DOWN] = 1 + 10; // Higher values for DOWN
qTable[row][col][Action.UP] = 1;
qTable[row][col][Action.LEFT] = 1;
qTable[row][col][Action.RIGHT] = 1;
}
}

return qTable;
}

function createQTable2(rows, cols, numActions) {
// Initialize the Q-table as a 3D array
const qTable = new Array(rows)
.fill(null)
.map(() =>
new Array(cols)
.fill(null)
.map(() => new Array(numActions).fill(0))
);

// Set higher values for the DOWN action
for (let row = 0; row < rows; row++) {
for (let col = 0; col < cols; col++) {
if (col === 3) {
qTable[row][col][Action.DOWN] = 1 + 30; // Higher values for DOWN
} else {
qTable[row][col][Action.DOWN] = 1;
}

qTable[row][col][Action.UP] = 1;
qTable[row][col][Action.LEFT] = 1;

qTable[row][col][Action.RIGHT] = 1 + 10;
}
}

console.log("QTable: ", qTable);
return qTable;
}

function createQTable3(rows, cols, numActions) {
// Initialize the Q-table as a 3D array
const qTable = new Array(rows)
.fill(null)
.map(() =>
new Array(cols)
.fill(null)
.map(() => new Array(numActions).fill(0))
);

// Set higher values for the DOWN action
for (let row = 0; row < rows; row++) {
for (let col = 0; col < cols; col++) {
qTable[row][col][Action.DOWN] = 1 + 10; // Higher values for DOWN
qTable[row][col][Action.UP] = 1;
qTable[row][col][Action.LEFT] = 1;
qTable[row][col][Action.RIGHT] = 1 + 29;
}
}

return qTable;
}

function getBestAction() {
const actions = qTable[row][col];
let bestAction = 0;
let highestValue = actions[0];

for (let action = 1; action < actions.length; action++) {
if (actions[action] > highestValue) {
highestValue = actions[action];
bestAction = action;
}
}

console.log("Actions: ", actions);
console.log("Best Action: ", bestAction);
return bestAction;
}

useEffect(() => {
//Implementing the setInterval method
const interval = setInterval(() => {
const action = getBestAction();
step(action);
const output = JSON.stringify(qTable);
console.log(output);
}, 1000);

//Clearing the interval
return () => clearInterval(interval);
}, [count]);

const step = (action) => {
switch (action) {
case Action.RIGHT:
setCol((col) => Math.min(col + 1, 4));
break;
case Action.LEFT:
setCol((col) => Math.max(col - 1, 0));
break;
case Action.DOWN:
setRow((row) => Math.min(row + 1, 4));
break;
case Action.UP:
setRow((row) => Math.max(row - 1, 0));
break;
}
};

return (
<div className="overflow-hidden rounded-lg bg-white shadow">
<div className="px-4 py-5 sm:p-6">
<Mine row={row} col={col} goal_row={0} goal_col={4} />
</div>
</div>
);
}
5 changes: 2 additions & 3 deletions client/src/RunGameScreen.js → client/src/RunGameScreen2.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, { useState, useEffect } from "react";
import Mine from "./Mine";

export default function RunGameScreen() {
export default function RunGameScreen2() {
const [count, setCount] = useState(0);
const [row, setRow] = useState(0);
const [col, setCol] = useState(0);
Expand Down Expand Up @@ -142,8 +142,7 @@ export default function RunGameScreen() {
return (
<div className="overflow-hidden rounded-lg bg-white shadow">
<div className="px-4 py-5 sm:p-6">
<h1>{count}</h1>
<Mine row={row} col={col} goal_row={4} goal_col={3} />
<Mine row={row} col={col} goal_row={0} goal_col={4} />
</div>
</div>
);
Expand Down
7 changes: 0 additions & 7 deletions client/src/Scaffold.js
Original file line number Diff line number Diff line change
Expand Up @@ -250,13 +250,6 @@ export default function Scaffold({ children }) {
</Disclosure>

<div className="py-10">
<header>
<div className="mx-auto max-w-7xl px-4 sm:px-6 lg:px-8">
<h1 className="text-3xl font-bold leading-tight tracking-tight text-gray-900">
Dashboard
</h1>
</div>
</header>
<main>
<div className="mx-auto max-w-7xl sm:px-6 lg:px-8">
{children}
Expand Down
15 changes: 12 additions & 3 deletions client/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,27 @@ import "./index.css";
import Scaffold from "./Scaffold";
import Mine from "./Mine";
import Home from "./Home";
import RunGameScreen from "./RunGameScreen";
import RunGameScreen1 from "./RunGameScreen1";
import RunGameScreen2 from "./RunGameScreen2";

const router = createBrowserRouter([
{
path: "/",
element: <Home />,
},
{
path: "mine",
path: "mine1",
element: (
<Scaffold>
<RunGameScreen />
<RunGameScreen1 />
</Scaffold>
),
},
{
path: "mine2",
element: (
<Scaffold>
<RunGameScreen2 />
</Scaffold>
),
},
Expand Down
Loading

0 comments on commit 54b2736

Please sign in to comment.