static Node[] generateMoves(Node node) { List <Node> nodes = new List <Node>(); for (int i = 0; i < 7; i++) { if (JoinFour.isMoveValid(node.player | node.opponent, i) && !JoinFour.hasPlayerWon(node.player) && !JoinFour.hasPlayerWon(node.opponent)) { nodes.Add(new Node { opponentPlaying = !node.opponentPlaying, player = (node.opponentPlaying) ? node.player : JoinFour.applyMove(node.player, node.opponent, i), opponent = (node.opponentPlaying) ? JoinFour.applyMove(node.opponent, node.player, i) : node.opponent, lastMove = i }); } } return(nodes.ToArray()); }
void MoveSubmitted(object sender, EventArgs args) { if (game.gameFinished) { return; } int tag = (int)((Button)sender).Tag; // Check if the move is valid if (!game.submitMove((int)Math.Floor((double)tag / 6))) { MessageBox.Show("That move is not valid. Please enter a valid move."); return; } else { redrawBoard(); if (game.gameFinished) { if (JoinFour.hasPlayerWon(game.players[0])) { MessageBox.Show("Player 1 has won!"); } else { MessageBox.Show("Player 2 has won!"); } } } }
public bool submitMove(int x) { if (!JoinFour.isMoveValid(players[0] | players[1], x)) { return(false); } if (playerOnePlaying) { players[0] = JoinFour.applyMove(players[0], players[1], x); if (JoinFour.hasPlayerWon(players[0])) { gameFinished = true; } else if (type == GameType.PlayerVsComputer) { players[1] = JoinFour.applyMove( players[1], players[0], AIPlayer.generateNextMove(players[1], players[0])); if (JoinFour.hasPlayerWon(players[1])) { gameFinished = true; } } else { playerOnePlaying = false; } } else { players[1] = JoinFour.applyMove(players[1], players[0], x); if (JoinFour.hasPlayerWon(players[1])) { gameFinished = true; } playerOnePlaying = true; } return(true); }
static double evaluateNode(Node node) { if (JoinFour.hasPlayerWon(node.player)) { return(100); } if (JoinFour.hasPlayerWon(node.opponent)) { return(-100); } double score = 0; score += 0.1 * getThreeOpenConnections(node.player, node.opponent); score -= 0.1 * getThreeOpenConnections(node.opponent, node.player); score += 0.01 * getTwoOpenConnections(node.player, node.opponent); score -= 0.01 * getTwoOpenConnections(node.opponent, node.player); return(score); }