I am very new to the use of groovy programming language and am trying to develop a simple game of Pelmanism (Pairs). The aim of the game is to match pairs of characters on a 4x4 board.
I have populated the board with the characters and the dialogue boxes pop up asking for the row and column number but when i input the characters i constantly get a message saying Sorry-you are wrong, even if I input the correct location of the characters.
I would appreciate if anyone can offer any guidance on what i am doing wrong? I think it is something to do with the way i have written the code to compare the two characters?
I have copied the source code I have written so far below. As i say i am very new to this so apologies if this sounds simple.
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 == 开发者_JS百科"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 (" --------------------")
}
The main problem is the comparison part at the end, as you don't actually comparing the characters at the specified positions. The comparison logic should basically be:
def firstChar = Guess[row1 - 1][column1 - 1]
def secondChar = Guess[row2 - 1][column2 - 1]
if (firstChar == secondChar) {
println "You got it!"
}
else {
println "Nope."
}
精彩评论