index.html
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>오셀로 게임</title>
<link rel="stylesheet" href="style.css">
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
</head>
<body>
<div class="container">
<h1>오셀로 게임</h1>
<div id="board"></div>
<div id="status"></div>
<div id="score">
<span id="black-score">흑: 2</span>
<span id="white-score">백: 2</span>
</div>
<button id="new-game">새 게임</button> <button id="how-to-play">게임 방법</button>
</div>
<!-- 게임 방법 모달 -->
<div id="modal" class="modal">
<div class="modal-content">
<span class="close">×</span>
<h2>오셀로 게임 방법</h2>
<ol>
<li>8x8 크기의 보드에서 진행됩니다.</li>
<li>게임 시작 시 중앙에 흑백 각 2개의 돌이 교차로 놓입니다.</li>
<li>흑이 먼저 시작하며, 플레이어는 번갈아가며 돌을 놓습니다.</li>
<li>돌은 상대방의 돌을 양쪽에서 감싸서 뒤집을 수 있는 위치에만 놓을 수 있습니다.</li>
<li>돌을 놓으면 상대방의 돌을 뒤집습니다.</li>
<li>더 이상 놓을 수 있는 위치가 없으면 차례를 넘깁니다.</li>
<li>양쪽 모두 돌을 놓을 수 없게 되면 게임이 종료됩니다.</li>
<li>게임 종료 시 더 많은 돌을 가진 플레이어가 승리합니다.</li>
</ol>
</div>
</div>
<script src="script.js"></script>
</body>
</html>
style.css
body {
font-family: Arial, sans-serif;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
background-color: #f0f0f0;
}
.container {
text-align: center;
}
#board {
display: grid;
grid-template-columns: repeat(8, 50px);
grid-gap: 2px;
margin: 20px auto;
background-color: #008000;
padding: 10px;
border-radius: 5px;
}
.cell {
width: 50px;
height: 50px;
background-color: #008000;
border: 1px solid #006400;
border-radius: 50%;
cursor: pointer;
}
.black {
background-color: black;
}
.white {
background-color: white;
}
#status, #score {
margin: 10px 0;
font-size: 18px;
}
#score span {
margin: 0 10px;
}
#new-game {
margin-top: 20px;
font-size: 16px;
padding: 10px 20px;
}
#how-to-play {
margin-top: 20px;
font-size: 16px;
padding: 10px 20px;
}
/* 기존 CSS 코드 유지 */
/* 모달 스타일 */
.modal {
display: none;
position: fixed;
z-index: 1;
left: 0;
top: 0;
width: 100%;
height: 100%;
overflow: auto;
background-color: rgba(0,0,0,0.4);
}
.modal-content {
background-color: #fefefe;
margin: 15% auto;
padding: 20px;
border: 1px solid #888;
width: 80%;
max-width: 600px;
}
.close {
color: #aaa;
float: right;
font-size: 28px;
font-weight: bold;
cursor: pointer;
}
.close:hover,
.close:focus {
color: black;
text-decoration: none;
cursor: pointer;
}
#how-to-play {
margin-bottom: 10px;
font-size: 16px;
padding: 5px 10px;
}
script.js
$(document).ready(function() {
const BOARD_SIZE = 8;
let board = [];
let currentPlayer = 'black';
function initGame() {
board = Array(BOARD_SIZE).fill().map(() => Array(BOARD_SIZE).fill(null));
board[3][3] = board[4][4] = 'white';
board[3][4] = board[4][3] = 'black';
currentPlayer = 'black';
renderBoard();
updateStatus();
updateScore();
}
function renderBoard() {
$('#board').empty();
for (let i = 0; i < BOARD_SIZE; i++) {
for (let j = 0; j < BOARD_SIZE; j++) {
const cell = $('<div>').addClass('cell').attr('data-row', i).attr('data-col', j);
if (board[i][j]) {
cell.addClass(board[i][j]);
}
$('#board').append(cell);
}
}
}
function updateStatus() {
$('#status').text(`현재 차례: ${currentPlayer === 'black' ? '흑' : '백'}`);
}
function updateScore() {
const blackCount = board.flat().filter(cell => cell === 'black').length;
const whiteCount = board.flat().filter(cell => cell === 'white').length;
$('#black-score').text(`흑: ${blackCount}`);
$('#white-score').text(`백: ${whiteCount}`);
}
function isValidMove(row, col) {
if (board[row][col]) return false;
const directions = [[-1,-1],[-1,0],[-1,1],[0,-1],[0,1],[1,-1],[1,0],[1,1]];
return directions.some(([dx, dy]) => {
let x = row + dx, y = col + dy;
if (x < 0 || x >= BOARD_SIZE || y < 0 || y >= BOARD_SIZE || board[x][y] !== getOpponent()) return false;
while (x >= 0 && x < BOARD_SIZE && y >= 0 && y < BOARD_SIZE) {
if (!board[x][y]) return false;
if (board[x][y] === currentPlayer) return true;
x += dx; y += dy;
}
return false;
});
}
function getOpponent() {
return currentPlayer === 'black' ? 'white' : 'black';
}
function flipDiscs(row, col) {
const directions = [[-1,-1],[-1,0],[-1,1],[0,-1],[0,1],[1,-1],[1,0],[1,1]];
directions.forEach(([dx, dy]) => {
let x = row + dx, y = col + dy;
let toFlip = [];
while (x >= 0 && x < BOARD_SIZE && y >= 0 && y < BOARD_SIZE && board[x][y] === getOpponent()) {
toFlip.push([x, y]);
x += dx; y += dy;
}
if (x >= 0 && x < BOARD_SIZE && y >= 0 && y < BOARD_SIZE && board[x][y] === currentPlayer) {
toFlip.forEach(([fx, fy]) => board[fx][fy] = currentPlayer);
}
});
}
function makeMove(row, col) {
if (!isValidMove(row, col)) return false;
board[row][col] = currentPlayer;
flipDiscs(row, col);
currentPlayer = getOpponent();
renderBoard();
updateStatus();
updateScore();
if (!hasValidMove()) {
currentPlayer = getOpponent();
updateStatus();
if (!hasValidMove()) {
endGame();
}
}
return true;
}
function hasValidMove() {
for (let i = 0; i < BOARD_SIZE; i++) {
for (let j = 0; j < BOARD_SIZE; j++) {
if (isValidMove(i, j)) return true;
}
}
return false;
}
function endGame() {
const blackCount = board.flat().filter(cell => cell === 'black').length;
const whiteCount = board.flat().filter(cell => cell === 'white').length;
let winner;
if (blackCount > whiteCount) winner = '흑';
else if (whiteCount > blackCount) winner = '백';
else winner = '무승부';
$('#status').text(`게임 종료! 승자: ${winner}`);
}
$('#board').on('click', '.cell', function() {
const row = $(this).data('row');
const col = $(this).data('col');
makeMove(row, col);
});
$('#new-game').click(initGame);
initGame();
});
$(document).ready(function() {
// 기존 JavaScript 코드 유지
// 모달 관련 코드
const modal = $('#modal');
const btn = $('#how-to-play');
const span = $('.close');
btn.click(function() {
modal.css('display', 'block');
});
span.click(function() {
modal.css('display', 'none');
});
$(window).click(function(event) {
if (event.target == modal[0]) {
modal.css('display', 'none');
}
});
// 기존의 initGame 함수 끝에 다음 코드 추가
function initGame() {
// ... 기존 코드 ...
// 게임 시작 시 모달 표시
modal.css('display', 'block');
}
// 나머지 기존 코드 유지
});
실시간 날씨(기온)확인하기 (0) | 2024.07.02 |
---|---|
벌초지 지도공유 애플리케이션 벌초맵앱 (0) | 2024.06.30 |
3명 이상 모여있다면? 이게임 어때? (0) | 2024.06.25 |
KOREAN POP GAME 심심풀이를 위한 웹게임 (0) | 2024.06.11 |
[게임]토끼와 당근 길찾기 게임 (0) | 2024.06.03 |