-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
239 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { GoogleGenerativeAI } from '@google/generative-ai'; | ||
import { useSearchParams } from 'next/navigation'; | ||
|
||
const api = String(process.env.GOOGLE_API_KEY); | ||
|
||
const genAI = new GoogleGenerativeAI(api); | ||
|
||
export async function POST(request: Request) { | ||
const gameRules = ` | ||
This is a Connect 4 game. | ||
1. The game is played on a grid that's 6 cells by 7 cells. | ||
2. Two players take turns. | ||
3. The pieces fall straight down, occupying the lowest available space within the column. | ||
4. The objective of the game is to connect four of one's own discs of the same color next to each other vertically, horizontally, or diagonally before your opponent. | ||
5. The game ends in a tie if the entire board is filled with discs and no player has won. | ||
You are playing as Yellow and your opponent is Red. Suggest the next move in this format: y (just the number of the column). | ||
` | ||
|
||
|
||
const { grid } = await request.json() | ||
|
||
const prompt = grid + gameRules; | ||
|
||
if (!prompt) { | ||
return new Response(JSON.stringify({ error: 'No prompt provided' })); | ||
} | ||
|
||
try { | ||
const model = genAI.getGenerativeModel({ model: 'gemini-pro' }); | ||
const result = await model.generateContent(prompt); | ||
const response = await result.response; | ||
const text = response.text(); | ||
console.log(text); | ||
return new Response(JSON.stringify({ move: text })); | ||
} catch (error) { | ||
console.error(error); | ||
return new Response(JSON.stringify({ error })); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
function findBestMove(grid: (string | null)[][]): number | null { | ||
// Placeholder for our player's identifier, e.g., "Red" or "Yellow" | ||
const ourPlayer = "Red"; | ||
const opponent = "Yellow"; // Assuming a two-player game | ||
|
||
// Strategy 1: Check if we need to block the opponent's winning move | ||
let blockingMove = findWinningMove(grid, opponent); | ||
if (blockingMove !== null) { | ||
return blockingMove; | ||
} | ||
|
||
// Strategy 2: Check if we can win next | ||
let winningMove = findWinningMove(grid, ourPlayer); | ||
if (winningMove !== null) { | ||
return winningMove; | ||
} | ||
|
||
// Strategy 3: Naïve approach - just pick the first non-full column | ||
for (let col = 0; col < grid[0].length; col++) { | ||
if (grid[0][col] === null) { | ||
return col; | ||
} | ||
} | ||
|
||
// No moves available | ||
return null; | ||
} | ||
|
||
function checkWin(grid: (string | null)[][], player: string): boolean { | ||
// Check horizontal, vertical, and diagonal win conditions | ||
// This is a simple implementation and can be optimized | ||
for (let row = 0; row < grid.length; row++) { | ||
for (let col = 0; col < grid[0].length; col++) { | ||
if ( | ||
// Horizontal | ||
(grid[row][col] === player && | ||
grid[row][col + 1] === player && | ||
grid[row][col + 2] === player && | ||
grid[row][col + 3] === player) || | ||
// Vertical | ||
(grid[row][col] === player && | ||
grid[row + 1]?.[col] === player && | ||
grid[row + 2]?.[col] === player && | ||
grid[row + 3]?.[col] === player) || | ||
// Diagonal / | ||
(grid[row][col] === player && | ||
grid[row + 1]?.[col + 1] === player && | ||
grid[row + 2]?.[col + 2] === player && | ||
grid[row + 3]?.[col + 3] === player) || | ||
// Diagonal \ | ||
(grid[row][col] === player && | ||
grid[row + 1]?.[col - 1] === player && | ||
grid[row + 2]?.[col - 2] === player && | ||
grid[row + 3]?.[col - 3] === player) | ||
) { | ||
return true; | ||
} | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
function findWinningMove(grid: (string | null)[][], player: string): number | null { | ||
// Create a copy of the grid to test potential moves | ||
let testGrid = JSON.parse(JSON.stringify(grid)); | ||
|
||
// Check each column to see if making a move there would result in a win | ||
for (let col = 0; col < testGrid[0].length; col++) { | ||
for (let row = testGrid.length - 1; row >= 0; row--) { | ||
if (testGrid[row][col] === null) { | ||
testGrid[row][col] = player; | ||
if (checkWin(testGrid, player)) { | ||
return col; | ||
} | ||
// Undo the move | ||
testGrid[row][col] = null; | ||
break; | ||
} | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
|
||
export async function POST(request: Request) { | ||
const { grid } = await request.json() | ||
|
||
if (!grid) { | ||
return new Response(JSON.stringify({ error: 'No grid provided' })); | ||
} | ||
|
||
try { | ||
const move = findBestMove(grid); | ||
console.log(move); | ||
return new Response(JSON.stringify({ move })); | ||
} catch (error) { | ||
console.error(error); | ||
return new Response(JSON.stringify({ error })); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters