public override void take_turn() { bool valid = false; bool good_input = false; int x; string s; while (!valid) { do { Game.draw_map(g); Console.Write("It's your turn. Drop a token in which column? (1..7)>"); s = Console.ReadLine(); if (int.TryParse(s, out x)) { if ((x >= 0) && (x <= 7 )) { if (g.check_col(x - 1, g)) { good_input = true; } else // if the column is full { Console.Write("Column {0} is full. Please choose another.\n", x); } } else //if not a number or in range { Console.Write("Please use the digits 1 through 7.\n"); } } } while (!good_input); g.move(x - 1, Game.player); valid = true; } }
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); }
static void Main(string[] args) { Game g = new Game(); g.setup(); g.draw(); int move = -1; int type = 1; while (move > -2) { if (g.player == 1 || type == 0) { ConsoleKeyInfo info = Console.ReadKey(); switch (info.Key) { case ConsoleKey.D0: move = 0; break; case ConsoleKey.D1: move = 1; break; case ConsoleKey.D2: move = 2; break; case ConsoleKey.D3: move = 3; break; case ConsoleKey.D4: move = 4; break; case ConsoleKey.D5: move = 5; break; case ConsoleKey.D6: move = 6; break; case ConsoleKey.Escape: move = -5; break; case ConsoleKey.H: type = 0; break; case ConsoleKey.U: type = 1; break; case ConsoleKey.I: type = 2; break; default: move = -1; break; } } if (g.player == 2 && type == 1) { Random r = new Random(); move = r.Next(0, 7); } if (g.player == 2 && type == 2) { // Clever logic for deciding moves } if (move == -5) { g.setup(); g.draw(); } else if (move == -1) { if (g.player == 1) { g.player = 2; } else if (g.player == 2) { g.player = 1; } } else if (move > -1) { g.move(move); g.draw(); } if (g.checkwin()) { Console.WriteLine("Player " + g.player + " has won."); Console.ReadKey(); g.setup(); g.draw(); Random r = new Random(); g.player = r.Next(1, 3); } if (g.player == 1) { g.player = 2; } else if (g.player == 2) { g.player = 1; } } }