佈署你的程式到Infinity free host
步驟一:
準備好你的程式,例:井字遊戲(單一個檔案,含html, css, JavaScript),將這個檔案放在你電腦上的資料夾。
index.html
<!DOCTYPE html>
<html lang="zh-TW">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Tic-Tac-Toe 井字遊戲</title>
<style>
body {
font-family: Arial, sans-serif;
text-align: center;
background-color: #f4f4f4;
}
h1 {
color: #333;
}
.game-container {
display: flex;
flex-direction: column;
align-items: center;
margin-top: 20px;
}
#board {
display: grid;
grid-template-columns: repeat(3, 100px);
grid-template-rows: repeat(3, 100px);
gap: 5px;
}
.cell {
width: 100px;
height: 100px;
display: flex;
align-items: center;
justify-content: center;
font-size: 2em;
background-color: white;
border: 2px solid #000;
cursor: pointer;
}
.cell.x {
color: green;
}
.cell.o {
color: darkred;
}
.cell:hover {
background-color: #ddd;
}
#status {
font-size: 1.2em;
margin-top: 10px;
}
#reset {
margin-top: 10px;
padding: 10px 20px;
font-size: 1em;
background-color: #007bff;
color: white;
border: none;
cursor: pointer;
border-radius: 5px;
}
#reset:hover {
background-color: #0056b3;
}
</style>
</head>
<body>
<h1>Tic-Tac-Toe 井字遊戲</h1>
<div class="game-container">
<div id="board">
<div class="cell" data-index="0"></div>
<div class="cell" data-index="1"></div>
<div class="cell" data-index="2"></div>
<div class="cell" data-index="3"></div>
<div class="cell" data-index="4"></div>
<div class="cell" data-index="5"></div>
<div class="cell" data-index="6"></div>
<div class="cell" data-index="7"></div>
<div class="cell" data-index="8"></div>
</div>
<p id="status">玩家 X 的回合</p>
<button id="reset">重新開始</button>
</div>
<script>
const board = document.getElementById("board");
const statusText = document.getElementById("status");
const resetButton = document.getElementById("reset");
const cells = document.querySelectorAll(".cell");
let currentPlayer = "X";
let boardState = ["", "", "", "", "", "", "", "", ""];
let gameActive = true;
const winPatterns = [
[0, 1, 2], [3, 4, 5], [6, 7, 8], // 橫排
[0, 3, 6], [1, 4, 7], [2, 5, 8], // 直排
[0, 4, 8], [2, 4, 6] // 斜線
];
// 監聽玩家點擊格子
cells.forEach(cell => {
cell.addEventListener("click", handleCellClick);
});
function handleCellClick(event) {
const index = event.target.dataset.index;
if (boardState[index] !== "" || !gameActive) return;
boardState[index] = currentPlayer;
event.target.textContent = currentPlayer;
event.target.classList.add(currentPlayer === "X" ? "x" : "o");
if (checkWin(currentPlayer)) {
statusText.textContent = `玩家 ${currentPlayer} 獲勝!`;
gameActive = false;
return;
}
if (boardState.every(cell => cell !== "")) {
statusText.textContent = "平局!";
gameActive = false;
return;
}
currentPlayer = currentPlayer === "X" ? "O" : "X";
statusText.textContent = `玩家 ${currentPlayer} 的回合`;
}
function checkWin(player) {
return winPatterns.some(pattern =>
pattern.every(index => boardState[index] === player)
);
}
// 重新開始遊戲
resetButton.addEventListener("click", () => {
boardState = ["", "", "", "", "", "", "", "", ""];
gameActive = true;
currentPlayer = "X";
statusText.textContent = "玩家 X 的回合";
cells.forEach(cell => {
cell.textContent = "";
cell.classList.remove("x", "o");
});
});
</script>
</body>
</html>
步驟二:
打開Infinity free host,你的網域,網址的File manager (檔案總管)

進入你網域的管理畫面,並點擊File Manager (網域上的檔案總管,顧名思義就是用來做檔案與目的操作。)

下面的畫面就你網域的網頁根目錄,htdocs是網頁根目錄,記住這個重要的特性 (Linux作業系統上的Web伺服器都是用這個名稱當網頁的根目錄名稱),這個目錄放所有你要讓使用者存取的網頁資料(資料夾與檔案)

在這個畫面,你可以點選目錄進入畫面,或點選 “..”回到上一層目錄。
按滑鼠右鍵可以叫出新增檔案 (New File) 及資料夾 (New Folder) 的彈出視窗:

通常,我們會建立一些目錄,免得一堆檔案,最後多到雜亂不好管理。
舉例,建一個xo的目錄,將index.html放入xo資料夾中:
你可以用拖拉的方式將本地端的檔案拉到網站上(但我測試常失敗,這免費空間有點頭痛,另外一種方式就是用新增檔案的方式,手動將程式貼上去。

將程式碼貼上,儲存 (Save):


一旦你程式準備好,你程式的網址為:
無ssl𠯘本(不安全) http://你的網域/xo/
有SSL版本 (安全) https://你的網域/xo/
上面的htdocs是預設的網頁根目錄 “/”,不需要出現在網址上。
以上面的例子,網址是:
https://fg.infinityfreeapp.com/xo
index.html省略不打 (因是是大家講好的,首頁檔案名稱,預設會抓取的)

上面的/?i=1是infinity這個網站主機產生的 (怎麼產生的,還要再了解,但這不重要),不需要給使用者這個。


