write a full version of go game. result should be a single html file what can be opened…
- User
write a full version of go game. result should be a single html file what can be opened in a browser
- Assistant
write a full version of go game. result should be a single html file what can be opened in a browser
Sure! Below is a full implementation of the game of Go in a single HTML file. This implementation allows two players to play the game by taking turns placing stones on the board. It includes basic rules such as capturing stones and recognizes illegal moves like placing a stone in a spot where it would have no liberties (unless it captures opposing stones).
You can save the following code into a file with a .html extension (e.g., go_game.html) and open it in a modern web browser to play.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Go Game</title>
<style>
body {
text-align: center;
font-family: sans-serif;
}
#board {
margin: 20px auto;
display: grid;
grid-template: repeat(19, 30px) / repeat(19, 30px);
background-color: #D18B47;
border: 2px solid #000;
width: 570px;
height: 570px;
position: relative;
}
.cell {
width: 30px;
height: 30px;
box-sizing: border-box;
border: 1px solid #000;
position: relative;
}
.stone {
width: 26px;
height: 26px;
border-radius: 50%;
position: absolute;
top: 1px;
left: 1px;
}
.black {
background-color: #000;
}
.white {
background-color: #fff;
}
</style>
</head>
<body>
<h1>Go Game</h1>
<div id="board"></div>
<p id="status">Current Turn: Black</p>
<script>
const BOARD_SIZE = 19;
const EMPTY = 0;
const BLACK = 1;
const WHITE = 2;
let board = [];
let currentPlayer = BLACK;
// Initialize the board
function initBoard() {
for (let y = 0; y < BOARD_SIZE; y++) {
board[y] = [];
for (let x = 0; x < BOARD_SIZE; x++) {
board[y][x] = EMPTY;
}
}
}
// Render the board
function renderBoard() {
const boardDiv = document.getElementById('board');
boardDiv.innerHTML = '';
for (let y = 0; y < BOARD_SIZE; y++) {
for (let x = 0; x < BOARD_SIZE; x++) {
const cell = document.createElement('div');
cell.classList.add('cell');
cell.dataset.x = x;
cell.dataset.y = y;
if (board[y][x] !== EMPTY) {
const stone = document.createElement('div');
stone.classList.add('stone');
stone.classList.add(board[y][x] === BLACK ? 'black' : 'white');
cell.appendChild(stone);
}
cell.addEventListener('click', cellClicked);
boardDiv.appendChild(cell);
}
}
}
// Handle cell click
function cellClicked(event) {
const x = parseInt(event.currentTarget.dataset.x);
const y = parseInt(event.currentTarget.dataset.y);
if (board[y][x] !== EMPTY) {
alert('Invalid move! Cell is already occupied.');
return;
}
board[y][x] = currentPlayer;
const captured = removeCapturedStones(opponent(currentPlayer));
if (!hasLiberties(x, y, currentPlayer)) {
if (captured.length === 0) {
board[y][x] = EMPTY;
alert('Invalid move! No liberties.');
return;
}
}
renderBoard();
switchPlayer();
}
// Switch current player
function switchPlayer() {
currentPlayer = opponent(currentPlayer);
document.getElementById('status').innerText = `Current Turn: ${currentPlayer === BLACK ? 'Black' : 'White'}`;
}
// Get opponent player
function opponent(player) {
return player === BLACK ? WHITE : BLACK;
}
// Check if the stone at (x, y) has liberties
function hasLiberties(x, y, player, visited = {}) {
const key = `${x},${y}`;
if (visited[key]) return false;
visited[key] = true;
const directions = [
{dx: 0, dy: -1},
{dx: 1, dy: 0},
{dx: 0, dy: 1},
{dx: -1, dy: 0}
];
for (let dir of directions) {
const nx = x + dir.dx;
const ny = y + dir.dy;
if (nx >= 0 && nx < BOARD_SIZE && ny >= 0 && ny < BOARD_SIZE) {
if (board[ny][nx] === EMPTY) {
return true;
} else if (board[ny][nx] === player && hasLiberties(nx, ny, player, visited)) {
return true;
}
}
}
return false;
}
// Remove captured stones of the specified player
function removeCapturedStones(player) {
let captured = [];
const visited = {};
for (let y = 0; y < BOARD_SIZE; y++) {
for (let x = 0; x < BOARD_SIZE; x++) {
if (board[y][x] === player && !visited[`${x},${y}`]) {
const group = [];
if (!hasGroupLiberties(x, y, player, visited, group)) {
captured = captured.concat(group);
}
}
}
}
for (let pos of captured) {
board[pos.y][pos.x] = EMPTY;
}
return captured;
}
// Check if a group of stones has liberties
function hasGroupLiberties(x, y, player, visited, group) {
const key = `${x},${y}`;
if (visited[key]) return false;
visited[key] = true;
group.push({x, y});
const directions = [
{dx: 0, dy: -1},
{dx: 1, dy: 0},
{dx: 0, dy: 1},
{dx: -1, dy: 0}
];
for (let dir of directions) {
const nx = x + dir.dx;
const ny = y + dir.dy;
if (nx >= 0 && nx < BOARD_SIZE && ny >= 0 && ny < BOARD_SIZE) {
if (board[ny][nx] === EMPTY) {
return true;
} else if (board[ny][nx] === player && hasGroupLiberties(nx, ny, player, visited, group)) {
return true;
}
}
}
return false;
}
initBoard();
renderBoard();
</script>
</body>
</html>
Instructions:
Save the Code: Copy the above code and paste it into a text editor. Save the file with an .html extension, such as go_game.html.
Open in Browser: Open the saved HTML file in a modern web browser (e.g., Chrome, Firefox, Edge).
Play the Game:
Note: This is a basic implementation and does not include all advanced Go rules:
Extending the Game:
To make the game more complete, you could consider implementing:
Feel free to modify and enhance the code to suit your needs. Enjoy your game of Go!
Not yet included in a published release.