【发布时间】:2019-08-01 09:14:24
【问题描述】:
我正在编写一个黑白棋引擎,我希望能够从服务器上的文件中获取开场书,然后将其作为一个数组分配给黑白棋类的“openings”属性。目前我已将它分配给黑白棋对象的“开口”属性,这是黑白棋类的(唯一)实例。但是,我觉得这似乎是一个糟糕的解决方案,并且必须存在更好的解决方案。
我尝试过使用 this 关键字,但在这种情况下它不引用黑白棋类。
class Reversi {
constructor(dims) {
this.board = new Board(dims)
this.display = new Display(dims)
this.gameRunning = true
this.blackTurn = true
this.missedTurns = 0
this.humanPlayers = []
// this.blackPlayer = document.getElementById("engineSelect").value
this.blackPlayer = "human"
this.redPlayer = "human"
this.aiDelay = 200 //miliseconds
this.freeTiles = dims**2 - 4
this.gameHistory = ""
Object.defineProperty(this, 'openings', {
configurable: true,
writable: true,
value: []
})
}
loadOpenings(callback) {
let xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
callback(this)
}
}
xhttp.open("GET", "books/openings.txt", true)
xhttp.send()
}
callback(xhttp) {
let openings = xhttp.responseText.split("\n")
for (let i = 0; i < openings.length; i++) {
console.log(openings[i])
openings[i] = openings[i].split(", ")
}
reversi.openings = openings
}
【问题讨论】:
标签: javascript ajax