问题描述
我试图做一个连接四个游戏中阶。目前,我有董事会打印出来,并要求玩家1的举动,一旦玩家1的动产数字的板打印出与在播放器1中选择的列一个X。然后玩家挑选2一些。我的问题是,一旦我挑一些该玩家的信充满整个列,您Ant构建在此基础之上。
继承人发生了什么例子
。 X 。 ○×。 。 。
。 X 。 ○×。 。 。
。 X 。 ○×。 。 。
。 X 。 ○×。 。 。
。 X 。 ○×。 。 。
。 X 。 ○×。 。 。
。 X 。 ○×。 。 。
。 X 。 ○×。 。 。
0 1 2 3 4 5 6 7
//初始化网格
VAL表= Array.fill(9,8),('。')
变种I = 0;
而(ⅰ&下; 8){
表(8)(ⅰ)=(1 +'0')。toChar
I = I + 1;
}/ * printGrid:打印出所提供的网格* /
高清printGrid(表:数组[数组[字符]){
table.foreach(X =>的println(x.mkString()))
}
/ * //个X的地方
高清placeMarker(){
VAL举动=的readInt
// VAR currentRow = 7
而(currentRow> = 0)
如果(表(currentRow)(移动)!=('。')){
currentRow =(currentRow-1)
表(currentRow)(移动)=('X')
回报(player2)}
其他{
表(currentRow)(移动)=('X')
回报(player2)
}
}//地方件Ø
高清placeMarker2(){
VAL举动=的readInt
// VAR currentRow = 7
而(currentRow> = 0)
如果(表(currentRow)(移动)!=('。')){
currentRow =(currentRow-1)
表(currentRow)(移动)=('O')
回报(PLAYER1)}
其他{
表(currentRow)(移动)=('O')
回报(PLAYER1)
}
}
* /高清placeMarker1(){
VAL举动=的readInt
VAR currentRow = 7
而(currentRow> = 0)
如果(表(currentRow)(移动)!=('。'))
{currentRow =(currentRow-1)}
其他{表(currentRow)(移动)=('X')}
}高清placeMarker2(){
VAL举动=的readInt
VAR currentRow = 7
而(currentRow> = 0)
如果(表(currentRow)(移动)!=('。'))
{currentRow =(currentRow-1)}
其他{表(currentRow)(移动)=('O')}
}//播放器1
高清PLAYER1(){
printGrid(表)
的println(玩家1轮到你。选择一列0-7)
placeMarker1()
}//播放器2
高清player2(){
printGrid(表)
的println(玩家2轮到你。选择一列0-7)
placeMarker2()
}对于(转< - 1到32){
PLAYER1
player2
}
您全局状态是搞乱您: VAR currentRow = 7
而不是试图跟踪跨所有列全球currentRow的,我建议两件事之一:
- 保留一个单独的currentRow在一个
currentRows
阵列每一列。 - 只需通过色谱柱每次迭代你把一件件地找到最低的空槽。
其实,它看起来像你最初做的第二项建议,但你注释掉当地 currentRow
变量,并宣布全局(实例级)一个替代。
I'm trying to make a connect four game in scala. Currently i have the board print out and ask player 1 for a move, once player 1 choses a number a board prints out with an X in the column where player 1 chose. then player 2 picks a number. My problem is that once i pick a number that player's letter fills the whole column and you ant build on top of that.
heres an example of what happens
. 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
}
Your global state is messing you up: var currentRow = 7
Instead of trying to keep track of a global "currentRow" across all columns, I'd recommend one of two things:
- Keep a separate "currentRow" for each column in a
currentRows
array. - Just iterate through the column each time you place a piece to find the lowest empty slot.
Actually, it looks like you were originally doing the second suggestion, but you commented out your local currentRow
variables and declared a global (instance-level) one instead.
这篇关于连接四个游戏在斯卡拉的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!