我正在为编码挑战编写常见的lisp代码,它是一个rpg-esque拼图,你需要计算战士所造成的总矫枉过度伤害。因为我对普通的lisp很新,我的代码可能非常糟糕。除非与错误相关,否则请不要发布常见的常见lisp编码提示。我计划在错误修复后将此代码发布到codereview
代码运行正常,直到if not xclass == '1' or xclass == '2' or xclass == '3':
print 'Invalid'
内部(底部)条件import java.util.Scanner;
public class GradeValues {
private static Scanner scanner;
public static void main(String[] args) {
boolean keepGoing = true;
String grade; // initializing variable for grade
while(keepGoing = true){
System.out.println("What percentage did you get? "); //Ask User the question
scanner = new Scanner(System.in); //starting the scanner
grade = scanner.nextLine(); //storing user input of percentage
int percentage = Integer.parseInt(grade); //converting string to a int
if(percentage >= 80 && percentage <= 100){
System.out.println("Your grade is an A! Awesome job!");
}else if(percentage >= 70 && percentage <= 79){
System.out.println("Your grade is an B! Nice work!");
}else if(percentage >= 60 && percentage <= 69){
System.out.println("Your grade is an C! That's average. =( ");
}else if(percentage >= 50 && percentage <= 59){
System.out.println("Your grade is an D! Come on man, you can do better.");
}else if(percentage < 50){
System.out.println("Your grade is an F! You need to hit the books again and try again.");
}else{
System.out.println("I think you type the wrong value.");
}
System.out.println("Would you like to check another grade?"); //Asking user if they want to do it again
Scanner choice = new Scanner(System.in); //Gets user input
String answer = choice.nextLine(); // Stores input in variable "answer"
if(answer == "yes"){
keepGoing = true; //While loop should keep going
}else{
keepGoing = false; //While loop should stop
break; //this should stop program
}
}
}
}
为真。我使用GNU Clisp 2.49来运行此代码。
keepGoing
最重要的部分是代码底部的var html = "";
for(y = 1; y <= 10; y++){
for (x = 1; x <= 10; x++) {
var imgId = "imgXY"+ "_" + x + "_" + y;
console.log("appending " + imgId);
html += '<img id="'+ imgId + '" src="gray.png" />';
//add positioning and css code inline
}
}
$('#game').append(html);
函数。运行此代码后,我收到错误tick
。
这是执行时打印的内容:
when (> overkill-damage 0)
(defun timer (initialization-time interval)
(list :init initialization-time :interval interval :ready nil :time-passed 0))
(defun tick-timer (timer)
(let ((newtime (1+ (getf timer :time-passed))))
(when (and (not (getf timer :ready)) (>= newtime (getf timer :init)))
(setf (getf timer :ready) t))
(setf (getf timer :time-passed) newtime)))
(defun timer-ready? (timer)
(and
(getf timer :ready)
(= 0 (mod (getf timer :time-passed) (getf timer :interval)))))
(defun weapon (damage timer)
(list :damage damage :timer timer))
(defun weapon-attack (weapon)
(tick-timer (getf weapon :timer))
(if (timer-ready? (getf weapon :timer))
(getf weapon :damage)
0))
(defun attack (character)
(reduce #'(lambda (total weapon) (+ (weapon-attack weapon) total)) (getf character :weapons) :initial-value 0))
(defun attack-monster (monster damage)
(- monster damage))
(defun calculate-overkill-damage (health)
(if (> health 0)
0
(abs health)))
(defparameter *warrior* `(:weapons ,(list (weapon 35 (timer 0 4)))))
(defparameter *mage* `(:weapons ,(list (weapon 80 (timer 2 8)))))
(defparameter *rogue* `(:weapons ,(list (weapon 20 (timer 0 3))
(weapon 30 (timer 0 4)))))
(defparameter *monsters* '(300 600 850 900 1100 3500))
(defparameter *current-monster* 0)
(defparameter *overkill* 0)
(defparameter *game-over* nil)
; I assume, for now, that when a monster dies, they will miss the rest of their attacks
(defun tick ()
(sleep .1)
(let* ((monster (nth *current-monster* *monsters*))
(new-health (attack-monster monster (attack *warrior*)))
(overkill-damage (calculate-overkill-damage new-health)))
(format t "Attacking~%-------~%Attacking monster ~a, which has ~a health." *current-monster* monster)
(format t "~%Dealt ~a overkill damage!" overkill-damage)
(when (> overkill-damage 0)
(do (format t "Dealt ~a overkill damage!" overkill-damage)
(setf *overkill* (+ *overkill* overkill-damage))
(format t "Total overkill damage is now ~a" *overkill*)
(setf *current-monster* (1+ *current-monster*))
(format t "Moving to next monster, ~a" *current-monster*)
(when (= *current-monster* (1- (length *monsters*)))
(setf *game-over* t))))
(let* ((new-health (attack-monster monster (attack *mage*)))
(new-health (attack-monster monster (attack *rogue*))))
(setf (nth *current-monster* *monsters*) new-health)
(format t "~%Monster is now at ~a health~%" (nth *current-monster* *monsters*)))))
(loop for x from 1 until (equal *game-over* t)
do (tick))
命令显示的代码甚至不存在,我真的不明白那里发生了什么。
即使我在tick
上调用macroexpand,代码*** - LET: T is a constant, may not be used as a variable
也不会显示在任何地方。
有谁知道发生了什么?或者,如果您有任何CLISP调试技巧来帮助我查明错误,请告诉我们!
答案 0 :(得分:6)
好吧,我不明白代码应该做什么,但你的错误来自DO
:http://www.lispworks.com/documentation/HyperSpec/Body/m_do_do.htm
正如文档所说,这是一个循环,其第一个参数是变量列表:
(do (format t "Dealt ~a overkill damage!" overkill-damage)
这会尝试将format
,t
,"Dealt ~a overkill damage!"
和overkill-damage
用作变量。
如果您只想在when
的正文中使用多个表单,则无需执行任何特殊操作。 when
支持开箱即用:
(when (> overkill-damage 0)
(format t "Dealt ~a overkill damage!" overkill-damage)
(setf *overkill* (+ *overkill* overkill-damage))
(format t "Total overkill damage is now ~a" *overkill*)
...)
答案 1 :(得分:2)
do
的第一个参数定义了循环的局部变量,如let
所做的那样;你正在使用它作为身体的起点。