문제

Assume computer is playing as C and the opponent is playing as O. The bot must be intelligent enough to win when provided an opportunity. X represents a cell that is not taken. Also assume that computer made the first move

for eg:

Input1
CCX
XOX
OXX

Output1
CCC
XOX
OXX 

What i want to know is how to approach this problem. Is there a specific algorithm to follow?. If yes please clarify it to me!

도움이 되었습니까?

해결책

Use the minimax algorithm.

Once that is implemented, define a simple heuristic, or evaluation function. It could go something like this:

function scoreBoard(board) {
  if(board.isWin()) {
    return 1;
  }
  else if(board.isTie()) {
    return 0;
  }
  else {
    return -1;
  }
}

다른 팁

What you are looking for is the Minimax Algorithm. You can find details here: http://en.wikipedia.org/wiki/Minimax

The idea behind the algorithm is to generate all possible states, assume that the computer is intelligent and chooses the best move(maximizing its gain). Your move should minimize the gain of the computer.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top