public override void take_turn() { int[] moves = { 0, 0, 0, 0, 0, 0, 0 }; //score for moving into each column int selected_column = 0; int[] choices = new int[7]; //will hold the choice or choices for where to move. for (int x = 0; x < 7; x++) { //first, see which move gives best chance at a win int len = g.check_move(x, Game.comp); switch (len) { case 0: //column is full. Can not move here. moves[x] = -1; break; case 1: //nothing around it. not great. moves[x] = 25; break; case 2: //there is a match, but nothing spectacular. only 2 in row. moves[x] = 50; break; case 3: //there is some real possibility here. 3 in row. moves[x] = 75; break; case 4: //this is a win! go here! moves[x] = 1000; break; default: //len is either negative or greater than 4 -- awesome or broken. if (len > 4) { moves[x] = 1000; //an even better win } else { moves[x] = -1; //I don't know hpw we got here, but don't move here } break; } // now see which moves benefit the enemy len = g.check_move(x, Game.player); switch (len) { case 0: //column is full. Can not move here. //moves[x] = -1; break; case 1: //nothing around it. not great. if (moves[x] > 25) { moves[x] -= 25; } else { moves[x] = 0; } break; case 2: //there is a match, but nothing spectacular. only 2 in row. if (moves[x] > 50) { moves[x] -= 50; } else { moves[x] = 0; } break; case 3: //there is some real possibility here. 3 in row. if (moves[x] > 75) { moves[x] -= 75; } else { moves[x] = 0; } break; case 4: //this is a win for the other guy! Go here to block! moves[x] = 1000; break; default: //len is either negative or greater than 4 -- awesome or broken. if (len > 4) { moves[x] = 1000; //an even better win for the other guy--> bigger block. } else { moves[x] = -1; //I don't know hpw we got here, but don't move here } break; } } //now take the best. int max = 0; int count = 0; int choice = 0; for (int i = 0; i < 7; i++) { if (moves[i] > max) { max = moves[i]; } } //get the highest score in max //couont how many moves scored that high. Put the index into choices for (int j = 0; j < 7; j++) { if (moves[j] == max) { count++; choices[count - 1] = j; } } if (count > 1) { //we have more than one move that scored well. we will pick at random from among these moves. Random r = new Random(); choice = (int)(r.NextDouble() * 1000) % count; //this should return an int from 0 to count-1. that is the index in choices that we want. selected_column = choices[choice]; } else //if there is only one { selected_column = choices[0]; } g.move(selected_column, Game.comp); }