2015-05-04 85 views
-1

我必須使用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 
} 
+2

請參考提到的線程,調整標題和第一句 - 我只是不明白你想說什麼.... – Markus

+0

抱歉,不澄清。當我運行這段代碼時,底部的兩行會填滿,但是當我在列中輸入X或O時,它只會切換第二個條目而不是下一行。繼承人對其他線程的沉溺http://stackoverflow.com/questions/27073660/connect-four-game-in-scala –

+1

奇怪的是,這讓我想起[Stack Sort](https://xkcd.com/1185/ )。返回主題:您似乎在每個「else」子句前都有一個額外的'},因此此代碼無法編譯。同樣,在兩者的結尾,你返回的'then'-body和'else'-body,所以'currentRow1' /'currentRow2'永遠不會進一步遞減。 –

回答

2

我試圖讓你的代碼可讀性和編譯,並試圖解決一些邏輯。

但是,我從來沒有使用Scala工作所以這只是第一草圖,你可能要繼續...

一些功能可以合併和currentRow需要修復。看到這裏:

object ConnectFour{ 
    val table = Array.fill(9,8)('-') 
    val currentRow = Array.fill(8)(8) 

    def main(args: Array[String]) { 
    var i = 0; 
    while(i < 8) { 
     table(8)(i) = (i+'0').toChar 
     i = i+1; 
    } 

    player(1) 
    } 

    def printBoard(table: Array[Array[Char]]) { 
    table.foreach(x => println(x.mkString(" "))) 
    } 

    def player(playerNr : Int){ 
    printBoard(table) 
    println("Player " + playerNr + " it is your turn. Choose a column 0-7") 
    var column = readAndVerifyInt 

    var nextUser = 1 : Int 
    var symbol = 'O' : Char 

    if(playerNr == 1) { 
     symbol = 'X' 
     nextUser = 2 
    } 

    var curR = currentRow(column) 
    while (curR >= 0) { 
     if (table(curR)(column) != ('-')) { 
     curR = curR-1 
     currentRow(column) = curR 
     } 
     table(curR)(column) = symbol 
     player(nextUser) 
    } 
    } 

    def readAndVerifyInt() : Int = { 
     var column = readInt 
     if (column >= 0 && column <= 7) { 
      return column 
     } else { 
      println(" > Please try again") 
      return readAndVerifyInt 
     } 
    } 
} 
+0

謝謝你的幫助,但我發郵件給我的教授,他幫助了我。 –

+0

我已經修復它永遠不會只爲自己 - 附加的代碼工作,比你更容易;) – Markus