Skip to content

Commit

Permalink
add static serve
Browse files Browse the repository at this point in the history
  • Loading branch information
rudrOwO committed Mar 18, 2023
1 parent 5b3364b commit d053c68
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 56 deletions.
1 change: 0 additions & 1 deletion back-end/ai/board.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ type Board struct {
saturation uint8 // How much of the board is filled up
}

// ? FIX SEGMENT COUNT BUG
func (board *Board) agentPerformanceEvaluation(piece uint8) int {
var segmentInstances [6]int
performance := 0
Expand Down
11 changes: 0 additions & 11 deletions back-end/ai/game.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,5 @@
package ai

/*
* UI
* board
* static eval
* minimax + pruning
* Utility of move
* Concurrency
? Caching
? Move Ordering
*/

type Response struct {
AIMove uint8 `json:"AIMove"`
IsGameOver bool `json:"isGameOver"`
Expand Down
4 changes: 3 additions & 1 deletion back-end/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ func main() {
r := gin.Default()
r.Use(cors.Default()) // Allow all origins

r.POST("/", func(c *gin.Context) {
r.Static("/", "./dist/")

r.POST("/ai", func(c *gin.Context) {
var grid Request

if err := c.BindJSON(&grid); err != nil {
Expand Down
1 change: 0 additions & 1 deletion front-end/.env

This file was deleted.

84 changes: 42 additions & 42 deletions front-end/src/components/Board.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { Box, Flex, SimpleGrid } from "@chakra-ui/react";
import { useEffect, useRef, useState } from "react";
import Stone, { stoneSize } from "./Stone";
import LineGrid from "./LineGrid";
import BoardLock from "./BoardLock";
import UndoButton from "./UndoButton";
import ClearButton from "./ClearButton";
import { Box, Flex, SimpleGrid } from "@chakra-ui/react"
import { useEffect, useRef, useState } from "react"
import Stone, { stoneSize } from "./Stone"
import LineGrid from "./LineGrid"
import BoardLock from "./BoardLock"
import UndoButton from "./UndoButton"
import ClearButton from "./ClearButton"

export const boardSize = `${(10 + (10 - 1) / 2) * stoneSize}rem`;
const stoneSpacing = `${stoneSize / 2}rem`;
export const boardSize = `${(10 + (10 - 1) / 2) * stoneSize}rem`
const stoneSpacing = `${stoneSize / 2}rem`
export enum StoneIndicator {
EMPTY,
WHITE,
Expand All @@ -16,54 +16,47 @@ export enum StoneIndicator {

const Board = () => {
const [board, setBoard] = useState<StoneIndicator[]>(() => {
const filler = new Array<StoneIndicator>(10 * 10);
const filler = new Array<StoneIndicator>(10 * 10)

for (let i = 0; i < 10 * 10; ++i) {
filler[i] = StoneIndicator.EMPTY;
filler[i] = StoneIndicator.EMPTY
}
return filler;
});
const [isFetching, setIsFetching] = useState(false);
const [isGameOver, setIsGameOver] = useState(false);
const history = useRef<number[]>([]);
return filler
})
const [isFetching, setIsFetching] = useState(false)
const [isGameOver, setIsGameOver] = useState(false)
const history = useRef<number[]>([])

useEffect(() => {
/*
* * Push move to history
* * Update move in board
* * Update isFetching state
* * Handle Game Over
* * Response JSON : {"AIMove": 0,"isGameOver":false}
*/
if (isFetching) {
const fetchAIMove = async () => {
const response = await fetch("http://localhost:5000/", {
const response = await fetch("http://localhost:5000/ai", {
method: "POST",
headers: {
"Content-type": "application/json",
},
body: JSON.stringify({
grid: board,
}),
});
})

const { AIMove, isGameOver } = await response.json();
const { AIMove, isGameOver } = await response.json()

history.current.push(AIMove);
history.current.push(AIMove)

setBoard(prevBoard => {
const mutatedBoard = [...prevBoard];
mutatedBoard[AIMove] = StoneIndicator.WHITE;
return mutatedBoard;
});
setBoard((prevBoard) => {
const mutatedBoard = [...prevBoard]
mutatedBoard[AIMove] = StoneIndicator.WHITE
return mutatedBoard
})

setIsFetching(false);
setIsGameOver(isGameOver);
};
setIsFetching(false)
setIsGameOver(isGameOver)
}

fetchAIMove();
fetchAIMove()
}
}, [isFetching]);
}, [isFetching])

return (
<Box>
Expand All @@ -82,11 +75,18 @@ const Board = () => {
/>
</Flex>
<LineGrid />
<SimpleGrid position="relative" zIndex={1} columns={10} spacing={stoneSpacing}>
<SimpleGrid
position="relative"
zIndex={1}
columns={10}
spacing={stoneSpacing}
>
{board.map((stoneIndicator, i) => (
<Stone
stoneIndicator={
stoneIndicator === StoneIndicator.WHITE ? StoneIndicator.WHITE : StoneIndicator.BLACK
stoneIndicator === StoneIndicator.WHITE
? StoneIndicator.WHITE
: StoneIndicator.BLACK
}
position={i}
key={i}
Expand All @@ -102,7 +102,7 @@ const Board = () => {
{isGameOver && <BoardLock message="Game Over!" />}
</Flex>
</Box>
);
};
)
}

export default Board;
export default Board

0 comments on commit d053c68

Please sign in to comment.