Skip to content

Instantly share code, notes, and snippets.

View mohsenheydari's full-sized avatar

Mohsen Heydari mohsenheydari

View GitHub Profile
attribute vec4 instancePosition;
attribute float instanceAngle;
varying vec2 vUV;
void main(){
vUV = uv;
vec3 rotatedPosition = position;
rotatedPosition.x = cos( instanceAngle ) * position.x - sin( instanceAngle ) * position.y;
@mohsenheydari
mohsenheydari / mctsbackpropagate.js
Created February 4, 2019 15:22
MCTS - backpropagate method of TreeSearch class
backpropagate(node, result) {
let tmpNode = node;
while(tmpNode !== null) {
tmpNode.state.visits++;
if (result != tmpNode.state.board.DRAW) {
if (tmpNode.state.player === result) {
tmpNode.state.wins++;
}
@mohsenheydari
mohsenheydari / mctssimulate.js
Created February 4, 2019 15:09
MCTS - simulate method of TreeSearch class
simulate(node) {
let state = node.state.clone(); //Temp state
state.board = node.state.board.clone();
//Play randomly until one player wins or game result in draw
while (!state.isTerminal) {
state.switchToNextPlayer();
let action = state.getRandomAction(state.player);
state.applyAction(action);
@mohsenheydari
mohsenheydari / selectpromisingchild.js
Created February 4, 2019 14:49
MCTS - Select promising child function in TreeSearch class
selectPromisingChild(node) {
while(!node.state.isTerminal && node.isFullyExpanded) {
node = this.uct.findBestChild(node);
}
return node;
}
@mohsenheydari
mohsenheydari / findnextaction.js
Created February 4, 2019 14:33
MCTS - function to find best next action for current state and player
findNextAction(board, player, iterations) {
let root = new Node();
root.state = new NodeState();
root.state.board = board;
root.state.player = 3 - player;//Opponent player is owner of root node
for (let i=0; i < iterations; i++) {
//1.Selection
let node = this.selectPromisingChild(root);
@mohsenheydari
mohsenheydari / gameboardrenderer.js
Created January 8, 2019 01:56
Tic Tac Toe board drawing class
'use strict';
define(function () {
class GmaeBoardRenderer{
constructor(canvas, canvasSize){
this.canvas = canvas;
this.ctx = canvas.getContext('2d');
this.canvas.width = canvasSize;
this.canvas.height = canvasSize;
@mohsenheydari
mohsenheydari / tic-tac-toe-simple-main.js
Created January 8, 2019 01:39
Simple Tic Tac Toe GmaeBoardRenderer class usage
requirejs(['gameboardrenderer'],
function (GmaeBoardRenderer) {
let player = 1;
let boardState = [0,0,0,0,0,0,0,0,0]; //Initial board state
let canvas = document.getElementById('board');
let gbr = new GmaeBoardRenderer(canvas, 500);
//Render initial state (grid)
gbr.render(boardState);
@mohsenheydari
mohsenheydari / ttt-find-clicked-cell-position.js
Created January 8, 2019 01:37
Converts clicked coordinates to cell index in Tic Tac Toe board
getCellIndex(position){
for (let x = 0;x < 3;x++) {
for (let y = 0;y < 3;y++) {
let xCordinate = x * this.cellSize;
let yCordinate = y * this.cellSize;
if (
position.x >= xCordinate && position.x <= xCordinate + this.cellSize &&
position.y >= yCordinate && position.y <= yCordinate + this.cellSize
@mohsenheydari
mohsenheydari / relative-cursor-position.js
Created January 8, 2019 01:36
Transforms document cursor position to position relative to canvas
getRelativeCursorPosition(event){
var rect = this.canvas.getBoundingClientRect();
return {
x: event.clientX - rect.left,
y: event.clientY - rect.top
}
}
@mohsenheydari
mohsenheydari / draw-ttt-gird.js
Created January 8, 2019 01:31
Draw Tic Tac Toe grid lines
drawGrid(){
let offset = 5;
let lineLenght = this.canvas.width - offset;
this.ctx.lineWidth = this.gridLineWidth;
this.ctx.lineCap = 'round';
this.ctx.strokeStyle = this.gridLineStyle;
this.ctx.beginPath();
//Horizontal lines
for (let y = 1;y <= 2;y++) {