0
\$\begingroup\$

Im studying Java OOP, and I have to make first assignment about making checkers board game. I have to get input from user if he plays, and than when his turn is over the computer will play. Computer's movement is random but if he has the opportunity to "eat" his rival, he has to do this and if not than making random move. Can someone help me with the coding?

\$\endgroup\$
5
  • \$\begingroup\$ On which part are you stuck exactly right now? \$\endgroup\$
    – wondra
    Mar 23, 2018 at 10:05
  • \$\begingroup\$ Computer turn, how can I write the code of selection random cell from line 2, for the first movement \$\endgroup\$ Mar 23, 2018 at 10:37
  • 2
    \$\begingroup\$ What have you tried so far? It could be even written as one-liner such as tokens.stream().collect(Collectors.collectingAndThen(Collectors.toList(), list -> Collections.shuffle(list).stream())).findFirst(token -> token.canMove()). \$\endgroup\$
    – wondra
    Mar 23, 2018 at 13:05
  • \$\begingroup\$ @wondra That is terrifying and exciting at the same time. Congrats \$\endgroup\$
    – Bálint
    Mar 23, 2018 at 13:10
  • \$\begingroup\$ hey, I have learned only Java and C programs. Have no idea what does it means. Thanks anyway! \$\endgroup\$ Mar 23, 2018 at 17:18

1 Answer 1

1
\$\begingroup\$

The most straightforward way would be to build a list of all possible moves and then randomly select from them.

  1. Initialize two empty lists, eatMoves and nonEatMoves
  2. For each rival token:
    • For each possible move of that token:
      • if the move involves eating an opponent:
        • Add move to eatMoves
      • else:
        • Add move to nonEatMoves
  3. Choose one of the moves:
    • If eatMoves is not empty:
      • Randomly choose from eatMoves list
    • else:
      • Randomly choose from nonEatMoves list
  4. Return the random move selection

Actually choosing a random number is straightforward: https://docs.oracle.com/javase/8/docs/api/java/util/Random.html

\$\endgroup\$
1
  • \$\begingroup\$ Hey, I've made it already, but Im strugeling to find the right code for the random. I know that this is the best way to 'start' computer first move. After using random, I'll use IF loops and FOR . \$\endgroup\$ Mar 23, 2018 at 17:19

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .