Info

This question is closed. Reopen it to edit or answer.

How do I code a function for a dot on a playing board to move to eat the food surrounding it efficiently?

1 view (last 30 days)
This game basically involves two dots competing against each other to eat the most "food" on a playing board. My dot is up against my professor's dot who uses the distance formula as the algorithm to get the most food. I tried using the function graphshortestpath but it apparently does not apply very well. I'm allowed two inputs and one output for my function. The inputs are the "food" and "my position" whereas the output is the vector that points to where my dot should go.

Answers (1)

Image Analyst
Image Analyst on 26 Oct 2015
Use zeros() to create a playing field, or canvass. Then use randperm() to place "food" elements at random locations in the playing field array. Then use it again to randomly place the two players somewhere in the matrix. Then define some parameters, like how far (how many elements) can a player see food from his current location, like a radius of 5 elements or whatever. Then have a loop where each player locates food within his vision distance and moves towards one food element according to some strategic algorithm that would allow him to eat the most visible food in the fewest number of moves. It's up to you to figure out this algorithm, probably based on some search or optimization algorithms you learned in class. If the player moved onto a food element, then consider the food eaten, otherwise it's just a move toward the food. Players alternate turns. You can have both players move/eat in a single iteration of the loop. Exit the loop if there are no more food items left. After the loop (or in it), use fprintf() to print out how many food items each player has eaten. Maybe you can have another outer loop where you can use questdlg() to ask the players if they want to play the game of life again. Good luck!

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!