0
我想在斯卡拉連接四場比賽。目前,我已經打印出棋盤並向玩家1要求移動,一旦玩家1選擇一個數字,棋盤就會在玩家1選擇的列中用X打印出來。然後玩家2挑選一個數字。我的問題是,一旦我選擇了一個玩家的字母填充整個列的數字,並且你在其上創建了一個數字。斯卡拉連接四場比賽
繼承人發生了什麼
. X . O X . . .
. X . O X . . .
. X . O X . . .
. X . O X . . .
. X . O X . . .
. X . O X . . .
. X . O X . . .
. X . O X . . .
0 1 2 3 4 5 6 7
// Initialize the grid
val table = Array.fill(9,8)('.')
var i = 0;
while(i < 8){
table(8)(i) = (i+'0').toChar
i = i+1;
}
/* printGrid: Print out the grid provided */
def printGrid(table: Array[Array[Char]]) {
table.foreach(x => println(x.mkString(" ")))
}
/*//place of pieces X
def placeMarker(){
val move = readInt
//var currentRow = 7
while (currentRow >= 0)
if (table(currentRow)(move) != ('.')){
currentRow = (currentRow-1)
table(currentRow)(move) = ('X')
return (player2)}
else{
table(currentRow)(move) = ('X')
return (player2)
}
}
//place of pieces O
def placeMarker2(){
val move = readInt
//var currentRow = 7
while (currentRow >= 0)
if (table(currentRow)(move) != ('.')){
currentRow = (currentRow-1)
table(currentRow)(move) = ('O')
return (player1)}
else{
table(currentRow)(move) = ('O')
return (player1)
}
}
*/
def placeMarker1(){
val move = readInt
var currentRow = 7
while (currentRow >= 0)
if (table(currentRow)(move) !=('.'))
{currentRow = (currentRow-1)}
else{table(currentRow)(move) = ('X')}
}
def placeMarker2(){
val move = readInt
var currentRow = 7
while (currentRow >= 0)
if (table(currentRow)(move) !=('.'))
{currentRow = (currentRow-1)}
else{table(currentRow)(move) = ('O')}
}
//player 1
def player1(){
printGrid(table)
println("Player 1 it is your turn. Choose a column 0-7")
placeMarker1()
}
//player 2
def player2(){
printGrid(table)
println("Player 2 it is your turn. Choose a column 0-7")
placeMarker2()
}
for (turn <- 1 to 32){
player1
player2
}
因此,當我在函數placeMarker1/placeMaker2中使用var CurrentRows = 7時,它只允許我從底部開始到第二行,而不是繼續向上。你認爲我應該繼續它的功能@daowen – acolisto 2014-11-22 03:35:22
@acolisto - 那是因爲邏輯不好。爲什麼你有一個循環,如果你永遠不會超過1次迭代? (條件的兩個分支(if/else)裏面都有一個'return')。真正的分支只應該*遞減currentRow,然後循環將繼續做它的事情:if(table(currentRow)(移動)!=('。')){currentRow =(currentRow-1)}其他{...} – DaoWen 2014-11-22 03:46:51
@acolisto - 您仍然需要'else'分支中的'return'。換句話說,一旦你放置這件作品,你就完成了,所以你回來了。 – DaoWen 2014-11-22 04:30:43