Improve Game Strategy with LISP Function Help | Random Stone Placement

  • Thread starter Adam
  • Start date
  • Tags
    Function
In summary, the function is part of a heuristic algorithm for a game, where the goal is to place a stone at a random point adjacent to the opponent's last move in order to block their winning row or create your own. However, the code currently freezes when the stone is placed on the left edge of the board, likely due to a lack of error checking for legal values on the board.
  • #1
Adam
65
1
Got a problem with this function. It is part of a heuristic thing for a game. If I can't get a winning row of five stones, or block a winning row from the opponent, or blah blah blah, I want it to place a stone at some random point adjacent to the opponent's last move. Basically think of a tic-tac-toe grid but larger. A 3x3 array, 0 to 9. If opponent places a stone in 4, then I want it to place randomly in 0 to 3 or 5 to 9. At the moment it almost works, but freezes if the stone is placed on the left edge of the board.

Code:
(defun rndplyr ()
  (if (null (get-history))
      (list 0 0 )
    (let ((hist (get-history))
          (lm (car (get-history))))
   
      (do ((row (+ (car lm) (+ (random 3) -1 ) ))
           (col (+ (cadr lm) (+ (random 3) -1 ) )))
          ((score (list row col) (get-board))
           (list row col)))))
  )

Any hints?
 
Computer science news on Phys.org
  • #2
Your code doesn't seem to error check to see if your value for row and col lie on the board... I surmise that the score function isn't written to handle illegal values and gets itself caught in an infinite loop.
 
  • #3


One possible issue could be the use of random numbers to determine the placement of the stone. Since the function is only looking at the opponent's last move, it may not be considering the overall game strategy. Instead of using random numbers, you could try implementing a logic-based approach where you consider the current state of the game and make a decision based on that.

For example, you could analyze the board and determine if there are any potential winning rows for either player. If there are, then you can prioritize blocking those or creating your own winning row. If there are no potential winning rows, then you could use the random placement as a last resort.

Another possible issue could be the use of a 3x3 grid for a larger game board. Depending on the size of the board, this may not be an effective strategy. You could consider expanding the grid or implementing a different data structure to better represent the game board.

Overall, it may be helpful to break down the function into smaller, more manageable parts and test each part separately to identify where the issue may be occurring. Additionally, seeking feedback from other players or experts in game strategy could also provide valuable insights and improvements for the function.
 

What is a LISP function?

A LISP function is a block of code written in the LISP programming language that performs a specific task. It can take in input, process it, and return an output.

How do I call a LISP function?

To call a LISP function, you need to use the function name followed by parentheses. Any necessary arguments should be placed inside the parentheses. For example, (add 2 3) would call the function "add" with the arguments 2 and 3.

Can I create my own LISP functions?

Yes, you can create your own LISP functions by using the "defun" keyword followed by the function name and its parameters. You can then write the code for the function within the parentheses and use it later in your program.

What is the purpose of the "return" statement in a LISP function?

The "return" statement in a LISP function is used to specify the value that the function will return when it is called. It is used to terminate the function and send back the desired output.

Are there any built-in LISP functions?

Yes, there are many built-in LISP functions that come with the language. These functions are already defined and can be used without having to write them yourself. Some examples include "length", "reverse", and "append".

Similar threads

  • Set Theory, Logic, Probability, Statistics
Replies
1
Views
922
  • Engineering and Comp Sci Homework Help
Replies
5
Views
5K
  • Programming and Computer Science
Replies
33
Views
6K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
18
Views
7K
  • General Discussion
Replies
1
Views
1K
  • Set Theory, Logic, Probability, Statistics
Replies
2
Views
1K
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
11
Views
9K
Back
Top