Note that there are some explanatory texts on larger screens.

plurals
  1. POMinimax for Connect Four (Unity3D and C#): Problems
    primarykey
    data
    text
    <p>I'm developing a Connect Four game with AI in Unity3D (C#). I use for this the MiniMax algorithm according to this (German) <a href="http://de.wikipedia.org/wiki/Minimax-Algorithmus" rel="nofollow">Pseudocode</a>.</p> <p>The AI is still playing pretty bad. It tries to get even 4 in a row, although there are only three free fields. The AI always goes through the row and blocks only when it is needed. Unfortunately, it also blocks not always.</p> <p><em><strong>Where to hide the problems? What have I forgotten?</em></strong></p> <p>How can I integrate random moves of the AI when no one wins or loses at the next move.</p> <p><strong>Here is my source code:</strong></p> <p>minimaxDepth = 4</p> <p>function call: Max (minimaxDepth, fieldCopy);</p> <pre><code>int Max (int depth, int[,] fieldCopy) { int maxValue = -9999; int moveValue; bool winAI = false; bool winHuman = false; bool isStoneBelow = false; for (int y = 0; y &lt; 6; y++) { for (int x = 0; x &lt; 7; x++) { if (y &gt; 0) { //is a stone under it? if (fieldCopy [x, y - 1] == 1 || fieldCopy [x, y - 1] == 2) { isStoneBelow = true; } else { isStoneBelow = false; } } else { isStoneBelow = true; } // possible move? if (fieldCopy [x, y] != 1 &amp;&amp; fieldCopy [x, y] != 2 &amp;&amp; isStoneBelow == true) { isStoneBelow = false; fieldCopy [x, y] = 2; //simulate move winAI = false; winHuman = false; //Is there a winner? if (CheckWin (x, y, 2, fieldCopy)) { winAI = true; winHuman = false; } //No more moves possible? if (depth &lt;= 1 || winAI == true) { moveValue = evaluationFunction (winAI, winHuman); //evaluate the move } else { moveValue = Min (depth - 1, fieldCopy); } fieldCopy [x, y] = 0; //Reset simulated move if (moveValue &gt; maxValue) { maxValue = moveValue; if (depth == minimaxDepth) { aiMoveX = x; // next move } } } } } return maxValue; } int Min (int depth, int[,] fieldCopy) { int minValue = 9999; int moveValue; bool winAI = false; bool winHuman = false; bool isStoneBelow = false; bool loopBreak = false; for (int y = 0; y &lt; 6; y++) { for (int x = 0; x &lt; 7; x++) { if (y &gt; 0) { //is a stone under it? if (fieldCopy [x, y - 1] == 1 || fieldCopy [x, y - 1] == 2) { isStoneBelow = true; } else { isStoneBelow = false; } } else { isStoneBelow = true; } // possible move? if (fieldCopy [x, y] != 1 &amp;&amp; fieldCopy [x, y] != 2 &amp;&amp; isStoneBelow == true) { isStoneBelow = false; fieldCopy [x, y] = 1; //simulate move winHuman = false; winAI = false; //Is there a winner? if (CheckWin (x, y, 1, fieldCopy)) { winHuman = true; winAI = false; } //No more moves possible? if (depth &lt;= 1 || winHuman == true) { moveValue = evaluationFunction (winAI, winHuman); //evaluate the move } else { moveValue = Max (depth - 1, fieldCopy); } fieldCopy [x, y] = 0; //Reset simulated move if (moveValue &lt; minValue) { minValue = moveValue; } } } } return minValue; } int evaluationFunction (bool winAI, bool winHuman) { if (winAI) { return 1; } else if (winHuman) { return -1; } else { return 0; } } </code></pre> <p>Thanks for your help!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload