Connect Four - MIT

Transcription

IntroductionSolvabilityRulesComputer Solution ImplementationConnect FourMarch 9, 2010Connect Four

IntroductionSolvabilityRulesComputer Solution ImplementationConnect Four is a tic-tac-toe like game in which two players dropdiscs into a 7x6 board. The first player to get four in a row (eithervertically, horizontally, or diagonally) wins.Connect Four

IntroductionSolvabilityRulesComputer Solution ImplementationThe game was first known as “The Captain’s Mistress”, but wasreleased in its current form by Milton Bradley in 1974. In 1988Victor Allis solved the game, showing that with perfect play byboth players, the first player can always win if he plays the middlecolumn first, and if he chooses another column first the secondplayer can always force a draw.Connect Four

IntroductionSolvabilityRulesComputer Solution ImplementationToday we will explore the different strategies involved in playingconnect four, how a computer could emulate these strategies, andhow these techniques relate to other artificial intelligence topicsinvolved in solving games with large search spaces.Connect Four

IntroductionSolvabilityRulesComputer Solution ImplementationFor convenience, we will call the first player white (W) and thesecond player black (B).Connect Four

IntroductionSolvabilityRulesComputer Solution ImplementationIRecall: a strong solution to a game means knowing theoutcome of the game from any given board position.IOne strategy to try would be storing all possible gamepositions in a database, exploring the tree of game play fromeach position, and determining the winner in each case.IWe will see that this strategy, at least at this time, is notreally feasible for Connect Four (and even much less so formore complex games like GO and chess).Connect Four

IntroductionSolvabilityRulesComputer Solution ImplementationFirst we look for an upper bound on the number of possibleConnect Four board positions.IEach grid square can be in one of 3 states: black, white, orempty.ISince there are 7x6 42 squares, this gives a very crudeupper bound of 342 1020 .IA not so much closer look reveals that we can get a muchtighter upper bound by noticing that many positions wecounted before were illegal. For instance, if one square isempty in a column, then all the squares above it must also beempty. So we throw these possible positions out. Removingthese configurations gives a better upper bound of 7.1 1013possible positions.Connect Four

IntroductionSolvabilityRulesComputer Solution ImplementationThere are other types of illegal positions that are harder to detect.IFor instance, if we are assuming that white moves first, thensome game configurations, such as a stack in one columnfrom bottom up of BWBWBW is impossible, since blackwould have had to move first.IIt turns out that no one has been able to weed out all of thesepositions from databasesIThe best lower bound on the number of possible positions hasbeen calculated by a computer program to be around1.6 1013 . So we would need at least that many positionsstored to do a brute force search of the game.Connect Four

IntroductionSolvabilityRulesComputer Solution ImplementationInstead of a brute force search, we can try a knowledge-basedapproach. Instead of searching the entire game space, we canformulate general rules that tell which player will win in whichsituations. We saw this approach fail last week for a computerplaying tic-tac-toe.Connect Four

IntroductionSolvabilityRulesComputer Solution Implementation1. If there is a winning move, take it.2. If your opponent has a winning move, take the move so hecan’t take it.3. Take the center square over edges and corners.4. Take corner squares over edges.5. Take edges if they are the only thing available.Connect Four

IntroductionSolvabilityRulesComputer Solution ImplementationApparently this strategy won’t always work. In general, we mustclassify these sorts of rules into two classes:IRules that guarantee a certain results (and require proof thatthey do so)IHeuristic rules that are generally advantageous but are notwithout downfall (like the strategy given above)Connect Four

IntroductionSolvabilityRulesComputer Solution ImplementationWe’ll explore possible strategies to follow for Connect Four below.First we’ll learn some terminology associated with describing thestrategy.Connect Four

IntroductionSolvabilityRulesComputer Solution ImplementationWe will number the 7 x 6 board with columns a to g left to rightand numbers 1 to 6 from bottom to top. So the coveted middlebottom square is d1.Connect Four

IntroductionSolvabilityRulesComputer Solution ImplementationIThreat: A threat is a square that if taken by opponent formsa game-winning group of four. For example, in the gamebelow White has threats at b1 and f1:Connect Four

IntroductionSolvabilityRulesComputer Solution ImplementationIUseless Threat: A useless threat is a threat that will never beable to be carried out by the opponent.Connect Four

IntroductionSolvabilityRulesComputer Solution ImplementationThe picture below illustrates the concept of a useless threat.Connect Four

IntroductionSolvabilityRulesComputer Solution ImplementationIt is clear that a threat can only be carried out if the opponent isforced to play the square below (or he allows you to play below thethreat). In analyzing threats, certain patterns show up in how thesquares are divided among players.Connect Four

IntroductionSolvabilityRulesComputer Solution ImplementationIOdd and Even ThreatsIIThe odd/evenness of a threat is determined by the rownumber. So d1 is an odd threat, etc.In normal game play, white is most likely to get odd squares,while black is most likely to get even squares. Why?Connect Four

IntroductionSolvabilityRulesComputer Solution ImplementationIn general, we see the following patterns:IWhite has an odd threat, Black even: White winsIWhite and Black both have even threats: there is no columnwhere an odd number of squares can be played, so bothplayers will get their normal squares (as defined above), andBlack will be able to refute Whites threat and win.IWhite has an even threat, Black an odd threat: draw.IWhite and Black both have odd threats: usually neither ofthese threats end up working and depend on other threats.In a careful analysis of threats it is important to make sure thattaking care of one threat does not allow another threat to becreated.Connect Four

IntroductionSolvabilityRulesComputer Solution ImplementationIZugzwang:IIIThe formal definition of this strange German word: a situationwhere a player is forced to make a move when he would rathermake no move at all.In connect four, a player is able to “control the zugzwang” ifthe player is able to guide the way odd and even squares aredivided up among players.As an example, we look at the following game situation (Allis26), where White is about to move:Connect Four

IntroductionSolvabilityRulesComputer Solution ImplementationConnect Four

IntroductionSolvabilityRulesComputer Solution ImplementationNote that all columns contain an even number of pieces, so Whitewill never fill up a column since it must take only odd squares. SoBlack can just play “follow-up” and mimic Whites every move.This will result in the following position:Connect Four

IntroductionSolvabilityRulesComputer Solution ImplementationConnect Four

IntroductionSolvabilityRulesComputer Solution ImplementationNow White must play either b1 or f1, which Black will follow, andwin the game with a group of four on the second row.Connect Four

IntroductionSolvabilityRulesComputer Solution ImplementationSo in conclusion, Zugzwang involves being able to divide up howeven and odd squares are distributed to the two players. Blackwanted only even squares because eventually it would be able tofulfill its threat at either b2 or f2. But if it had wanted oddsquares, it could have just stopped playing follow up and played ina different column.Connect Four

IntroductionSolvabilityRulesComputer Solution ImplementationAs an example of using a knowledge based approach to teach acomputer to play a game, the following rules were used inprogramming VICTOR to win connect four.IEach rule classifies threats and gives solutions to some ofthem.IEach rule is valid for the player that controls the Zugzwang,which is assumed to be black in the following examples. Eachof these “rules” is a possible winning connection for the player.Connect Four

IntroductionSolvabilityRulesComputer Solution ImplementationIClaimeven:IIIController of zugzwang can get all empty even squares whichare not directly playable by letting the opponent play all emtpyodd squares.Required: Two squares, directly above each other. Bothsquares are empty, the upper square must be even.Solutions: All groups which contain the upper square.Connect Four

IntroductionSolvabilityRulesComputer Solution ImplementationConnect Four

IntroductionSolvabilityRulesComputer Solution ImplementationIBaseinverse:IIIBased on the fact that a player cannot play two directlyplayable squares in one turn.Required: Two directly playable squaresSolutions: All groups which contain both squares.Connect Four

IntroductionSolvabilityRulesComputer Solution ImplementationConnect Four

IntroductionSolvabilityRulesComputer Solution ImplementationIVertical:IIIBased on the face that a player cannot play two men in thesame column in one turn, while by playing one man in thecolumn, the square directly above becomes immediatelyplayable.Required: two squares directly above each other. Both squaresempty, upper square must be odd.Solutions: all groups which contain both squaresConnect Four

IntroductionSolvabilityRulesComputer Solution ImplementationConnect Four

IntroductionSolvabilityRulesComputer Solution ImplementationIAftereven:IIISide-effect of one or more claimevens. If a player in control ofzugzwang can complete a group using squares from claimeven,he will eventually be able to finish the group.Required: a group which can be completed by the controller ofthe zugzwang, using only squares of a set of claimevens.Solutions: all groups which have at least one square in allaftereven columns above the empty aftereven group in thatcolumn. Also, all groups which are solved by the claimevens.Connect Four

IntroductionSolvabilityRulesComputer Solution ImplementationConnect Four

IntroductionSolvabilityRulesComputer Solution ImplementationILowinverse:IIIBased on the fact that the sum of two odd numbers is even.Required: two different columns, each with 2 squares lyingdirectly above each other. All must be empty and the uppersquare must be odd.Solution: All groups which contain both upper squares, allgroups which are solved by verticals.Connect Four

IntroductionSolvabilityRulesComputer Solution ImplementationConnect Four

IntroductionSolvabilityRulesComputer Solution ImplementationIHighinverse:IIBased on the same principle as lowinverse:Required: Two columns with 3 empty squares each, uppersquare is even.Connect Four

IntroductionSolvabilityRulesComputer Solution ImplementationSolutions: all groups which contain the two upper squares, groupswhich contain the two middle squares, all vertical groups whichcontain the two highest squares of one of the highinverse columnsIIf the lower square of the first columns i directly playable: allgroups which contain both the lower square of the firstcolumn and the upper square of the second.IIf the lower square of the second column is directly playable:all groups which contain both the lower square of the secondcolumn and the upper square of the first column.Connect Four

IntroductionSolvabilityRulesComputer Solution ImplementationIBaseclaim:IIIICombination of two baseinverses and a claimeven.Required: Three directly playable squares and the square abovethe second playable square.The non-playable square must be even.Solutions:IIAll groups which contain the first playable square and thesquare above the second playable square.All groups which contain the second and third playable square.Connect Four

IntroductionSolvabilityRulesComputer Solution ImplementationConnect Four

IntroductionSolvabilityRulesComputer Solution ImplementationIBefore:IIIBased on a combination of claimevens and verticalsRequired: A group without men of the opponent, which iscalled the Before group. All empty squares of the Before groupshould not lie in the upper row of the board.Solutions: All groups which contain all squares which aresuccessors of empty squares in the Before group.IIAll groups which are solved by the Verticals which are part ofthe Before.All groups which are solved by the Claimevens which are partof the BeforeConnect Four

IntroductionSolvabilityRulesComputer Solution ImplementationConnect Four

IntroductionSolvabilityRulesComputer Solution ImplementationISpecialbefore:IIA version of the beforeRequired:IIIIIA group without men of the opponent, which is called theSpecialbefore group.A directly playable square in another column.All empty squares of the Specialbefore group should not lie inthe upper row of the board.One empty square of the Before group must be playable.Solutions: All groups which contain all successors of emptysquares of the Specialbefore group and the extra playablesquare.IIIAll groups which contain the two playable squares.All groups which are solved by one of the Claimevens.All groups which are solved by one of the Verticals.Connect Four

IntroductionSolvabilityRulesComputer Solution ImplementationConnect Four

IntroductionSolvabilityRulesComputer Solution ImplementationVictor Allis’s program VICTOR developed a method of finding anoptimal strategy based on the 9 rules given above. The positionevaluator (white or black) is given a description of the board andcomes up with an optimal next move.Connect Four

IntroductionSolvabilityRulesComputer Solution ImplementationFirst all possible instances of the nine rules above are found andchecked against all 69 possibilities to connect winning groups. Therule applications that solve at least one problem are stored in a listof solutions with a list of the groups solved by the solution and alist of other solutions that can be used to solve the problem.Connect Four

IntroductionSolvabilityRulesComputer Solution ImplementationThe next step is finding which solutions can work together. Firstall solutions are assembled as nodes into an undirected graph,where two nodes are connected if and only if they can’t be usedsimultaneously. These connections are stored in an adjacencymatrix. Then, the problems (threats) are added as nodes, andsolutions are connected with problems if they solve the problem(no problems are connected).Connect Four

IntroductionSolvabilityRulesComputer Solution ImplementationNote that two rules might not necessarily be able to be used at thesame time. The following table describes the relationships betweenrules (taken from Allis, section 7.4):Connect Four

IntroductionSolvabilityRulesComputer Solution ImplementationConnect Four

IntroductionSolvabilityRulesComputer Solution ImplementationThen we solve the following problem:Given: Two sets of nodes, S(olutions) and P(roblems). Try to findan independent subset C of S with the property that P iscontained in the set of all neighbors of C , B(C ). (Note this is apotentially NP-complete problem)Connect Four

IntroductionSolvabilityRulesComputer Solution ImplementationThe following recursive algorithm was used by Allis:FindChosenSet(P, S) {if (P EmptySet) {Eureka() /* We have found a subset C meetsall constraints. */} else {MostDifficultNode NodeWithLeastNumberOfNeighbours(P);for (all neighbours of MostDifficultNode) {FindChosenSet(P - { MostDifficultNode },S - AllNeighboursOf(ChosenNeighbour));}}}Connect Four

IntroductionSolvabilityRulesComputer Solution ImplementationOne method used to solve game trees is conspiracy-numbersearch.IIn the instance of connect four, consider three types of nodes:-1 black can at least draw the game, 1 the game is a win forwhite, or 0 the game is as yet undecided.INow any node that has as a child a node with a value of 1can be colored 1. Any node that has all nodes colored -1 canbe colored -1.INote it is much easier to change a node to a 1 than a -1.Connect Four

IntroductionSolvabilityRulesComputer Solution ImplementationIThe conspiracy number of a node is a tuple of counts for thenumber of children needed to “conspire” to change the valueof the node to each of the possible values.ILet (x, y ) be the conspiracy number of a node, with x thenumber of nodes that need to conspire to change the value to1, and y to -1.IWe know x will always be 1, since we only need one childe ofvalue 1 to change our value to 1.IBut y is the number of ons of the node yet to be evaluated ifall those evaluated so far have been -1.IIf sons have already been evaluated to 1, then y is .Connect Four

IntroductionSolvabilityRulesComputer Solution ImplementationThe purpose of conpsiracy number search is to evaluated as fewnodes as possible to find the result of the game tree. Therefore, wetry to avoid evaluating nodes with large conspiracy numbers. If wewant to evaluate a node at the top of the tree, we choose theneighbors with the lowest conspiracy numbers to evaluate until weare sure of their value. We move up the tree in this way, wheneverpossible avoiding evalutating nodes with large conspiracy numbers.Connect Four

IntroductionSolvabilityRulesComputer Solution ImplementationConnect Four tournament!Connect Four

The game was rst known as \The Captain’s Mistress", but was released in its current form by Milton Bradley in 1974. In 1988 Victor Allis solved the game, showing that with perfect play by both players, the rst player can always win if he plays the middle column rst, and if he chooses another column rst the second player can always force a draw.File Size: 551KBPage Count: 55