3-D Tic-Tac-Toe: A Rule-Based Approach
Andrew Kennihan

Phil Myers
3.1 Gammill
Gammill [4] discusses nk Tic-Tac-Toe games at length (in
Quintic, n = 5 and k = 3). The...
taken, the interesting space is taken. This will cause two separate
rows with four taken spaces and is essentially check-m...
find most powerful empty position
with most open intersecting rows
choose the most powerful non end
piece (X _ _ _ X)
set ...
Upcoming SlideShare
Loading in …5



Published on

  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide


  1. 1. 3-D Tic-Tac-Toe: A Rule-Based Approach Andrew Kennihan PSU CSE Dept. ask5127@psu.edu Phil Myers PSU CSE Dept. prm5031@psu.edu ABSTRACT Tic-Tac-Toe is a popular game played throughout the world. The most common version consists of a board with 3 X 3 dimensions. This game is not very interesting in regards to artificial intelligence, as it has already been solved. In a 3 X 3 game, the second player can force a draw by playing optimally. However, there are more interesting versions of the game with larger dimensions that have not been solved. That is, no optimal playing strategy has been determined. One such version is the 5 X 5 X 5 Tic-Tac-Toe game [1]. We shall hereafter refer to the 5 X 5 X 5 game as Quintic. In this paper, we will describe a strategy for Quintic that we hope will win a competition in our Artificial Intelligence class. We will start out by discussing the artificial intelligence algorithm we have chosen (rule-based expert system). We will then give a brief overview of our research into how to apply a rule-based expert system to a Tick-Tac-Toe game. Finally, we will describe our rule-based expert system in detail. Categories and Subject Descriptors I.2.1 [Artificial Intelligence]: Applications and Expert Systems – games. General Terms Algorithms Keywords Tic-Tac-Toe, rule-based expert system 1. INTRODUCTION The idea for rule-based expert systems can be traced back to Allen Newell and Herbert Simon of Carnegie Mellon. In the early 1970s, they proposed the production system model. The idea of this model is that humans solve problems by applying their knowledge to a given problem represented by problem-specific information [2]. A rule-based expert system applies this idea to computers by supplying a program with problem-specific rules to follow. The rules in the rule-based expert system represent the human knowledge in the production system model. When a condition of a rule is met, an action is taken. This is how a problem is solved in a rule-based expert system. The three main parts of a rule-based expert system are the knowledge base, the data base, and the inference engine. The knowledge base is the set of rules. It is made by an expert in the domain of the problem. The database is the set of facts that are input into the knowledge base. The facts are normally gathered by the user. The inference engine links the knowledge base to the database. It allows the expert system to reach a solution to the problem [2]. Carlton Taylor PSU CSE Dept. cpt5023@psu.edu As with all artificial intelligence algorithms, there are advantages and disadvantages to using a rule-based expert system. One advantage is that rules have a uniform IF-THEN structure that makes them easy to write. Also, a rule-based expert system allows for the separation of knowledge and processing. This allows the system to be changed easily. One disadvantage is that a rule-based expert system does not learn [2]. 2. WHY A RULE-BASED SYSTEM? We began this project by researching the simple 3 X 3 Tic-TacToe game. As stated earlier, the optimal strategy for this game has been defined. A player can play the optimal game by following these rules [3]: 1. Win: If the player has two in a row, play the third to get three in a row. 2. Block: If the opponent has two in a row, play the third to block them. 3. Fork: Create an opportunity where you can win in two ways. 4. Block opponent's fork: o Option 1: Create two in a row to force the opponent into defending, as long as it doesn't result in them creating a fork or winning. For example, if "X" has a corner, "O" has the center, and "X" has the opposite corner as well, "O" must not play a corner in order to win. (Playing a corner in this scenario creates a fork for "X" to win.) o Option 2: If there is a configuration where the opponent can fork, block that fork. 5. Center: Play the center. 6. Opposite corner: If the opponent is in the corner, play the opposite corner. 7. Empty corner: Play in a corner square. 8. Empty side: Play in a middle square on any of the 4 sides. These rules could easily be used in a rule-based expert system to develop an artificial intelligence program that would play the optimal game. So, we decided that since a rule-based expert system is the most natural algorithm to use for 3 X 3 Tic-Tac-Toe, it would be the most natural algorithm to use for Quintic as well. We would just need to take the rules of the 3 X 3 game and adapt them for Quintic. Another reason we chose the rule-based expert system is that as novice artificial intelligence students, we did not want to pick an algorithm that was overly complex. Rule-based expert systems are fairly easy to define and understand. This is in contrast to other algorithms such as neural networks, which we found difficult to grasp (in part because we haven’t learned about them in class yet).
  2. 2. 3. BACKGROUND RESEARCH 3.1 Gammill Gammill [4] discusses nk Tic-Tac-Toe games at length (in Quintic, n = 5 and k = 3). There were several important concepts that we took from this paper. For example, one way to evaluate the importance of a position is to figure out the number of winning lines that go through that position. Equation (1) gives the number of winning lines in an nk board. ( ) (1) So, in Quintic there are 109 winning lines. The position through which the most winning lines go is the center position (13 lines go through it). This makes the center the most valuable position. Gammill goes on to specifically focus on Qubic, which is a 4 3 game. Qubic was unsolved at the time (although it was later solved by Patashnik [1]), just as Quintic is unsolved now. So, Gammill’s Qubic strategies warranted our attention. Gammill described opening, middle and end game strategies. The idea of strategies for different parts of the game was new to us, as the 32 game is too simple for this. We included rules for the different stages of the game. Gammill also discusses the importance of sequences which force winning moves. We included these sequences in our rules as “forks”, which we shall discuss in more detail later. 3.2 Patashnik Perhaps the most important development in the study of Tic-TacToe in recent years was the solution of Qubic by Patashnik [1]. Patashnik proved that the first player is able to force a win. He used over 1500 hours of computer time in his proof. Patashnik did not use a pure rule-based expert system to solve Qubic. As shown in Figure 1, his algorithm was part brute-force search and part rule-based, but it was nonetheless helpful. For example, we included rules similar to “second-player three-in-a-row?”, “firstplayer forced sequence?”, and “second-player forced sequence”. Patashnik’s paper was also important in our realization that we could not make a rule-based system that could play the optimal Quintic game. He lists which Tic-Tac-Toe games have been solved, and Quintic was not one of them. So the best we could hope for is to come up with heuristic rules that are superior to our opponents in class. 4. DESIGN DESCRIPTION The following is as description of our knowledge base for the rules that we plan to implement. Before every turn begins, our rule-based expert system must check the board to see if our player or the opponent can potentially win on their next turn. We do this by looking for four marked spaces in a row by one contestant and an open space that would result in a winning combination. If this exists, take that space to win or prevent a win. Next, we needed to determine the value of different positions. To Permission to make digital or hard copies of all or part of this work for personal or classroom use is granted without fee provided that copies are not made or distributed for profit or commercial advantage and that copies bear this notice and the full citation on the first page. To copy otherwise, or republish, to post on servers or to redistribute to lists, requires prior specific permission and/or a fee. Conference’10, Month 1–2, 2010, City, State, Country. Copyright 2010 ACM 1-58113-000-0/00/0010…$10.00. Figure 1. Patashnik’s algorithm [1]. achieve this, we first converted the three dimensional board into a three dimensional array. This allowed us to visualize the board and make it easier to observe winning lines. We then calculated all of the 109 different winning lines that were possible, because overlooking any lines would be devastating to our artificial intelligence’s success [6]. We then began evaluating the game board to determine which spaces had more opportunities to make a winning line. We determined that the absolute middle space is the most valuable position, as it has the most opportunities to form a winning line at 13. The second most advantageous position on the cube is to have the absolute corners, as they intersect 7 winning lines. The centers that are not the absolute center intersect 5 winning lines. The non-absolute corners have 4 winning combinations. Our rule-based expert system is designed to have three game states; the beginning, the middle and the end. Each state is designated by a rule. At the beginning of the game, our strategy is to take the most valuable position as defined by the number of winning lines intersecting the position. We don’t have to consider the opponent’s move since at this point in the game, since the opponent will not have made enough moves to string together a line. After the initial game board is set up, middle phase of the game begins. This period is when the most strategy is involved. Both contestants will attempt to “fork” to create an opportunity to win with two or more different ways. Forks are generally done by taking spaces in two intersecting rows, while not taking the intersecting space. Finally, once both rows have three spaces
  3. 3. taken, the interesting space is taken. This will cause two separate rows with four taken spaces and is essentially check-mate [5]. This concept is extended even farther in three dimensions because you could implement a fork with 3 intersecting rows. It is very important that our rule-based system can fork, because it is the only winning strategy other than hoping the opponent makes a very poor decision. Table 1. Database Object Allowed Values strategy win block Defending against a fork is even more important than attacking with a fork. The best defense against an opponent’s fork is finding two intersecting rows with at least two taken spaces in each. Then, take the intersecting space. This concept is simple, however, can be very complicated in three dimensions. For example, the absolute center has 13 winning combinations. This could be very difficult to block all of the different forks possible. Our rule-based expert system must block the forking attempts of opponents and create forks of its own. This is where things get complicated. Our system must read the current board for a forking possibility and continue pursuing it in following turns when not playing defense. It also has to realize when the fork has been blocked by the opponent and know to try again. take_best_open_space fork antifork player_state 4_in_a_row create_fork We have written all of this into a knowledge base. The syntax for a user to change the rules is: If Object is Allowed Value Then Object is Allowed Value continue_fork blocking_fork blocked_fork This allows the user to modify, update or create new rules for the rule-based expert system. 5. KNOWLEDGE BASE & DATABASE The following are the rules of our knowledge base. The database representing the objects and their allowed values is shown in table 1. best_power_position corner If player_state is 4_in_a_row Then strategy is win in_line game_state beginning middle Rule 3: If game_state is beginning Then strategy is take_best_open_space Rule 4: If game_state is middle And player_state is idle Then strategy is antifork and player_state is blocking_fork absolute_center absolute_corner Rule 1: Rule 2: If opponent_state is 4_in_a_row Then strategy is block idle end opponent_state idle 4_in_a_row Rule 5: If game_state is middle And player_state is blocked_fork Then strategy is fork and player_state is create_fork Rule 6: If game_state is middle And player_state is create_fork Then strategy is fork and player_state is create_fork Rule 7: If game_state is middle And player_state is continue_fork Then strategy is fork
  4. 4. find most powerful empty position with most open intersecting rows choose the most powerful non end piece (X _ _ _ X) set player_state to continue_fork 6. PSUEDOCODE /*Sample Program*/ initialize player_state to idle initialize opponent state to idle Check_game_status(){ if moveNumber is… BEGINNING_RANGE: set game_status to beginning MIDDLE_RANGE: set game_status to middle END_RANGE: set game_status to end if rows or diagonals contain 4 player pieces set player_state to 4_in_a_row if rows or diagonals contain 4 opponent pieces set opponent_state to 4_in_row } //fire function pointers Win(){ if player_state is 4_in_a_row place piece in empty position in that row else find player’s most dangerous row find open position in row place piece in winning position } Block(){ if opponent_state is 4_in_a_row place piece in empty position in that row else find opponent’s most dangerous row find open position in row place piece in blocking position set player_state to idle } Antifork(){ if opponent owns 3 pieces in any two intersecting rows and intersection point is empty place piece in intersecting position of those two rows set player_state to blocked_fork else set player_state to create_fork fire rules } Fork(){ if player_state is create_fork if player owns pieces in any two intersecting rows and intersecting point is empty choose the most powerful non end piece (X _ _ _ X) set player_state to continue_fork else else if player_state is continue_fork if opponent has piece in intersecting rows set player_state to create_fork fire rules else if middle positions are open place piece in most powerful middle position in intersecting rows else play piece in intersecting point of rows set player_state to idle else Take_best_open_space() } Take_best_open_space(){ if absolute center is open place piece in absolute center else if absolute corner is open place piece in absolute corner else if corner is open place piece in corner else Block() } We do not necessarily expect the current state of our knowledge base, database and pseudocode to be the same as our final product. We do hope that our work in these three topics demonstrates that we are on the right track. 7. REFERENCES [1] Patashnik, O. Qubic: 4x4x4 Tic-Tac-Toe Bell. Laboratories Mathematics Magazine, Vol. 53, No. 4 (Sep., 1980), pp. 202216 [2] Negnevitsky, M. Artificial Intelligence: A Guide to Intelligent Systems [3] Crowley, K. and Siegler, R. S. Flexible Strategy Use in Young Children's Tic-Tac-Toe. Cognitive Science (1993) [4] Gammill, R. An Examination of TIC-TAC-TOE like Games. Association for Computing Machinery (1974) [5] Pilgrim, R. TIC-TAC-TOE: Introducing Expert Systems to Middle School Students. Association for Computing Machinery (1995) [6] Paul, J. Tic-Tac-Toe in n-Dimensions. Mathematical Association of America (1978)