我必須使用scala製作連接四遊戲。我附加了代碼,但每次遊戲運行並獲得第3行時,它都會繼續更改第二行條目,而不是轉到下一行。任何幫助,將不勝感激。我發現another thread這個代碼在這裏,無法弄清楚如何得到它的工作:斯卡拉Connect-Four遊戲
// makes the board
val table = Array.fill(9,8)('-')
var i = 0;
while(i < 8){
table(8)(i) = (i+'0').toChar
i = i+1;
}
// prints starting board
def printBoard(table: Array[Array[Char]]) {
table.foreach(x => println(x.mkString(" ")))
}
//player 1 moves
def playerMove1(){
val move = readInt
var currentRow1 = 7
while (currentRow1 >= 0)
if (table(currentRow1)(move) != ('-')) {
currentRow1 = (currentRow1-1)
table(currentRow1)(move) = ('X')
return (player2)}
} else {
table(currentRow1)(move) = ('X')
return (player2)
}
}
//player 2 moves
def playerMove2(){
val move = readInt
var currentRow2 = 7
while (currentRow2 >= 0)
if (table(currentRow2)(move) != ('-')) {
currentRow2 = (currentRow2-1)
table(currentRow2)(move) = ('O')
return (player1)}
} else {
table(currentRow2)(move) = ('O')
return (player1)
}
}
//player 1
def player1(){
printBoard(table)
println("Player 1 it is your turn. Choose a column 0-7")
playerMove1()
}
//player 2
def player2(){
printBoard(table)
println("Player 2 it is your turn. Choose a column 0-7")
playerMove2()
}
for (turn <- 1 to 32){
player1
player2
}
請參考提到的線程,調整標題和第一句 - 我只是不明白你想說什麼.... – Markus
抱歉,不澄清。當我運行這段代碼時,底部的兩行會填滿,但是當我在列中輸入X或O時,它只會切換第二個條目而不是下一行。繼承人對其他線程的沉溺http://stackoverflow.com/questions/27073660/connect-four-game-in-scala –
奇怪的是,這讓我想起[Stack Sort](https://xkcd.com/1185/ )。返回主題:您似乎在每個「else」子句前都有一個額外的'},因此此代碼無法編譯。同樣,在兩者的結尾,你返回的'then'-body和'else'-body,所以'currentRow1' /'currentRow2'永遠不會進一步遞減。 –