我对使用groovy编程语言非常陌生,我正在尝试开发一种简单的Pelmanism游戏(Pairs)。游戏的目的是匹配4x4板上的字符对。
我已经填充了字符和弹出的对话框,询问行和列号,但是当我输入字符时,我不断收到消息说抱歉 - 你错了,即使我输入了正确的位置人物。
如果有人能就我做错了什么提供任何指导,我将不胜感激?我认为这与我编写代码比较两个字符的方式有关吗?
我已经复制了我到目前为止编写的源代码。正如我所说,我对此非常新,所以如果这听起来很简单就道歉。
import javax.swing.*
def int ask(message) {
def txt = JOptionPane.showInputDialog(message)
def temp = Integer.parseInt(txt)
boolean valid=false
while(!valid){
if (temp<1||temp>4){
txt = JOptionPane.showInputDialog("Number must be in the range 1 to 4")
temp = Integer.parseInt(txt)
} else {
valid=true
}
}
return temp
return answer
}
def askReply (message) {
def txt = JOptionPane.showInputDialog(message)
boolean valid=false
while(!valid){
if (txt=="N" || txt=="n"|| txt=="Y"||txt=="y"){
valid=true
} else {
txt = JOptionPane.showInputDialog("Enter Y or N only")
}
return txt
}
}
println ("This is a game of Pelmanism")
println ("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
println ("This is a memory game")
println ("where the player has to match a pair of hidden symbols")
println ("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
println ("In this game the symbols are Alphabetical characters")
println ("You must guess the position of two equal characters")
println ("by entering the row and column number")
println ("for each pair that you think are a match")
println ("------------------------------------------------------")
println (" ")
def val = ['A', 'A', 'B', 'B', 'C', 'C', 'D', 'D', 'E', 'E', 'F', 'F', 'G', 'G', 'H', 'H']
Collections.shuffle (val)
println val
boolean cont=true // to control the while loop to end the game
def reply // to hold Y/N reply from player
int row1, column1, row2, column2
def counter=0
def Pelmanism = new Object [4][4] // original layout
def Guess = new Object [4][4] // array for guesses
for (r in 0..3) {
for (c in 0..3) {
Pelmanism[r][c] = val [(r*4) + c]
Guess[r][c] = val [(r*4) + c]
}
}
println (" 1 2 3 4")
println (" --------------------")
for (r in 0..3) {
print (r+1)
print (" ")
for (c in 0..3) {
printf ("| %s ", Guess[r][c])
}
println("|")
println (" --------------------")
}
println()
println (" 1 2 3 4")
println (" --------------------")
for (r in 0..3) {
print (r+1)
print (" ")
for (c in 0..3) {
printf ("| X ")
}
println("|")
println (" --------------------")
}
println()
while(cont) {
row1 = ask ("What is the row number? ")
column1 = ask ("What is the column number? ")
row2 = ask ("What is the row number?")
column2 = ask ("What is the column number?")
if (row1 && column1 == row2 && column2==Guess){
counter++
Guess[row1 && column1 == row2 && column2]=Guess
println ("Correct - well done")
} else {
println ("Sorry you are wrong")
}
if (counter>32){
end = true
} else {
reply = JOptionPane.showInputDialog("Do you want to continue? (Y/N)")
boolean valid = false
while(!valid){
if (reply == "N" || reply =="n" || reply == "Y" || reply == "y"){
valid = true
} else {
reply = JOptionPane.showInputDialog("Enter Y or N only")
}
}
if (reply == "N" || reply == "n"){
end = true
}
}
}
cont=false // for testing delete when program working
// output original layout at the end of the game
println()
println (" 1 2 3 4")
println (" --------------------")
for (r in 0..3) {
print (r+1)
print (" ")
for (c in 0..3) {
printf ("| %s ", Pelmanism[r][c])
}
println("|")
println (" --------------------")
}
答案 0 :(得分:1)
主要问题是最后的比较部分,因为您实际上并没有比较指定位置的字符。比较逻辑基本上应该是:
def firstChar = Guess[row1 - 1][column1 - 1]
def secondChar = Guess[row2 - 1][column2 - 1]
if (firstChar == secondChar) {
println "You got it!"
}
else {
println "Nope."
}