WBE_Praktikum_7/code/public/connect4.html

195 lines
4.6 KiB
HTML
Raw Normal View History

2022-11-24 11:10:59 +01:00
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Vier gewinnt</title>
<link rel="stylesheet" href="styles.css">
2022-11-24 21:06:23 +01:00
<script src="connect4-winner.js"></script>
2022-11-24 11:10:59 +01:00
<script>
/*
* This solution sould be considered as a proof of concept the code
* definitely needs some cleanup and documentation
*/
2022-11-24 21:06:23 +01:00
let datakey = ''
2022-11-24 11:10:59 +01:00
let state = {
board: [
[ '', '', '', '', '', '', '' ],
[ '', '', '', '', '', '', '' ],
[ '', '', '', '', '', '', '' ],
[ '', '', '', '', '', '', '' ],
[ '', '', '', '', '', '', '' ],
[ '', '', '', '', '', '', '' ]
],
2022-11-24 21:06:23 +01:00
next: 'blue'
2022-11-24 11:10:59 +01:00
}
2022-11-24 21:06:23 +01:00
const url = "http://localhost:3000/"
2022-11-24 11:10:59 +01:00
const SERVICE = "http://localhost:3000/api/data/c4state?api-key=c4game"
// Initialize game
//
function initGame () {
2022-11-24 21:06:23 +01:00
console.log("initializing game")
fetch(url + "api/data/" + datakey + "?api-key=c4game", {
method: 'POST',
headers: { 'Content-type': 'application/json' },
body: JSON.stringify(state)
}).then(response => response.json())
.then(data => {
datakey = data.id
})
2022-11-24 11:10:59 +01:00
let board = showBoard()
attachEventHandler(board)
}
// Show board
//
function showBoard () {
2022-11-24 21:06:23 +01:00
console.log("showing board")
let actualPlayerText = document.getElementById("actualPlayer")
if(actualPlayerText){
actualPlayerText.innerText = state.next
}
2022-11-24 11:10:59 +01:00
let board = document.querySelector(".board")
// first remove all fields
while (board.firstChild) { board.removeChild(board.firstChild) }
// your implementation
2022-11-24 21:06:23 +01:00
for(row = 0; row < state.board.length; row++) {
for(column = 0; column < state.board[0].length; column++) {
let attributes = {"id": column.toString() + ":" + row.toString(), "class": "field"}
let children = elt("div", {"class": state.board[row][column] + " piece"})
board.append(elt("div", attributes, children))
}
}
2022-11-24 11:10:59 +01:00
return board
}
// Helper function for DOM manipulation
//
function elt (type, attrs, ...children) {
let node = document.createElement(type)
for (a in attrs) {
node.setAttribute(a, attrs[a])
}
for (let child of children) {
if (typeof child != "string") node.appendChild(child)
else node.appendChild(document.createTextNode(child))
}
return node
}
// Attach event handler to board
//
function attachEventHandler (board) {
2022-11-24 21:06:23 +01:00
console.log("attaching Eventhandler")
2022-11-24 11:10:59 +01:00
// your implementation
2022-11-24 21:06:23 +01:00
board.addEventListener("click", event => {
let field = event.target
if(field.classList.contains("piece")){
field = field.parentNode
}
let column = Number(field.id.split(":")[0])
console.log("calculated column")
setColumn(column)
showBoard()
2022-11-24 11:10:59 +01:00
})
}
2022-11-24 21:06:23 +01:00
function setColumn(column) {
console.log("setting Column")
let row = state.board.length - 1
const column_number = column
while(row >= 0){
if(state.board[row][column_number] !== ''){
row--
} else {
if(state.next === ''){
console.log("not setting Field becuase there is a winner.")
} else {
setField(row, column_number)
}
break
}
}
}
function setField(row, column) {
state.board[row][column] = state.next
showBoard()
if(connect4Winner(state.next, state.board)){
document.getElementById("state-text").innerText = "Player " + state.next + " won!"
state.next = ''
} else {
switchNextColor()
}
}
function switchNextColor() {
if(state.next === "red") {
state.next = "blue"
} else {
state.next = "red"
}
}
2022-11-24 11:10:59 +01:00
// Get current state from server and re-draw board
//
function loadState () {
// ...
// your implementation
// ...
2022-11-24 21:06:23 +01:00
console.log("loading State")
fetch(url + "api/data/" + datakey + "?api-key=c4game", {
method: 'GET',
headers: { 'Content-type': 'application/json' }
}).then(response => response.json())
.then(data => {
state = data
showBoard()
})
2022-11-24 11:10:59 +01:00
}
// Put current state to server
//
function saveState () {
2022-11-24 21:06:23 +01:00
console.log("saving state")
fetch(url + "api/data/" + datakey + "?api-key=c4game", {
method: 'PUT',
headers: { 'Content-type': 'application/json' },
body: JSON.stringify(state)
}).then(response => response.json())
.then(data => {
state = data
showBoard()
})
2022-11-24 11:10:59 +01:00
}
2022-11-24 21:06:23 +01:00
</script>
2022-11-24 11:10:59 +01:00
</head>
<body>
<div class="board"></div>
<div class="controls">
<button onclick="loadState()">Load</button>
<button onclick="saveState()">Save</button>
2022-11-24 21:06:23 +01:00
<p id="state-text"><a id="actualPlayer"></a>'s turn</p>
2022-11-24 11:10:59 +01:00
</div>
<script>
initGame()
</script>
</body>
2022-11-24 21:06:23 +01:00
</html>